Ribbon负载均衡

是什么

SpringCloud Ribbon是基于Netfix Ribbon实现的一套客户端均衡负载工具
简单地说,Ribbon是Netfix发布的开源项目,其主要功能是提供客户端的软件负载均衡的算法

能干嘛

负载均衡(Load Balance)简称LB,在分布式上常用的应用。
简单地说,就是将客户端连接请求分摊到多个服务上,从而达到系统的HA(高可用)。
Dubbo和Spring Cloud都提供了负载均衡,Spring cloud的负载均衡算法可以自定义。
LB(负载均衡)分为集中式LB和进程内LB.

  1. 集中式LB:在服务的消费方和提供方之间使用独立的LB设施(可以是硬件,如F5;也可以是软件,如Nginx)由该设施负责通过某种策略将请求转发至服务的提供方。
  2. 进程内LB:将LB逻辑集成到消费方,消费方从Eureka注册中心获取哪些地址可用,然后自己再从中选择一个合适的服务器。
  3. Ribbon就属于进程内LB,它只是一个类库,集成于消费方进程,消费方通过它来获取服务提供方的地址。

官网资料

https://github.com/Netflix/ribbon/wiki/Getting-Started

Ribbon的核心组件IRule

作用:根据特定算法,从服务列表选取一个要访问的服务。
Spring Cloud的IRule可选七种负载均衡算法(Spring Cloud自带),包含以下策略:

  1. RoundRobinRule 轮询
  2. RandomRule 随机
  3. AvailabilityFilteringRule 会先过滤掉由于多次访问而处于断路跳闸状态的服务,还有并发量超过阀值的服务,然后对剩下的服务使用轮询策略进行访问
  4. WeightedResponseTimeRule 根据响应时间计算服务的权重,响应时间越快,服务权重越高,被选中的概率更高,刚启动时统计信息不足,会使用RoundRobinRule策略,等统计信息足够后,会切换到WeightedResponseTimeRule策略
  5. RetryRule 先按照RoundRobinRule策略进行服务访问,如果访问失败则在指定的时间内会继续重试,获取可访问服务,对于访问失败的服务如果连续访问几次后还是失败就不会访问失败的服务。
  6. BestAvailableRule 会先过滤掉由于多次访问而处于断路器跳闸的服务和并发访问量超过阀值的服务,然后选择并发量最小的服务。
  7. ZoneAvoidanceRule 默认规则,复合判断server所在区域的性能和可用性,选择server服务器。

是一个接口。下面是github上IRule的源码:

public interface IRule{
    /*
     * choose one alive server from lb.allServers or
     * lb.upServers according to key
     * 
     * @return choosen Server object. NULL is returned if none
     *  server is available 
     */

    public Server choose(Object key);
    
    public void setLoadBalancer(ILoadBalancer lb);
    
    public ILoadBalancer getLoadBalancer();    
}

ILoadBalancer又是一个接口,下面是github上IRule的源码:

public interface ILoadBalancer {

	/**
	 * Initial list of servers.
	 * This API also serves to add additional ones at a later time
	 * The same logical server (host:port) could essentially be added multiple times
	 * (helpful in cases where you want to give more "weightage" perhaps ..)
	 * 
	 * @param newServers new servers to add
	 */
	public void addServers(List<Server> newServers);
	
	/**
	 * Choose a server from load balancer.
	 * 
	 * @param key An object that the load balancer may use to determine which server to return. null if 
	 *         the load balancer does not use this parameter.
	 * @return server chosen
	 */
	public Server chooseServer(Object key);
	
	/**
	 * To be called by the clients of the load balancer to notify that a Server is down
	 * else, the LB will think its still Alive until the next Ping cycle - potentially
	 * (assuming that the LB Impl does a ping)
	 * 
	 * @param server Server to mark as down
	 */
	public void markServerDown(Server server);
	
	/**
	 * @deprecated 2016-01-20 This method is deprecated in favor of the
	 * cleaner {@link #getReachableServers} (equivalent to availableOnly=true)
	 * and {@link #getAllServers} API (equivalent to availableOnly=false).
	 *
	 * Get the current list of servers.
	 *
	 * @param availableOnly if true, only live and available servers should be returned
	 */
	@Deprecated
	public List<Server> getServerList(boolean availableOnly);

	/**
	 * @return Only the servers that are up and reachable.
     */
    public List<Server> getReachableServers();

    /**
     * @return All known servers, both reachable and unreachable.
     */
	public List<Server> getAllServers();
}

AbstractLoadBalancer是ILoadBalancer的一个实现抽象类
RandomRule实现了AbstractLoadBalancerRule,RoundRobinRule继承了AbstractLoadBalancerRule。。。说明根据多态使用IRule接口就可以使用各个负载均衡算法。也可以通过实现IRule接口自定义负载均衡算法。

你可能感兴趣的:(分布式微服务)