オープニング
この例は、H2メモリデータベースを統合するスプリングブートに基づいており、ユニットテストとデータベースに依存しないことを実現し、RestTemplateを使用してSpring BootのRestfulサービスを消費します。
RestTemplateを使用してSpring Bootを消費するREST FULDサービスの例では、Spring Boot Serviceを呼び出すときは、サービスURLを構成ファイルに書き込む必要があるが、これら2つの方法のうち、IPアドレスが変更されると、プログラムを変更してサービスを再配置する必要があると述べました。この問題は、リボンを使用する場合、効果的に回避できます。
序文:
ソフトロードバランスを実装するには、サーバー上のロードバランスとクライアントのロードバランスを実装するには、2つの方法があります。
サーバー側のロードバランシング:ブラウザが背景にリクエストを発行すると、最初にリクエストが逆プロキシサーバーに送信されます。逆プロキシサーバーは、クライアントが展開したIP:ポートマッピングテーブルとロードバランシングポリシーに基づいてリクエストを送信するサーバーを決定します。一般に、Nginx逆プロキシテクノロジーが使用されます。
クライアントのロードバランシング:ブラウザが背景にリクエストを行うと、クライアントはサーバーに登録された利用可能なサービス情報をサービス登録者(たとえば:eurekaサーバー)にプルし、その後、どのサーバーを押してロードバランスポリシーに従ってリクエストを送信します。このプロセス全体はクライアント側で行われ、逆プロキシサーバーの参加を必要としません。
1。eurekaサーバーを開始します
この例を参照してください:Spring CloudでEurekaサーバーを開始します
2。マイクロサービスを起動し、eurekaサーバーに登録します
スプリングクラウド - スプリングブートサービスをユーレカサーバーに登録
負荷分散の効果を示すために、別のサービスを開始するには、ポート番号を一貫性に変更する必要があることに注意してください
3.リボンサポートを追加します
1.リボンの依存関係を追加します
2.負荷分散サポートを追加します
パッケージcom.chhliu.springboot.restful; Import org.springframework.beans.factory.annotation.autowired; Import org.springframework.boot.springApplication; Import org.springframework.boot.autoconfigure.springbootapplication; org.springframework.boot.web.client.resttemplateBuilderをインポートします。 org.springframework.cloud.client.loadbalancer.loadbalancedをインポートします。 Import org.springframework.cloud.netflix.eureka.EnableTeureKaclient; org.springframework.context.annotation.beanをインポートします。 org.springframework.web.client.resttemplateをインポートします。 @SpringBootApplication @ENABLEDEUREKACLIENT PUBLIC CLASS SPRINGBOOTRESTTEMPLATEAPPLICATION {@Autowired PrivateRestTemplateBuilder Builder; @bean @loadbalanced //ロードバランシングサポートの追加は非常に簡単です。 @loadbalancedアノテーションをreptemplateに追加するだけです。その後、rettemplateには負荷分散の機能があります。 @loadbalancedアノテーションが追加されていない場合、java.net.unknownhostexception:springboot-h2例外が報告されます。この時点で、RestTemplateがサービス名からIP:ポートにマッピングでき、マッピング関数がLoadBalancerclientによって実装されるため、サービス名をEurekaサーバーに登録してサービスを呼び出すことはできません。 public rettemplate rettemplate(){return builder.build(); } public static void main(string [] args){springApplication.run(springbootresttemplateapplication.class、args); }} 3.呼び出しのマイクロサービスのURLを変更します
パッケージcom.chhliu.springboot.restful.controller; Import org.springframework.beans.factory.annotation.autowired; Import org.springframework.web.bind.annotation.getMapping; org.springframework.web.bind.annotation.pathvariableをインポートします。 org.springframework.web.bind.annotation.restControllerをインポートします。 org.springframework.web.client.resttemplateをインポートします。 com.chhliu.springboot.restful.vo.userをインポートします。 @restController public class rettemplatecontroller {@autowired privateResttemplateRestTemplate; @getMapping( "/template/{id}")パブリックユーザーfindbyid(@pathvariable long id){//元のIP:ポートフォームをeurekaサーバーに登録されているアプリケーション名に変更します。 System.out.println(u); uを返します。 }}4. eurekaサーバーのステータスを確認します
5。ブラウザでは、http:// localhost:7904/template/2アドレスを何度も更新します
6。テスト結果
7900ポートサービス:
hibernate:select user0.idはid1_0_0_、user0.age as age2_0_0_、user0.balance as balance3_0_0_、name4_0_0_、user0_.username as username5_0_0_ as user0_where0_.id = as username5_0_0_ hibernate:select user0.idはid1_0_0_、user0.age as age2_0_0_、user0.balance as balance3_0_0_、name4_0_0_、user0_.username as username5_0_0_ as user0_where0_.id = as username5_0_0_ hibernate:select user0.idはid1_0_0_、user0.age as age2_0_0_、user0.balance as balance3_0_0_、name4_0_0_、user0_.username as username5_0_0_ as user0_where0_.id = as username5_0_0_ hibernate:user0.idをid1_0_0_、user0.balanceとしてbalance3_0_0_、user0.name as name4_0_0_、user0_.username as username5_0_0_ from user0_ as user0_.id =?
7901ポートサービス:
hibernate:select user0.idはid1_0_0_、user0.age as age2_0_0_、user0.balance as balance3_0_0_、name4_0_0_、user0_.username as username5_0_0_ as user0_where0_.id = as username5_0_0_ hibernate:select user0.idはid1_0_0_、user0.age as age2_0_0_、user0.balance as balance3_0_0_、name4_0_0_、user0_.username as username5_0_0_ as user0_where0_.id = as username5_0_0_ hibernate:select user0.idはid1_0_0_、user0.age as age2_0_0_、user0.balance as balance3_0_0_、name4_0_0_、user0_.username as username5_0_0_ as user0_where0_.id = as username5_0_0_ hibernate:user0.idをid1_0_0_、user0.balanceとしてbalance3_0_0_、user0.name as name4_0_0_、user0_.username as username5_0_0_ from user0_ as user0_.id =? hibernate:select user0.idはid1_0_0_、user0.age as age2_0_0_、user0.balance as balance3_0_0_、name4_0_0_、user0_.username as username5_0_0_ as user0_where0_.id = as username5_0_0_
7904ポートサービス:
user [id = 2、username = user2、name = li si、age = 20、balance = 100.00] 2017-01-23 09:58:05.682 Info 7436 - [erlistupdater-0] c.netflix.config.chaindydynamicproperty:Flipping Property:Springboot-H2.ribbonのUsebrot-H2.ribbonの使用者である。 niws.loadbalancer.abailabilityfilteringrule.activeconnectionslimit = 2147483647 user [id = 2、username = user2、name = li si、age = 20、balance = 100.00] user [id = 2、username = user2、name = li si、age = 20、balance = 100]ユーザー年齢= 20、残高= 100.00] user [id = 2、username = user2、name = li si、age = 20、balance = 100.00] user [id = 2、username = user2、name = li si、age = 20、balance = 100.00] user [id = 2、username = user2、name = li si、age = li si = 100]ユーザー[id = 100]ユーザー年齢= 20、残高= 100.00] user [id = 2、username = user2、name = li si、age = 20、残高= 100.00]
上記のテスト結果から、合計7904ポートサービスが9回調整され、そのうち7904ポートサービスが4回調整され、7901ポートが5回調整されたことがわかります。
上記の手順の後、リボンは基本的にクライアントの負荷分散機能を実現するために使用されます。
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。