SpringCloudNetflix-Ribbon

目錄

How to Include Ribbon

Customizing the Ribbon Client

Customizing the Default for All Ribbon Clients

Customizing the Ribbon Client by Setting Properties

Using Ribbon with Eureka

Example: How to Use Ribbon Without Eureka

Example: Disable Eureka Use in Ribbon

Using the Ribbon API Directly

Caching of Ribbon Configuration


參考:https://cloud.spring.io/spring-cloud-netflix/2.2.x/reference/html/#spring-cloud-ribbon

Ribbon是一個客戶端負載均衡器,它爲您提供了對HTTP和TCP客戶機行爲的大量控制。Feign已經使用了Ribbon,所以,如果您使用@FeignClient,這個部分也適用

Ribbon的核心概念是命名客戶端。每個負載平衡器都是一個組件集合的一部分,這些組件一起工作,按需聯繫遠程服務器,並且這個集合有一個您作爲應用程序開發人員給它的名稱(例如,通過使用@FeignClient註釋)。根據需要,Spring Cloud使用RibbonClientConfiguration爲每個命名的客戶機創建一個新的集成作爲一個ApplicationContext。其中包括一個ILoadBalancer、一個RestClient和一個ServerListFilter

How to Include Ribbon

要在項目中包含Ribbon,請使用group ID爲org.springframework.cloud和spring-cloud-starter-netflix-ribbon的artifact ID

Customizing the Ribbon Client

您可以通過使用<client>.ribbon.*中的外部屬性來配置一些Ribbon客戶端。這類似於原生使用Netflix api,只是可以使用SpringBoot配置文件

Spring Cloud還允許您通過使用@RibbonClient聲明額外的配置(在RibbonClientConfiguration之上)來完全控制客戶機,如下面的示例所示

@Configuration
@RibbonClient(name = "custom", configuration = CustomConfiguration.class)
public class TestConfiguration {

在這種情況下,客戶端由RibbonClientConfiguration中已經存在的組件和CustomConfiguration中的組件組成(後者通常會覆蓋前者)

CustomConfiguration類必須是@Configuration類,但是要注意它不在主應用程序上下文的@ComponentScan中。否則,它將被所有的@RibbonClients共享。如果使用@ComponentScan(或@SpringBootApplication),則需要採取步驟避免包含它(例如,可以將其放在一個單獨的、不重疊的包中,或者在@ComponentScan中指定要顯式掃描的包)

下表顯示了Spring Cloud Netflix默認爲Ribbon提供的bean

Bean Type Bean Name Class Name

IClientConfig

ribbonClientConfig

DefaultClientConfigImpl

IRule

ribbonRule

ZoneAvoidanceRule

IPing

ribbonPing

DummyPing

ServerList<Server>

ribbonServerList

ConfigurationBasedServerList

ServerListFilter<Server>

ribbonServerListFilter

ZonePreferenceServerListFilter

ILoadBalancer

ribbonLoadBalancer

ZoneAwareLoadBalancer

ServerListUpdater

ribbonServerListUpdater

PollingServerListUpdater

創建其中一種類型的bean並將其放在@RibbonClient配置中(如上面的FooConfiguration),可以覆蓋所描述的每個bean,如下面的示例所示

@Configuration(proxyBeanMethods = false)
protected static class FooConfiguration {

    @Bean
    public ZonePreferenceServerListFilter serverListFilter() {
        ZonePreferenceServerListFilter filter = new ZonePreferenceServerListFilter();
        filter.setZone("myTestZone");
        return filter;
    }

    @Bean
    public IPing ribbonPing() {
        return new PingUrl();
    }

}

前面示例中的include語句用PingUrl替換了NoOpPing,並提供了一個自定義serverListFilter

Customizing the Default for All Ribbon Clients

通過使用@RibbonClients註釋並註冊一個默認配置,可以爲所有的Ribbon客戶機提供默認配置,如下面的示例所示

@RibbonClients(defaultConfiguration = DefaultRibbonConfig.class)
public class RibbonClientDefaultConfigurationTestsConfig {

    public static class BazServiceList extends ConfigurationBasedServerList {

        public BazServiceList(IClientConfig config) {
            super.initWithNiwsConfig(config);
        }

    }

}

@Configuration(proxyBeanMethods = false)
class DefaultRibbonConfig {

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

    @Bean
    public IPing ribbonPing() {
        return new PingUrl();
    }

    @Bean
    public ServerList<Server> ribbonServerList(IClientConfig config) {
        return new RibbonClientDefaultConfigurationTestsConfig.BazServiceList(config);
    }

