本文主要研究一下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


codecraft
11.9k 声望2k 粉丝

当一个代码的工匠回首往事时,不因虚度年华而悔恨,也不因碌碌无为而羞愧,这样,当他老的时候,可以很自豪告诉世人,我曾经将代码注入生命去打造互联网的浪潮之巅,那是个很疯狂的时代,我在一波波的浪潮上留下...


引用和评论

0 条评论