spring健康检查

引入依赖

		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

在配置文件中加入

management.endpoints.web.exposure.include=*
management.endpoint.health.enabled=true
management.endpoint.health.show-details=always

可以通过访问 服务名:端口号/actuator

{
_links: {
self: {
href: "http://localhost:8080/actuator",
templated: false
},
beans: {
href: "http://localhost:8080/actuator/beans",
templated: false
},
caches: {
href: "http://localhost:8080/actuator/caches",
templated: false
},
caches-cache: {
href: "http://localhost:8080/actuator/caches/{cache}",
templated: true
},
health: {
href: "http://localhost:8080/actuator/health",
templated: false
},
health-path: {
href: "http://localhost:8080/actuator/health/{*path}",
templated: true
},
info: {
href: "http://localhost:8080/actuator/info",
templated: false
},
conditions: {
href: "http://localhost:8080/actuator/conditions",
templated: false
},
configprops: {
href: "http://localhost:8080/actuator/configprops",
templated: false
},
configprops-prefix: {
href: "http://localhost:8080/actuator/configprops/{prefix}",
templated: true
},
env-toMatch: {
href: "http://localhost:8080/actuator/env/{toMatch}",
templated: true
},
env: {
href: "http://localhost:8080/actuator/env",
templated: false
},
loggers-name: {
href: "http://localhost:8080/actuator/loggers/{name}",
templated: true
},
loggers: {
href: "http://localhost:8080/actuator/loggers",
templated: false
},
heapdump: {
href: "http://localhost:8080/actuator/heapdump",
templated: false
},
threaddump: {
href: "http://localhost:8080/actuator/threaddump",
templated: false
},
prometheus: {
href: "http://localhost:8080/actuator/prometheus",
templated: false
},
metrics-requiredMetricName: {
href: "http://localhost:8080/actuator/metrics/{requiredMetricName}",
templated: true
},
metrics: {
href: "http://localhost:8080/actuator/metrics",
templated: false
},
scheduledtasks: {
href: "http://localhost:8080/actuator/scheduledtasks",
templated: false
},
mappings: {
href: "http://localhost:8080/actuator/mappings",
templated: false
}
}
}

需要实现自定义健康检查断点,需要引入依赖

		<dependency>
            <groupId>io.micrometer</groupId>
            <artifactId>micrometer-registry-prometheus</artifactId>
        </dependency>

定义一个名为MyHealth的actuator

/**
 * 1. 通过实现HealthIndicator接口来定制健康状态对象(Health)返回
 * 2. 继承AbstractHealthIndicator
 */
@Component
public class MyHealthIndicator extends AbstractHealthIndicator {

    @Autowired
    HealthDemo healthDemo;
    @Override
    protected void doHealthCheck(Health.Builder builder) throws Exception {
        // 自定义检查方法
        int check = healthDemo.check();
        if (check == 1) {
            // 存活
            builder.up().
                    withDetail("code",200)
                    .build();
        } else {
            // 下线
            builder.down()
                    .withDetail("code", 404)
                    .build();
        }
    }
}

通过http://localhost:8080/actuator/health

{
	status: "UP",
	components: {
		db: {
			status: "UP",
			details: {
				database: "MySQL",
				validationQuery: "isValid()"
			}
		},
		diskSpace: {
			status: "UP",
			details: {
				total: 1000240963584,
				free: 385116925952,
				threshold: 10485760,
				exists: true
			}
		},
		my: {
			status: "UP",
			details: {
				code: 200
			}
		},
		ping: {
			status: "UP"
		}
	}
}

使用MeterRegistry实现计数

@Component
public class HealthDemo {
    Counter counter = null;
    public int check() {
        // 业务代码判断组件是否是存活状态
        return 1;
    }

    public HealthDemo(MeterRegistry meterRegistry) {
        // 得到一个名为myHealth.health的计数器
        counter = meterRegistry.counter("myHealth.health");
    }

    public void haha() {
        System.out.println("haha");
        counter.increment();
    }
}
 @PostMapping("/haha")
    public void haha(@RequestBody @Valid GetDeviceListByDeptReq req) {
        healthDemo.haha();
    }

调用后http://localhost:8080/actuator/metrics/myHealth.health查看


{
	name: "myHealth.health",
	description: null,
	baseUnit: null,
	measurements: [{
		statistic: "COUNT",
		value: 11
	}],
	availableTags: []
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值