1. 程式人生 > >spring-cloud-starter-ribbon提供客戶端的軟件負載均衡算法

spring-cloud-starter-ribbon提供客戶端的軟件負載均衡算法

sub ida ice nec eat 讀取 web service ror

Ribbon是什麽?

Ribbon是Netflix發布的開源項目,主要功能是提供客戶端的軟件負載均衡算法,將Netflix的中間層服務連接在一起。Ribbon客戶端組件提供一系列完善的配置項如連接超時,重試等。簡單的說,就是在配置文件中列出Load Balancer(簡稱LB)後面所有的機器,Ribbon會自動的幫助你基於某種規則(如簡單輪詢,隨即連接等)去連接這些機器。我們也很容易使用Ribbon實現自定義的負載均衡算法。

LB方案分類

目前主流的LB方案可分成兩類:一種是集中式LB, 即在服務的消費方和提供方之間使用獨立的LB設施(可以是硬件,如F5, 也可以是軟件,如nginx),由該設施負責把訪問請求通過某種策略轉發至服務的提供方;另一種是進程內LB,將LB邏輯集成到消費方,消費方從服務註冊中心獲知有哪些地址可用,然後自己再從這些地址中選擇出一個合適的服務器。Ribbon就屬於後者,它只是一個類庫,集成於消費方進程,消費方通過它來獲取到服務提供方的地址。

三:Ribbon的主要組件與工作流程

Ribbon的核心組(均為接口類型)有以下幾個:

ServerList

用於獲取地址列表。它既可以是靜態的(提供一組固定的地址),也可以是動態的(從註冊中心中定期查詢地址列表)。

ServerListFilter

僅當使用動態ServerList時使用,用於在原始的服務列表中使用一定策略過慮掉一部分地址。

IRule

選擇一個最終的服務地址作為LB結果。選擇策略有輪詢、根據響應時間加權、斷路器(當Hystrix可用時)等。

Ribbon在工作時首選會通過ServerList來獲取所有可用的服務列表,然後通過ServerListFilter過慮掉一部分地址,最後在剩下的地址中通過IRule選擇出一臺服務器作為最終結果。

四:Ribbon提供的主要負載均衡策略介紹

1、簡單輪詢負載均衡(RoundRobin)

以輪詢的方式依次將請求調度不同的服務器,即每次調度執行i = (i + 1) mod n,並選出第i臺服務器。

2、隨機負載均衡 (Random)

隨機選擇狀態為UP的Server

3、加權響應時間負載均衡 (WeightedResponseTime)

根據相應時間分配一個Weight,相應時間越長,Weight越小,被選中的可能性越低。

4、區域感知輪詢負載均衡(ZoneAvoidanceRule)

復合判斷Server所在區域的性能和Server的可用性選擇Server

Ribbon自帶負載均衡策略比較

策略名 策略聲明 策略描述 實現說明
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的權重。當剛開始運行,沒有形成statas時,使用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。

五、使用

引入POM

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-ribbon</artifactId>
    </dependency>

基於RestTemplate

定義

package com.jsoft.testzookeeper.client.config;


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 Config {

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

說明:標註@LoadBalanced來開啟負載均衡能力,默認為輪詢方式。

使用

package com.jsoft.testzookeeper.client.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class HelloService {

    @Autowired
    private RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "sayHelloFallback")
    public String sayHello(String name) {
        return restTemplate.getForEntity("http://service-zookeeper/hello?name=" + name, String.class).getBody();
    }

    private String sayHelloFallback(String name) {
        return "service error";
    }
}

更多的配置參考官方文檔:https://github.com/Netflix/ribbon/wiki/Features,比如下面的規則配置(沒實踐過):

springboot-h2.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RandomRule // Ribbon的負載均衡策略  

說明:springboot-h2為ClientName

基於Feign:

Feign客戶端在集成時沒什麽配置項目,主要是基於配置文件來定義規則,例子參考示例代碼,下面主要是配置文件配置。

除了上面的配置外,還可以有如下指定:

主要是基於ZooKeeper的依賴關系去實現,官方參考:http://cloud.spring.io/spring-cloud-zookeeper/multi/multi_spring-cloud-zookeeper-dependencies.html,樣例配置如下:

spring.cloud.zookeeper.dependencies.service-zookeeper.required=true
spring.cloud.zookeeper.dependencies.service-zookeeper.path=/service-zookeeper
spring.cloud.zookeeper.dependencies.service-zookeeper.loadBalancerType=ROUND_ROBIN

說明:service-zookeeper是指定的別名,最終會去到/service-zookeeper,loadBalancerType是規則。

總結:

1、感覺基於ZK的負載均衡規則和Ribbon的原生規則兩個分不開,主要看樣子好像是顆粒度不同,全局已經分不同服務等設置。

2、以上結論待實踐中。估計會有很多吭。

Maven示例:

https://github.com/easonjim/spring-cloud-demo/tree/master/ZooKeeper

參考:

http://www.ccblog.cn/96.htm(以上內部部分轉自此篇文章)

http://www.cnblogs.com/wangjing666/p/6985047.html

http://blog.csdn.net/w_x_z_/article/details/71156009(負載均衡規則自定義)

http://blog.csdn.net/shunhua19881987/article/details/75466797

http://www.idouba.net/netflix-source-ribbon-rule/

http://blog.csdn.net/daybreak1209/article/details/53582366

http://blog.csdn.net/liuchuanhong1/article/details/54728406

http://www.jianshu.com/p/7bb96e2dc944

http://www.cnblogs.com/wangjing666/p/6994451.html

spring-cloud-starter-ribbon提供客戶端的軟件負載均衡算法