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

03-05 18:01