zoukankan      html  css  js  c++  java
  • 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选择服务实例,一段时间内没有选择出服务则线程终止

    响应时间权重:根据平均响应时间来计算权重

    举个简单的列子,就是4个实例,A,B,C,D平均响应时间为10,40,80,100,所以总响应时间是10+40+80+100=230,每个实例权重为总响应时间与实际自身的平均响应时间的差的累积所得,所以A,B,C,D的权重分别如下:
    实例A: 230-10=220
    实例B:220+(230-40)=410
    实例C:410+(230-80)=560
    实例D:560+(230-100)=690

    所以实例A:[0.220]
    实例B:(220,410]
    实例C:(410,560]
    实例D:(560,690)

    最空闲连接策略:当前空闲的提供者将优先被选取给消费者使用

    下面以轮询策略演示为例,配置后的使用结果如下:

    访问10次:http://localhost:9301/getAll/2:

    image.png

    附上代码地址:https://github.com/fengcharly/springCloud-ribbon-turbine.git

  • 相关阅读:
    CXF.bat
    终于懂了:FWinControls子控件的显示是由Windows来管理,而不是由Delphi来管理(显示透明会导致计算无效区域的方式有所不同——透明的话应减少剪裁区域,所以要进行仔细计算)
    DELPHI语法基础学习笔记-Windows 句柄、回调函数、函数重载等(Delphi中很少需要直接使用句柄,因为句柄藏在窗体、 位图及其他Delphi 对象的内部)
    Delphi体系内部的4种消息传递办法(Send,Post,Perform,Dispatch)
    delphi高手突破之异常及错误处理
    Android 短信模块分析(三) MMS入口分析
    delphi如何加上spliter分割条,任意调整大小
    浅拷贝与深度拷贝(原型模式)
    delphi高手突破学习笔记之面向对象类和对象的本质(有汇编解释 good)
    delphi模态窗体最小化会隐藏的问题
  • 原文地址:https://www.cnblogs.com/charlypage/p/9344689.html
Copyright © 2011-2022 走看看