Monitor and manage production environment spring boot actuator

The spring-boot-actuator module provides a module for monitoring and managing the production environment, and can use http, jmx, ssh, telnet, etc. to pull management and monitoring applications. Auditing, health, and metrics gathering are automatically added to the application.

 

First, write a basic spring boot project.

 

Maven based project adding 'starter' dependency 

 

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

 

 

You can see some logs like this in the startup information

 

2015-08-28 09:57:40.953  INFO 4064 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/trace],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2015-08-28 09:57:40.954  INFO 4064 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/mappings],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2015-08-28 09:57:40.955  INFO 4064 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/env/{name:.*}],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EnvironmentMvcEndpoint.value(java.lang.String)
2015-08-28 09:57:40.956  INFO 4064 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/env],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2015-08-28 09:57:40.957  INFO 4064 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/configprops],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2015-08-28 09:57:40.958  INFO 4064 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/info],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2015-08-28 09:57:40.958  INFO 4064 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/dump],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2015-08-28 09:57:40.959  INFO 4064 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/health],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.HealthMvcEndpoint.invoke(java.security.Principal)
2015-08-28 09:57:40.961  INFO 4064 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/autoconfig],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2015-08-28 09:57:40.962  INFO 4064 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/metrics/{name:.*}],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.MetricsMvcEndpoint.value(java.lang.String)
2015-08-28 09:57:40.963  INFO 4064 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/metrics],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()

 

 

Specific description:

ID Description Sensitive
autoconfig Displays an auto-configuration report showing all auto-configuration candidates and why they were or were not applied true
beans Displays a complete list of all Spring Beans in an application true
configprops Displays a curated list of all @ConfigurationProperties true
dump perform a thread dump true
env Expose properties from Spring ConfigurableEnvironment true
health Displays the application's health information (a simple 'status' when accessed with an unauthenticated connection, full information details when accessed with an authenticated connection) false
info Display arbitrary application information false
metrics Displays 'metrics' information for the current application true
mappings Displays a curated list of all @RequestMapping paths true
shutdown Allows apps to shut down gracefully (not enabled by default) true
trace Show trace information (defaults to the latest HTTP requests) true

 

health

 

For example: http://localhost:8080/health

you can get the result

 

{
    status: "UP",
}

In the application configuration add

 

 

endpoints.health.sensitive=false

 

Visit http://localhost:8080/health

 

{
    status: "UP",
    diskSpace: {
        status: "UP",
        free: 32516145152,
        threshold: 10485760
    },
    db: {
        status: "UP",
        database: "Microsoft SQL Server",
        hello: 1440729256277
    }
}

 Health information for some other conditions that can be checked. The following HealthIndicators are automatically configured by Spring Boot (when appropriate):

 

 

name description
DiskSpaceHealthIndicator Low disk space detection
DataSourceHealthIndicator Check if connection can be obtained from DataSource
MongoHealthIndicator Check if a Mongo database is available (up)
RabbitHealthIndicator Check if a Rabbit server is available (up)
RedisHealthIndicator Check if a Redis server is available (up)
SolrHealthIndicator Check if a Solr server is available (up)

 

Custom is also possible, you can register Spring beans that implement the HealthIndicator interface, and the Health response needs to contain a status and optional details for display.

import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;

@Component
public class MyHealth implements HealthIndicator {

    @Override
    public Health health() {
        int errorCode = check(); // perform some specific health check
        if (errorCode != 0) {
        return Health.down().withDetail("Error Code", errorCode).build();
        }
        return Health.up().build();
    }
}

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327034338&siteId=291194637