    @Bean
    public ServerListSubsetFilter serverListFilter() {
        ServerListSubsetFilter filter = new ServerListSubsetFilter();
        return filter;
    }

}

Customizing the Ribbon Client by Setting Properties

從版本1.2.0開始,Spring Cloud Netflix現在通過設置與Ribbon documentation兼容的屬性來支持定製Ribbon客戶機

這允許您在啓動時在不同的環境中改變行爲。

下面的列表顯示了支持的屬性

  • <clientName>.ribbon.NFLoadBalancerClassName: Should implement ILoadBalancer

  • <clientName>.ribbon.NFLoadBalancerRuleClassName: Should implement IRule

  • <clientName>.ribbon.NFLoadBalancerPingClassName: Should implement IPing

  • <clientName>.ribbon.NIWSServerListClassName: Should implement ServerList

  • <clientName>.ribbon.NIWSServerListFilterClassName: Should implement ServerListFilter

在這些屬性中定義的類優先於使用@RibbonClient(configuration=MyRibbonConfig.class)和Spring Cloud Netflix提供的默認值定義的bean

要設置名爲users的服務名稱的IRule,您可以設置以下屬性

users:
  ribbon:
    NIWSServerListClassName: com.netflix.loadbalancer.ConfigurationBasedServerList
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.WeightedResponseTimeRule

Using Ribbon with Eureka

當Eureka與Ribbon一起使用時(即兩者都在類路徑上),ribbonServerList被DiscoveryEnabledNIWSServerList的擴展覆蓋,它填充來自Eureka的服務器列表。它還將IPing接口替換爲NIWSDiscoveryPing,它委託Eureka來確定服務器是否已啓動。

默認安裝的ServerList是一個DomainExtractingServerList。它的目的是讓負載平衡器在不使用AWS AMI元數據(Netflix所依賴的元數據)的情況下使用元數據。默認情況下,服務器列表是用實例元數據中提供的 “zone” 信息構造的(因此,在遠程客戶端上設置eureka.instance.metadataMap.zone)。如果缺少該zone,並且設置了approximateZoneFromHostname標誌,則可以使用服務器主機名中的域名作爲zone的代理。

一旦zone信息可用,就可以在ServerListFilter中使用它。默認情況下,它用於在與客戶機相同的zone中定位服務器,因爲默認情況下是ZonePreferenceServerListFilter,默認情況下,客戶端區域的確定方式與遠程實例相同(即通過eureka.instance.metadataMap.zone

傳統的客戶端zone設置方法是通過一個名爲“@zone”的配置屬性。如果它可用,Spring Cloud優先使用該設置,而不是其他所有設置(注意,在YAML配置中必須引用key)

如果沒有其他來源的zone數據,則根據客戶端配置(而不是實例配置)進行猜測。我們把eureka.client.availabilityZones,它是從zone名稱映射到zone列表,併爲實例自己的zone(即eureka.client)提取第一個zone。region,默認爲“us-east-1”,以與本地Netflix兼容

 Example: How to Use Ribbon Without Eureka

Eureka是一種提取遠程服務器發現的方便方法,這樣您就不必在客戶機中硬編碼它們的url。然而,如果你不喜歡使用Eureka, Ribbon和Feign也可以。假設您已經爲“stores”聲明瞭一個@RibbonClient,而Eureka沒有被使用(甚至在類路徑中也沒有)。Ribbon客戶端默認是一個已配置的服務器列表。您可以提供如下配置

stores:
  ribbon:
    listOfServers: example.com,google.com

Example: Disable Eureka Use in Ribbon

ribbon:
  eureka:
   enabled: false

Using the Ribbon API Directly

您還可以直接使用LoadBalancerClient,如下面的示例所示

public class MyClass {
    @Autowired
    private LoadBalancerClient loadBalancer;

    public void doStuff() {
        ServiceInstance instance = loadBalancer.choose("stores");
        URI storesUri = URI.create(String.format("https://%s:%s", instance.getHost(), instance.getPort()));
        // ... do something with the URI
    }
}

Caching of Ribbon Configuration

每個名爲client的Ribbon都有Spring Cloud維護的對應子應用程序上下文。這個應用程序上下文在第一次請求命名客戶機時被延遲加載。通過指定Ribbon客戶機的名稱,可以將這種延遲加載行爲改爲在啓動時快速加載這些子應用程序上下文,如下面的示例所示

ribbon:
  eager-load:
    enabled: true
    clients: client1, client2, client3

 

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