聊聊NacosConfigEndpointAutoConfiguration

67次阅读

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

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

  • NacosConfigEndpointAutoConfiguration

正文完
 0