Health check of spring shoe 2.0 actuator

In the popular service grid architecture, Spring boot is especially suitable as an application development framework because of its various advantages.

Speaking of micro-service architecture of service grid, the main feature is to separate service development from service governance, and then combine them with containerized Paas platform, which depends on the tacit cooperation between them. In other words, they all expose standard interfaces and can be interwoven with each other.

One of the key points in the architecture design of service grid is all-round monitoring, so generally, the service development framework we choose needs to be supported by convenient and powerful monitoring functions. It is very convenient to start monitoring in Spring boot application, and the monitoring surface is also very wide, and it also supports flexible customization.

In the application of spring shoe, the monitoring function is realized by the spring shoe-starter-actuator assembly. It provides many HTTP or JMX endpoints to monitor and manage your spring boot application, and you can selectively turn some functions on and off. When your spring boot application introduces the following dependencies, it will automatically have audit, health check and indicator monitoring functions.

Specific use method:

"*" indicates that all monitoring endpoints are enabled and can be enabled separately, such as health, information and indicators.

The configuration information of general monitoring and management endpoints is as follows:

The above configuration information is for reference only. Please refer to the official documents for details. Because the version of spring boot is updated quickly, the configuration method may change.

Today, I will focus on the health check function in actuator monitoring and management. It is very important to know the health status of online applications at any time, especially those under the popular container cloud platform. Their automatic recovery and expansion depend on the health check function.

When we open the health endpoint of health, we can find that the application health information is a summary information. When we visited http:/127.0.0.1:101/actuator/health, we got the following information.

To view detailed application health information, you need to configure managed values. Endpoint. health. show- Details as always. After the configuration is completed, we will visit http:/127.0.0.1:101/actuate again.

From the detailed health information of the above application, it is found that the health information includes disk space, redis and DB. Spring boot application with monitoring enabled is indeed connected with redis and oracle DB, and the actuator automatically monitors, which is really convenient and useful.

After testing, it is found that any health status of all monitoring items in details is DOWN, and the health status of the whole application is also DOWN.

The health information of Spring boot is obtained from various HealthIndicator in ApplicationContext.

Collected in Beans, the Spring boot framework contains a large number of implementation classes of HealthIndicators, and of course, you can also realize your own health status.

By default, the state of the final spring boot application is summarized by HealthAggregator, and the summary algorithm is:

The health indicators included in the Spring boot framework currently include:

Sometimes it is necessary to provide customized health check information. You can do this by implementing the interface of HealthIndicator and registering the implementation class as spring bean. You need to implement the health () method and return customized health status response information, which should include the status code and detailed information to be displayed. For example, the following is the implementation class of the interface HealthIndicator:

In addition, in addition to several state types defined by Spring boot, we can also customize state types to represent new system states. In this case, you also need to implement the interface HealthAggregator, or continue to use the default implementation of HealthAggregator by configuring management.health.status.order

For example, in the implementation class of a custom health check HealthIndicator, the custom status type "fatal" is used. In order to configure the severity of this status type, you need to add the following configuration to the application's configuration file:

During the health check, the HTTP status code in the response reflects the overall health status (for example, UP corresponds to 200, and OUT_OF_SERVICE and DOWN correspond to 503). Similarly, you need to set the corresponding HTTP status code for the user-defined status type. For example, the following configuration can map fatal to 503 (service unavailable):

The following is a list of HTTP status codes corresponding to the built-in health status types:

This paper mainly introduces the use method and principle of application health check function provided in Spring boot, and introduces a little content of Actuator. The main content comes from the official document and source code of spring boot 2.0. 1, and some of my own ideas. I hope everyone will support me a lot.