基於 spring-boot-starter-actuator health節點檢查
• 前提條件:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
• 健康節點http請求地址:
• 健康狀態響應數據:
{
"description": "Composite Discovery Client",
"status": "UP",
"discoveryComposite": {
"description": "Composite Discovery Client",
"status": "UP",
"discoveryClient": {
"description": "Composite Discovery Client",
"status": "UP",
"services": [
"fileservice",
"dj-atliwen-edi-mqconsumption"
]
},
"eureka": {
"description": "Eureka discovery client has not yet successfully connected to a Eureka server",
"status": "UP",
"applications": {
"SPRING-CLOUD-ZIPKIN": 1,
"DJ-WANGRUI-EDI-GLOBAL": 1
}
}
},
"diskSpace": {
"status": "UP",
"total": 285775753216,
"free": 173917204480,
"threshold": 10485760
},
"rabbit": {
"status": "UP",
"version": "3.7.7"
},
"db": {
"status": "UP",
"database": "MySQL",
"hello": 1
},
"binders": {
"status": "UP",
"rabbit": {
"status": "UP",
"binderHealthIndicator": {
"status": "UP",
"version": "3.7.7"
}
}
},
"configServer": {
"status": "UP",
"propertySources": [
"configClient",
"http://10.10.7.60:20080/zygfengyuwuzu/config-dev.git/application.yml"
]
},
"hystrix": {
"status": "UP"
}
}
• 異常狀態響應數據:
{
"status": "DOWN",
"discoveryComposite": {
"description": "Composite Discovery Client",
"status": "UP",
"discoveryClient": {
"description": "Composite Discovery Client",
"status": "UP",
"services": [
"fileservice",
"security-wo-remote"
]
},
"eureka": {
"description": "Eureka discovery client has not yet successfully connected to a Eureka server",
"status": "UP",
"applications": {
"SPRING-CLOUD-ZIPKIN": 1,
"DJ-WANGRUI-EDI-GLOBAL": 1
}
}
},
"diskSpace": {
"status": "UP",
"total": 285775753216,
"free": 173917204480,
"threshold": 10485760
},
"rabbit": {
"status": "UP",
"version": "3.7.7"
},
"db": {
"status": "DOWN",
"error": "org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up."
},
"binders": {
"status": "UP",
"rabbit": {
"status": "UP",
"binderHealthIndicator": {
"status": "UP",
"version": "3.7.7"
}
}
},
"configServer": {
"status": "UP",
"propertySources": [
"configClient",
"http://10.10.7.60:20080/zygfengyuwuzu/config-dev.git/application.yml"
]
},
"hystrix": {
"status": "UP"
}
}
• 異常恢復之后響應數據:
{
"description": "Composite Discovery Client",
"status": "UP",
"discoveryComposite": {
"description": "Composite Discovery Client",
"status": "UP",
"discoveryClient": {
"description": "Composite Discovery Client",
"status": "UP",
"services": [
"fileservice",
"security-wo-remote"
]
},
"eureka": {
"description": "Eureka discovery client has not yet successfully connected to a Eureka server",
"status": "UP",
"applications": {
"SPRING-CLOUD-ZIPKIN": 1,
"DJ-WANGRUI-EDI-GLOBAL": 1
}
}
},
"diskSpace": {
"status": "UP",
"total": 285775753216,
"free": 173917155328,
"threshold": 10485760
},
"rabbit": {
"status": "UP",
"version": "3.7.7"
},
"db": {
"status": "UP",
"database": "MySQL",
"hello": 1
},
"binders": {
"status": "UP",
"rabbit": {
"status": "UP",
"binderHealthIndicator": {
"status": "UP",
"version": "3.7.7"
}
}
},
"configServer": {
"status": "UP",
"propertySources": [
"configClient",
"http://10.10.7.60:20080/zygfengyuwuzu/config-dev.git/application.yml"
]
},
"hystrix": {
"status": "UP"
}
}
• 自定義健康節點:異常狀態
{
"status": "DOWN",
"test": {
"status": "DOWN",
"Error Code": 1
},
"discoveryComposite": {
"description": "Composite Discovery Client",
"status": "UP",
"discoveryClient": {
"description": "Composite Discovery Client",
"status": "UP",
"services": [
"fileservice",
"dj-atliwen-edi-mqconsumption"
]
},
"eureka": {
"description": "Eureka discovery client has not yet successfully connected to a Eureka server",
"status": "UP",
"applications": {
"SPRING-CLOUD-ZIPKIN": 1,
"SPRING-CLOUD-DASHBOARD": 1
}
}
},
"diskSpace": {
"status": "UP",
"total": 285775753216,
"free": 173916606464,
"threshold": 10485760
},
"rabbit": {
"status": "UP",
"version": "3.7.7"
},
"db": {
"status": "UP",
"database": "MySQL",
"hello": 1
},
"binders": {
"status": "UP",
"rabbit": {
"status": "UP",
"binderHealthIndicator": {
"status": "UP",
"version": "3.7.7"
}
}
},
"configServer": {
"status": "UP",
"propertySources": [
"configClient",
"http://10.10.7.60:20080/zygfengyuwuzu/config-dev.git/application.yml"
]
},
"hystrix": {
"status": "UP"
}
}
• 自定義健康監控節點 :正常狀態
{
"status": "UP",
"test": {
"status": "UP"
},
"discoveryComposite": {
"description": "Composite Discovery Client",
"status": "UP",
"discoveryClient": {
"description": "Composite Discovery Client",
"status": "UP",
"services": [
"fileservice",
"dj-atliwen-edi-mqconsumption"
]
},
"eureka": {
"description": "Eureka discovery client has not yet successfully connected to a Eureka server",
"status": "UP",
"applications": {
"SPRING-CLOUD-ZIPKIN": 1,
"DJ-WANGRUI-EDI-GLOBAL": 1
}
}
},
"diskSpace": {
"status": "UP",
"total": 285775753216,
"free": 173916557312,
"threshold": 10485760
},
"rabbit": {
"status": "UP",
"version": "3.7.7"
},
"db": {
"status": "UP",
"database": "MySQL",
"hello": 1
},
"binders": {
"status": "UP",
"rabbit": {
"status": "UP",
"binderHealthIndicator": {
"status": "UP",
"version": "3.7.7"
}
}
},
"configServer": {
"status": "UP",
"propertySources": [
"configClient",
"http://10.10.7.60:20080/zygfengyuwuzu/config-dev.git/application.yml"
]
},
"hystrix": {
"status": "UP"
}
}
• Spring boot 啟動后執行健康檢查節點
/**
* @author 李文
* @create 2019-03-14 16:22
**/
@Slf4j
@Component
public class HealthApplication implements ApplicationRunner
{
@Autowired
ConfigurableApplicationContext applicationContext;
@Override
public void run(ApplicationArguments applicationArguments) {
Map<String, HealthIndicator> handcars = applicationContext.getBeansOfType(HealthIndicator.class);
boolean b = true;
for (Map.Entry<String, HealthIndicator> entry : handcars.entrySet()) {
log.error(" entry.getValue() " + entry.getValue().getClass());
Health health = entry.getValue().health();
if (!health.getStatus().equals(Status.UP)) {
log.error(" 啟動健康檢查異常 {} ", health.toString());
b = false;
}
}
if (!b) {
applicationContext.close();
}
}
}
• 自定義健康監控節點
/**
* 自定義健康監控節點
*
* @author 李文
* @create 2019-03-14 14:14
**/
@Component
public class TestHealthIndicator implements HealthIndicator
{
@Override
public Health health() {
int errorCode = check();
if (errorCode != 0) {
return Health.down().withDetail("Error Code", errorCode).build();
}
return Health.up().build();
}
private int check() {
return 0;
}
}
補充信息:
默認狀態節點名稱為類名稱 如 TestHealthIndicator 為 test 節點
非spring boot 項目也可以集成。
HealthIndicator組件只帶的檢查項
CassandraHealthIndicator 檢查Cassandra數據庫狀況 (介於關系數據庫和非關系數據庫之間的開源產品,是最像關系數據庫的NOSQL數據庫)
DiskSpaceHealthIndicator 低磁盤空間檢查 (10 * 1024 * 1024 單位是bytes)
DataSourceHealthIndicator 檢查是否能從DataSource獲取連接 (檢查數據庫情況 只支持spring-boot-data-jpa)
關鍵代碼:
private JdbcTemplate jdbcTemplate;
private static final String DEFAULT_QUERY = "SELECT 1";
this.jdbcTemplate.query()
builder.withDetail("hello", result);
ElasticsearchHealthIndicator 檢查Elasticsearch集群狀況
JmsHealthIndicator 檢查JMS消息代理狀況
MailHealthIndicator 檢查郵件服務器狀況
MongoHealthIndicator 檢查Mongo數據庫狀況
RabbitHealthIndicator 檢查Rabbit服務器狀況
RedisHealthIndicator 檢查Redis服務器狀況
SolrHealthIndicator 檢查Solr服務器狀況
ConfigServerHealthIndicator 檢查是否能夠讀取配置文件(在應用配置中心Maven 后才生效)
關鍵日志信息:
Registering application zjs-crypto with eureka with status UP
//通知注冊中心當前狀態修改為UP
Saw local status change event StatusChangeEvent [timestamp=1552551178514, current=UP, previous=STARTING]
DiscoveryClient_ZJS-CRYPTO/10.10.12.27:zjs-crypto:11001: registering service…
// 通知請求執行成功
DiscoveryClient_ZJS-CRYPTO/10.10.12.27:zjs-crypto:11001 - registration status: 204
//健康檢查異常
啟動健康檢查異常 UNKNOWN {error=no property sources located}
Unregistering application zjs-crypto with eureka with status DOWN
//通知注冊中心修改當前狀態為DOWN
Saw local status change event StatusChangeEvent [timestamp=1552551180100, current=DOWN, previous=UP]
DiscoveryClient_ZJS-CRYPTO/10.10.12.27:zjs-crypto:11001: registering service…
// 通知請求執行成功
DiscoveryClient_ZJS-CRYPTO/10.10.12.27:zjs-crypto:11001 - registration status: 204
實際觸發的驗證類:
自定義健康監控節點類
com.zjs.password.healthIndicator.CustomHealthIndicator
com.zjs.password.healthIndicator.TestHealthIndicator
Spirng boot 相關包自帶的健康監控節點類
org.springframework.cloud.client.discovery.health.DiscoveryCompositeHealthIndicator
org.springframework.boot.actuate.health.DiskSpaceHealthIndicator
org.springframework.boot.actuate.health.RabbitHealthIndicator
org.springframework.boot.actuate.health.DataSourceHealthIndicator
org.springframework.boot.actuate.health.CompositeHealthIndicator
org.springframework.cloud.config.client.ConfigServerHealthIndicator
注意!
請使用spring boot 支持的組件。尤其是數據庫,只針對 spring-boot-data-jpa !! 如使用其他數據庫 請自定義健康節點方法自行實現。
PS : 根據引入的maven包不通,所以觸發的健康驗證類也不一樣。可想而知,是不同的包自己實現自己的健康驗證方法。