天天看點

Ribbon【負載均衡政策】

ribbon有7種負載均衡政策可供選擇:

政策類 命名 描述
RandomRule 随機政策 随機選擇server
RoundRobinRule 輪詢政策 按照順序選擇server(ribbon預設政策)
RetryRule 重試政策 在一個配置時間段内,當選擇server不成功,則一直嘗試選擇一個可用的server
BestAvailableRule 最低并發政策 逐個考察server,如果server斷路器打開,則忽略,再選擇其中并發連結最低的server
AvailabilityFilteringRule 可用過濾政策 過濾掉一直失敗并被标記為circuit tripped的server,過濾掉那些高并發連結的server(active connections超過配置的門檻值)
ResponseTimeWeightedRule 響應時間權重重政策 根據server的響應時間配置設定權重,響應時間越長,權重越低,被選擇到的機率也就越低。響應時間越短,權重越高,被選中的機率越高,這個政策很貼切,綜合了各種因素,比如:網絡,磁盤,io等,都直接影響響應時間
ZoneAvoidanceRule 區域權重政策 綜合判斷server所在區域的性能,和server的可用性,輪詢選擇server并且判斷一個AWS Zone的運作性能是否可用,剔除不可用的Zone中的所有server

如果想要建立一個全局的負載政策,隻需添加一個配置類,也可自己擴充,添加邏輯,如下:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;

@Configuration
public class RibbonConfiguration {

    @Bean
    public IRule ribbonRule() {
        return new RandomRule();
    }
}
           

如果想要對某個服務源設定特有的政策,可以在工程啟動類上添加@RibbonClient注解,當然,對應配置代碼也需要調整:

/**
 * 自定義-标記注解
 */
public @interface AvoidScan {

}
           
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;

/**
 * Ribbon負載政策配置類,IClientConfig是針對用戶端的管理配置器,配合@RibbonClient注解使用
 */
@Configuration
@AvoidScan
public class RibbonConfiguration {

    @Autowired
    private IClientConfig config;

    @Bean
    public IRule ribbonRule(IClientConfig config) {
        return new RandomRule();
    }
}
           
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.cloud.netflix.ribbon.RibbonClients;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
import cn.springcloud.book.config.AvoidScan;
import cn.springcloud.book.config.TestConfiguration;

/**
 * 工程啟動類
 */
@SpringBootApplication
@EnableDiscoveryClient
@RibbonClient(name = "client-a", configuration = RibbonConfiguration.class)//表示針對client-a服務使用的負責政策是經過RibbonConfiguration配置類的。
//@RibbonClients(value = {
//        @RibbonClient(name = "client-a", configuration = RibbonConfiguration.class),
//        @RibbonClient(name = "client-b", configuration = RibbonConfiguration.class)
//})//這種方式跟@RibbonClient類似,不過這個是針對多個服務進行政策指定。
@ComponentScan(excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, value = {AvoidScan.class})})//表示讓工程在啟動的時候,不讓Spring掃描被@AvoidScan注解标記的類,
                                                                                                                 //因為配置的是針對特殊服務的負載政策,不是全局的,如果不排除,啟動就會報錯。
public class RibbonLoadbalancerApplication {

    public static void main(String[] args) {
        SpringApplication.run(RibbonLoadbalancerApplication.class, args);
    }

}
           

如果想使用配置檔案的方式,進行配置負責政策,文法是 client name.ribbon.*,client name是我們自己給服務取的名字,即:spring.application.name設定的值。如下:

client-a:
  ribbon:
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule #針對client-a服務使用随機政策
           

ribbon的重試機制,預設是開啟的,需要添加逾時與重試的政策配置,如下:

client-a:
  ribbon:
    ConnectTimeout: 30000
    ReadTimeout: 60000
    MaxAutoRetries: 1 #對第一次請求的服務的重試次數
    MaxAutoRetriesNextServer: 1 #要重試的下一個服務的最大數量(不包括第一個服務)
    OkToRetryOnAllOperations: true
#說明:這裡配置的ConnectTimeout和ReadTimeout是當HTTP用戶端使用的是HttpClient才生效,這個時間最終會被設定到HttpClient中。
#在設定的時候需要結合hystrix的逾時時間來綜合考慮,針對使用的場景,設定太小會導緻很多請求失敗,設定太大會導緻熔斷控制變差。
提供了7個核心接口:

接口	簡述	預設實作
IClientConfig	定義ribbon中管理配置的接口	DefaultClientConfigImpl
IRule	定義ribbon中負載均衡政策的接口	ZoneAvoidanceRule
IPing	定義定期ping服務,檢查可用性的接口	DummyPing
ServerList<Server>	定義擷取服務清單方法的接口	ConfigurationBasedServerList
ServerListFilter<Server>	定義特定場景下,擷取服務清單的方法接口	ZonePreferenceServerListFilter
ILoadBalancer	定義負載均衡選擇服務的核心方法接口	ZoneAwareLoadBalancer
ServerListUpdater	為DynamicServerListLoadBalancer定義動态更新服務清單的接口	PollingServerListUpdater