3.Spring Cloud初相識--------Ribbon客戶端負載均衡

来源:https://www.cnblogs.com/TimerHotel/archive/2018/10/24/springcloud_03.html
-Advertisement-
Play Games

前言: 在生產環境中,未避免單點故障,每個微服務都會做高可用部署。 通白的說,就是每一個一模一樣的服務會根據需求提供多分在多台機器上。 那麼在大併發的情況下,如何分配服務可以快速得到響應,就成為了我們要解決的問題。 Ribbon就是一款優秀的客戶端負載均衡機制。 什麼是客戶端負載均衡呢? 就是由服務 ...


前言:

在生產環境中,未避免單點故障,每個微服務都會做高可用部署。

通白的說,就是每一個一模一樣的服務會根據需求提供多分在多台機器上。

那麼在大併發的情況下,如何分配服務可以快速得到響應,就成為了我們要解決的問題。

Ribbon就是一款優秀的客戶端負載均衡機制。

什麼是客戶端負載均衡呢?

就是由服務的消費方來設定負載均衡策略,選擇服務。

就像我們去超市買東西進行結賬時,選擇人少的櫃臺排隊。

我們是消費方,排哪個隊有我們自己決定。

配置測試環境:

1.配置三台服務提供者機器

2.修改埠號分別為:8001,8002,8003

3.修改HelloController返回字元串內容

(1)8001:

package com.xm.cloud.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    
    @GetMapping("/hello")
    public String sayHello() {
        return "Hello Spring Cloud!   001號機器";
    }

}

(2)8002:

package com.xm.cloud.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    
    @GetMapping("/hello")
    public String sayHello() {
        return "Hello Spring Cloud!   002號機器";
    }

}

(3)8003:

package com.xm.cloud.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    
    @GetMapping("/hello")
    public String sayHello() {
        return "Hello Spring Cloud!   003號機器";
    }

}

4.修改消費服務HelloController

package com.xm.cloud.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class HelloController {
    
    @Autowired
    private RestTemplate restTemplate;
    
    @GetMapping("/hello")
    public List<String> sayHello() {
        List<String> list = new ArrayList<>();
        for(int i=0;i<30;i++) {
            list.add(restTemplate.getForObject("http://CL-HELLO-PRODUCER/hello", String.class));
        }
        return list;
    }

}

實踐:

1.測試預設的負載均衡策略(輪詢:RoundRobinRule):

(1)預設cfg:

package com.xm.cloud.cfg;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class MyConfiguration {
    
    @LoadBalanced
    @Bean
    RestTemplate restTemplate() {
        return new RestTemplate();
    }

}

(2)測試:localhost:8080/hello

0 "Hello Spring Cloud! 002號機器"
1 "Hello Spring Cloud! 003號機器"
2 "Hello Spring Cloud! 001號機器"
3 "Hello Spring Cloud! 002號機器"
4 "Hello Spring Cloud! 003號機器"
5 "Hello Spring Cloud! 001號機器"
6 "Hello Spring Cloud! 002號機器"
7 "Hello Spring Cloud! 003號機器"
8 "Hello Spring Cloud! 001號機器"
9 "Hello Spring Cloud! 002號機器"
10 "Hello Spring Cloud! 003號機器"
11 "Hello Spring Cloud! 001號機器"
12 "Hello Spring Cloud! 002號機器"
13 "Hello Spring Cloud! 003號機器"
14 "Hello Spring Cloud! 001號機器"
15 "Hello Spring Cloud! 002號機器"
16 "Hello Spring Cloud! 003號機器"
17 "Hello Spring Cloud! 001號機器"
18 "Hello Spring Cloud! 002號機器"
19 "Hello Spring Cloud! 003號機器"
20 "Hello Spring Cloud! 001號機器"
21 "Hello Spring Cloud! 002號機器"
22 "Hello Spring Cloud! 003號機器"
23 "Hello Spring Cloud! 001號機器"
24 "Hello Spring Cloud! 002號機器"
25 "Hello Spring Cloud! 003號機器"
26 "Hello Spring Cloud! 001號機器"
27 "Hello Spring Cloud! 002號機器"
28 "Hello Spring Cloud! 003號機器"
29 "Hello Spring Cloud! 001號機器"

2.測試隨機策略(RandomRule):

(1)修改cfg:

package com.xm.cloud.cfg;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;

@Configuration
public class MyConfiguration {
    
    @LoadBalanced
    @Bean
    RestTemplate restTemplate() {
        return new RestTemplate();
    }
    
    @Bean
    public IRule myRule() {
        return new RandomRule();
    }

}

(2)測試結果:

