Spring Cloud Feign 负载均衡策略配置
发布日期:2021-09-18 00:51:42
浏览次数:6
分类:技术文章
本文共 3094 字,大约阅读时间需要 10 分钟。
Spring Cloud Feign 负载均衡策略配置
feignClient中修改ribbon的配置1.使用@FeignClient注解发现服务
服务提供者的controller:
@RestController public class StudentController { @Autowired private StudentService studentService; @GetMapping("/getAll/{id}") public Student getAll(@PathVariable("id")Integer id){ System.out.println("stu-provide:localhost:5865==>消费者查询学生时间:"+new Date().toLocaleString()); Student stu = studentService.getAllStu(id); return stu; } }
消费者端:
//使用FeignClient 告知发布方的应用名称 默认使用ribbon进行负载均衡 @FeignClient(name="stu-provide") public interface TestFeign { @RequestMapping(value = "/getAll/{id}",method = RequestMethod.GET) public Student getAll(@PathVariable("id") Integer id); }
在使用@FeignClient注解的时候 是默认使用了ribbon进行客户端的负载均衡的,默认的是随机的策略,那么如果我们想要更改策略的话,需要修改消费者yml中的配置,如下:
server: port: 9301 eureka: client: healthcheck: enable: true serviceUrl: defaultZone: http://user:password123@localhost:8761/eureka # defaultZone: http://eureka1:8761/eureka,http://eureka2:8762/eureka,http://eureka3:8763/eureka instance: hostname: localhost ipAddress: localhost prefer-ip-address: true instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}} spring: application: name: stu-consumer datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/test?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8;useSSL=true username: **** password: **** type: com.alibaba.druid.pool.DruidDataSource initialSize: 5 minIdle: 5 maxActive: 30 maxWait: 10000 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMills: 300000 validationQuery: SELECT 1 FROM DUAL session: store-type: none # 配置ribbon stu-provide: ribbon: # NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule #配置规则 随机 # NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RoundRobinRule #配置规则 轮询 # NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RetryRule #配置规则 重试 # NFLoadBalancerRuleClassName: com.netflix.loadbalancer.WeightedResponseTimeRule #配置规则 响应时间权重 NFLoadBalancerRuleClassName: com.netflix.loadbalancer.BestAvailableRule #配置规则 最空闲连接策略 ConnectTimeout: 500 #请求连接超时时间 ReadTimeout: 1000 #请求处理的超时时间 OkToRetryOnAllOperations: true #对所有请求都进行重试 MaxAutoRetriesNextServer: 2 #切换实例的重试次数 MaxAutoRetries: 1 #对当前实例的重试次数
这里我们可以看到ribbon的策略主要有以下几种:
com.netflix.loadbalancer.RandomRule #配置规则 随机 com.netflix.loadbalancer.RoundRobinRule #配置规则 轮询 com.netflix.loadbalancer.RetryRule #配置规则 重试 com.netflix.loadbalancer.WeightedResponseTimeRule #配置规则 响应时间权重 com.netflix.loadbalancer.BestAvailableRule #配置规则 最空闲连接策略
随机:几个提供者间随机访问
轮询:轮流访问
重试:在一段时间内通过RoundRobinRule选择服务实例,一段时间内没有选择出服务则线程终止
响应时间权重:根据平均响应时间来计算权重
原文链接:https://blog.csdn.net/guoqiusheng/article/details/88898426转载地址:https://blog.csdn.net/yucaifu1989/article/details/105020317 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
能坚持,总会有不一样的收获!
[***.219.124.196]2024年04月01日 07时50分20秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
javascript解析json
2019-04-27
WinDbg安装与使用
2019-04-27
推荐阅读的多核编程技术书籍
2019-04-27
维基百科上的算法和数据结构链接很强大
2019-04-27
选择排序
2019-04-27
PHP session回收机制
2019-04-27
最新的全球编程语言,操作系统,web服务器等使用率分析报告
2019-04-27
用C语言写PHP扩展
2019-04-27
PHP Extension programming
2019-04-27
海量数据处理
2019-04-27
PHP防止注入攻击
2019-04-27
多路IO复用模型 select epoll 等
2019-04-27
Linux Epoll介绍和程序实例
2019-04-27
output_buffering详细介绍
2019-04-27
php缓冲 output_buffering和ob_start
2019-04-27
php error_reporting 详解
2019-04-27
剖析PHP中的输出缓冲
2019-04-27
HTTP响应头不缓存
2019-04-27
PHP安装扩展mcrypt以及相关依赖项 【PHP安装PECL扩展的方法】
2019-04-27
Javascript到PHP加密通讯的简单实现
2019-04-27