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