聊聊NacosDiscoveryHealthIndicatorAutoConfiguration

本文主要研究一下NacosDiscoveryHealthIndicatorAutoConfigurationjava

NacosDiscoveryHealthIndicatorAutoConfiguration

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

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

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,不是的話則標記爲downspring

doc

相關文章
相關標籤/搜索