本文主要研究一下NacosConfigEndpointAutoConfiguration

NacosConfigEndpointAutoConfiguration

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

@Configuration
public class NacosConfigEndpointAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnEnabledEndpoint
    public NacosConfigEndpoint nacosEndpoint() {
        return new NacosConfigEndpoint();
    }

}
  • NacosConfigEndpointAutoConfiguration创建了NacosConfigEndpoint

NacosConfigEndpoint

nacos-spring-boot-project/nacos-config-spring-boot-actuator/src/main/java/com/alibaba/boot/nacos/actuate/endpoint/NacosConfigEndpoint.java

@Endpoint(id = NacosConfigConstants.ENDPOINT_PREFIX)
public class NacosConfigEndpoint
        implements ApplicationListener<NacosConfigMetadataEvent> {

    @Autowired
    private ApplicationContext applicationContext;

    private Map<String, JSONObject> nacosConfigMetadataMap = new HashMap<>();

    @ReadOperation
    public Map<String, Object> invoke() {
        Map<String, Object> result = new HashMap<>();

        if (!(ClassUtils.isAssignable(applicationContext.getEnvironment().getClass(),
                ConfigurableEnvironment.class))) {
            result.put("error", "environment type not match ConfigurableEnvironment: "
                    + applicationContext.getEnvironment().getClass().getName());
        }
        else {

            result.put("nacosConfigMetadata", nacosConfigMetadataMap.values());

            result.put("nacosConfigGlobalProperties",
                    PropertiesUtils.extractSafeProperties(applicationContext.getBean(
                            CONFIG_GLOBAL_NACOS_PROPERTIES_BEAN_NAME, Properties.class)));
        }

        return result;
    }

    @Override
    public void onApplicationEvent(NacosConfigMetadataEvent event) {
        String key = buildMetadataKey(event);
        if (StringUtils.isNotEmpty(key) && !nacosConfigMetadataMap.containsKey(key)) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("groupId", event.getGroupId());
            jsonObject.put("dataId", event.getDataId());
            if (ClassUtils.isAssignable(event.getSource().getClass(),
                    AnnotationMetadata.class)) {
                jsonObject.put("origin", "NacosPropertySource");
                jsonObject.put("target",
                        ((AnnotationMetadata) event.getSource()).getClassName());
            }
            else if (ClassUtils.isAssignable(event.getSource().getClass(),
                    NacosConfigListener.class)) {
                jsonObject.put("origin", "NacosConfigListener");
                Method configListenerMethod = (Method) event.getAnnotatedElement();
                jsonObject.put("target",
                        configListenerMethod.getDeclaringClass().getName() + ":"
                                + configListenerMethod.toString());
            }
            else if (ClassUtils.isAssignable(event.getSource().getClass(),
                    NacosConfigurationProperties.class)) {
                jsonObject.put("origin", "NacosConfigurationProperties");
                jsonObject.put("target", event.getBeanType().getName());
            }
            else if (ClassUtils.isAssignable(event.getSource().getClass(),
                    Element.class)) {
                jsonObject.put("origin", "NacosPropertySource");
                jsonObject.put("target", event.getXmlResource().toString());
            }
            else {
                throw new RuntimeException("unknown NacosConfigMetadataEvent");
            }
            nacosConfigMetadataMap.put(key, jsonObject);
        }
    }

    private String buildMetadataKey(NacosConfigMetadataEvent event) {
        if (event.getXmlResource() != null) {
            return event.getGroupId() + NacosUtils.SEPARATOR + event.getDataId()
                    + NacosUtils.SEPARATOR + event.getXmlResource();
        }
        else {
            if (event.getBeanType() == null && event.getAnnotatedElement() == null) {
                return StringUtils.EMPTY;
            }
            return event.getGroupId() + NacosUtils.SEPARATOR + event.getDataId()
                    + NacosUtils.SEPARATOR + event.getBeanType() + NacosUtils.SEPARATOR
                    + event.getAnnotatedElement();
        }
    }

}
  • NacosConfigEndpoint实现了ApplicationListener;其响应了NacosConfigMetadataEvent,它会判断nacosConfigMetadataMap是否存在指定key的信息,没有则解析event的数据构造该key的config metadata,然后放到nacosConfigMetadataMap;它还提供了ReadOperation的invoke方法,返回的map包含nacosConfigMetadata及nacosConfigGlobalProperties信息

小结

NacosConfigEndpointAutoConfiguration创建了NacosConfigEndpoint;NacosConfigEndpoint实现了ApplicationListener;其响应了NacosConfigMetadataEvent,它会判断nacosConfigMetadataMap是否存在指定key的信息,没有则解析event的数据构造该key的config metadata,然后放到nacosConfigMetadataMap;它还提供了ReadOperation的invoke方法,返回的map包含nacosConfigMetadata及nacosConfigGlobalProperties信息

doc


codecraft
11.9k 声望2k 粉丝

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