Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于@EnableDiscoveryClient注解。 #333

Closed
eacdy opened this issue Jan 31, 2019 · 0 comments
Closed

关于@EnableDiscoveryClient注解。 #333

eacdy opened this issue Jan 31, 2019 · 0 comments
Assignees
Labels

Comments

@eacdy
Copy link
Contributor

eacdy commented Jan 31, 2019

正在为spring cloud alibaba系列博客攒搞,发现@EnableDiscoveryClient有点问题。故而提出。

在Spring Cloud官方文档中,从Edgware开始,已经不再强制在启动类上添加@EnableDiscoveryClient注解了。
原文如下:

3.1 @EnableDiscoveryClient

Spring Cloud Commons provides the @EnableDiscoveryClient annotation. This looks for implementations of the DiscoveryClient interface with META-INF/spring.factories. Implementations of the Discovery Client add a configuration class to spring.factories under the org.springframework.cloud.client.discovery.EnableDiscoveryClient key. Examples of DiscoveryClient implementations include Spring Cloud Netflix Eureka, Spring Cloud Consul Discovery, and Spring Cloud Zookeeper Discovery.

By default, implementations of DiscoveryClient auto-register the local Spring Boot server with the remote discovery server. This behavior can be disabled by setting autoRegister=false in @EnableDiscoveryClient.

[Note]
@EnableDiscoveryClient is no longer required. You can put a DiscoveryClient implementation on the classpath to cause the Spring Boot application to register with the service discovery server.

而个人测试时发现,如果用Nacos作为服务发现,客户端不添加EnableDiscoveryClient 注解,应用启动都启动不了。这点不太符合官方的规划。

之所以不需要加EnableDiscoveryClient的动机,我在书里也有描述:

在Spring Cloud Edgware以及更高版本中,只需添加相关依赖,即可自动注册。这是由于在实际项目中,我们可能希望实现“不同环境不同配置”的效果——例如:在开发环境中,不注册到Eureka Server上,而是服务提供者、服务消费者直连,便于调测;在生产环境中,我们又希望能够享受服务发现的优势——服务消费者无需知道服务提供者的绝对地址。为适应该需求,Spring Cloud Commons进行了改进,相关Issue:spring-cloud/spring-cloud-commons#218

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants