我们知道health端点用于查看Spring Boot应用的健康状态,提供了用于检测磁盘的DiskSpaceHealthIndicator、检测DataSource连接是否可用的DataSourceHealthIndicator、检测XXX内置服务(XXX代表内置的Elasticsearch、JMS、Mail、MongoDB、Rabbit、Redis、Solr等)是否可用的XXXHealthIndicator等健康指标检测器。在Spring Boot中,这些检测器都是通过HealthIndicator接口实现,并且根据依赖关系的引入实现自动化装配。 当Spring Boot自带的HealthIndicator接口实现类不能满足我们的需求时,就需要自定义HealthIndicator接口实现类。自定义HealthIndicator接口实现类很简单,只需要实现HealthIndicator接口,并重写接口方法health,返回一个Health对象。
自定义HealthIndicator 1.创建HealthIndicator接口实现类MyHealthIndicator 2.将健康详细信息显示给所有用户 3.测试运行
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.health</groupId> <artifactId>SpringBootHealthIndicator</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <properties> <!-- 声明项目配置依赖编码格式为 utf-8 --> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <fastjson.version>1.2.24</fastjson.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!-- 添加MySQL依赖 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> <version>8.0.13</version><!--$NO-MVN-MAN-VER$ --> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
spring.datasource.url=jdbc:mysql://localhost:3306/springbootjpa?serverTimezone=UTC&autoReconnect=true spring.datasource.username=root spring.datasource.password=admin spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.jpa.database=MYSQL spring.jpa.show-sql=true spring.jpa.hibernate.ddl-auto=update spring.jackson.serialization.indent-output=true management.endpoints.web.exposure.include=* management.endpoint.health.show-details=always
package com.ch.ch10_2.health; import org.springframework.boot.actuate.health.Health; import org.springframework.boot.actuate.health.HealthIndicator; import org.springframework.stereotype.Component; @Component public class MyHealthIndicator implements HealthIndicator { @Override public Health health() { int errorCode = check(); if (errorCode != 0) { // down方法表示异常,withDetail方法添加任意多的异常信息 return Health.down().withDetail("message", "error:" + errorCode).build(); } // up方法表示健康 return Health.up().build(); } /** * 模拟返回一个错误状态 */ private int check() { return 1; } }
2.将健康详细信息显示给所有用户 在配置文件application.properties中,配置将详细健康信息显示给所有用户。配置内容如下: #将详细健康信息显示给所有用户 management.endpoint.health.show-details=always
3.测试运行 health的对象名默认为类名去掉HealthIndicator后缀,并且首字母小写,因此该例的health对象名为my。 启动应用程序主类Ch102Application,并通过“http://localhost:8080/actuator/health/my”测试运行
package com.ch.ch10_2; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Ch102Application { public static void main(String[] args) { SpringApplication.run(Ch102Application.class, args); } }
package com.ch.ch10_2.endPoint; import java.util.HashMap; import java.util.Map; import org.springframework.boot.actuate.endpoint.annotation.Endpoint; import org.springframework.boot.actuate.endpoint.annotation.ReadOperation; import org.springframework.boot.actuate.endpoint.annotation.WriteOperation; import org.springframework.stereotype.Component; import com.zaxxer.hikari.HikariConfigMXBean; import com.zaxxer.hikari.HikariDataSource; import com.zaxxer.hikari.HikariPoolMXBean; //注册为端点,id不能使用驼峰法(dataSource),需要以-分割 @Endpoint(id = "data-source") @Component public class DataSourceEndpoint { // HikariDataSource提供多个监控信息 HikariDataSource ds; public DataSourceEndpoint(HikariDataSource ds) { this.ds = ds; } @ReadOperation public Map<String, Object> info() { Map<String, Object> map = new HashMap<String, Object>(); // 连接池配置 HikariConfigMXBean configBean = ds.getHikariConfigMXBean(); map.put("max", configBean.getMaximumPoolSize()); // 连接池运行状态 HikariPoolMXBean mxBean = ds.getHikariPoolMXBean(); map.put("active", mxBean.getActiveConnections()); map.put("idle", mxBean.getIdleConnections()); // 连接池无连接时,等待获取连接的线程个数 map.put("wait", mxBean.getThreadsAwaitingConnection()); return map; } @WriteOperation public void setMax(int max) { ds.getHikariConfigMXBean().setMaximumPoolSize(max); } }
package com.ch.ch10_2; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest public class Ch102ApplicationTests { @Test public void contextLoads() { } }