一、前言
在上一篇部落格中,小編向大家介紹了負載均衡工具Ribbon,是不是很颠覆呀,是不是很好用呀。從中大家有沒有感覺到他的負載均衡政策呀,對的,Ribbon内置的預設政策是輪詢。在這篇部落格中,小編就帶大家領略一下Ribbon自定義政策。
二、Ribbon的負載均衡政策有哪些?

首先上面的這張圖是Ribbon選擇政策,我們使用的政策重點是最下面的6個子類:
政策名 | 政策聲明 | 政策描述 | 實作說明 |
---|---|---|---|
BestAvailableRule | public class BestAvailableRule extends ClientConfigEnabledRoundRobinRule | 選擇一個最小的并發請求的server | 逐個考察Server,如果Server被tripped了,則忽略,在選擇其中ActiveRequestsCount最小的server |
AvailabilityFilteringRule | public class AvailabilityFilteringRule extends PredicateBasedRule | 過濾掉那些因為一直連接配接失敗的被标記為circuit | tripped的後端server,并過濾掉那些高并發的的後端server(active connections 超過配置的門檻值) 使用一個AvailabilityPredicate來包含過濾server的邏輯,其實就就是檢查status裡記錄的各個server的運作狀态 |
WeightedResponseTimeRule | public class WeightedResponseTimeRule extends RoundRobinRule | 根據響應時間配置設定一個weight,響應時間越長,weight越小,被選中的可能性越低。 | 一個背景線程定期的從status裡面讀取評價響應時間,為每個server計算一個weight。Weight的計算也比較簡單responsetime 減去每個server自己平均的responsetime是server的權重。當剛開始運作,沒有形成status時,使用roubine政策選擇server。 |
RetryRule | public class RetryRule extends AbstractLoadBalancerRule | 對標明的負載均衡政策機上重試機制。 | 在一個配置時間段内當選擇server不成功,則一直嘗試使用subRule的方式選擇一個可用的server |
RoundRobinRule | public class RoundRobinRule extends AbstractLoadBalancerRule | roundRobin方式輪詢選擇server | 輪詢index,選擇index對應位置的server |
RandomRule | public class RandomRule extends AbstractLoadBalancerRule | 随機選擇一個server | 在index上随機,選擇index對應位置的server |
ZoneAvoidanceRule | public class ZoneAvoidanceRule extends PredicateBasedRule | 複合判斷server所在區域的性能和server的可用性選擇server | 使用ZoneAvoidancePredicate和AvailabilityPredicate來判斷是否選擇某個server,前一個判斷判定一個zone的運作性能是否可用,剔除不可用的zone(的所有server),AvailabilityPredicate用于過濾掉連接配接數過多的Server。 |
三、自定義政策
這裡我借鑒上一篇部落格的架構,對ribbon子產品進行修改:假定這次修改為随機通路RandomRule。
3.1修改配置檔案
eureka:
client:
service-url:
defaultZone: http://localhost:/eureka/
server:
port:
spring:
application:
name: servie-ribbon
client1:
ribbon:
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
在配置檔案中,我們指明了client1服務要使用
com.netflix.loadbalancer.RandomRule
政策。
client1:
ribbon:
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
3.2 修改啟動類
添加了執行個體化與配置檔案對應的政策類。
package com.wl.ribbon;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
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 RibbonApplication {
public static void main(String[] args) {
SpringApplication.run(RibbonApplication.class, args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
}
@Bean
public IRule ribbonRule() {
return new RandomRule();//執行個體化與配置檔案對應的政策類
}
}
3.3 編寫controller
注入了LoadBalancerClient ,通過LoadBalancerClient 指明要使用的政策。
package com.wl.ribbon.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
/**
* Created by Ares on 2018/4/18.
*/
@RestController
public class HelloController {
@Autowired
RestTemplate restTemplate;
@Autowired
private LoadBalancerClient loadBalancerClient;
@RequestMapping(value = "/getUser", method = RequestMethod.GET)
public String getUser(@RequestParam("id") String id) {
this.loadBalancerClient.choose("CLIENT1");//随機通路政策
return restTemplate.getForEntity("http://CLIENT1/user/findById?id="+id, String.class).getBody();
}
@RequestMapping(value = "/hi")
public String hi(@RequestParam("id") String id){
return restTemplate.getForObject("http://CLIENT1/user/findById?id="+id,String.class);
}
}
3.4 運作
運作結果就是随機通路,和輪詢結果明顯不一樣。
四、小結
通過這次的使用,可以對比Dubbo的負載均衡政策,有興趣的通知可以了解一下。dubbo也有loadbalance 負載均衡政策,可選值:random,roundrobin,leastactive,分别表示:随機,輪循,最少活躍調用。是以一通百通。