SpringCloud官⽅⽂档中⽂版-客户端负载均衡:Ribbon
Client Side Load Balancer: Ribbon 客户端负载均衡:Ribbon
Ribbon is a client side load balancer which gives you a lot of control over the behaviour of HTTP and TCP clients. Feign already uses Ribbon, so if you are using @FeignClient then this section also applies.
Ribbon是⼀个客户端负载均衡器,它给开发⼈员提供了对HTTP和TCP客户端⾏为很多的控制权。Feigh已经在使⽤Ribbon,因此如果开发⼈员使⽤@FeignClient,本节依然适⽤。
A central concept in Ribbon is that of the named client. Each load balancer is part of an ensemble of components that work together to contact a remote server on demand, and the ensemble has a name that you give it as an application developer (e.g. using the @FeignClient annotation). Spring Cloud creates a new ensemble as an ApplicationContext on demand for each named client using RibbonClientConfiguration. This contains (amongst other things) an ILoadBalancer, a RestClient, and a ServerListFilter.
Ribbon的核⼼概念是命名的客户端。每⼀个负载均衡器都是组件组合的⼀部分,它们在需要的时候⼀起
⼯作去和远程服务器通信,开发⼈员可以给这个组合起⼀个名字(例如使⽤@FeignClient注解)。Spring Cloud使⽤RibbonClientConfiguration为每个命名的客户端根据需要创建⼀个新的集合作为应⽤上下⽂(ApplicationContext)。集合包括ILoadBalancer,RestClient和ServerListFilter(当然还有其他未列出的)。
How to Include Ribbon 如何引⼊Ribbon
To include Ribbon in your project use the starter with group org.springframework.cloud and artifact id spring-cloud-starter-ribbon. See the  for details on setting up your build system with the current Spring Cloud Release Train.
引⼊org.springframework.cloud的spring-cloud-starter-ribbon项⽬。详见
Customizing the Ribbon Client
You can configure some bits of a Ribbon client using external properties in <client>.ribbon.*, which is no different than using the Netflix APIs natively, except that you can use Spring Boot configuration files. The native options can be inspected as static fields in CommonClientConfigKey (part of ribbon-core).
开发⼈员可以⽤<client>.ribbon.*外部配置来开启Ribbon的功能,这种⽅式除了使⽤了Spring Boot 配置⽂件外,和使⽤原⽣的Nexflix APIs没什么不同。原⽣的选项在CommonClientConfigKey的常量中可以看到(是ribbon-core的⼀部分)。
Spring Cloud also lets you take full control of the client by declaring additional configuration (on top of the RibbonClientConfiguration) using @RibbonClient. Example:
Spring Cloud也允许开发⼈员声明额外的配置(在RibbonClientConfiguration之上)-@RibbonClient来取得客户端的全部控制权。例如:
@Configuration
@RibbonClient(name = "foo", configuration = FooConfiguration.class)
public class TestConfiguration {
}
In this case the client is composed from the components already in RibbonClientConfiguration together with any in FooConfiguration (where the latter generally will override the former).
本例中,客户端由已在RibbonClientConfiguration中的组件以及FooConfiguration中的任意组件组成(后者通常覆盖前者)。
WARNING The FooConfiguration has to be @Configuration but take care that it is not in a @ComponentScan for the main application context, otherwise it will be shared by all the @RibbonClients. If you use @ComponentScan (or
@SpringBootApplication) you need to take steps to avoid it being included (for instance put it in a separate, non-overlapping package, or specify the packages to scan explicitly in the @ComponentScan).
警告 FooConfiguration必须有@Configuration,但注意它并不在主应⽤上下⽂的@ComponentScan中,否则它会被所有的
@RibbonClients分享(意思就是覆盖所有客户端的默认值)。如果开发⼈员使⽤@ComponentScan(或@SpringBootApplication),那就必须采取措施避免被覆盖到(例如将其放⼊⼀个独⽴的,不重叠的包中,或以@ComponentScan指明要扫描的包。
Spring Cloud Netflix provides the following beans by default for ribbon (BeanType beanName: ClassName):
Spring Cloud Netflix默认为Ribbon提供以下bean(BeanType beanName: ClassName):
IClientConfig ribbonClientConfig: DefaultClientConfigImpl
springboot中文IRule ribbonRule: ZoneAvoidanceRule
IPing ribbonPing: NoOpPing
ServerList<Server> ribbonServerList: ConfigurationBasedServerList
ServerListFilter<Server> ribbonServerListFilter: ZonePreferenceServerListFilter
ILoadBalancer ribbonLoadBalancer: ZoneAwareLoadBalancer
ServerListUpdater ribbonServerListUpdater: PollingServerListUpdater
Creating a bean of one of those type and placing it in a @RibbonClient configuration (such as FooConfiguration above) allows you to override each one of the beans described. Example:
创建以上Bean中的⼀个并放⼊@RibbonClient配置中(可以是上⾯的FooConfiguration),可以覆盖默认的Bean。例如:
@Configuration
public class FooConfiguration { @Bean public IPing ribbonPing(IClientConfig config) { return new PingUrl(); } }
This replaces the NoOpPing with PingUrl.
上⾯的@Bean⽤PingUrl覆盖了NoOpPing。
Customizing the Ribbon Client using properties ⽤属性⾃定义Ribbon客户端
Starting with version 1.2.0, Spring Cloud Netflix now supports customizing Ribbon clients using properties to be compatible with the 。
从1.2.0开始,Spring Cloud Netflix开始⽀持并兼容属性⾃定义Ribbon客户端,详见。
This allows you to change behavior at start up time in different environments.
这样开发⼈员就可以在启动时根据不同环境来改变客户端⾏为。
The supported properties are listed below and should be prefixed by <clientName>.ribbon.:
下列属性加上<clientName>.ribbon.前缀就是Ribbon⽀持的属性配置。
NFLoadBalancerClassName: should implement ILoadBalancer
NFLoadBalancerRuleClassName: should implement IRule
NFLoadBalancerPingClassName: should implement IPing
NIWSServerListClassName: should implement ServerList
NIWSServerListFilterClassName should implement ServerListFilter
NOTE Classes defined in these properties have precedence over beans defined using
@RibbonClient(configuration=MyRibbonConfig.class) and the defaults provided by Spring Cloud Netflix.
注意 这些类⽐使⽤@RibbonClient(configuration=MyRibbonConfig.class)定义的Bean和由Spring Cloud Netflix提供的默认的Bean 优先定义。
To set the IRule for a service name users you could set the following:
要为服务名称⽤户设置IRule,开发⼈员可以设置以下内容:
NFLoadBalancerRuleClassName: comflix.loadbalancer.WeightedResponseTimeRule
See the  for implementations provided by Ribbon.
Ribbon实现详见:
Using Ribbon with Eureka 和Eureka⼀起使⽤
When Eureka is used in conjunction with Ribbon (i.e., both are on the classpath) the ribbonServerList is overridden with an extension of DiscoveryEnabledNIWSServerList which populates the list of servers from Eureka. It also replaces the IPing interface with NIWSDiscoveryPing which delegates to Eureka to determine if a server is up. The ServerList that is installed by default is a DomainExtractingServerList and the purpose of this is to make physical metadata available to the load balancer without using AWS AMI metadata (which is what Netflix relies on). By default the server list will be constructed with "zone" information as provided in the instance metadata (so on the remote clients set ), and if that is missing it can use the domain name from the server hostname as a proxy for zone (if the flag approxi
mateZoneFromHostname is set). Once the zone information is available it can be used in a ServerListFilter. By default it will be used to locate a server in the same zone as the client because the default is a ZonePreferenceServerListFilter. The zone of the client is determined the same way as the remote instances by default, i.e. via
当Eureka与Ribbon结合使⽤时(也就是说都在classpath中),RibbonServerList将被扩展为DiscoveryEnabledNIWSServerList,该扩展从Eureka缓存信息中获取并填充服务器列表信息。DiscoveryEnabledNIWSServerList还⽤NIWSDiscoveryPing代替了IPing接⼝,NIWSDiscoveryPing委托Eureka确认服务是否启动。默认情况下安装的ServerList是⼀个DomainExtractingServerList,其⽬的是使物理元数据可⽤于负载均衡器,⽽不使⽤AWS AMI元数据(Netflix依赖于此)。服务器列表默认由实例元数据提供的“zone”信息构建,如果这些信息缺失,那么会使⽤服务器主机的域名作为⼀个zone的代理(前提是设定了approximateZoneFromHostname标志)。⼀旦zone信息确定,ServerListFilter就会使⽤。只要默认值为ZonePreferenceServerListFilter,它将⽤于查与客户端相同的区域中的服务器。默认情况下,客户端的zone与远程实例的⽅式相同,即通过。
NOTE The orthodox "archaius" way to set the client zone is via a configuration property called "@zone", and Spring Cloud will use that in preference to all other settings if it is available (note that t
he key will have to be quoted in YAML configuration).
注意 设置客户端区域的正统“archaius”⽅式是通过⼀个名为“@zone”的配置属性,如果可⽤Spring Cloud将优先于所有其他设置使⽤。(请注意,该配置项必须在YAML配置中引⽤)。
NOTE If there is no other source of zone data then a guess is made based on the client configuration (as opposed to the instance configuration). We take eureka.client.availabilityZones, which is a map from region name to a list of zones, and pull out the first zone for the instance’s own region (i.e. the ion, which defaults to "us-east-1" for comatibility with native Netflix).
注意 如果没有其他的zone数据源,那么可以基于客户端配置(与实例配置相反)进⾏猜测, 我们将eureka.client.availabilityZones(从区域名称映射到区域列表),并拉出实例本⾝region的第⼀个zone(即ion,默认为“us-east-1” “为了与本机Netflix 的兼容性)。
Example: How to Use Ribbon Without Eureka 如何单独使⽤Ribbon
Eureka is a convenient way to abstract the discovery of remote servers so you don’t have to hard code their URLs in clients, but if you prefer not to use it, Ribbon and Feign are still quite amenable. Suppose you have declared a
@RibbonClient for "stores", and Eureka is not in use (and not even on the classpath). The Ribbon client defaults to a configured server list, and you can supply the configuration like this:
Eureka是⼀个便利的抽象远程服务发现的⽅式,有了它,开发⼈员就不⽤在代码中将URL写死,如果你想写死也不是不可以,Ribbon和Feign是很灵活的。假设你已经声明了给“stores”⼀个@RibbonClient,⽽且没有使⽤Eureka(甚⾄在classpath中也不出现)。Ribbon客户端有⼀个默认的服务器列表,它⽀持以下配置:
listOfServers: le
Example: Disable Eureka use in Ribbon ⼲掉Eureka
Setting the property abled = false will explicitly disable the use of Eureka in Ribbon. 设置
abled = false。
ribbon:
eureka:
enabled: false
Using the Ribbon API Directly 直接使⽤Ribbon API
You can also use the LoadBalancerClient directly. Example:
public class MyClass {
@Autowired private LoadBalancerClient loadBalancer; public void doStuff() { ServiceInstance instance = loadBalancer.choose("stores"); URI storesUri =
Caching of Ribbon Configuration Ribbon配置缓存
Each Ribbon named client has a corresponding child Application Context that Spring Cloud maintains, this application
context is lazily loaded up on the first request to the named client. This lazy loading behavior can be changed to instead
eagerly load up these child Application contexts at startup by specifying the names of the Ribbon clients.
Spring Cloud为每⼀个命名的Ribbon客户端维护了相应的⼦应⽤上下⽂,这个上下⽂在客户端第⼀次被请求的时候懒加载。不过明确指定
这些Ribbon客户端的名字可以让它们在启动时就加载。(这⾥不是很清楚client的名字是什么)
ribbon:
eager-load:
enabled: true
clients: client1, client2, client3
dreamingodd原创⽂章,如转载请注明出处。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。