序文:マイクロサービスアーキテクチャには、必然的に単一のマイクロサービスの複数のインスタンスがあります。では、クライアントは複数のマイクロサービスのインスタンスにリクエストをどのように割り当てることができますか?ここでは、負荷分散を使用する必要があります
1。リボンの紹介
リボンは、Netflixがリリースしたロードバランサーであり、HTTPおよびTCPクライアントの動作を制御するのに役立ちます。リボンのサービスプロバイダーアドレスリストを構成した後、リボンは自動的にサービス消費者が何らかのロードバランシングアルゴリズムに基づいて要求するのに役立ちます。リボンは、ポーリング、ランダム性など、デフォルトで多くの負荷分散アルゴリズムを提供します。これもカスタマイズできます。
リボンのgithub:https://github.com/netflix/ribbon
RibbonとEurekaをSpringCloudで使用する場合、リボンはEurekaServerからのサービスプロバイダーアドレスのリストを自動的に取得し、ロードバランスアルゴリズムに基づいて取得します。
2。リボンの実際の戦闘
1。 eurekaserver、eurekaclient1、eurekaclient2を作成します。私は以前にユーレカの使用について言及したことがあるので、ここにコードがあります:
eurekaserver:
serverApplication.java
@springbootapplication@enableeurekaserverpublic class serverApplication {public static void main(string [] args){springApplication.run(serverApplication.class、args); }}pom.xml
<?xml version = "1.0" encoding = "utf-8"?> <project xmlns = "http://maven.apache.org/pom/4.0.0" xmlns:xsi = "http://www.w3.org/2001/xmlschema-instance <http://www.w3.org/2001 xsi:schemalocation = "http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.0.sdsd"> <modelversion> 4.0.0 </modelversion> <グループ> com.cn </groupid> <Artifactid> eureka-ribbon-server </artifactid> <version> 1.0-snapshot </version> <properties> <propert.build.sourceencoding> utf-8 </project.build.sourceencoding> <java.version> 1.8 </java.version> </properties> <parent> <groupid> org.springframework.boot </groupid> <artifactid> spring-boot-boot-starter-parent </artifactid> <version> 1.5.13.Release </version> </parent> <dependencies> <shiplency> org.springframework。 <gripid> org.springframework.cloud </groupid> <artifactid> spring-cloud-clouter-eureka-server </artifactid> </dependency> </dependency> </dependency> </dependency <artifactid> spring-cloud-dependencies </artifactid> <version> edgware.sr3 </version> <type> pom </type> <scope>インポート</scope> </dependency> </dependency> </dependencymanagement> <! - スプリングブートマベンプラグインの追加<artifactid> spring-boot-maven-plugin </artifactid> </plugin> </plugins> </build> </project>
Application.Properties
server.port = 8761#注:これらの2つの構成はデフォルトで真です。 falseに変更する必要があります。そうしないと、エラーが報告されます。 connot connect server#eurekaserver eureka.client.client.register-with-eureka = false#eurekaserver eureka.client.fetch-registry = falseeureka.client.service-url.defaultzoneから登録情報を取得するかどうかを示します。
eurekaclient1:
pom.xml
<?xml version = "1.0" encoding = "utf-8"?> <project xmlns = "http://maven.apache.org/pom/4.0.0" xmlns:xsi = "http://www.w3.org/2001/xmlschema-instance <http://www.w3.org/2001 xsi:schemalocation = "http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.0.sdsd"> <modelversion> 4.0.0 </modelversion> <グループ> com.cn </groupid> <Artifactid> eureka-ribbon-client </artifactid> <version> 1.0-snapshot </version> <properties> <project.build.sourceencoding> utf-8 </project.build.sourceencoding> <java.version> 1.8 </java.version> </properties> <親> <groupid> org.springframework.boot </groupid> <artifactid> spring-boot-boot-starter-parent </artifactid> <version> 1.5.13.Release </version> </parent> <dependencies> <dependy> <groupId> org.springframework.cloud < </Dependency> <Dependency> <groupId> org.springframework.cloud </groupid> <artifactid> spring-cloud-starter-eureka </artifactid> </dependency> </dependency> </dependency> <依存関係> <依存関係> <グループ<artifactid> spring-cloud-dependencies </artifactid> <version> edgware.sr3 </version> <type> pom </type> <scope>インポート</scope> </dependency> </dependency> </dependencymanagement> <! - スプリングブートマベンプラグインの追加<artifactid> spring-boot-maven-plugin </artifactid> </plugin> </plugins> </build> </project>
server.port = 8762spring.application.name = client-8762eureka.client.service-url.defaultzone = http:// localhost:8761/eureka/
StartupクラスのコンテナにRESTTEMPLATEリモートコールインスタンスを追加し、LoadBalancedAnonotationを追加して、RestTemplateにバランスをロードする機能を備えています。
@springbootapplication@enabledisiscoveryclientpublic class clientApplication {public static void main(string [] args){springApplication.run(clientApplication.class、args); }/** * @description:@loadbalanced annotationを追加してretttemplateにロードバランス機能を追加 * @return: * @author: * @date:2018/6/15 */ @bean @loadbalanced public resttemplate getRestTemplate() }}コントローラーを作成し、RESTTEMPLATEとロードバルカルコリエントインスタンスを注入します。
パッケージcom.cn.controller; Import org.springframework.beans.factory.annotation.autowired; import org.springframework.cloud.cluent.serviceInstance; Import org.springframework.cloud.client.loadbalancer.loadbalancerclient; import import org.springframework.web.bind.annotation.getMapping; Import org.springframework.web.bind.annotation.responsebody; Import org.springframework.web.client.resttemplate;/** * @program:springclougnedscripion:@authprawe: * @authprawe: **/ @ControllPublic Class RibbonController {@autowired private loadbalancerclient loadbalancerclient; @autowired privateRestemplateStemplate; @getMapping( "/loadInstance")@responsebody public string loadinstance(){serviceInstance shoice = this.loadbalancerclient.choose( "client-87"); system.out.println(choice.getServiceId()+":"+choice.gethost()+":"+choice.getport()); return shoice.getServiceId() + ":" + choice.gethost() + ":" + choice.getport(); }}パッケージcom.cn; Import org.springframework.boot.springApplication; Import org.springframework.boot.autoconfigure.springbootframework.cloud.client.discovery.enablediscoverient; 535504 *@Create:2018-06-15 16:05 **/@springbootapplication@enablediscoveryclientPublic class clientApplication {public static void main(string [] args){springApplication.run(clientApplication.class、args); }}eurekaclient2:
pom.xmlはeurekaclient1と一致しています
application.xml:
server.port = 8763spring.application.name = client-87eureka.client.service-url.defaultzone = http:// localhost:8761/eureka
clientController.java:
パッケージcom.cn.contorller; Import org.springframework.tertype.controller; Import org.springframework.web.bind.annotation.getMapping; import org.springframework.web.bind.annotation.responsebody; 2018-06-15 16:12 **/ @ControllPublic Class ClientController {@getMapping( "/getUser")@responsebody public String getUser(){System.out.println( "get user succude"); 「ユーザーを成功させる」を返します。 }}2。起動シーケンス:
①。 eurekaserverを開始=》 eurekaclient1 =》 eurekaclient2;
②。次に、eurekaclient2のapplication.porpertiesのサーバー.port = 8763をserver.port = 8764に変更し、プロジェクトを再度開始します。
③。次のように、eurekaserver構成ページ(http:// localhost:8761/)を開きます。
④。アドレスバーにhttp:// localhost:8762/loadinstanceを入力して、数回更新します。下の図に示すように、ポートインスタンスはそれを呼び出すたびに異なることがわかります。
⑤。写真に示されているように、コンソールを見ています。
この時点で、リボンはすでに開始されています。とても簡単ではありませんか?しかし、これは最も単純なアプリケーションであり、時間の無駄です...学習に終わりはありません!
サンプルコード:https://gitee.com/lfalex/springcloud-example