Solon

solon.health

v2.7.1 native </> markdown

此插件,由社区成员(浅念)贡献

<dependency>
    <groupId>org.noear</groupId>
    <artifactId>solon.health</artifactId>
</dependency>

1、描述

基础扩展插件,为 Solon Web 或服务提供公共的健康检测支持。只要加上插件,什么都不做:

  • 就能提供健康检测地址:/healthz

2、使用示例

如果有需要,再添加几个自定义的检测指标:

@Configuration
public class Config {
    @Bean
    public void initHealthCheckPoint() {
        //test...
        HealthChecker.addIndicator("preflight", Result::succeed);
        HealthChecker.addIndicator("test", Result::failure);
        HealthChecker.addIndicator("boom", () -> {
            throw new IllegalStateException();
        });
    }
}

检测效果

GET /healthz
Response Code:
200 : Everything is fine
503 : At least one procedure has reported a non-healthy state
500 : One procedure has thrown an error or has not reported a status in time
Response:
{"status":"ERROR","details":{"test":{"status":"DOWN"},"boom":{"status":"ERROR"},"preflight":{"status":"UP","details":{"total":987656789,"free":6783,"threshold":7989031}}}}

3、实战示例

@Component("persistent")
public class PersistentHealth implements HealthIndicator{
    @Inject
    UserMapper userMapper;
    
    @Inject
    RedisClient redisClient;

    @Override
    public Result get(){
        userMapper.getUser(1);
        redisClient.getHash("user:1");
        
        return Result.succeed();
    }
}

v2.2.3 之前,只支持手动注册。需要补充配置代码:

@Configuration
public class Config {
    @Bean
    public void initHealthCheckPoint(@Inject PersistentHealth persistent) {
        HealthChecker.addIndicator("persistent", persistent);
    }
}