Spring Boot Actuator provides production-ready features to monitor and manage your applications. This guide explores Actuator's capabilities, how to enable it, and secure its endpoints.
🌟 Why Use Spring Boot Actuator?
Spring Boot Actuator allows developers to:
- Monitor application health
- Gather metrics and information
- Expose management endpoints for operational tasks
🌟 Prerequisites
📋 Ensure you have the following:
- ☕ Java Development Kit (JDK) 17+
- 💼 Maven or Gradle installed
- 🄄 A Java IDE (e.g., IntelliJ IDEA, Eclipse)
🛠️ Step 1: Add Dependencies
To enable Actuator, add the following dependency to your Spring Boot project:
- Maven:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
- Gradle:
implementation 'org.springframework.boot:spring-boot-starter-actuator'
📜 Step 2: Enable Actuator Endpoints
By default, Actuator exposes several endpoints to provide application insights. You can enable these endpoints in your application.properties
or application.yml
file.
Configuration Example:
management.endpoints.web.exposure.include=health,info,metrics management.endpoint.health.show-details=always
📖 Step 3: Using Actuator Endpoints
Here are some commonly used Actuator endpoints:
/actuator/health
: Provides application health status./actuator/info
: Displays application metadata./actuator/metrics
: Offers application performance metrics.
You can access these endpoints via a browser or API tools like cURL.
Example:
curl -X GET http://localhost:8080/actuator/health
📘 Step 4: Customize Actuator Endpoints
You can customize Actuator endpoints to suit your needs. For example, you can define additional metadata in the application.properties
file for the /actuator/info
endpoint:
info.app.name=My Application info.app.version=1.0.0 info.app.description=Spring Boot Actuator Example
🔒 Step 5: Secure Actuator Endpoints
For production environments, it's essential to secure Actuator endpoints. Use Spring Security to restrict access.
Example Security Configuration
package com.example.demo.config; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.web.SecurityFilterChain; @Configuration public class SecurityConfig { public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { http .authorizeHttpRequests() .requestMatchers("/actuator/**").hasRole("ADMIN") .anyRequest().authenticated() .and() .httpBasic(); return http.build(); } }
Example Security Configuration
package com.example.demo.config import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Configuration import org.springframework.security.config.annotation.web.builders.HttpSecurity import org.springframework.security.web.SecurityFilterChain @Configuration class SecurityConfig { @Bean fun securityFilterChain(http: HttpSecurity): SecurityFilterChain { http .authorizeHttpRequests() .requestMatchers("/actuator/**").hasRole("ADMIN") .anyRequest().authenticated() .and() .httpBasic() return http.build() } }
▶️ Running the Application
Run the application using the following command:
./mvnw spring-boot:run
Access the Actuator endpoints at:
http://localhost:8080/actuator/health
🧪 Test the API
You can test the Actuator endpoints using cURL or browser:
- Health Endpoint:
curl -X GET http://localhost:8080/actuator/health
- Info Endpoint:
curl -X GET http://localhost:8080/actuator/info
Spring Boot Actuator simplifies monitoring and managing your application. This guide covered enabling, customizing, and securing Actuator endpoints to improve your Spring Boot project.