0 "Hello Spring Cloud! 002號機器"
1 "Hello Spring Cloud! 003號機器"
2 "Hello Spring Cloud! 003號機器"
3 "Hello Spring Cloud! 002號機器"
4 "Hello Spring Cloud! 003號機器"
5 "Hello Spring Cloud! 001號機器"
6 "Hello Spring Cloud! 001號機器"
7 "Hello Spring Cloud! 002號機器"
8 "Hello Spring Cloud! 002號機器"
9 "Hello Spring Cloud! 002號機器"
10 "Hello Spring Cloud! 001號機器"
11 "Hello Spring Cloud! 003號機器"
12 "Hello Spring Cloud! 002號機器"
13 "Hello Spring Cloud! 003號機器"
14 "Hello Spring Cloud! 003號機器"
15 "Hello Spring Cloud! 002號機器"
16 "Hello Spring Cloud! 001號機器"
17 "Hello Spring Cloud! 001號機器"
18 "Hello Spring Cloud! 002號機器"
19 "Hello Spring Cloud! 003號機器"
20 "Hello Spring Cloud! 001號機器"
21 "Hello Spring Cloud! 003號機器"
22 "Hello Spring Cloud! 002號機器"
23 "Hello Spring Cloud! 002號機器"
24 "Hello Spring Cloud! 003號機器"
25 "Hello Spring Cloud! 002號機器"
26 "Hello Spring Cloud! 001號機器"
27 "Hello Spring Cloud! 001號機器"
28 "Hello Spring Cloud! 002號機器"
29 "Hello Spring Cloud! 001號機器"

3.測試最佳可用策略(最佳可用):

(1)修改cfg:

package com.xm.cloud.cfg;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

import com.netflix.loadbalancer.BestAvailableRule;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;

@Configuration
public class MyConfiguration {
    
    @LoadBalanced
    @Bean
    RestTemplate restTemplate() {
        return new RestTemplate();
    }
    
    @Bean
    public IRule myRule() {
        return new BestAvailableRule();
    }

}

(2)測試結果:

0 "Hello Spring Cloud! 003號機器"
1 "Hello Spring Cloud! 003號機器"
2 "Hello Spring Cloud! 003號機器"
3 "Hello Spring Cloud! 003號機器"
4 "Hello Spring Cloud! 003號機器"
5 "Hello Spring Cloud! 003號機器"
6 "Hello Spring Cloud! 003號機器"
7 "Hello Spring Cloud! 003號機器"
8 "Hello Spring Cloud! 003號機器"
9 "Hello Spring Cloud! 003號機器"
10 "Hello Spring Cloud! 003號機器"
11 "Hello Spring Cloud! 003號機器"
12 "Hello Spring Cloud! 003號機器"
13 "Hello Spring Cloud! 003號機器"
14 "Hello Spring Cloud! 003號機器"
15 "Hello Spring Cloud! 003號機器"
16 "Hello Spring Cloud! 003號機器"
17 "Hello Spring Cloud! 003號機器"
18 "Hello Spring Cloud! 003號機器"
19 "Hello Spring Cloud! 003號機器"
20 "Hello Spring Cloud! 003號機器"
21 "Hello Spring Cloud! 003號機器"
22 "Hello Spring Cloud! 003號機器"
23 "Hello Spring Cloud! 003號機器"
24 "Hello Spring Cloud! 003號機器"
25 "Hello Spring Cloud! 003號機器"
26 "Hello Spring Cloud! 003號機器"
27 "Hello Spring Cloud! 003號機器"
28 "Hello Spring Cloud! 003號機器"
29 "Hello Spring Cloud! 003號機器"

4.測試重試負載均衡策略(RetryRule)

(1)修改cfg:

package com.xm.cloud.cfg;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

import com.netflix.loadbalancer.BestAvailableRule;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import com.netflix.loadbalancer.RetryRule;

@Configuration
public class MyConfiguration {
    
    @LoadBalanced
    @Bean
    RestTemplate restTemplate() {
        return new RestTemplate();
    }
    
    @Bean
    public IRule myRule() {
        return new RetryRule();
    }

}

(2)測試結果:

0 "Hello Spring Cloud! 001號機器"
1 "Hello Spring Cloud! 002號機器"
2 "Hello Spring Cloud! 003號機器"
3 "Hello Spring Cloud! 001號機器"
4 "Hello Spring Cloud! 002號機器"
5 "Hello Spring Cloud! 003號機器"
6 "Hello Spring Cloud! 001號機器"
7 "Hello Spring Cloud! 002號機器"
8 "Hello Spring Cloud! 003號機器"
9 "Hello Spring Cloud! 001號機器"
10 "Hello Spring Cloud! 002號機器"
11 "Hello Spring Cloud! 003號機器"
12 "Hello Spring Cloud! 001號機器"
13 "Hello Spring Cloud! 002號機器"
14 "Hello Spring Cloud! 003號機器"
15 "Hello Spring Cloud! 001號機器"
16 "Hello Spring Cloud! 002號機器"
17 "Hello Spring Cloud! 003號機器"
18 "Hello Spring Cloud! 001號機器"
19 "Hello Spring Cloud! 002號機器"
20 "Hello Spring Cloud! 003號機器"
21 "Hello Spring Cloud! 001號機器"
22 "Hello Spring Cloud! 002號機器"
23 "Hello Spring Cloud! 003號機器"
24 "Hello Spring Cloud! 001號機器"
25 "Hello Spring Cloud! 002號機器"
26 "Hello Spring Cloud! 003號機器"
27 "Hello Spring Cloud! 001號機器"
28 "Hello Spring Cloud! 002號機器"
29 "Hello Spring Cloud! 003號機器"

