序
本文主要研究一下 NacosDiscoveryHealthIndicatorAutoConfiguration
NacosDiscoveryHealthIndicatorAutoConfiguration
nacos-spring-boot-project/nacos-discovery-spring-boot-actuator/src/main/java/com/alibaba/boot/nacos/discovery/actuate/autoconfigure/NacosDiscoveryHealthIndicatorAutoConfiguration.java
@Configuration
@ConditionalOnClass({HealthIndicator.class})
@AutoConfigureBefore({EndpointAutoConfiguration.class})
@AutoConfigureAfter(NacosDiscoveryAutoConfiguration.class)
@ConditionalOnEnabledHealthIndicator(NacosDiscoveryConstants.ENDPOINT_PREFIX)
public class NacosDiscoveryHealthIndicatorAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public NacosDiscoveryHealthIndicator nacosDiscoveryHealthIndicator() {return new NacosDiscoveryHealthIndicator();
}
}
- NacosDiscoveryHealthIndicatorAutoConfiguration 注册了 NacosDiscoveryHealthIndicator
NacosDiscoveryHealthIndicator
nacos-spring-boot-project/nacos-discovery-spring-boot-actuator/src/main/java/com/alibaba/boot/nacos/discovery/actuate/health/NacosDiscoveryHealthIndicator.java
public class NacosDiscoveryHealthIndicator extends AbstractHealthIndicator {
@Autowired
private ApplicationContext applicationContext;
private static final String UP_STATUS = "up";
@Override
protected void doHealthCheck(Health.Builder builder) throws Exception {builder.up();
NacosServiceFactory nacosServiceFactory = CacheableEventPublishingNacosServiceFactory.getSingleton();
for (NamingService namingService : nacosServiceFactory.getNamingServices()) {if (namingService instanceof NacosServiceMetaData) {NacosServiceMetaData nacosServiceMetaData = (NacosServiceMetaData) namingService;
Properties properties = nacosServiceMetaData.getProperties();
builder.withDetail(
JSON.toJSONString(PropertiesUtils.extractSafeProperties(properties)),
namingService.getServerStatus());
}
if (!namingService.getServerStatus().equalsIgnoreCase(UP_STATUS)) {builder.down();
}
}
}
}
- NacosDiscoveryHealthIndicator 继承了 AbstractHealthIndicator,其 doHealthCheck 方法会遍历 namingService,如果 namingService 为 NacosServiceMetaData 则将 properties 的 json 作为 key,serverStatus 作为 value 填充到 detail 中;接着判断其 serverStatus 是否为 up,不是的话则标记为 down
小结
NacosDiscoveryHealthIndicatorAutoConfiguration 注册了 NacosDiscoveryHealthIndicator;NacosDiscoveryHealthIndicator 继承了 AbstractHealthIndicator,其 doHealthCheck 方法会遍历 namingService,如果 namingService 为 NacosServiceMetaData 则将 properties 的 json 作为 key,serverStatus 作为 value 填充到 detail 中;接着判断其 serverStatus 是否为 up,不是的话则标记为 down
doc
- NacosDiscoveryHealthIndicatorAutoConfiguration