Ribbon是Netflix 发布的开源项目,主要功能是提供客户端的软件负载均衡算法,将Netflix的中间层服务连接在一起。Ribbon客户端组 件提供一系列完善的配置项如连接超时,重试等。简单的说,就是在配置文件中列出Load Balancer后面所有的机器,Ribbon会自动的帮助你基于某种规则(如简单轮询,随即连接等)去连接这些机器。我们也很容易使用Ribbon实现 自定义的负载均衡算法。
Ribbon提供的主要负载均衡策略介绍
简单轮询负载均衡(RoundRobin)
以轮询的方式依次将请求调度不同的服务器,即每次调度执行i = (i + 1) mod n,并选出第i台服务器。
随机负载均衡 (Random)
随机选择状态为UP的Server
加权响应时间负载均衡 (WeightedResponseTime)
一 个后台线程定期的从status里面读取评价响应时间,为每个server计算一个weight。Weight的计算也比较简单responsetime 减去每个server自己平均的responsetime是server的权重。当刚开始运行,没有形成statas时,使用roubine策略选择 server。
区域感知轮询负载均衡(ZoneAware)
区域感知负载均衡内置电路跳闸逻辑,可被配置基于区域同源关系(Zone Affinity,也就是更倾向于选择发出调用的服务所在的托管区域内,这样可以降低延迟,节省成本)选择目标服务实例。它监控每个区域中运行实例的行 为,而且能够实时的快速丢弃一整个区域。这样在面对整个区域故障时,帮我们提升了弹性。
Ribbon自带负载均衡策略比较
策略名 | 策略父类 | 策略描述 | 实现说明 |
BestAvailableRule | ClientconfigEnabledRoundRobinRule | 选择一个最小的并发请求的server | 逐个考察Server,如果Server被tripped了,则忽略,在选择其中ActiveRequestsCount最小的server |
AvailabilityFilteringRule | PredicateBasedRule | 过滤掉那些因为一直连接失败的被标记为circuit tripped的后端server,并过滤掉那些高并发的的后端server(active connections 超过配置的阈值) | 使用一个AvailabilityPredicate 来包含过滤server的逻辑,其实就就是检查status里记录的各个server的运行状态 |
WeightedResponseTimeRule | RoundRobinRule | 根据相应时间分配一个weight,相应时间越长,weight越小,被选中的可能性越低。 | 一 个后台线程定期的从status里面读取评价响应时间,为每个server计算一个weight。Weight的计算也比较简单responsetime 减去每个server自己平均的responsetime是server的权重。当刚开始运行,没有形成statas时,使用roubine策略选择 server。 |
RetryRule | AbstractLoadBalancerRule | 对选定的负载均衡策略机上重试机制。 | 在一个配置时间段内当选择server不成功,则一直尝试使用subRule的方式选择一个可用的server |
RoundRobinRule | AbstractLoadBalancerRule | roundRobin方式轮询选择server | 轮询index,选择index对应位置的server |
RandomRule | AbstractLoadBalancerRule | 随机选择一个server | 在index上随机,选择index对应位置的server |
ZoneAvoidanceRule | PredicateBasedRule | 复合判断server所在区域的性能和server的可用性选择server | 使 用ZoneAvoidancePredicate和AvailabilityPredicate来判断是否选择某个server,前一个判断判定一个 zone的运行性能是否可用,剔除不可用的zone(的所有server),AvailabilityPredicate用于过滤掉连接数过多的 Server。 |
Ribbon架构图
在Spring Cloud中使用Ribbon
不管是与Spring Cloud Zuul集成还是与Spring Cloud Feign集成,都是同样的方法,首先我需要了解清楚再Spring Cloud中Ribbon默认提供的Bean
Spring Cloud Netflix provides the following beans by default for ribbon (BeanType beanName: ClassName):
1、
IClientConfig
ribbonClientConfig:DefaultClientConfigImpl
2、
IRule
ribbonRule:ZoneAvoidanceRule
3、
IPing
ribbonPing:NoOpPing
4、
ServerList<Server>
ribbonServerList:ConfigurationBasedServerList
5、
ServerListFilter<Server>
ribbonServerListFilter:ZonePreferenceServerListFilter
6、
ILoadBalancer
ribbonLoadBalancer:ZoneAwareLoadBalancer
1、对某个服务指定特定的负载均衡策略
如果是对某个服务指定特定的负载均衡策略,则需要使用:RibbonClient
注解,如下:
@Configuration public class FooConfiguration { @Bean public IPing ribbonPing() { return new PingUrl(false,"/info.json"); } /** * 负载均衡策略 * @return */ @Bean public IRule ribbonRule() { // return new BestAvailableRule(); //选择一个最小的并发请求的server // return new WeightedResponseTimeRule(); //根据相应时间分配一个weight,相应时间越长,weight越小,被选中的可能性越低。 // return new RetryRule(); //对选定的负载均衡策略机上重试机制。 // return new RoundRobinRule(); //roundRobin方式轮询选择server // return new RandomRule(); //随机选择一个server // return new ZoneAvoidanceRule(); //复合判断server所在区域的性能和server的可用性选择server return new AvailabilityFilteringRule(); } }
声明特定服务(foo)配置
@Configuration @RibbonClient(name = "foo", configuration = FooConfiguration.class) public class TestConfiguration { }
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler) // package org.springframework.cloud.netflix.ribbon; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import org.springframework.cloud.netflix.ribbon.RibbonClientConfigurationRegistrar; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; @Configuration @Import({RibbonClientConfigurationRegistrar.class}) @Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface RibbonClient { String value() default ""; String name() default ""; Class<?>[] configuration() default {}; }
name:特定服务名称
2、对所有服务指定特定负载均衡策略
指定所有服务的负载均衡策略使用注解:RibbonClients
package cn.com.agree.gateway.autoconfig; import com.netflix.loadbalancer.AvailabilityFilteringRule; import com.netflix.loadbalancer.IPing; import com.netflix.loadbalancer.IRule; import com.netflix.loadbalancer.PingUrl; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * Created by meyer on 2017/2/22. */ @Configuration public class RibbonConfiguration { /** * ping中心 * @return */ @Bean public IPing ribbonPing() { return new PingUrl(false, "/info.json"); } /** * 负载均衡策略 * @return */ @Bean public IRule ribbonRule() { // return new BestAvailableRule(); //选择一个最小的并发请求的server // return new WeightedResponseTimeRule(); //根据相应时间分配一个weight,相应时间越长,weight越小,被选中的可能性越低。 // return new RetryRule(); //对选定的负载均衡策略机上重试机制。 // return new RoundRobinRule(); //roundRobin方式轮询选择server // return new RandomRule(); //随机选择一个server // return new ZoneAvoidanceRule(); //复合判断server所在区域的性能和server的可用性选择server return new AvailabilityFilteringRule(); } }
触发配置
package cn.com.agree.gateway; import cn.com.agree.gateway.autoconfig.RibbonConfiguration; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.SpringCloudApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.hystrix.EnableHystrix; import org.springframework.cloud.netflix.ribbon.RibbonClients; import org.springframework.cloud.netflix.zuul.EnableZuulProxy; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.env.Environment; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.UrlBasedCorsConfigurationSource; import org.springframework.web.filter.CorsFilter; /** * Created by meyer on 2017/2/13. */ @EnableZuulProxy @EnableHystrix @SpringCloudApplication @RibbonClients(defaultConfiguration = {RibbonConfiguration.class}) public class GatewayApplication { public static void main(String[] args) { SpringApplication.run(GatewayApplication.class, args); } }
RibbonClients
源码
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler) // package org.springframework.cloud.netflix.ribbon; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import org.springframework.cloud.netflix.ribbon.RibbonClient; import org.springframework.cloud.netflix.ribbon.RibbonClientConfigurationRegistrar; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; @Configuration @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE}) @Documented @Import({RibbonClientConfigurationRegistrar.class}) public @interface RibbonClients { RibbonClient[] value() default {}; Class<?>[] defaultConfiguration() default {}; }
转载请注明:晓窗博客 » Sping Cloud Netflix Ribbon负载均衡