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
 
        


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM