The Actuator provides additional features that can monitor and manage online applications using HTTP or JMX, including auditing, service health and metrics collection, which are accessed through endpoints. Each endpoint can be enabled, disabled and exposed via Http or JMX.

When dependencies are added to a project, Endpoints are automatically registered with the application as they become available and can be accessed via HTTP or JMX

Configuration and simple use

Begin to use
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
Copy the code

Start the project access /actuator/ Health and get the following

{"status":"UP"}
Copy the code

The/skeletonoid uses the default URL prefix to access endpoints, and /health displays the health information of applications.

You can modify the URL prefix of endpoints to change the health access address to /simple/health

management.endpoints.web.base-path=/simple
Copy the code

Because endpoints contain sensitive information, you should be careful when exposing endpoints. Common Endpoints and default exposures are listed below

ID describe JMX Web
health Applying health Information YES YES
info Arbitrary application information YES NO
beans Show all beans information in the application YES NO
env The ConfigurableEnvironment in Spring exposes the attribute YES NO
metrics Application metrics YES NO
headdump Get the hprof dump file N/A NO
loggers Displays and modifies application logging configurations YES NO

You can choose to expose and filter undesired Endpoints by using the following command

Property Default
management.endpoints.jmx.exposure.exclude
management.endpoints.jmx.exposure.include *
management.endpoints.web.exposure.exclude
management.endpoints.web.exposure.include The health, the info
Enabling and disabling the endpoint

      
       . Enabled to enable shutdown endpoint
      
All endpoints except shutdown are enabled by default
management:
  endpoint:
    shutdown:
      enabled: true
Copy the code
Expose and filter unwanted endpoints
management:
  endpoints:
    web:
      exposure:
        Use HTTP to access /actuator/health, /actuator/beans, and /actuator/info
        include: health,info,beans
        Info endpoint is not accessible through HTTP
        exclude: info
Copy the code

If include and exclude are used together, exclude takes precedence, so info is not accessible through HTTP

Custom Endpoint
  • Create the Bean, add an annotation @endpoint, @webendpoint, @endPointwebextension to the class, and add @readOperation or @writeOperation or @deleteOperation to the method. For web applications, the endpoint can be exposed via HTTP using Jersey, SpringMVC, Spring WebFlux, or SpingMVC if both Jersey and SpingMVC are available.

    @ServleTendPoint and @Controllerendpoint can also be used, but @endpoint and @webendpoint are recommended

    Here we use @endpoint as an example, and refer to the complete demo code provided for the rest

    The @endpoint ID property must be set

    @Configuration
    //id must be set
    @Endpoint(id = "myCustomEndpoints")
    public class MyCustomEndpoints {
        @ReadOperation
        public ShopData getData(a){
            return new ShopData("Shop girl"."Global Village");
        }
        @Data
        @AllArgsConstructor
        public static class ShopData{
            private String name;
            privateString address; }}Copy the code
    management:
      endpoints:
        web:
          exposure:
          # # myCustomEndpoints exposure
            include: health,info,beans,env,myCustomEndpoints
    Copy the code

    Access: physical/myCustomEndpoints returns the {” name “:” the bartender “, “address” : “global village”}

Common Endpoint details
  • Health endpoint

    You can use the health indicator to determine the health status of an application. You can view detailed health information through the following property configuration

    There are only three options: nerver, When – Authorized, always default micro nerver

    If show-components is not specified, show-details will be displayed with the value
    # if show-components is specified, the configuration of show-components will prevail
    management.endpoint.health.show-details=always
    management.endpoint.health.show-components=always
    Copy the code

    Springboot comes with some common health metrics (listed only in a few)

    • DataSourceHealthIndicator: check the database connection status
    • DiskSpaceHealthIndicator: Checks whether the disk space is insufficient
    • ElasticsearchRestHealthIndicator: check ES cluster is up
    • Mail: Check whether the email server is in up state
    • Rabbit: Check whether the RabbitMQ server is in the Up state
    • Redis: Check whether redis is in up state

    Customize health indicators

    /** * Verify that the value of custom.url is correct, if it is correct, the state is up, if not, it is down */
    @Component
    public class MyCustomIndicator implements HealthIndicator {
    
        @Value("${custom.url}")
        private String customUrl;
    
        @Override
        public Health health(a) {
            if("http://www.baidu.com".equalsIgnoreCase(customUrl)){
                return Health.up().build();
            }
            return Health.down().withDetail("customUrl",customUrl).build(); }}Copy the code
    custom.url = http://www.baidu.com
    Copy the code
    {"status":"UP"."components": {"diskSpace": {"status":"UP"."details": {"total":250685575168."free":46600232960."threshold":10485760."exists":true}},"myCustomIndicator": {"status":"UP"},"ping": {"status":"UP"}}}
    Copy the code

    Custom.url is incorrectly configured. The result is as follows:

    custom.url = http://www.baidu.com001
    Copy the code
    {"status":"DOWN"."components": {"diskSpace": {"status":"UP"."details": {"total":250685575168."free":46598496256."threshold":10485760."exists":true}},"myCustomIndicator": {"status":"DOWN"."details": {"customUrl":"http://www.baidu.com001"}},"ping": {"status":"UP"}}}
    Copy the code
  • info endpoint

    SpringBoot includes many auto-configured InfoContributor features listed below are a few commonly used ones

    • The information in the EnvironmentInfoContributor public Environment
    • GitInfoContributor exposes git information if the git.properties file is available
    • BuildInfoContributor public build information if META/INF/build-info.properties is available

    Customize Application Information

    # Customize the data exposed by ifO endpoints by setting the info.* Spring attribute
    info.app.encoding = UTF-8
    #info.app.java.source = 8
    info.app.java.source = @java.version@
    Copy the code

    The running results are as follows:

    {"app": {"encoding":"UTF-8"."java": {"source":"1.8.0 comes with _191"}}}
    Copy the code

    Obtain build info if necessary

    <build>
      <plugins>
        <plugin>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-maven-plugin</artifactId>
          <executions>
            <execution>
              <goals>
                <goal>build-info</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
      </plugins>
    </build>
    Copy the code

    Then run the result as follows

    {"app": {"encoding":"UTF-8"."java": {"source":"1.8.0 comes with _191"}},"build": {"version":"1.0 the SNAPSHOT"."artifact":"actuator-simple"."name":"actuator-simple"."group":"com.hs.springboot"."time":"The 2021-06-04 T02:51:53. 878 z"}}
    Copy the code

  • Loggers endpoint

    Use the /actuator/loggers command to view and modify the log level of an application. You can use them to view all log levels

    {"levels": ["OFF"."ERROR"."WARN"."INFO"."DEBUG"."TRACE"]."loggers": {"ROOT": {"configuredLevel":"INFO"."effectiveLevel":"INFO"},"com": {"configuredLevel":null."effectiveLevel":"INFO"},"com.hs": {"configuredLevel":null."effectiveLevel":"INFO"},"com.hs.springboot": {"configuredLevel":null."effectiveLevel":"INFO"},"com.hs.springboot.actuator": {"configuredLevel":null."effectiveLevel":"INFO"},"com.hs.springboot.actuator.ActuatorSimpleApplication": {"configuredLevel":null."effectiveLevel":"INFO"},"io": {"configuredLevel":null."effectiveLevel":"INFO"},"io.micrometer":
    Copy the code

    If you want to access a specific log configuration, for example, that of ROOT, the access path is as follows: /actuator/loggers/ROOT

    EffectiveLevel and configuredLevel are the same if configuredLevel is configured. If configuredLevel is not displayed, the configuredLevel is null
    {"configuredLevel":"INFO"."effectiveLevel":"INFO"}
    Copy the code

    Modify the ROOT log level request path /actuator/loggers/ROOT Dynamically adjust the log level parameters of the application as follows:

    {
        "configuredLevel": "DEBUG"
    }
    Copy the code

Conclusion:

The Operating Actuator can view status information of applications and customize health checks that meet its own requirements

System integration with monitoring software such as Prometheus (integration with Prometheus later)

Full code address

References:

Spring Boot Actuator

Spring Boot Actuator Web API