聊聊NacosConfigEndpointAutoConfiguration

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

    @Autowired
    private ApplicationContext applicationContext;

    private Map nacosConfigMetadataMap = new HashMap<>();

    @ReadOperation
    public Map invoke() {
        Map 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

  • NacosConfigEndpointAutoConfiguration

你可能感兴趣的:(nacos)