六、负载均衡-Ribbon 的负载均衡策略
2017-07-23 09:28
465 查看
一、 内置负载均衡策略的介绍
1、 BestAvailableRule:选择一个最小的并发请求的 server,逐个考察 Server,如果 Server 被 tripped 了,则忽略。
2、 AvailabilityFilteringRule:过滤掉那些因为一直连接失败的被标记为 circuit tripped 的后端 server,并过滤掉那些高并发的的后端 server。
3、 ZoneAvoidanceRule:复合判断 server 所在区域的性能和 server 的可用性选择 server。(使用 ZoneAvoidancePredicate 和AvailabilityPredicate 来判断是否选择某个 server,前一个判断判定一个 zone 的运行性能是否可用,剔除不可用的 zone 的所有 server,AvailabilityPredicate 用于过滤掉连接数过多的 Server。)
4、 RandomRule:随机策略,在 index 上随机,选择 index 对应位置的 server。
5、 RetryRule :对选定的负载均衡策略机上重试机制,在一个配置时间段内当选择 server 不成功,则 一直尝试使用 subRule 的方式选择一个可用的 server。
6、 RoundRobinRule:轮询策略,轮询 index,选择 index 对应位置的 server(默认策略)。
7、 WeightedResponseTimeRule:权重策略(基于轮询策略,根据响应时间分配一个 weight(权重),响 应时间越长,weight 越小,被选中的可能性越低) 。
二、 负载均衡的自定义
1、 通过代码实现
2、 通过配置文件实现
三、 通过代码实现
随机算法
配置类:
说明:只需要简单的 2 个配置,就可以指定轮询的算法。
注意:在 RandomRuleConfig 的类里面,加上@Configuration 注解会导致所有的策略都被覆盖。
原因:如果 RandomRuleConfig 被 SpringContext 扫描,否则会被@RibbonClients 共用,会覆盖。
四、 同时使用 2 种以上的不同策略算法
方法 1:移除@Configuration 注解
配置类:
说明:新增 RoundRobinRuleConfig 类,移除@Configuration 注解,同时也要把 RandomRuleConfig 的
@Configuration 注解移除
方法 2:指定@ComponentScan 的扫描路径(默认是扫描主类下面所有的文件夹) 。
方法 3:把它移到主类的外面,让主类扫描不到。
说明:只要不让 SpringContext 扫描到即可。
五、 通过配置配置文件实现不同的算法 配置文件添加:
1、 BestAvailableRule:选择一个最小的并发请求的 server,逐个考察 Server,如果 Server 被 tripped 了,则忽略。
2、 AvailabilityFilteringRule:过滤掉那些因为一直连接失败的被标记为 circuit tripped 的后端 server,并过滤掉那些高并发的的后端 server。
3、 ZoneAvoidanceRule:复合判断 server 所在区域的性能和 server 的可用性选择 server。(使用 ZoneAvoidancePredicate 和AvailabilityPredicate 来判断是否选择某个 server,前一个判断判定一个 zone 的运行性能是否可用,剔除不可用的 zone 的所有 server,AvailabilityPredicate 用于过滤掉连接数过多的 Server。)
4、 RandomRule:随机策略,在 index 上随机,选择 index 对应位置的 server。
5、 RetryRule :对选定的负载均衡策略机上重试机制,在一个配置时间段内当选择 server 不成功,则 一直尝试使用 subRule 的方式选择一个可用的 server。
6、 RoundRobinRule:轮询策略,轮询 index,选择 index 对应位置的 server(默认策略)。
7、 WeightedResponseTimeRule:权重策略(基于轮询策略,根据响应时间分配一个 weight(权重),响 应时间越长,weight 越小,被选中的可能性越低) 。
二、 负载均衡的自定义
1、 通过代码实现
2、 通过配置文件实现
三、 通过代码实现
随机算法
package com.comsys.education.configuration; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.netflix.loadbalancer.IRule; import com.netflix.loadbalancer.RandomRule; @Configuration public class RandomRuleConfig { @Bean public IRule randomRule() { return new RandomRule(); } }
配置类:
package com.comsys.education.configuration; import org.springframework.cloud.netflix.ribbon.RibbonClient; import org.springframework.context.annotation.Configuration; @Configuration @RibbonClient(name = "spring-cloud-provider", configuration = RandomRuleConfig.class) public class ProviderConfiguration { }
说明:只需要简单的 2 个配置,就可以指定轮询的算法。
注意:在 RandomRuleConfig 的类里面,加上@Configuration 注解会导致所有的策略都被覆盖。
原因:如果 RandomRuleConfig 被 SpringContext 扫描,否则会被@RibbonClients 共用,会覆盖。
四、 同时使用 2 种以上的不同策略算法
方法 1:移除@Configuration 注解
package com.comsys.education.configuration; import org.springframework.context.annotation.Bean; import com.netflix.loadbalancer.IRule; import com.netflix.loadbalancer.RoundRobinRule; // @Configuration public class RoundRobinRuleConfig { @Bean public IRule roundRobinRule() { return new RoundRobinRule(); } }
配置类:
package com.comsys.education.configuration; import org.springframework.cloud.netflix.ribbon.RibbonClient; import org.springframework.context.annotation.Configuration; @Configuration @RibbonClient(name = "spring-cloud-provider2", configuration = RoundRobinRuleConfig.class) public class Provider2Configuration { }
说明:新增 RoundRobinRuleConfig 类,移除@Configuration 注解,同时也要把 RandomRuleConfig 的
@Configuration 注解移除
方法 2:指定@ComponentScan 的扫描路径(默认是扫描主类下面所有的文件夹) 。
方法 3:把它移到主类的外面,让主类扫描不到。
说明:只要不让 SpringContext 扫描到即可。
五、 通过配置配置文件实现不同的算法 配置文件添加:
#设置策略 spring-cloud-provider.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RandomRule spring-cloud-provider2.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RandomRule 说明:格式为:应用名.ribbon.NFLoadBalancerRuleClassName=xxx
相关文章推荐
- Spring Cloud微服务开发笔记5——Ribbon负载均衡策略规则定制
- 客户端负载均衡Ribbon之一:Spring Cloud Netflix负载均衡组件Ribbon介绍
- Ribbon负载均衡策略配置
- 【SpringCloud】Netflix源码解析之Ribbon:负载均衡策略的定义和实现
- SpringCloud(第 007 篇)电影微服务,使用定制化 Ribbon 在客户端进行负载均衡,使用 RibbonClient 不同服务不同配置策略
- spring cloud中Ribbon自定义负载均衡策略
- SpringCloud(第 007 篇)电影微服务,使用定制化 Ribbon 在客户端进行负载均衡,使用 RibbonClient 不同服务不同配置策略
- Ribbon负载均衡策略配置
- Ribbon负载均衡策略与自定义配置
- Ribbon负载均衡策略配置
- SpringCloud(第 007 篇)电影微服务,使用定制化 Ribbon 在客户端进行负载均衡,使用 RibbonClient 不同服务不同配置策略
- 阿里云基于ECS的SLB负载均衡策略科普文章
- 分布式环境中的负载均衡策略
- 四层负载均衡和七层负载均衡的区别
- Nginx负载均衡策略
- Ribbon实现客户端的负载均衡
- SpringCloud 入门 | 负载均衡之rest+ribbon
- 网易MySQL中间件的负载均衡策略及性能优化 - MySQL
- nginx负载均衡的4中配置策略
- 四层负载均衡和七层负载均衡的区别