サービスガバナンスは、マイクロサービスアーキテクチャで最もコアで基本的なモジュールであると言えます。主に、さまざまなマイクロサービスインスタンスの自動登録と発見を実現するために使用されます。
Spring Cloud Eurekaは、Spring Cloud Netflix Microservices Suiteの一部であり、主にマイクロサービスアーキテクチャのサービスガバナンス機能を完了する責任があります。
この記事では、簡単な小さな例を使用して、ユーレカを通じてサービスを管理する方法を共有しています。
=============私はゴージャスな分割線==================================
1.サービス登録センターを構築します
最初に完全なディレクトリ構造をリストします。
建設プロセスは次のとおりです。
1. Mavenプロジェクトの作成:Eureka(特定の実装省略)
2. POMファイルを変更し、依存関係を導入します
<Project XMLNS = "http://maven.apache.org/pom/4.0.0" xmlns:xsi = "http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation = "http://maven.apach/4.0. http://maven.apache.org/xsd/maven-4.0.0.xsd "> <modelversion> 4.0.0 </modelversion> <groupid> com.sam </groupid> <artifactid> eureka </artifactid> <バージョン> 0.0.1-snapshot </version <parent> <groupid> org.springframework.boot </groupid> <artifactid> spring-boot-boot-starter-parent </artifactid> <version> 1.5.1.release </version> </parent> <properties> <javaversion> 1.8 </javaversion> </properties> < <groupid> org.springframework.cloud </groupid> <artifactid> spring-cloud-dependencies </artifactid> <bersion> camden.sr6 </version> <type> pom </type> <scope> Import </scope> </redency> </dependency> </dependency> < <groupId> org.springframework.cloud </groupid> <artifactid> sprud-cloud-starter-eureka-server </artifactid> </dependency> </dependency> </dependencies> </project>
3.スタートアップクラスを作成します
/** * * @EnableTeureKaserver *プロジェクトがeurekaであることを指定するために使用されたサービス登録センター */@enabableeurekaserver@springbootapplicationpublic class eurekaapp {public static void main(string [] args){springapplication.run(eurekaapp.class、args); }} 4。Application.Propertiesファイルを構成します
#Tomcatサービスポート番号サーバーをセットします。ポート= 1111#サービス名Spring.Application.Name = eureka-Serviceeureka.instance.hostname = localhost#レジスタセンターはeureka.client.client-with-eureka = false registationセンターを発見する必要はありません。 leureka.client.serviceurl.defaultzone = http:// $ {eureka.instance.hostname}:$ {server.port}/eureka 5.サービスを開始してアクセスすると、次のような写真が表示されます。
2.サービスプロバイダーを登録します
最初に完全なディレクトリ構造をリストします。
建設プロセスは次のとおりです。
1. Mavenプロジェクトの作成:Hello-Service(特定の実装省略)
2. POMファイルを変更し、依存関係を導入します
<Project XMLNS = "http://maven.apache.org/pom/4.0.0" xmlns:xsi = "http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation = "http://maven.apach/4.0. http://maven.apache.org/xsd/maven-4.0.0.0.xsd "> <modelversion> 4.0.0 </modelversion> <groupid> com.sam </groupid> <artifactid> hello-service </artifactid> <バージョン> 0.0.1-snapshot </version <parent> <groupid> org.springframework.boot </groupid> <artifactid> spring-boot-boot-starter-parent </artifactid> <version> 1.5.1.release </version> </parent> <properties> <javaversion> 1.8 </javaversion> </properties> <groupid> org.springframework.cloud </groupid> <artifactid> spring-cloud-dependencies </artifactid> <bersion> camden.sr6 </version> <type> pom </type> <scope>インポート</scope> </dependency> </dependency> </dependencies> < <gripid> org.springframework.cloud </groupid> <artifactid> spring-cloud-starter-eureka </artifactid> </dependency> </dependency> </dependencies> </project>
3.スタートアップクラスを作成します
/***** @enablediscoveryclient*サービスを使用してeureka server*を使用してサービス登録と発見を実装します**/@enablediscoveryclient@sprinadbootapplicationクラスhelloapp {public static void main(] string [] args){springapplication.run(hellapp.class、args); }}4.コントローラーを作成します
@RestControllerPublic Class HelloController {logger logger = loggerFactory.getLogger(hellocontroller.class); @Autowired DiscoveryClient DiscoveryClient; @RequestMapping( "/hello")public string hello(){serviceinstance instance = discoveryclient.getLocalserviceInstance(); // Service ID logger.info( "*********" + instance.getServiceid()); 「こんにちは、これはhello-service」を返します。 }} 5。Application.Propertiesファイルを構成します
server.port = 9090#サービス名Spring.Application.Name = Hello-Service#サービス登録センターのURLを設定します。このサービスは、サービス登録センターeureka.client.serviceurl.defaultzone = http:// localhost:1111/eurekaに登録する必要があります。
6。開始とテスト
1.)開始後、Hello-Serviceコンソールにはこの単語があります(xxxはPC名を表します)
コードを次のようにコピーします:登録インスタンスhello-service/xxx:hello-service:9090 with status up(replication = false)
次の単語はeurekaコンソールに印刷されます(xxxはあなたのPC名を表します)
コードを次のようにコピーします:登録インスタンスhello-service/xxx:hello-service:9090 with status up(replication = false)
2.)LocalHost:1111に再びアクセスすると、登録センターに登録されているサービスがあることがわかります
3。サービスの発見と消費
完全なディレクトリ構造は次のとおりです。
建設プロセス:
1。Mavenプロジェクトを作成します(特定の実装省略)
2. POMファイルを変更し、依存関係を導入します
<Project XMLNS = "http://maven.apache.org/pom/4.0.0" xmlns:xsi = "http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation = "http://maven.apach/4.0. http://maven.apache.org/xsd/maven-4.0.0.xsd "> <modelversion> 4.0.0 </modelversion> <groupid> com.sam </groupid> <artifactid> hello-consumer </artifactid> <バージョン> 0.0.1-snapshot </version <parent> <groupid> org.springframework.boot </groupid> <artifactid> spring-boot-boot-starter-parent </artifactid> <version> 1.5.1.release </version> </parent> <properties> <javaversion> 1.8 </javaversion> </properties> <groupid> org.springframework.cloud </groupid> <artifactid> spring-cloud-dependencies </artifactid> <version> camden.sr6 </version> <type> pom </type> <scope>インポート</scope> </dependency> </dependency> </dependency> < <groupid> org.springframework.cloud </groupid> <artifactid> spring-cloud-starter-eureka </artifactid> </dependency> <! - 導入リボン依存関係は、負荷分散を実現するために使用されます。ここで使用しているだけなので、他の場所で紹介しません - > <依存関係> <groupid> org.springframework.cloud </groupid> <artifactid> spring-cloud-starter-ribbon </artifactid> </dependency> </dependencies> </project>
ここには、Hello-Serviceサービスプロバイダーよりも多くのリボン依存関係があります
3.スタートアップクラスを作成します
@enabledisIscoveryClient@SpringBootApplicationPublic Class ConsumerApp {//@beanはメソッドに適用され、メソッドリターン値をbean @bean @loadbalanced //@loadbalancedに設定するために使用されます。 } public static void main(string [] args){springApplication.run(consumerApp.class、args); }}@EnableDisCoveryClientもここで使用して、サービスがeurekaサーバーを使用してサービスの登録と発見を実現できるようにする必要があります
4.コントローラーを作成します
@RestControllerPublic Class ConsumerController {//ここに挿入されたRESTTEMPLATEは、@Bean経由でcom.sam.sam.consumerapp @autowired rettemplate rettemplateで構成されたインスタンスです。 @RequestMapping( "/hello-consumer")public string helloconsumer(){// hello-serviceサービスを呼び出して、サービス名はここで使用されていることに注意してください。 「こんにちは消費者仕上げ!!!」を返します。 }}5。Application.Propertiesファイルを構成します
server.port = 9999spring.application.name = hello-consumereureka.client.serviceurl.defaultzone = http:// localhost:1111/eureka#構成プロジェクトは、サービスプロバイダーと同じです。
6。開始、テスト1.)Eurekaを開始します。バランスの責任の影響を示すために、Hello-Serviceは2つのサービスを開始します。 2つのサービスを開始するための特定の手順は次のとおりです
上記は、ポート番号9090のHello-Service1のスタートアップステップです。同じ方法がHello-Service2に設定され、ポート番号9091(特定の実装は省略されています)。
2.)Hello-Consumerを開始します
3.)http:// localhost:1111/再びアクセスすると、2つのハローサービスサービス(ポート番号は9090および9091)と1つのhello-consumeサービスがあることがわかります
4.)http:// localhost:9999/hello-consumerに複数回アクセスした後、hello-service1とhello-service2が順番に呼び出されることがわかります(バランスの責任があります)。両方のコンソールを介してコンテンツを確認できます(loggerlogger.info( "****************" + intance intance intances entsentsersid)はい、これは印刷です)
4。概要
上記の例は、基本的なサービスガバナンスを実現しています:
PS:私が使用しているIDEはSpring Tool Suiteであることをここで説明させてください。これはSpring Eclipseのカスタマイズされたバージョンであり、開発にSpringを使用するのに便利です。興味のある友達はそれについてBaiduで学ぶことができます。
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。