聊聊NacosConfigHealthIndicatorAutoConfiguration

66次阅读

共计 2425 个字符,预计需要花费 7 分钟才能阅读完成。

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

  • NacosConfigHealthIndicatorAutoConfiguration

正文完
 0