Monitor and manage the production environment

Introduction

The spring-boot-actuator module provides a module for monitoring and managing the production environment. You can use http, jmx, ssh, telnet, etc. to manage and monitor applications. Auditing,

Health, and data gathering are automatically added to the application.

Implemetation

A simple spring boot proj

First, write a basic spring boot project.

Maven-based projects add ‘starter’ dependencies

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

When startting, some log messages can be found.

1
2
2014-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()
2014-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()

explation of the log

Specific description:

ID desc 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 collated list of all @ConfigurationProperties true
dump Execute a thread dump true
env Exposure from the Spring ConfigurableEnvironment property true
health Showing the health information of the application (a simple ‘status’ is displayed when an unauthenticated connection is accessed, and all information is displayed using an authenticated connection) false
info Display any application information false
metrics Displays the currently applied ‘metrics’ information true
mappings Displays a collated list of all @RequestMapping paths true
shutdown Allows the app to turn off gracefully (not enabled by default) true
trace Display trace information (defaults to some recent HTTP requests) true

Health Check

For example: http://localhost:7231/health
You can get results

1
{Status: "UP",}

Add in the application configuration

Endpoints.health.sensitive = false
At the second visit http://localhost:7231/health

1
2
3
{Status: "UP", diskSpace: {status: "UP", free: 32516145152, threshold: 10485760},
Db: {status: "UP", database: "Microsoft SQL Server", hello: 1440729256277}
}}

You can check for health information in some other cases. The following HealthIndicators are automatically configured by Spring Boot (at the appropriate time):

Name Description
DiskSpaceHealthIndicator Low disk space detection
DataSourceHealthIndicator Checks whether the connection can be obtained from the DataSource
MongoHealthIndicator checks whether a Mongo database is up (up)
RabbitHealthIndicator Checks whether a Rabbit server is up (up)
RedisHealthIndicator checks whether a Redis server is up (up)
SolrHealthIndicator checks whether a Solr server is up (up)

Customization Of course, you can register to achieve the HealthIndicator interface Spring beans, Health response needs to include a status and optional for display details.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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 ();
}
}

Trace

Visit http://localhost:7231/trace to see the results, default to some of the latest HTTP requests

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
[
{
Timestamp: 1440728799269,
Info: {
Method: "GET",
Path: "/ health",
Headers: {
Request: {
Host: "localhost: 7231",
Connection: "keep-alive",
Q = 0.9, image / webp, * / *; q = 0.8 ", and the following expression is acceptable:" text / html, application / xhtml + xml, application / xml;
User-agent: "Mozilla / 5.0 (Windows NT 6.1; WOW64) AppleWebKit / 537.36 (KHTML, like Gecko) Chrome / 38.0.2125.122 Safari / 537.36"
Accept-encoding: "gzip, deflate, sdch",
Accept-language: "zh-CN, zh = q, 0.8; en; q = 0.6"
Ra-ver: "3.0.7",
Ra-sid: "74E754D8-20141117-085628-93e7a4-1dd60b"
},
Response: {
X-Application-Context: "executecount: integration: 7231",
Content-Type: "application / json; charset = UTF-8",
Transfer-Encoding: "chunked",
Content-Encoding: "gzip",
Vary: "Accept-Encoding",
date: "Fri, 28 Aug 2015 02:26:39 GMT",
Status: "200"
}
}
}
}
]

Look at InMemoryTraceRepository, the default is 100 events, if necessary, you can define your own InMemoryTraceRepository instance. If desired, you can create your own alternative TraceRepository implementation.

ref

Building a RESTful Web Service with Spring Boot Actuator

Spring Boot Reference Guide

supervise the health of spring boot