序
本文主要研究一下 NacosDiscoveryEndpointsAutoConfiguration
NacosDiscoveryEndpointsAutoConfiguration
nacos-spring-boot-project/nacos-discovery-spring-boot-actuator/src/main/java/com/alibaba/boot/nacos/discovery/actuate/autoconfigure/NacosDiscoveryEndpointsAutoConfiguration.java
@Configuration
public class NacosDiscoveryEndpointsAutoConfiguration {
@Bean
@ConditionalOnMissingBean
@ConditionalOnEnabledEndpoint
public NacosDiscoveryEndpoint nacosDiscoveryEndpoint() {return new NacosDiscoveryEndpoint();
}
}
- NacosDiscoveryEndpointsAutoConfiguration 注册了 NacosDiscoveryEndpoint
NacosDiscoveryEndpoint
nacos-spring-boot-project/nacos-discovery-spring-boot-actuator/src/main/java/com/alibaba/boot/nacos/discovery/actuate/endpoint/NacosDiscoveryEndpoint.java
@Endpoint(id = NacosDiscoveryConstants.ENDPOINT_PREFIX)
public class NacosDiscoveryEndpoint {
@Autowired
private ApplicationContext applicationContext;
private static final Integer PAGE_SIZE = 100;
@ReadOperation
public Map<String, Object> invoke() {Map<String, Object> result = new HashMap<>();
result.put("nacosDiscoveryGlobalProperties",
PropertiesUtils.extractSafeProperties(applicationContext.getBean(DISCOVERY_GLOBAL_NACOS_PROPERTIES_BEAN_NAME, Properties.class)));
NacosServiceFactory nacosServiceFactory = CacheableEventPublishingNacosServiceFactory.getSingleton();
JSONArray array = new JSONArray();
for (NamingService namingService : nacosServiceFactory.getNamingServices()) {JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("servicesOfServer",
namingService.getServicesOfServer(0, PAGE_SIZE));
jsonObject.put("subscribeServices", namingService.getSubscribeServices());
array.add(jsonObject);
}
catch (Exception e) {jsonObject.put("serverStatus", namingService.getServerStatus() + ":"
+ NacosUtils.SEPARATOR + e.getMessage());
}
}
result.put("namingServersStatus", array);
return result;
}
}
- NacosDiscoveryEndpoint 提供了一个 ReadOperation,它返回的 map 里头会有 nacosDiscoveryGlobalProperties 及 namingServersStatus;namingServersStatus 是个 JSONArray,每个 JSONArray 的元素是个 JSONObject,JSONObject 有 servicesOfServer、subscribeServices;其中 servicesOfServer 通过 namingService.getServicesOfServer(0, PAGE_SIZE) 获取,PAGE_SIZE 为 100
NacosDiscoveryBeanDefinitionRegistrar
nacos-spring-context-0.3.3-sources.jar!/com/alibaba/nacos/spring/context/annotation/discovery/NacosDiscoveryBeanDefinitionRegistrar.java
public class NacosDiscoveryBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar, EnvironmentAware {
private Environment environment;
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
AnnotationAttributes attributes = AnnotationAttributes.fromMap(importingClassMetadata.getAnnotationAttributes(EnableNacosDiscovery.class.getName()));
// Register Global Nacos Properties Bean
registerGlobalNacosProperties(attributes, registry, environment, DISCOVERY_GLOBAL_NACOS_PROPERTIES_BEAN_NAME);
// Register Nacos Common Beans
registerNacosCommonBeans(registry);
// Register Nacos Discovery Beans
registerNacosDiscoveryBeans(registry);
}
@Override
public void setEnvironment(Environment environment) {this.environment = environment;}
}
- NacosDiscoveryBeanDefinitionRegistrar 实现了 ImportBeanDefinitionRegistrar, EnvironmentAware 接口,其 registerBeanDefinitions 从 importingClassMetadata 获取 EnableNacosDiscovery 的 attributes,然后通过 registerGlobalNacosProperties 方法注册 DISCOVERY_GLOBAL_NACOS_PROPERTIES_BEAN_NAME
NacosBeanUtils
nacos-spring-context-0.3.3-sources.jar!/com/alibaba/nacos/spring/util/NacosBeanUtils.java
public abstract class NacosBeanUtils {
//......
public static void registerGlobalNacosProperties(AnnotationAttributes attributes,
BeanDefinitionRegistry registry,
PropertyResolver propertyResolver,
String beanName) {if (attributes == null) {return; // Compatible with null}
AnnotationAttributes globalPropertiesAttributes = attributes.getAnnotation("globalProperties");
registerGlobalNacosProperties((Map<?, ?>) globalPropertiesAttributes, registry, propertyResolver,
beanName);
}
public static void registerGlobalNacosProperties(Map<?, ?> globalPropertiesAttributes,
BeanDefinitionRegistry registry,
PropertyResolver propertyResolver,
String beanName) {Properties globalProperties = resolveProperties(globalPropertiesAttributes, propertyResolver);
registerSingleton(registry, beanName, globalProperties);
}
public static void registerSingleton(BeanDefinitionRegistry registry, String beanName, Object singletonObject) {
SingletonBeanRegistry beanRegistry = null;
if (registry instanceof SingletonBeanRegistry) {beanRegistry = (SingletonBeanRegistry) registry;
} else if (registry instanceof AbstractApplicationContext) {
// Maybe AbstractApplicationContext or its sub-classes
beanRegistry = ((AbstractApplicationContext) registry).getBeanFactory();}
// Register Singleton Object if possible
if (beanRegistry != null) {
// Determine in advance whether injected with beans
if (!beanRegistry.containsSingleton(beanName)) {beanRegistry.registerSingleton(beanName, singletonObject);
}
}
}
//......
}
- NacosBeanUtils 的 registerGlobalNacosProperties 方法从 AnnotationAttributes 获取 globalProperties 的 AnnotationAttributes,然后通过 registerGlobalNacosProperties 解析 AnnotationAttributes 的属性,最后 registerSingleton
小结
NacosDiscoveryEndpointsAutoConfiguration 注册了 NacosDiscoveryEndpoint;NacosDiscoveryEndpoint 提供了一个 ReadOperation,它返回的 map 里头会有 nacosDiscoveryGlobalProperties 及 namingServersStatus;namingServersStatus 是个 JSONArray,每个 JSONArray 的元素是个 JSONObject,JSONObject 有 servicesOfServer、subscribeServices;其中 servicesOfServer 通过 namingService.getServicesOfServer(0, PAGE_SIZE) 获取,PAGE_SIZE 为 100
doc
- NacosDiscoveryEndpointsAutoConfiguration