1.什么是Ribbon
Spring Cloud Ribbon是 基于Netflix Ribbon 實作的一套客戶端的負載均衡工具,Ribbon
客戶端組件提供一系列的完善的配置,如超時,重試等,通過Load Balancer( LB )獲取到服
務提供的所有機器實體,Ribbon會自動基于某種規則(輪詢,隨機)去呼叫這些服務,
Ribbon也可以實作我們自己的負載均衡演算法,
1.1 什么是客戶端的負載均衡
行程內的LB,他是一個類別庫集成到消費端,通過消費端進行獲取提供者的地址,
生活中: 類似與你去火車站排隊進站(有三條通道),只要是正常人,都會排隊到人少的隊
伍中去,
程式中: 我們消費端 能獲取到服務提供者地址串列,然后根據某種策略去獲取一個地址進行
呼叫,

1.2 什么是服務端的負載均衡
生活中:類似與你去火車站排隊進站的時候,有一個火車站的引導員告訴你說三號通道人
少,你去三號通道排隊,
程式中:就是你消費者呼叫的是ng的地址,由ng來選擇 請求的轉發(輪詢 權重,ip_hash等)

2. 快速整合Ribbon
引入依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring‐cloud‐starter‐netflix‐eureka‐client</artifactId>
</dependency>
<!‐‐添加ribbon的依賴, eureka client啟動器已經依賴了ribbon,可以不配‐‐>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring‐cloud‐starter‐netflix‐ribbon</artifactId>
</dependency>
添加@LoadBalanced注解
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
3. Ribbon內核原理
3.1 Ribbon呼叫流程

3.2 Ribbon負載均衡策略

-
RandomRule: 隨機選擇一個Server,
-
RetryRule: 對選定的負載均衡策略機上重試機制,在一個配置時間段內當選擇
Server不成功,則一直嘗試使用subRule的方式選擇一個可用的server, -
RoundRobinRule: 輪詢選擇, 輪詢index,選擇index對應位置的Server,
-
AvailabilityFilteringRule: 過濾掉一直連接失敗的被標記為circuit tripped的
后端Server,并過濾掉那些高并發的后端Server或者使用一個AvailabilityPredicate
來包含過濾server的邏輯,其實就是檢查status里記錄的各個Server的運行狀態, -
BestAvailableRule: 選擇一個最小的并發請求的Server,逐個考察Server,如
果Server被tripped了,則跳過, -
WeightedResponseTimeRule: 根據回應時間加權,回應時間越長,權重越
小,被選中的可能性越低, -
ZoneAvoidanceRule: 復合判斷Server所在區域的性能和Server的可用性選擇
Server,
3.2.1 修改默認負載均衡策略
@Configuration
public class AppConfig {
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
@Bean
public IRule myRule() {
return new RandomRule();
}
}
3.2.2 自定義負載均衡策略
1)自定義負載演算法
public class MyRandomRule extends AbstractLoadBalancerRule {
/** 總共被呼叫的次數,目前要求每臺被呼叫5次 */
private int total = 0;
/** 當前提供服務的機器號 */
private int currentIndex = 0;
public Server choose(ILoadBalancer lb, Object key) {
if (lb == null) {
return null;
}
Server server = null;
while (server == null) {
if (Thread.interrupted()) {
return null;
}
//激活可用的服務
List<Server> upList = lb.getReachableServers();
//所有的服務
List<Server> allList = lb.getAllServers();
int serverCount = allList.size();
if (serverCount == 0) {
return null;
}
if (total < 5) {
server = upList.get(currentIndex);
total++;
} else {
total = 0;
currentIndex++;
if (currentIndex >= upList.size()) {
currentIndex = 0;
}
}
if (server == null) {
Thread.yield();
continue;
}
if (server.isAlive()) {
return (server);
}
// Shouldn't actually happen.. but must be transient or a bug.
server = null;
Thread.yield();
}
return server;
}
@Override
public Server choose(Object key) {
return choose(getLoadBalancer(), key);
}
@Override
public void initWithNiwsConfig(IClientConfig clientConfig) {
}
}
2)在SpringBoot主程式掃描的包外定義配置類
@Configuration
public class MySelfRule {
@Bean
public IRule myRule(){
return new MyRandomRule();
}
}
注意:自定義的負載均衡策略不能寫在@SpringbootApplication注解的@CompentScan
掃描得到的地方,否則自定義的配置類就會被所有的 RibbonClients共享,

3)使用@RibbonClient指定負載均衡策略,在SpringBoot主程式添加 @RibbonClient 引
入配置類
@SpringBootApplication
@RibbonClient(name = "service‐order",configuration = MySelfRule.class)
public class ServiceMemberApplication {}
3.3 Ribbon相關介面
參考: org.springframework.cloud.netflix.ribbon.RibbonClientConfiguration
IClientConfig:Ribbon的客戶端配置,默認采用DefaultClientConfigImpl實作,
IRule:Ribbon的負載均衡策略,默認采用ZoneAvoidanceRule實作,該策略能夠在多區
域環境下選出最佳區域的實體進行訪問,
IPing:Ribbon的實體檢查策略,默認采用DummyPing實作,該檢查策略是一個特殊的
實作,實際上它并不會檢查實體是否可用,而是始侄訓傳true,默認認為所有服務實體都是
可用的,
ServerList:服務實體清單的維護機制,默認采用ConfigurationBasedServerList實作,
ServerListFilter:服務實體清單過濾機制,默認采ZonePreferenceServerListFilter,該
策略能夠優先過濾出與請求方處于同區域的服務實體,
ILoadBalancer:負載均衡器,默認采用ZoneAwareLoadBalancer實作,它具備了區域
感知的能力
修改配置:
# 使用<clientName>.ribbon.<key>=<value>的形式進行配置
# 參考org.springframework.cloud.netflix.ribbon.PropertiesFactory
# PingUrl: Get http://192.168.3.1:8010/
# 指定IPing
service‐order.ribbon.NFLoadBalancerPingClassName=\
com.netflix.loadbalancer.PingUrl
# 指定IRule
service‐order.ribbon.NFLoadBalancerRuleClassName=\
com.netflix.loadbalancer.RandomRule
4 Ribbon原始碼分析

(需要原圖的朋友可以關注我的微信公眾號:輸入:Ribbon)
最后整理了一些資料!分享給需要面試刷題的朋友,也祝愿大家順利拿到自己想要的offer,這份資料主要包含了Java基礎,資料結構,jvm,多執行緒等等,由于篇幅有限,以下只展示小部分面試題,有需要完整版的朋友可以點一點鏈接跳轉領取,鏈接:戳這里免費下載,獲取碼:csdn

轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/229836.html
標籤:其他
