一、官方文檔解讀
二、自定義Ribbon客戶端-【方式一】配置類
2.1、自定義負載規則
步驟一、增加TestConfiguration配置類
@Configuration public class TestConfiguration { public IRule ribbonRule() { return new RandomRule();//隨機負載 } }
其中:RibbonClient中name 微服務名稱,configuration配置類
注意:configuration等於的TestConfiguration必須是@Configuration,但要注意它不在主應用程序上下文的@ComponentScan中,否則它將被所有@RibbonClients共享。如果使用@ComponentScan(或@SpringBootApplication),則需要采取措施以避免包含它(例如,將其放在單獨的,不重疊的包中,或者指定要在@ComponentScan中顯式掃描的包)。
方式1、TestConfiguration不放在spring boot啟動類的當前包或子包中即可
方式2、如果TestConfiguration確實需要放在當前包,需要設置如下
增加注解

public @interface ExcludeFromComponentScan { }
將注解增加至TestConfiguration

@Configuration @ExcludeFromComponentScan public class TestConfiguration { public IRule ribbonRule() { return new RandomRule(); } }
將排除注解增加至啟動類
@ComponentScan(excludeFilters= {@ComponentScan.Filter(type=FilterType.ANNOTATION,value=ExcludeFromComponentScan.class)})
步驟二、將:@RibbonClient(name = "microservice-provider-user", configuration = TestConfiguration.class)放到啟動類中
多個可以@RibbonClient(name = "microservice-provider-user2", configuration = TestConfiguration.class)
此時只是自定義的按照新的規則負載,原有的還是按照默認的輪詢方式使用
2.2、源碼查看
查看:@RibbonClient注解發現主要屬性為 RibbonClientConfiguration,查看其實現
Spring Cloud Netflix默認為Ribbon提供以下Bean(BeanType beanName:ClassName):
IClientConfig
ribbonClientConfig:DefaultClientConfigImpl
IRule
ribbonRule:ZoneAvoidanceRule
IPing
ribbonPing:DummyPing
ServerList<Server>
ribbonServerList:ConfigurationBasedServerList
ServerListFilter<Server>
ribbonServerListFilter:ZonePreferenceServerListFilter
ILoadBalancer
ribbonLoadBalancer:ZoneAwareLoadBalancer
ServerListUpdater
ribbonServerListUpdater:PollingServerListUpdater
三、自定義Ribbon客戶端-【方式二】配置文件
從1.2.0版開始,Spring Cloud Netflix現在支持使用屬性自定義Ribbon客戶端以與Ribbon文檔兼容。
配置屬性
<clientName>.ribbon.:
NFLoadBalancerClassName: should implement ILoadBalancer
NFLoadBalancerRuleClassName: should implement IRule
NFLoadBalancerPingClassName: should implement IPing
NIWSServerListClassName: should implement ServerList
NIWSServerListFilterClassName should implement ServerListFilter
注意:配置優先級高於代碼自定義和默認配置,並且不會有代碼方式的干擾
示例
users: #微服務名稱
ribbon:
NIWSServerListClassName: com.netflix.loadbalancer.ConfigurationBasedServerList
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.WeightedResponseTimeRule
NFLoadBalancerRuleClassName
,可以通過這個配置項定制需要的負載均衡規則,可以是ribbon提供的原生的幾種規則類,也可以是自己實現的規則類,這些類都實現了IRule接口。指定負載均衡器的實現類。當然,可以設置自己實現的負載均衡器。
NFLoadBalancerPingClassName
用於配置查看服務器是否存活。
NIWSServerListClassName
是服務器列表的處理類,用來維護服務器列表的。Ribbon已經實現了動態服務器列表。
NIWSServerListFilterClassName
是服務器的攔截類。
四、Ribbon內置負載均衡規則
Ribbon框架按照不同需求,已經為我們實現了許多實現了IRule接口的實現類,適用於常用的負載均衡規則。以下規則能夠實現大部分負載均衡需求的應用場景,如果有更復雜的需求,可以自己實現IRule。
內置負載均衡規則類 | 規則描述 |
RoundRobinRule | 簡單輪詢服務列表來選擇服務器。它是Ribbon默認的負載均衡規則。 |
AvailabilityFilteringRule | 對以下兩種服務器進行忽略: (1)在默認情況下,這台服務器如果3次連接失敗,這台服務器就會被設置為“短路”狀態。短路狀態將持續30秒,如果再次連接失敗,短路的持續時間就會幾何級地增加。 注意:可以通過修改配置loadbalancer.<clientName>.connectionFailureCountThreshold來修改連接失敗多少次之后被設置為短路狀態。默認是3次。 (2)並發數過高的服務器。如果一個服務器的並發連接數過高,配置了AvailabilityFilteringRule規則的客戶端也會將其忽略。並發連接數的上線,可以由客戶端的<clientName>.<clientConfigNameSpace>.ActiveConnectionsLimit屬性進行配置。
|
WeightedResponseTimeRule | 為每一個服務器賦予一個權重值。服務器響應時間越長,這個服務器的權重就越小。這個規則會隨機選擇服務器,這個權重值會影響服務器的選擇。 |
ZoneAvoidanceRule | 以區域可用的服務器為基礎進行服務器的選擇。使用Zone對服務器進行分類,這個Zone可以理解為一個機房、一個機架等。 |
BestAvailableRule | 忽略哪些短路的服務器,並選擇並發數較低的服務器。 |
RandomRule | 隨機選擇一個可用的服務器。 |
Retry | 重試機制的選擇邏輯 |
附錄:AvailabilityFilteringRule的三個默認配置
# successive connection failures threshold to put the server in circuit tripped state, default 3 niws.loadbalancer.<clientName>.connectionFailureCountThreshold # Maximal period that an instance can remain in "unusable" state regardless of the exponential increase, default 30 niws.loadbalancer.<clientName>.circuitTripMaxTimeoutSeconds # threshold of concurrent connections count to skip the server, default is Integer.MAX_INT <clientName>.<clientConfigNameSpace>.ActiveConnectionsLimit