spring cloud中通过配置文件自定义Ribbon负载均衡策略
一、Ribbon中的负载均衡策略
1、Ribbon中支持的负载均衡策略
AvailabilityFilteringRule:过滤掉那些因为一直连接失败的被标记为circuit tripped的后端server,并过滤掉那些高并发的的后端server(active connections 超过配置的阈值) | 使用一个AvailabilityPredicate来包含过滤server的逻辑,其实就就是检查status里记录的各个server的运行状态
RandomRule:随机选择一个server
BestAvailabl:选择一个最小的并发请求的server,逐个考察Server,如果Server被tripped了,则忽略
RoundRobinRule:roundRobin方式轮询选择, 轮询index,选择index对应位置的server
WeightedResponseTimeRule:根据响应时间分配一个weight(权重),响应时间越长,weight越小,被选中的可能性越低
RetryRule:对选定的负载均衡策略机上重试机制,在一个配置时间段内当选择server不成功,则一直尝试使用subRule的方式选择一个可用的server
ZoneAvoidanceRule:复合判断server所在区域的性能和server的可用性选择server
ResponseTimeWeightedRule:作用同WeightedResponseTimeRule,二者作用是一样的,ResponseTimeWeightedRule后来改名为WeightedResponseTimeRule
二、验证
1、自定义负载均衡策略
# 自定义负载均衡策略
springboot-h2.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RandomRule // 自定义使用随机策略,springboot-h2是服务应用名
2、修改调用代码
package com.chhliu.springboot.restful.controller; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate; import com.chhliu.springboot.restful.vo.User; @RestController
public class RestTemplateController {
@Autowired
private RestTemplate restTemplate; @Autowired
private LoadBalancerClient loadBalancerClient; @GetMapping("/template/{id}")
public User findById(@PathVariable Long id) {
ServiceInstance serviceInstance = this.loadBalancerClient.choose("springboot-h2");
System.out.println("===" + ":" + serviceInstance.getServiceId() + ":" + serviceInstance.getHost() + ":"
+ serviceInstance.getPort());// 打印当前调用服务的信息
User u = this.restTemplate.getForObject("http://springboot-h2/user/" + id, User.class);
System.out.println(u);
return u;
}
}
3、测试
服务调用关系如下:
测试结果如下:
===:springboot-h2:127.0.0.1:7902
User [id=2, username=user2, name=李四, age=20, balance=100.00]
===:springboot-h2:127.0.0.1:7901
User [id=2, username=user2, name=李四, age=20, balance=100.00]
===:springboot-h2:127.0.0.1:7902
User [id=2, username=user2, name=李四, age=20, balance=100.00]
===:springboot-h2:127.0.0.1:7901
User [id=2, username=user2, name=李四, age=20, balance=100.00]
===:springboot-h2:127.0.0.1:7902
User [id=2, username=user2, name=李四, age=20, balance=100.00]
===:springboot-h2:127.0.0.1:7902
User [id=2, username=user2, name=李四, age=20, balance=100.00]
===:springboot-h2:127.0.0.1:7902
User [id=2, username=user2, name=李四, age=20, balance=100.00]
===:springboot-h2:127.0.0.1:7902
User [id=2, username=user2, name=李四, age=20, balance=100.00]
===:springboot-h2:127.0.0.1:7902
User [id=2, username=user2, name=李四, age=20, balance=100.00]
===:springboot-h2:127.0.0.1:7901
User [id=2, username=user2, name=李四, age=20, balance=100.00]
===:springboot-h2:127.0.0.1:7901
User [id=2, username=user2, name=李四, age=20, balance=100.00]
===:springboot-h2:127.0.0.1:7902
User [id=2, username=user2, name=李四, age=20, balance=100.00]
===:springboot-h2:127.0.0.1:7902
User [id=2, username=user2, name=李四, age=20, balance=100.00]
===:springboot-h2:127.0.0.1:7901
User [id=2, username=user2, name=李四, age=20, balance=100.00]
===:springboot-h2:127.0.0.1:7901
User [id=2, username=user2, name=李四, age=20, balance=100.00]
发现选择7901端口服务和7902端口服务确实是随机的!
spring cloud中通过配置文件自定义Ribbon负载均衡策略的更多相关文章
- Spring Cloud (3)B Ribbon 负载均衡 IRule
package com.service.config; import com.netflix.loadbalancer.IRule;import com.netflix.loadbalancer.Ra ...
- SPring cloud (3)A Ribbon 负载均衡 配置初步
1.引用pom <dependency> <groupId>org.springframework.cloud</groupId> <artifactId&g ...
- Spring Cloud入门教程(二):客户端负载均衡(Ribbon)
对于大型应用系统负载均衡(LB:Load Balancing)是首要被解决一个问题.在微服务之前LB方案主要是集中式负载均衡方案,在服务消费者和服务提供者之间又一个独立的LB,LB通常是专门的硬件,如 ...
- spring Cloud中,解决Feign/Ribbon整合Hystrix第一次请求失败的问题?
Spring Cloud中,Feign和Ribbon在整合了Hystrix后,可能会出现首次调用失败的问题,要如何解决该问题呢? 造成该问题的原因 Hystrix默认的超时时间是1秒,如果超过这个时间 ...
- 《Spring Cloud》学习(二) 负载均衡!
第二章 负载均衡 负载均衡是对系统的高可用.网络压力的缓解和处理能力扩容的重要手段之一.Spring Cloud Ribbon是一个基于 HTTP 和 TCP 的客户端负载均衡工具,它基于Netfli ...
- Ribbon负载均衡策略与自定义配置new
Ribbon负载均衡策略 配置 对调用的某个服务启用某种负载策略 1)通过配置文件配置 hello: ribbon: NFLoadBalancerRuleClassName:com.netflix.l ...
- Ribbon负载均衡策略与自定义配置
Ribbon负载均衡策略 配置 对调用的某个服务启用某种负载策略 1)通过配置文件配置 hello: ribbon: NFLoadBalancerRuleClassName:com.netflix.l ...
- springcloud(十四)、ribbon负载均衡策略应用案例
一.eureka-server服务中心项目不再创建 二.eureka-common-empdept公共组件项目不再掩饰 三.创建eureka-client-provider-empdept-one提供 ...
- Spring Cloud官方文档中文版-客户端负载均衡:Ribbon
官方文档地址为:http://cloud.spring.io/spring-cloud-static/Dalston.SR2/#_spring_cloud_netflix 文中例子我做了一些测试在:h ...
随机推荐
- [effictive c++] 条款04 确定对象被使用前已被初始化
成员初始化 在c和c++ 中,使用为初始化的类型经常会引发不可预料的错误,从而使得我们要花费巨大的时间用于调试查找问题,所以确定对象被使用前已被初始化是个非常好的习惯. 永远在使用之前对对象进行初始化 ...
- HDU 2178.猜数字【分析能力练习】【读题能力练习】【8月10】
猜数字 Problem Description A有1数m.B来猜.B每猜一次,A就说"太大"."太小"或"对了" . 问B猜n次能够猜到的 ...
- Singular value encountered in calculation for ROI
在ENVI中对一幅TM影像进行监督分类,在进行compute ROI separability时提示Singular value encountered in calculation for ROI, ...
- Java从零开始学十(Arrays类对数组的常用方法)
一.Arrays类常用方法 二.简单例子 package com.pb.demo1; import java.util.Arrays; /* * Arrays工具类的中的几个常用方法 * 1.copy ...
- Oracle分组取第一条数据
看看曾经的私密日志.原来自己之前被非常多小而简单的问题困惑过. 看着那时候我们还是新手中的新手做的备忘笔记! 事实上就是用了Oracle的统计函数而已! 曾经的日记(看样子应该是曾经公司的源代码,呵呵 ...
- ONVIFclient搜索设备获取rtsp地址开发笔记(精华篇)
概要: 眼下ONVIF协议家族设备已占领数字监控行业半壁江山以上.亲,作为开发人员的你还在犹豫是否了解下吗?本文介绍了ONVIFclient从设备搜索,鉴权,能力获取,媒体信息获取 ...
- Using QuickExec
Fiddler's QuickExec box allows you to launch script-commands quickly. Keyboard Shortcuts Hit ALT+Q t ...
- JDBC的简单操作
JDBC是一组能够执行SQL语句的API 由于传统的数据库操作方式需要程序员掌握各个不同的数据库的API,极其不便 因此java定义了JDBC这一标准的接口和类,为程序员操作数据库提供了统一的方式 J ...
- 远程binlog
binlog介绍 binlog,即二进制日志,它记录了数据库上的所有改变. 改变数据库的SQL语句执行结束时,将在binlog的末尾写入一条记录,同时通知语句解析器,语句执行完毕. binlog格式 ...
- xcode8 的坑 Info.plist 配置app权限
好多更新完Xcode8 的小盆友们(我也是小盆友啦),会发现当我们调用系统功能,相册,相机,麦克风等会出现崩溃,而控制台打印出一堆乱七八糟的看不懂的东西,但是最后一句话是有用的,给出了崩溃的原因 啦, ...