本文主要研究一下NacosConfigHealthIndicatorAutoConfiguration

NacosConfigHealthIndicatorAutoConfiguration

nacos-spring-boot-project/nacos-config-spring-boot-actuator/src/main/java/com/alibaba/boot/nacos/actuate/autoconfigure/NacosConfigHealthIndicatorAutoConfiguration.java

@Configuration
@ConditionalOnClass({ HealthIndicator.class })
@AutoConfigureBefore({ EndpointAutoConfiguration.class })
@AutoConfigureAfter(NacosConfigAutoConfiguration.class)
@ConditionalOnEnabledHealthIndicator(NacosConfigConstants.ENDPOINT_PREFIX)
public class NacosConfigHealthIndicatorAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public NacosConfigHealthIndicator nacosConfigHealthIndicator() {
        return new NacosConfigHealthIndicator();
    }

}
  • NacosConfigHealthIndicatorAutoConfiguration注册了NacosConfigHealthIndicator

NacosConfigHealthIndicator

nacos-spring-boot-project/nacos-config-spring-boot-actuator/src/main/java/com/alibaba/boot/nacos/actuate/health/NacosConfigHealthIndicator.java

public class NacosConfigHealthIndicator 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 (ConfigService configService : nacosServiceFactory.getConfigServices()) {
            if (configService instanceof NacosServiceMetaData) {
                NacosServiceMetaData nacosServiceMetaData = (NacosServiceMetaData) configService;
                Properties properties = nacosServiceMetaData.getProperties();
                builder.withDetail(
                        JSON.toJSONString(
                                PropertiesUtils.extractSafeProperties(properties)),
                        configService.getServerStatus());
            }
            if (!configService.getServerStatus().toLowerCase().equals(UP_STATUS)) {
                builder.down();
            }
        }
    }
}
  • NacosConfigHealthIndicator继承了AbstractHealthIndicator;它会从nacosServiceFactory获取configService,判断configService.getServerStatus()是否是up,不是则标记为down;对于NacosServiceMetaData类型的configService,则会获取nacosServiceMetaData.getProperties()作为key,configService.getServerStatus()作为value写入到health的details

小结

NacosConfigHealthIndicatorAutoConfiguration注册了NacosConfigHealthIndicator;NacosConfigHealthIndicator继承了AbstractHealthIndicator;它会从nacosServiceFactory获取configService,判断configService.getServerStatus()是否是up,不是则标记为down;对于NacosServiceMetaData类型的configService,则会获取nacosServiceMetaData.getProperties()作为key,configService.getServerStatus()作为value写入到health的details

doc


codecraft
11.9k 声望2k 粉丝

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