簡單區分下,他們在功能上是一致的:寫在啟動類的上,開啟服務注冊發現功能。
不同的是,當注冊中心不一樣時,像:eureka、consul、zookeeper,使用是也有了區別。
EnableDiscoveryClient注解在common包中,通過項目的classpath來決定使用哪種實現,而EnableEurekaClient注解在netflix包中,只會使用eureka這種實現方式;
所以,使用EnableDiscoverClient,對任何注冊中心都適用。而EnableEurekaClient是為eureka服務的。
springcloud的Dalston或更早期的版本EnableEurekaClient是包含EnableDiscoverClient注解的,這種情況使用什么已經沒區別了。
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @EnableDiscoveryClient public @interface EnableEurekaClient { }
但是在之后版本,如Greenwich版本中是這樣的:
package org.springframework.cloud.netflix.eureka; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Inherited; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited public @interface EnableEurekaClient { }
官網推薦使用:EnableDiscoveryClient
想詳細了解可以看這篇博客:https://blog.csdn.net/boling_cavalry/article/details/82668480