5.規則比較

策略 介紹
RoundRobinRule 簡單輪詢服務列表來選擇伺服器。它是Ribbon預設的負載均衡規則。
RandomRule 隨機選擇一個可用的伺服器。
RetryRule 預設輪詢,重試多次失敗的機器從輪詢列表中淘汰。
BestAvailableRule 忽略哪些短路的伺服器,並選擇併發數較低的伺服器。
ZoneAvoidanceRule 以區域可用的伺服器為基礎進行伺服器的選擇。使用Zone對伺服器進行分類,這個Zone可以理解為一個機房、一個機架等。
WeightedResponseTimeRule 為每一個伺服器賦予一個權重值。伺服器響應時間越長,這個伺服器的權重就越小。這個規則會隨機選擇伺服器,這個權重值會影響伺服器的選擇。
AvailabilityFilteringRule 該策略繼承自上面介紹的抽象策略PredicateBasedRule,所以它也繼承了“先過濾清單,再輪詢選擇”的基本處理邏輯。

自定義負載均衡策略步驟

1.實現IRule介面

2.cfg註冊覆蓋預設負載均衡策略


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 第四篇打算作為系列最後一篇,這裡嘗試談談緩存的一些併發交互場景,包括與資料庫(特指 RDBMS)交互,和一些獨立的高併發場景相關補充處理方案(若涉及具體應用同樣將主要以Redis舉例)。 另見:分散式系統之緩存的微觀應用經驗談(三)(數據分片和集群篇) (https://yq.aliyun.com/... ...
  • 單體架構 一個歸檔包(例如war格式)包含所有功能的應用程式,通常稱為單體應用。 如圖: 儘管該應用已經使用了MVC分層與模塊化,但是由於所有部件最終都打包在一個war包中,該war包包含了整個系統所有的業務功能,這樣的應用系統稱為單體應用。 單體架構的缺陷: 1.複雜度高:隨著代碼的增多,會導致業 ...
  • 設計模式:觀察者模式 當一個對象的狀態發生改變時,依賴他的對象會全部收到通知,並自動更新。 使用場景 一個事件發生後,要執行一連串更新操作。傳統的編程方式,就是在事件的代碼之後直接加入處理邏輯,當更新得邏輯增多之後,代碼會變得難以維護,這種方式是耦合的,侵入式的,增加新的邏輯需要改變事件主題的代碼。 ...
  • 緩存穿透 概念 訪問一個不存在的key,緩存不起作用,請求會穿透到DB,流量大時DB會掛掉。 解決方案 1. 採用布隆過濾器,使用一個足夠大的bitmap,用於存儲可能訪問的key,不存在的key直接被過濾; 2. 訪問key未在DB查詢到值,也將空值寫進緩存,但可以設置較短過期時間。 緩存雪崩 概 ...
  • 下麵總結並演示了 Redis 的 常用管理命令、key 操作、字元串、集合、列表、散列類型的操作命令。 你需要掌握的 Redis 知識 "史上最全 Redis 高可用解決方案總結" "為什麼分散式一定要有Redis?" "Spring Boot Redis Cluster 實戰乾貨" "Spring ...
  • 系統介紹: 1.系統採用主流的 SSM 框架 jsp JSTL bootstrap html5 (PC瀏覽器使用) 2.springmvc +spring4.3.7+ mybaits3.3 SSM 普通java web(非maven, 附贈pom.xml文件) 資料庫:mysql 3.開發工具:my ...
  • GraphQuery " " " " " " " " " " " " GraphQuery is a query language and execution engine tied to any backend service. It is . Project Address: "GraphQue ...
  • Learun FrameWork 強大工作流引擎,讓OA更智能 互聯網的發展促使企業在信息化的道路上不斷探索,而隨著企業信息化進程的不斷深入,OA協同辦公的概念也逐步進入大眾的視野。 OA的選型關乎企業的生存發展,除了需要重視“OA技術、OA品牌、OA產品、OA服務”四大要素之外,更重要的其實是讓O ...
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...