Spring Cloud Feign 负载均衡策略配置

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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值