Eurekaを使用してサービスガバナンスを達成します
機能:サービスガバナンスを実現します(サービス登録と発見)
はじめに:Spring Cloud Eurekaは、Spring Cloud Netflixプロジェクトの下でのサービスガバナンスモジュールです。 Spring Cloud Netflixプロジェクトは、Spring Cloudのサブプロジェクトの1つです。その主なコンテンツは、Netflixの一連のオープンソース製品をパッケージ化することです。 Spring Bootアプリケーション用のNetflix OSS統合を自己構成します。いくつかの簡単な注釈を使用すると、開発者はアプリケーションで一般的に使用されるモジュールをすばやく構成し、巨大な分散システムを構築できます。その主なモジュールには、サービスディスカバリー(Eureka)、サーキットブレーカー(Hystrix)、インテリジェントルーティング(Zuul)、クライアントロードバランシング(リボン)などが含まれます。
プロジェクトの実践:
サービス登録センター:ユーレカサーバー
機能:サービス登録センターは、サービス登録機能を提供します
サービスプロバイダー:Eureka-Client
機能:サービス登録センターにサービスを登録します
サービス登録センター:ユーレカサーバー
新しいスプリングブートプロジェクトの作成:eureka-server、およびそのpom.xml構成は次のとおりです。
<properties> <project.build.sourceencoding> utf-8 </project.build.sourceencoding> <project.reporting.outputencoding> utf-8 </project.reporting.outputencoding <groupid> org.springframework.cloud </groupid> <artifactid> spring-cloud-starter-eureka-server </artifactid> </dependency> </dependency> <dependencymanagement> <dependency> <依存関係> <groupid> org.springframework.cloud <バージョン> dalston.sr1 </version> <type> pom </type> <scope>インポート</scope> </dependency> </dependencies> </depencementmanagement>
サービス登録センターの機能を実装するのは非常に簡単です。プロジェクトのスタートアップクラスeurekaserverapplicationで@enableteurekaserverアノテーションを使用するだけです。
@EnableEureKaserver @SpringBootApplication Public Class eurekaserverApplication {public static void main(string [] args){new SpringApplicationBuilder(eurekaserverapplication.class).web(true).run(args); }}デフォルトでは、サービス登録センターもクライアントとして登録しようとするため、クライアント登録動作を無効にし、次の情報をApplication.Properties構成ファイルに追加する必要があります。
spring.application.name = eureka-serverserver.port = 1001eureka.instance.hostname = localhosteureka.client.register-with-eureka = falseeureka.client.fetch-registry = fals
eurekaserverapplicationを開始するには、http:// localhost:9001/にアクセスして、eurekaのページをご覧ください。 Red Boxの場所から、現在のサービス登録センターに登録されているタスクサービスインスタンスがないことがわかります。
サービスプロバイダー:Eureka-Client
各インスタンスが登録された後、登録センターにハートビートを送信するには鼓動が必要です。クライアントがサーバーで登録すると、ホストとポート、URL、ホームページなどなどのメタデータが提供されます。EurekaServerは、各クライアントインスタンスからハートビートメッセージを受信します。ハートビートが登場する場合、インスタンスは通常、登録サーバーから削除されます。
新しいスプリングブートプロジェクトの作成:eureka-client、およびそのpom.xml構成は次のとおりです。
<properties> <project.build.sourceencoding> utf-8 </project.build.sourceencoding> <project.reporting.outputencoding> utf-8 </project.reporting.outputencoding <groupid> org.springframework.cloud </groupid> <artifactid> spring-cloud-starter-eureka </artifactid> </dependency> <依存関係> <groupid> org.springframework.boot </groupid> <artifactid> spring-boot-starter-edeptid </seprency> </</</</</</</</edepancy <DependencyManagement> <Dependency> <Dependency> <GroupId> org.springframework.cloud </groupid> <artifactid> spring-cloud-dependencies </artifactid> <bersion> dalston.sr1 </version> <type> pom </type> <scope> intalt </scope> </dependencies> </dependy
また、サービスプロバイダーを実装することは非常に簡単です。プロジェクトのスタートアップクラスeurekaclientApplicationで@EnableTeureKaclientアノテーションを使用してください。
@EnableEureKaclient @SpringBootApplication public class eurekaclientApplication {public static void main(string [] args){new SpringApplicationBuilder(eurekaclientApplication.class).web(true).run(args); }}Application.Propertiesで以下を構成します
spring.application.name = eureka-clientserver.port = 9002eureka.client.serviceurl.defaultzone = http:// localhost:9001/eureka/
spring.application.nameプロパティを通じて、サービスにアクセスできるように、呼び出すときにマイクロサービスの名前を指定できます。
eureka.client.serviceurl.defaultzoneプロパティは、サービス登録センターの構成コンテンツに対応し、サービス登録センターの場所を指定します。
Server.portプロパティを使用して、さまざまなポートを設定します。
eurekaclientApplicationクラスを開始します
http:// localhost:9001/を更新すると、当社のサービスプロバイダーがサービス登録センターに登録していることがわかります
新しいDiscoveryControllerを作成します
discoveryclient.getServices()を使用して登録されたサービス名を取得し、@valueを使用して構成ファイルの情報をIPに割り当てます
@RestControllerPublic Class DiscoveryController {@Autowired Private DiscoveryClient DiscoveryClient; @value( "$ {server.port}")private string ip; @getMapping( "/client")public string client(){string services = "services:"+discoveryclient.getServices()+"ip:"+ip; System.out.println(services);返品サービス。 }}http:// localhost:9002/clientをご覧ください
最後に、2つのアノテーション@EnableTeureKaclientと @enablediscoveryclientについて説明させてください
まず第一に、両方の注釈はサービス発見の機能を実現できます。 Spring Cloud(Eureka、Consul、Zookeeperなど)には多くのディスカバリーサービスがあります。
@EnableTeureKaclientは、Spring-Cloud-Netflixに基づいています。このサービスは登録センターとしてEurekaを使用し、使用シナリオは比較的単一です。
@EnableDisCoveryClientは、Spring-Cloud-Commonsに基づいています。このサービスは、他の登録センターを採用しています。
github:https://github.com/mingyuhub/springcloud
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。