The main feature of Spring Boot is AutoConfig (automatic configuration), and for us users, it is just various starters.
Spring Boot-Actuator also provides a starter, which is automatically configured for us. In terms of use, we only need to add a starter to our dependencies and then start the project.
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId></dependency>
Commonly used Endpoint
Spring Boot-actuator provides many useful EndPoints and provides various monitoring of Spring Boot applications. Let’s talk about my commonly used EndPoints:
/health application health status
/configprops Get the configuration information of the application, because Spring Boot may be a separate Jar package when it is published, and the configuration file may be included. When we need to check the configuration file, we can use ConfigpropsEndPoint to see if some configurations are correct.
/trace The latest http request information
HealthEndPoint
When we visit http://localhost:8088/health, we can see that HealthEndPoint provides us with default monitoring results, including disk detection and database detection.
{ "status": "UP", "diskSpace": { "status": "UP", "total": 398458875904, "free": 315106918400, "threshold": 10485760 }, "db": { "status": "UP", "database": "MySQL", "hello": 1 }} In fact, looking at the Spring Boot-actuator source code, you will find that the information provided by HealthEndPoint is not limited to this. Under the org.springframework.boot.actuate.health package, you will find ElasticsearchHealthIndicator, RedisHealthIndicator, RabbitHealthIndicator, etc.
That is, HealthEndPoint also provides health information for components such as ES, Redis.
Custom Indicator Extend HealthEndPoint
Looking at the source code, disk and database health information are implemented by DiskSpaceHealthIndicator and DataSourceHealthIndicator. When we monitor some of our customized components, we can also implement an Indicator:
@Componentpublic class User implements HealthIndicator { /** * user monitoring access: http://localhost:8088/health * * @return Custom Health Monitor*/ @Override public Health health() { return new Health.Builder().withDetail("usercount", 10) //Custom monitoring content.withDetail("userstatus", "up").up().build(); }}At this time, we visit again: http://localhost:8088/health The result returned at this time is as follows, including our customized User health information.
{ "status": "UP", "user": { "status": "UP", "usercount": 10, "userstatus": "up" }, "diskSpace": { "status": "UP", "total": 398458875904, "free": 315097989120, "threshold": 10485760 }, "db": { "status": "UP", "database": "MySQL", "hello": 1 }}Custom EndPoint
In fact, in addition to extending HealthEndPoint to add some health checks, we can also customize some EndPoints to provide some information display during the program runtime:
@Configurationpublic class EndPointAutoConfig { @Bean public Endpoint<Map<String, Object>> customEndPoint() { return new SystemEndPoint(); }}@ConfigurationProperties(prefix="endpoints.customsystem")public class SystemEndPoint extends AbstractEndpoint<Map<String, Object>> { public SystemEndPoint(){ super("customsystem"); } @Override public Map<String, Object> invoke() { Map<String,Object> result= new HashMap<>(); Map<String, String> map = System.getenv(); result.put("username",map.get("USERNAME")); result.put("computername",map.get("COMPUTERNAME")); result.put("userdomain",map.get("USERDOMAIN")); return result; }}Visit http://localhost:8088/customsystem to view our customized EndPoint, and the result is as follows:
{ "username": "xxx", "userdomain": "DESKTOP-6EAN1H4", "computername": "DESKTOP-6EAN1H4"}After we add actuator to our Spring Boot application, the expected health interface returns the result should be something like the following:
{ status: "UP", diskSpace: { status: "UP", total: 250182889472, free: 31169568768, threshold: 10485760 }, db: { status: "UP", database: "H2", hello: 1 }}If it's just returned status
{ status: "UP"}You need to add new configuration for the application. Taking the yml configuration file as an example, you need to add the following configuration:
management: security: enabled: falseendpoints: health: sensitive: false
management.endpoint.health.show-details=always
Summarize
The above is the SpringBoot implementation project health check and monitoring introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!