登録センターとサービスプロバイダーの2つの基本的なコンポーネントを完了しました。この記事では、Spring Cloud Ribbonを使用してクライアント側にバランスを読み込むコールサービスを紹介します。
負荷分散(LB:負荷分散)は、解決する最初の問題です。マイクロサービスの前に、LBソリューションは主に集中荷重バランス溶液であり、サービス消費者とサービスプロバイダーの間の別の独立したLBでした。 LBは通常、F5などの専用ハードウェア、またはVS、Haproxyなどのソフトウェアベースです。LBのすべてのサービスのアドレスマッピングテーブルがあります。サービス消費者がターゲットサービスを呼び出すと、最初にLBへのリクエストを開始します。 LBは、特定のポリシー(ラウンドロビンなど)を使用して、リクエストをターゲットサービスにバランスさせます。
マイクロサービスの出現は、LBの実装に関する別のアイデアを提供します。LBの機能を、集中型デバイスまたはサーバーによって提供されるのではなく、ライブラリの形でサービス消費者のプロセスに統合することです。このソリューションは、ソフトロードバランシングまたはクライアントロードバランシングと呼ばれます。 Spring Cloudでは、Eurekaのサービス登録機能と併せて、リボンサブプロジェクトは、RESTクライアントの負荷分散を実装します。
Spring Cloud Ribbonの消費者にサービスを提供します
新しいSpring-Cloud-Sample-Tutorial-Consumerプロジェクトを作成します
Spring-Cloud-Sample-Tutorialの下に、新しいSpring-Cloud-Sample-Sample-Consumerサブプロジェクトを作成します
リボンとユーレカの依存関係を追加します
<dependencies> <Dependency> <groupId> org.springframework.cloud </groupid> <artifactid> spring-cloud-starter-eureka </artifactid> </dependency> <依存関係> <groupId> <gripid> org.springframework.boot </groupid> <artifactid> spring-boot-starter-web </artifactid> </dependency> </dependencies>
applicationin.properties、登録センターアドレスを構成します
spring.application.name = consumerserver.port = 30001eureka.client.service-url.defaultzone = http:// localhost:10001/eureka/、http:// localhost:10002/eureka/
usercontrollerを作成し、@loadbalanced annotationを追加し、リボンロードバランスを有効にします
@controller @requestMapping( "user")public class usercontroller {@bean @loadbalanced rettemplate initresttemplate(){return new resttemplate(); } @autowired privateRestemplateStemplate; @RequestMapping( "add")@responsebody public string add(string username、string age){return resttemplate.getForentity( "http:// rodcer/user/add"、string.class、username、age).getbody(); }}ConsumerApplicationを作成し、@EnableTeureKaclientを追加し、サービス登録を有効にします
@enabaleeurekaclient@springbootapplicationpublic class ConsumerApplication {public static void main(string [] args){springApplication.run(ConsumerApplication.class、args); }}クラスター展開プロデューサー
クラスターのプロデューサーをシミュレートするために、新しいApplication-Profile1.PropertiesとApplication-Profile2.Propertiesがプロデューサープロジェクトで作成されます。
Application-Profile1.Properties
spring.application.name = producterserver.port = 20001eureka.client.service-url.defaultzone = http:// localhost:10001/eureka/、http:// localhost:10002/eureka/
Application-Profile2.Properties
spring.application.name = producterserver.port = 20002eureka.client.service-url.defaultzone = http:// localhost:10001/eureka/、http:// localhost:10002/eureka/
負荷効果をテストするために、呼び出されたサービスのポートを入力しました
@controller@requestmapping( "user")public class usercontroller {private logger logger = loggerfactory.getLogger(getClass()); @Autowired Private DiscoveryClientクライアント。 @RequestMapping( "add")@responsebody public string adduser(string username、string age){return " + client.getLocalserviceInstance()。gethost() +": " + client.getLocalserviceInstance()。getport(); }}テストを開始します
登録センターを開始します
それぞれActiveプロファイルをProfile1とProfile2に構成し、2回開始し、レジストリクラスターのサービススタートアップを完了します。
サービスプロバイダーを開始します
上記と同じようにサービスプロバイダーを開始します。
消費者へのサービスを開始します
サービスプロバイダーはスタンドアロンベースで実行でき、消費者アプリケーションは正常に開始できます。
確認する
http:// localhost:30001/user/browserから追加します
もう一度訪問:
負荷分散コールサービスが成功しており、デフォルトはローテーショントレーニングに従ってロードバランスです。
要約します
この記事では、クライアントロードバランシングにスプリングクラウドリボンを使用するための呼び出しを紹介して完了します。
次に、サービス監視にSpring Cloudを使用する方法を引き続き紹介します。
ソースコードのダウンロード
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。