Spring Boot Actuator is a sub-project of Spring Boot that provides production-ready features to help you monitor and manage your application. It offers a set of built-in endpoints that allow you to gain insights into your application's health, metrics, and environment, as well as control it dynamically.
Spring Boot Actuator provides several out-of-the-box endpoints that can be used to monitor and interact with your application. These endpoints can be accessed over HTTP, JMX, or using Spring Boot Admin.
To use Actuator in your Spring Boot application, you need to add the Actuator dependency to your pom.xml file:
org.springframework.boot spring-boot-starter-actuator
If you are using Gradle, add the following to your build.gradle file:
implementation 'org.springframework.boot:spring-boot-starter-actuator'
By default, only a few endpoints are enabled. You can enable additional endpoints in your application.yml file:
management: endpoints: web: exposure: include: "*" # This exposes all available endpoints endpoint: health: show-details: always # Show detailed health information
Once Actuator is set up, you can access the various endpoints provided by it. Here are some commonly used endpoints:
The /actuator/health endpoint provides information about the health of your application:
GET http://localhost:8080/actuator/health
Example response:
{ "status": "UP", "components": { "db": { "status": "UP", "details": { "database": "H2", "result": 1 } }, "diskSpace": { "status": "UP", "details": { "total": 499963174912, "free": 16989374464, "threshold": 10485760, "exists": true } } } }
The /actuator/metrics endpoint provides various metrics related to your application:
GET http://localhost:8080/actuator/metrics
Example response:
{ "names": [ "jvm.memory.used", "jvm.gc.pause", "system.cpu.usage", "system.memory.usage", "http.server.requests" ] }
To get details of a specific metric:
GET http://localhost:8080/actuator/metrics/jvm.memory.used
Example response:
{ "name": "jvm.memory.used", "description": "The amount of used memory", "baseUnit": "bytes", "measurements": [ { "statistic": "VALUE", "value": 5.1234567E7 } ], "availableTags": [ { "tag": "area", "values": [ "heap", "nonheap" ] }, { "tag": "id", "values": [ "PS Eden Space", "PS Survivor Space", "PS Old Gen", "Metaspace", "Compressed Class Space" ] } ] }
The /actuator/env endpoint provides information about the environment properties:
GET http://localhost:8080/actuator/env
Example response:
{ "activeProfiles": [], "propertySources": [ { "name": "systemProperties", "properties": { "java.runtime.name": { "value": "Java(TM) SE Runtime Environment" }, "java.vm.version": { "value": "25.181-b13" } } }, { "name": "systemEnvironment", "properties": { "PATH": { "value": "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" }, "HOME": { "value": "/root" } } } ] }
The /actuator/info endpoint provides information about the application:
GET http://localhost:8080/actuator/info
To customize the information, add properties in your application.yml:
info: app: name: My Spring Boot Application description: This is a sample Spring Boot application version: 1.0.0
By default, all Actuator endpoints are accessible without authentication. To secure these endpoints, you can use Spring Security. Add the Spring Security dependency to your pom.xml:
org.springframework.boot spring-boot-starter-security
Update your application.yml to restrict access:
management: endpoints: web: exposure: include: "*" # Expose all endpoints endpoint: health: show-details: always # Show detailed health information spring: security: user: name: admin # Default username password: admin # Default password # Restrict actuator endpoints to authenticated users management: endpoints: web: exposure: include: "*" endpoint: health: show-details: always security: enabled: true roles: ACTUATOR
Create a security configuration class to configure HTTP security:
import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; @Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/actuator/**").hasRole("ACTUATOR") .anyRequest().authenticated() .and() .httpBasic(); } }
With this configuration, only authenticated users with the ACTUATOR role can access the Actuator endpoints.
You can create custom Actuator endpoints to expose additional information or functionality specific to your application. Here’s an example of creating a custom endpoint:
import org.springframework.boot.actuate.endpoint.annotation.Endpoint; import org.springframework.boot.actuate.endpoint.annotation.ReadOperation; import org.springframework.stereotype.Component; @Endpoint(id = "custom") @Component public class CustomEndpoint { @ReadOperation public String customEndpoint() { return "Custom Actuator Endpoint"; } }
Access your custom endpoint at:
GET http://localhost:8080/actuator/custom
Spring Boot Actuator provides a robust set of tools to help you monitor and manage your application. By leveraging its built-in endpoints and the ability to create custom endpoints, you can gain valuable insights into your application’s performance and health. Secure these endpoints with Spring Security to ensure that only authorized users have access, and you’ll have a production-ready application that’s easy to manage and monitor.
Actuator is an essential part of any Spring Boot application, enabling you to keep your finger on the pulse of your application’s runtime environment and quickly respond to issues as they arise. Start using Spring Boot Actuator today to enhance your application’s observability and operational capabilities.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3