聊聊NacosConfigHealthIndicatorAutoConfiguration

本文主要研究一下NacosConfigHealthIndicatorAutoConfigurationjava

NacosConfigHealthIndicatorAutoConfiguration

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

@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.javagithub

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的detailsspring

doc

相關文章
相關標籤/搜索