spring cloud服務發現注解之@EnableDiscoveryClient與@EnableEurekaClient區別


在使用服務發現的時候有兩種注解,

一種為@EnableDiscoveryClient,

一種為@EnableEurekaClient,

用法上基本一致,下文是從stackoverflow上面找到的對這兩者的解釋

There are multiple implementations of "Discovery Service" (eureka, consul, zookeeper). 
@EnableDiscoveryClient lives in spring-cloud-commons and picks the implementation on the classpath.
@EnableEurekaClient lives in spring-cloud-netflix and only works for eureka. If eureka is on your classpath, they are effectively the same.

意思也就是spring cloud中discovery service有許多種實現(eureka、consul、zookeeper等等)

@EnableDiscoveryClient基於spring-cloud-commons;而 @EnableEurekaClient基於spring-cloud-netflix

對@EnableEurekaClient的源碼如下:

/**
 * Convenience annotation for clients to enable Eureka discovery configuration
 * (specifically). Use this (optionally) in case you want discovery and know for sure that
 * it is Eureka you want. All it does is turn on discovery and let the autoconfiguration
 * find the eureka classes if they are available (i.e. you need Eureka on the classpath as
 * well).
 *
 * @author Dave Syer
 * @author Spencer Gibb
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@EnableDiscoveryClient
public @interface EnableEurekaClient {

}

注解@EnableEurekaClient上有@EnableDiscoveryClient注解,可以說基本就是EnableEurekaClient有@EnableDiscoveryClient的功能,另外上面的注釋中提到,其實@EnableEurekaClient注解就是一種方便使用eureka的注解而已,可以說使用其他的注冊中心后,都可以使用@EnableDiscoveryClient注解,

但是使用@EnableEurekaClient的情景,就是在服務采用eureka作為注冊中心的時候,使用場景較為單一。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM