用过spring boot的都应该知道里面有一个actuator的starter,这个actuator其实非常有用,它提供了很多监控的endpoint,比如今天要讲的health,info。。。。最近在弄的spring boot admin2.0其实也是以这个jar为基础来做的
今天我们来讲一下health的endpoint。首先说一下spring boot 1.x和spring boot 2.x是有一些不一样的地方的,首先2.x版本的endpoint的地址也有有变化的,在1.x版本链接的基础上加“actuator”,以及想eureka jar的名称也不一样,配置也有些地方不一样,这些都是要注意的,如果你要从spring boot 1.x切换2.x
现在回归主题,来解剖一下这个health的endpoint
首先看一下入口HealthEndpoint这个类
标记1
这是actuator中提供大家实现自定义endpoint的注解,@endpoint也就是类似于@Controller,@ReadOperation, @WriteOperation, @DeleteOperation注解,分别对应生成Get/Post/Delete的Mapping
标记2
项目启动初始化HealthEndpoint中,属性healthIndicator被赋予CompositeHealthIndicator类,当我们访问health时,就会执行CompositeHealthIndicator的health方法
接下来看一下CompositeHealthIndicator类的health方法(看代码的注解)
#根据Spring boot的自动装配,根据相应的条件有没有满足,去加载各种的Indicator
public void addHealthIndicator(String name, HealthIndicator indicator) {
this.indicators.put(name, indicator);
}
#循环调用之前收集到的Indicator中的health方法,将结果集放在
#一个map里面
public Health health() {
Map<String, Health> healths = new LinkedHashMap();
Iterator var2 =