亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

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

發布時間:2020-09-12 14:42:08 來源:腳本之家 閱讀:320 作者:牛奮lch 欄目:編程語言

開篇

本例是在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、添加負載均衡支持

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-h3異常,此時無法通過注冊到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

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-h3/user/" + id, User.class); 
    System.out.println(u); 
    return u; 
  } 
} 

四、查看Eureka Server狀態

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

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

六、測試結果

7900端口服務:

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端口服務:

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端口服務:

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-h3.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實現了客戶端負載均衡的功能

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

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

郑州市| 江北区| 察哈| 祥云县| 卢湾区| 布尔津县| 虞城县| 安岳县| 达拉特旗| 青冈县| 临江市| 应城市| 永春县| 包头市| 体育| 陕西省| 濮阳市| 平湖市| 专栏| 团风县| 黄骅市| 东山县| 崇州市| 博客| 安溪县| 会泽县| 福海县| 樟树市| 武汉市| 五指山市| 厦门市| 高淳县| 惠安县| 宿迁市| 辽宁省| 广德县| 庆安县| 清苑县| 青铜峡市| 中宁县| 于都县|