跟我學Spring Cloud(Finchley版)-08-Ribbon深入

上一節講了Ribbon的入門姿勢,本節深入探討Ribbon的高級特性。

內置負載均衡規則

負載均衡規則是Ribbon的核心,下面來看一下Ribbon內置的負載均衡規則。

  • AvailabilityFilteringRule:過濾掉一直連接失敗的被標記爲circuit tripped的後端Server,並過濾掉那些高併發的後端Server或者使用一個AvailabilityPredicate來包含過濾server的邏輯,其實就就是檢查status裏記錄的各個Server的運行狀態;
  • BestAvailableRule:選擇一個最小的併發請求的Server,逐個考察Server,如果Server被tripped了,則跳過。
  • RandomRule:隨機選擇一個Server;
  • ResponseTimeWeightedRule:作用同WeightedResponseTimeRule,二者作用一樣;
  • RetryRule:對選定的負載均衡策略機上重試機制,在一個配置時間段內當選擇Server不成功,則一直嘗試使用subRule的方式選擇一個可用的server;
  • RoundRobinRule:輪詢選擇, 輪詢index,選擇index對應位置的Server;
  • WeightedResponseTimeRule:根據響應時間加權,響應時間越長,權重越小,被選中的可能性越低;
  • ZoneAvoidanceRule:複合判斷Server所在區域的性能和Server的可用性選擇Server;

如需自定義負載均衡規則,只需實現IRule 接口或繼承AbstractLoadBalancerRule、PredicateBasedRule即可 ,讀者可參考RandomRuleRoundRobinRuleZoneAvoidanceRule 等內置Rule編寫自己的負載均衡規則。

Ribbon配置自定義【細粒度配置】

Ribbon可實現精確到目標服務的細粒度配置。例如A服務調用服務B,A服務調用C,可以針對B服務一套配置,針對C服務另一套配置。

方式1、代碼配置方式

在Spring Cloud中,Ribbon的默認配置如下(格式是BeanType beanName: ClassName):

  • IClientConfig ribbonClientConfig: DefaultClientConfigImpl
  • IRule ribbonRule: ZoneAvoidanceRule
  • IPing ribbonPing: NoOpPing
  • ServerList<Server> ribbonServerList: ConfigurationBasedServerList
  • ServerListFilter<Server> ribbonServerListFilter: ZonePreferenceServerListFilter
  • ILoadBalancer ribbonLoadBalancer: ZoneAwareLoadBalancer
  • ServerListUpdater ribbonServerListUpdater: PollingServerListUpdater

代碼示例

  • 創建一個空類,並在其上添加@Configuration 註解和@RibbonClient 註解。

    /**
    * 使用RibbonClient,爲特定的目標服務自定義配置。
    * 使用@RibbonClient的configuration屬性,指定Ribbon的配置類。
    * 可參考的示例:
    * http://spring.io/guides/gs/client-side-load-balancing/
    * @author 周立
    */
    @Configuration
    @RibbonClient(name = "microservice-provider-user", configuration = RibbonConfiguration.class)
    public class TestConfiguration {
    }

    由代碼可知,使用@RibbonClient 註解的configuration屬性,即可自定義指定名稱Ribbon客戶端的配置。

  • 創建Ribbon的配置類。

    /**
    * 該類爲Ribbon的配置類
    * 注意:該類不能放在主應用程序上下文@ComponentScan所掃描的包中,否則配置將會被所有Ribbon Client共享。
    * @author 周立
    */
    @Configuration
    public class RibbonConfiguration {
    @Bean
    public IRule ribbonRule() {
      // 負載均衡規則,改爲隨機
      return new RandomRule();
    }
    }

配套代碼

GitHub:https://github.com/eacdy/spring-cloud-study/tree/master/2018-Finchley/microservice-consumer-movie-ribbon-config-java

Gitee:https://gitee.com/itmuch/spring-cloud-study/tree/master/2018-Finchley/microservice-consumer-movie-ribbon-config-java

方式2、屬性配置方式【推薦】

<clientName>.ribbon. 如下屬性

  • NFLoadBalancerClassName: should implement ILoadBalancer
  • NFLoadBalancerRuleClassName: should implement IRule
  • NFLoadBalancerPingClassName: should implement IPing
  • NIWSServerListClassName: should implement ServerList
  • NIWSServerListFilterClassName should implement ServerListFilter

代碼示例

user:
  ribbon:
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule

TIPS

屬性配置的優先級高於代碼配置。

配套代碼

GitHub:https://github.com/eacdy/spring-cloud-study/tree/master/2018-Finchley/microservice-consumer-movie-ribbon-config-properties

Gitee:https://gitee.com/itmuch/spring-cloud-study/tree/master/2018-Finchley/microservice-consumer-movie-ribbon-config-properties

Ribbon配置自定義【全局配置】

方式1、代碼配置方式

@RibbonClients(defaultConfiguration = DefaultRibbonConfig.class)
public class RibbonClientDefaultConfigurationTestsConfig {
}
@Configuration
class DefaultRibbonConfig {
  @Bean
  public IRule ribbonRule() {
    return new RandomRule();
  }
}

方法2、屬性配置方式【推薦】

和上文細粒度配置類似,只需將目標服務名稱前綴去掉即可。

ribbon:
  NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule

Ribbon Eager加載

默認情況下Ribbon是懶加載的——首次請求Ribbon相關類纔會初始化,這會導致首次請求過慢的問題,你可以配置飢餓加載,讓Ribbon在應用啓動時就初始化。

ribbon:
  eager-load:
    enabled: true
    # 多個用,分隔
    clients: microservice-provider-user

本文首發

http://www.itmuch.com/spring-cloud/finchley-8/

乾貨分享

全是乾貨

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章