Spring Cloud実装中@EnableDiscoveryClientと@EnableEurekaClientの違い
2433 ワード
1、Spring Cloudコード実現
1つのマイクロサービスをEureka Serverに登録する(または、Consul、redis、Zookeeperなどの他のサービス発見コンポーネント).Eureka 2.0は閉源後、Consulが主流になる利点がある.
pom.xmlファイル、Eureka Client(または他のサービス発見コンポーネントのClient)依存性の追加:
プロファイルアプリケーション.yml:
起動クラスに注記を追加@EnableEurekaClientまたは@EnableDiscoveryClient
Spring Cloud Edgwareから、@EnableDiscoveryClientまたは@EnableEurekaClientは省略できます.関連依存を加えて構成するだけで、マイクロサービスをサービス発見コンポーネントに登録できます.
両者は連絡先とは異なります.
共通点:登録センターに発見され、変更サービスにスキャンされます.
相違点:@EnableEurekaClientは登録センターとしてEurekaにのみ適用され、シーンは単一である.@EnableDiscoveryClientは、他の登録センターであってもよい.
2、ソースコード
EnableDiscoveryClient
EnableEurekaClient
1つのマイクロサービスをEureka Serverに登録する(または、Consul、redis、Zookeeperなどの他のサービス発見コンポーネント).Eureka 2.0は閉源後、Consulが主流になる利点がある.
pom.xmlファイル、Eureka Client(または他のサービス発見コンポーネントのClient)依存性の追加:
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
プロファイルアプリケーション.yml:
server:
port: 8089
spring:
application:
name: order_service
eureka:
client:
serviceUrl:
defaultZone: http://127.0.0.1:9001/eureka/
起動クラスに注記を追加@EnableEurekaClientまたは@EnableDiscoveryClient
@EnableDiscoveryClient
@SpringBootApplication
public class ProviderUserApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderUserApplication.class, args);
}
}
Spring Cloud Edgwareから、@EnableDiscoveryClientまたは@EnableEurekaClientは省略できます.関連依存を加えて構成するだけで、マイクロサービスをサービス発見コンポーネントに登録できます.
両者は連絡先とは異なります.
共通点:登録センターに発見され、変更サービスにスキャンされます.
相違点:@EnableEurekaClientは登録センターとしてEurekaにのみ適用され、シーンは単一である.@EnableDiscoveryClientは、他の登録センターであってもよい.
2、ソースコード
EnableDiscoveryClient
package org.springframework.cloud.client.discovery;
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;
import org.springframework.context.annotation.Import;
/**
* Annotation to enable a DiscoveryClient implementation.
* @author Spencer Gibb
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import(EnableDiscoveryClientImportSelector.class)
public @interface EnableDiscoveryClient {
/**
* If true, the ServiceRegistry will automatically register the local server.
*/
boolean autoRegister() default true;
}
EnableEurekaClient
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 {
}