前一阵看到有篇博客说cloud从Edgware版本开始,可以不加@EnableDiscoveryClient注解,只要配置好注册中心的相关配置即可自动开启服务注册功能,比较好奇其中的原理,研究了一番特意记录下来
先看一下@EnableDiscoveryClient注解,只有一个属性:autoRegister, 默认为true
@SpringBootApplication@EnableDiscoveryClient(autoRegister = true)public class NacosApplication { public static void main(String[] args) { SpringApplication.run(NacosApplication.class, args); } }
内部信息(代码里只保留了重点)
@Import(EnableDiscoveryClientImportSelector.class)public @interface EnableDiscoveryClient { boolean autoRegister() default true;}
EnableDiscoveryClientImportSelector
@Overridepublic String[] selectImports(AnnotationMetadata metadata) { String[] imports = super.selectImports(metadata); AnnotationAttributes attributes = AnnotationAttributes.fromMap( metadata.getAnnotationAttributes(getAnnotationClass().getName(), true)); //获取@EnableDiscoveryClient的autoRegister属性 boolean autoRegister = attributes.getBoolean("autoRegister"); //开启自动注册的话,将AutoServiceRegistrationConfiguration类的全限定名返回,会被spring加载到bean容器 if (autoRegister) { List<String> importsList = new ArrayList<>(Arrays.asList(imports)); importsList.add( "org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationConfiguration"); imports = importsList.toArray(new String[0]); } //关闭自动注册的话,将***.auto-registration.enabled设置为false,放入环境属性中(其他地方会用到) else { Environment env = getEnvironment(); if (ConfigurableEnvironment.class.isInstance(env)) { ConfigurableEnvironment configEnv = (ConfigurableEnvironment) env; LinkedHashMap<String, Object> map = new LinkedHashMap<>(); map.put("spring.cloud.service-registry.auto-registration.enabled", false); MapPropertySource propertySource = new MapPropertySource( "springCloudDiscoveryClient", map); configEnv.getPropertySources().addLast(propertySource); } } return imports;}
AutoServiceRegistrationConfiguration
//当**.registration.enabled属性为true时,此配置类会被加载//spring-cloud-commons.jar包内的spring-configuration-metadata.json定义了此属性默认为true//即: 不配置@EnableDiscoveryClient或者配置@EnableDiscoveryClient设置autoRegister为true时//AutoServiceRegistrationConfiguration会被加载,当@EnableDiscoveryClient的autoRegister//为false时,此配置类不会被加载@Configuration(proxyBeanMethods = false)@EnableConfigurationProperties(AutoServiceRegistrationProperties.class)@ConditionalOnProperty(value = "spring.cloud.service-registry.auto-registration.enabled", matchIfMissing = true)public class AutoServiceRegistrationConfiguration {}
AutoServiceRegistrationConfiguration是实现自动注册的入口,具体的逻辑在由注册中心实现(如Nacos\Eureka...)
spring: application: name: nacosTest cloud: nacos: discovery: enabled: true(默认为true,可以不配置) server-addr: 192.168.1.10 port: 8848 username: nacos password: nacos
Nacos实现逻辑
spring-cloud-starter-alibaba-nacos-discovery.jar/META-INF/spring.factories:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.alibaba.cloud.nacos.discovery.NacosDiscoveryAutoConfiguration,\ com.alibaba.cloud.nacos.ribbon.RibbonNacosAutoConfiguration,\ com.alibaba.cloud.nacos.endpoint.NacosDiscoveryEndpointAutoConfiguration,\ com.alibaba.cloud.nacos.registry.NacosServiceRegistryAutoConfiguration,\ com.alibaba.cloud.nacos.discovery.NacosDiscoveryClientConfiguration,\ com.alibaba.cloud.nacos.discovery.reactive.NacosReactiveDiscoveryClientConfiguration,\ com.alibaba.cloud.nacos.discovery.configclient.NacosConfigServerAutoConfiguration,\ com.alibaba.cloud.nacos.NacosServiceAutoConfigurationorg.springframework.cloud.bootstrap.BootstrapConfiguration=\ com.alibaba.cloud.nacos.discovery.configclient.NacosDiscoveryClientConfigServiceBootstrapConfiguration
打开NacosServiceRegistryAutoConfiguration.java, 内部实现了服务自动注册逻辑
@Configuration(proxyBeanMethods = false)@EnableConfigurationProperties//确保spring.cloud.nacos.discovery.enabled为true(开启nacos的服务注册与发现功能)@ConditionalOnNacosDiscoveryEnabled//默认为true,只有开启了@EnableDiscoveryClient注解,并且配置autoRegister为false时,//此属性值才为false@ConditionalOnProperty(value = "spring.cloud.service-registry.auto-registration.enabled", matchIfMissing = true)@AutoConfigureAfter({ AutoServiceRegistrationConfiguration.class, AutoServiceRegistrationAutoConfiguration.class, NacosDiscoveryAutoConfiguration.class })public class NacosServiceRegistryAutoConfiguration { @Bean public NacosServiceRegistry nacosServiceRegistry( NacosDiscoveryProperties nacosDiscoveryProperties) { return new NacosServiceRegistry(nacosDiscoveryProperties); } @Bean @ConditionalOnBean(AutoServiceRegistrationProperties.class) public NacosRegistration nacosRegistration( ObjectProvider<List<NacosRegistrationCustomizer>> registrationCustomizers, NacosDiscoveryProperties nacosDiscoveryProperties, ApplicationContext context) { return new Nac......原文转载:http://www.shaoqun.com/a/853301.html
跨境电商:https://www.ikjzd.com/
houzz:https://www.ikjzd.com/w/236
四海商舟:https://www.ikjzd.com/w/1516
网上1号店:https://www.ikjzd.com/w/2263
前一阵看到有篇博客说cloud从Edgware版本开始,可以不加@EnableDiscoveryClient注解,只要配置好注册中心的相关配置即可自动开启服务注册功能,比较好奇其中的原理,研究了一番特意记录下来先看一下@EnableDiscoveryClient注解,只有一个属性:autoRegister,默认为true@SpringBootApplication@EnableDiscoveryC
拍拍:https://www.ikjzd.com/w/2205
c88:https://www.ikjzd.com/w/1017.html
某跨境大卖因涉嫌合同诈骗案,已被立案侦查!:https://www.ikjzd.com/articles/107398
亚马逊想干嘛?竟故意允许一些卖家违规?:https://www.ikjzd.com/articles/107399
W-8BEN席卷亚马逊,法国和意大利即将严查VAT:https://www.ikjzd.com/articles/107400
中国亚马逊卖家数量排名公布 / 亚马逊测试一键式产品评论系统:https://www.ikjzd.com/articles/107401
女人喜欢男人㖭 女人喜欢被㖭是什么感觉:http://lady.shaoqun.com/a/247876.html
被两个男人同时前后做 两个人一前一后一进一出着:http://www.30bags.com/m/a/249860.html
唤起平淡性生活的秘诀 9种做爱技巧激发性高潮 :http://lady.shaoqun.com/a/407848.html
卖家终结者?亚马逊、独立站起诉3万卖家侵权狂赚60亿:你要赶紧保护好商标专利了!:https://www.ikjzd.com/articles/146446
亚马逊是如何检测是否在刷单的?:https://www.ikjzd.com/articles/146436
Facebook这些小知识点,你造嘛?:https://www.ikjzd.com/articles/146431
No comments:
Post a Comment