国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|JavaScript|易語言|

服務器之家 - 編程語言 - JAVA教程 - 詳解spring cloud中使用Ribbon實現客戶端的軟負載均衡

詳解spring cloud中使用Ribbon實現客戶端的軟負載均衡

2021-03-22 13:55牛奮lch JAVA教程

這篇文章主要介紹了詳解spring cloud中使用Ribbon實現客戶端的軟負載均衡,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

開篇

本例是在springboot整合h2內存數據庫,實現單元測試與數據庫無關性使用resttemplate消費spring boot的restful服務兩個示例的基礎上改造而來

在使用resttemplate來消費spring boot的restful服務示例中,我們提到,調用spring boot服務的時候,需要將服務的url寫死或者是寫在配置文件中,但這兩種方式,無論哪一種,一旦ip地址發生了變化,都需要改動程序,并重新部署服務,使用ribbon的時候,可以有效的避免這個問題。

前言:

負載均衡的實現方式有兩種,分別是服務端的負載均衡和客戶端的負載均衡

服務端負載均衡:當瀏覽器向后臺發出請求的時候,會首先向反向代理服務器發送請求,反向代理服務器會根據客戶端部署的ip:port映射表以及負載均衡策略,來決定向哪臺服務器發送請求,一般會使用到nginx反向代理技術。

客戶端負載均衡:當瀏覽器向后臺發出請求的時候,客戶端會向服務注冊器(例如:eureka server),拉取注冊到服務器的可用服務信息,然后根據負載均衡策略,直接命中哪臺服務器發送請求。這整個過程都是在客戶端完成的,并不需要反向代理服務器的參與。

一、啟動eureka server

請參考該例:spring cloud中啟動eureka server

二、啟動微服務,并注冊到eureka server上

spring cloud-將spring boot服務注冊到eureka server上

為了演示負載均衡的效果,再啟動一個為服務,注意需要將端口號改成不一致

三、添加ribbon支持

1、添加ribbon的依賴

詳解spring cloud中使用Ribbon實現客戶端的軟負載均衡

2、添加負載均衡支持

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.chhliu.springboot.restful;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.boot.springapplication;
import org.springframework.boot.autoconfigure.springbootapplication;
import org.springframework.boot.web.client.resttemplatebuilder;
import org.springframework.cloud.client.loadbalancer.loadbalanced;
import org.springframework.cloud.netflix.eureka.enableeurekaclient;
import org.springframework.context.annotation.bean;
import org.springframework.web.client.resttemplate;
@springbootapplication
@enableeurekaclient
public class springbootresttemplateapplication {
   
  @autowired
  private resttemplatebuilder builder;
 
  @bean
  @loadbalanced // 添加負載均衡支持,很簡單,只需要在resttemplate上添加@loadbalanced注解,那么resttemplate即具有負載均衡的功能,如果不加@loadbalanced注解的話,會報java.net.unknownhostexception:springboot-h2異常,此時無法通過注冊到eureka server上的服務名來調用服務,因為resttemplate是無法從服務名映射到ip:port的,映射的功能是由loadbalancerclient來實現的。
  public resttemplate resttemplate() {
    return builder.build();
  }
 
  public static void main(string[] args) {
    springapplication.run(springbootresttemplateapplication.class, args);
  }
}

3、修改調用微服務的url

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.chhliu.springboot.restful.controller;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.web.bind.annotation.getmapping;
import org.springframework.web.bind.annotation.pathvariable;
import org.springframework.web.bind.annotation.restcontroller;
import org.springframework.web.client.resttemplate; 
import com.chhliu.springboot.restful.vo.user;
 
@restcontroller
public class resttemplatecontroller {
  @autowired
  private resttemplate resttemplate;
   
  @getmapping("/template/{id}")
  public user findbyid(@pathvariable long id) {// 將原來的ip:port的形式,改成注冊到eureka server上的應用名即可
    user u = this.resttemplate.getforobject("http://springboot-h2/user/" + id, user.class);
    system.out.println(u);
    return u;
  }
}

四、查看eureka server狀態

詳解spring cloud中使用Ribbon實現客戶端的軟負載均衡

五,在瀏覽器中,多次刷新http://localhost:7904/template/2地址

六、測試結果

7900端口服務:

?
1
2
3
4
hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=?
hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=?
hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=?
hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=?

7901端口服務:

?
1
2
3
4
5
hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=?
hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=?
hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=?
hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=?
hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=?

7904端口服務:

?
1
2
3
4
5
6
7
8
9
10
user [id=2, username=user2, name=李四, age=20, balance=100.00]
2017-01-23 09:58:05.682 info 7436 --- [erlistupdater-0] c.netflix.config.chaineddynamicproperty : flipping property: springboot-h2.ribbon.activeconnectionslimit to use next property: niws.loadbalancer.availabilityfilteringrule.activeconnectionslimit = 2147483647
user [id=2, username=user2, name=李四, age=20, balance=100.00]
user [id=2, username=user2, name=李四, age=20, balance=100.00]
user [id=2, username=user2, name=李四, age=20, balance=100.00]
user [id=2, username=user2, name=李四, age=20, balance=100.00]
user [id=2, username=user2, name=李四, age=20, balance=100.00]
user [id=2, username=user2, name=李四, age=20, balance=100.00]
user [id=2, username=user2, name=李四, age=20, balance=100.00]
user [id=2, username=user2, name=李四, age=20, balance=100.00]

從上面的測試結果可以看出,總共調了7904端口服務9次,其中7904端口服務調7900端口服務4次,調7901端口5次,剛好是9次

經過上面的幾個步驟,就基本使用ribbon實現了客戶端負載均衡的功能

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:http://blog.csdn.net/liuchuanhong1/article/details/54691566

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 成人综合视频在线 | 97久久久| av在线电影观看 | 懂色一区二区三区av片 | 免费看国产黄色 | 91精品视频在线播放 | 国产精品尤物麻豆一区二区三区 | 欧美综合色 | 国产福利视频在线观看 | 日韩中文字幕在线视频 | 亚洲一区中文字幕 | 欧美日韩在线免费观看 | 免费成年人视频在线观看 | 一区二区三区免费看 | 99久久婷婷国产综合精品电影 | 日本午夜精品 | 欧美综合一区 | 日韩av专区 | 国产精品一区二区三区免费 | 一区二区三区四区在线播放 | 亚洲欧美v国产一区二区 | 精品成人 | 欧美一区永久视频免费观看 | 热精品| 国产精品成人一区二区三区夜夜夜 | 国产在线看片 | 黄色国产网站 | 欧美一区二区三区视频 | 欧美一级在线 | 欧日韩在线视频 | 久久久久久久久久久免费 | 人人澡人人透人人爽 | 欧美精品在线一区 | 久久综合一区 | 国产精品污www在线观看 | h色视频在线观看 | 国产超碰人人爽人人做人人爱 | 欧美成人第一页 | 黄色成人在线视频 | 日韩av在线免费 | 日韩小视频网站 |