Talk NacosConfigEndpointAutoConfiguration

sequence

In this paper, we look 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 created 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 achieve the ApplicationListener; response of NacosConfigMetadataEvent, it is determined whether nacosConfigMetadataMap specified key information exists, no config metadata parsing the data structure of key event, and then put nacosConfigMetadataMap; it provides ReadOperation invoke method, returns map contains information nacosConfigMetadata and nacosConfigGlobalProperties

summary

NacosConfigEndpointAutoConfiguration created NacosConfigEndpoint; NacosConfigEndpoint achieve the ApplicationListener; response of NacosConfigMetadataEvent, it is determined whether nacosConfigMetadataMap specified key information exists, no config metadata parsing the data structure of key event, and then put nacosConfigMetadataMap; also provides the ReadOperation invoke method, returns the map information comprises nacosConfigMetadata and nacosConfigGlobalProperties

doc

Guess you like

Origin juejin.im/post/5d8ed22d6fb9a04e054d7e99