spring-cloud-starter-ribbon提供客户端的软件负载均衡算法

Ribbon是什么?

Ribbon是Netflix发布的开源项目,主要功能是提供客户端的软件负载均衡算法,将Netflix的中间层服务连接在一起。Ribbon客户端组件提供一系列完善的配置项如连接超时,重试等。简单的说,就是在配置文件中列出Load Balancer(简称LB)后面所有的机器,Ribbon会自动的帮助你基于某种规则(如简单轮询,随即连接等)去连接这些机器。我们也很容易使用Ribbon实现自定义的负载均衡算法。

LB方案分类

目前主流的LB方案可分成两类:一种是集中式LB, 即在服务的消费方和提供方之间使用独立的LB设施(可以是硬件,如F5, 也可以是软件,如nginx),由该设施负责把访问请求通过某种策略转发至服务的提供方;另一种是进程内LB,将LB逻辑集成到消费方,消费方从服务注册中心获知有哪些地址可用,然后自己再从这些地址中选择出一个合适的服务器。Ribbon就属于后者,它只是一个类库,集成于消费方进程,消费方通过它来获取到服务提供方的地址。

三:Ribbon的主要组件与工作流程

Ribbon的核心组(均为接口类型)有以下几个:

ServerList 

用于获取地址列表。它既可以是静态的(提供一组固定的地址),也可以是动态的(从注册中心中定期查询地址列表)。

ServerListFilter 

仅当使用动态ServerList时使用,用于在原始的服务列表中使用一定策略过虑掉一部分地址。

IRule 

选择一个最终的服务地址作为LB结果。选择策略有轮询、根据响应时间加权、断路器(当Hystrix可用时)等。

Ribbon在工作时首选会通过ServerList来获取所有可用的服务列表,然后通过ServerListFilter过虑掉一部分地址,最后在剩下的地址中通过IRule选择出一台服务器作为最终结果。

四:Ribbon提供的主要负载均衡策略介绍

1、简单轮询负载均衡(RoundRobin)

以轮询的方式依次将请求调度不同的服务器,即每次调度执行i = (i + 1) mod n,并选出第i台服务器。

2、随机负载均衡 (Random)

随机选择状态为UP的Server

3、加权响应时间负载均衡 (WeightedResponseTime)

根据相应时间分配一个Weight,相应时间越长,Weight越小,被选中的可能性越低。 

4、区域感知轮询负载均衡(ZoneAvoidanceRule)

复合判断Server所在区域的性能和Server的可用性选择Server

Ribbon自带负载均衡策略比较

策略名 策略声明 策略描述 实现说明
BestAvailableRule public class BestAvailableRule extends ClientConfigEnabledRoundRobinRule 选择一个最小的并发请求的server 逐个考察Server,如果Server被tripped了,则忽略,在选择其中ActiveRequestsCount最小的server
AvailabilityFilteringRule public class AvailabilityFilteringRule extends PredicateBasedRule 过滤掉那些因为一直连接失败的被标记为circuit tripped的后端server,并过滤掉那些高并发的的后端server(active connections 超过配置的阈值) 使用一个AvailabilityPredicate来包含过滤server的逻辑,其实就就是检查status里记录的各个server的运行状态
WeightedResponseTimeRule public class WeightedResponseTimeRule extends RoundRobinRule 根据相应时间分配一个weight,相应时间越长,weight越小,被选中的可能性越低。 一 个后台线程定期的从status里面读取评价响应时间,为每个server计算一个weight。Weight的计算也比较简单responsetime 减去每个server自己平均的responsetime是server的权重。当刚开始运行,没有形成statas时,使用roubine策略选择 server。
RetryRule public class RetryRule extends AbstractLoadBalancerRule 对选定的负载均衡策略机上重试机制。 在一个配置时间段内当选择server不成功,则一直尝试使用subRule的方式选择一个可用的server
RoundRobinRule public class RoundRobinRule extends AbstractLoadBalancerRule roundRobin方式轮询选择server 轮询index,选择index对应位置的server
RandomRule public class RandomRule extends AbstractLoadBalancerRule 随机选择一个server 在index上随机,选择index对应位置的server
ZoneAvoidanceRule public class ZoneAvoidanceRule extends PredicateBasedRule 复合判断server所在区域的性能和server的可用性选择server 使 用ZoneAvoidancePredicate和AvailabilityPredicate来判断是否选择某个server,前一个判断判定一个 zone的运行性能是否可用,剔除不可用的zone(的所有server),AvailabilityPredicate用于过滤掉连接数过多的 Server。

五、使用

引入POM

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-ribbon</artifactId>
    </dependency>

基于RestTemplate

定义

package com.jsoft.testzookeeper.client.config;


import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class Config {

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

说明:标注@LoadBalanced来开启负载均衡能力,默认为轮询方式。

使用

package com.jsoft.testzookeeper.client.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class HelloService {

    @Autowired
    private RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "sayHelloFallback")
    public String sayHello(String name) {
        return restTemplate.getForEntity("http://service-zookeeper/hello?name=" + name, String.class).getBody();
    }

    private String sayHelloFallback(String name) {
        return "service error";
    }
}

更多的配置参考官方文档:https://github.com/Netflix/ribbon/wiki/Features,比如下面的规则配置(没实践过):

springboot-h2.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RandomRule // Ribbon的负载均衡策略  

说明:springboot-h2为ClientName

基于Feign:

Feign客户端在集成时没什么配置项目,主要是基于配置文件来定义规则,例子参考示例代码,下面主要是配置文件配置。

除了上面的配置外,还可以有如下指定:

主要是基于ZooKeeper的依赖关系去实现,官方参考:http://cloud.spring.io/spring-cloud-zookeeper/multi/multi_spring-cloud-zookeeper-dependencies.html,样例配置如下:

spring.cloud.zookeeper.dependencies.service-zookeeper.required=true
spring.cloud.zookeeper.dependencies.service-zookeeper.path=/service-zookeeper
spring.cloud.zookeeper.dependencies.service-zookeeper.loadBalancerType=ROUND_ROBIN

说明:service-zookeeper是指定的别名,最终会去到/service-zookeeper,loadBalancerType是规则。

总结:

1、感觉基于ZK的负载均衡规则和Ribbon的原生规则两个分不开,主要看样子好像是颗粒度不同,全局已经分不同服务等设置。

2、以上结论待实践中。估计会有很多吭。

Maven示例:

https://github.com/easonjim/spring-cloud-demo/tree/master/ZooKeeper

参考:

http://www.ccblog.cn/96.htm(以上内部部分转自此篇文章)

http://www.cnblogs.com/wangjing666/p/6985047.html

http://blog.csdn.net/w_x_z_/article/details/71156009(负载均衡规则自定义)

http://blog.csdn.net/shunhua19881987/article/details/75466797

http://www.idouba.net/netflix-source-ribbon-rule/

http://blog.csdn.net/daybreak1209/article/details/53582366

http://blog.csdn.net/liuchuanhong1/article/details/54728406

http://www.jianshu.com/p/7bb96e2dc944

http://www.cnblogs.com/wangjing666/p/6994451.html

原文地址:https://www.cnblogs.com/EasonJim/p/7614058.html