Ribbon基础入门和特性讲解

Posted by zhida on February 1, 2017

介绍

ribbon 是一个客户端负载均衡器,可以简单的理解成类似于 nginx的负载均衡模块的功能。

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.

使用方法

application.yml:

ribbon: 
  eureka:
    enabled: true
  MaxAutoRetries: 0
  MaxAutoRetriesNextServer: 0

ps:有一些属性是在feign中使用的 , 在配置文件中配置不生效; springboot中的feign集成了ribbon

pom.xml:

因为是在feign中集成的 ,所以参考feign的依赖

程序入口添加注释:


@RibbonClient(name = "yea-ribbon", configuration = YeaRibbonConfiguration.class)
public class Application {

	public static void main(String[] args) {feign
		SpringApplication.run(Application.class, args);
	}

}

自定义属性:RibbonConfiguration

public class YeaRibbonConfiguration {
	
	@Autowired
	IClientConfig ribbonClientConfig;

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

	@Bean
	public IRule ribbonRule(IClientConfig config) {
		return new AvailabilityFilteringRule();
	}
}

其他知识点

自定义 Ribbon client

可以使用 <client>.ribbon.* 外部配置文件进行自定义, 和使用 原生的Netflix APIs 没有区别。

Spring Clound 可以让你完全控制 Ribbon client , 通过使用 @Ribbon Client来声明额外的配置。

@Configuration
@RibbonClient(name = "foo", configuration = FooConfiguration.class)
public class TestConfiguration {
}
@Configuration
public class FooConfiguration {
    @Bean
    public IPing ribbonPing(IClientConfig config) {
        return new PingUrl();
    }
}

Note:同样的 ,不能被Compose sacn 扫描到,不然就变成全局了,或者不要 @Configuration

Ribbon的属性选项

Spring Cloud Netflix 为 ribbon 提供了以下的默认属性

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

使用文件的属性值自定义Ribbon Client

Starting with version 1.2.0, Spring Cloud Netflix now supports customizing Ribbon clients using properties to be compatible with the Ribbon documentation

This allows you to change behavior at start up time in different environments.

The supported properties are listed below and should be prefixed by .ribbon.:

NFLoadBalancerClassName: should implement ILoadBalancer

NFLoadBalancerRuleClassName: should implement IRule

NFLoadBalancerPingClassName: should implement IPing

NIWSServerListClassName: should implement ServerList

NIWSServerListFilterClassName should implement ServerListFilter

NOTE: 属性值的定义 > @RibbonClient(configuration=MyRibbonConfig.class) > 默认值

application.yml样例

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

Using Ribbon with Eureka

当Eureka使用Ribbon的时候,ribbonServerList将会被 DiscoveryEnabledNIWSServerList覆盖,用来填充服务列表。 同样的使用NIWSDiscoveryPing 这个属性替代 IPing这个属性,如果服务是启动的将由 eureka去分配。

Ribbon获取eureka实例地址的策略如下:

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 eureka.instance.metadataMap.zone),

and if that is missing it can use the domain name from the server hostname as a proxy for zone (if the flag approximateZoneFromHostname 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.instance.metadataMap.zone.

舍弃Eureka单独使用Ribbon

使用Eureka是一种很方便的将物理地址和实例抽离的方法,当你不喜欢用这种方式的时候, Ribbon and Feign 依然是可以满足你的需求, 假设你声明了一个名为”stores”的 @RibbonClient,Eureka 没有被使用 (甚至都不在classpath). The Ribbon client 可以使用 server list属性配置

application.yml

stores:
  ribbon:
    listOfServers: example.com,google.com
Example: Disable Eureka use in Ribbon

使用属性ribbon.eureka.enabled = false 将禁止Ribbon的Eureka特性

application.yml

ribbon:
  eureka:
   enabled: false

直接使用Ribbon API

你也可以直接使用LoadBalancerClient对象

public class MyClass {
    @Autowired
    private LoadBalancerClient loadBalancer;

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

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.

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

参考网站

Ribbon documentation