【Feign/Ribbon/Hystirx】Feign/Ribbon/Hystirx超时配置


背景

  由于项目中使用到Feign/Ribbon/Hystrix组件,在不通的场景需要配置各自组件组件的超时时间,网上找了很多博客,这块如何配置,配置的优先级以及分别适合在什么样的场景下使用没有很准确的说明
  本篇博客,主要针对项目中的业务场景说明这块的配置和使用场景

配置

  Ribbon的配置   

    1.Ribbon默认的配置
      首先Ribbon默认的配置可以查看类

      com.netflix.client.config.DefaultClientConfigImpl

           在该类中所有所有的默认属性位于  loadDefaultValues()方法

复制代码
 public void loadDefaultValues() {
        putDefaultIntegerProperty(CommonClientConfigKey.MaxHttpConnectionsPerHost, getDefaultMaxHttpConnectionsPerHost());
        putDefaultIntegerProperty(CommonClientConfigKey.MaxTotalHttpConnections, getDefaultMaxTotalHttpConnections());
        putDefaultBooleanProperty(CommonClientConfigKey.EnableConnectionPool, getDefaultEnableConnectionPool());
        putDefaultIntegerProperty(CommonClientConfigKey.MaxConnectionsPerHost, getDefaultMaxConnectionsPerHost());
        putDefaultIntegerProperty(CommonClientConfigKey.MaxTotalConnections, getDefaultMaxTotalConnections());
        putDefaultIntegerProperty(CommonClientConfigKey.ConnectTimeout, getDefaultConnectTimeout());                     --默认连接时间
        putDefaultIntegerProperty(CommonClientConfigKey.ConnectionManagerTimeout, getDefaultConnectionManagerTimeout()); 
        putDefaultIntegerProperty(CommonClientConfigKey.ReadTimeout, getDefaultReadTimeout());                           --默认请求时间
        putDefaultIntegerProperty(CommonClientConfigKey.MaxAutoRetries, getDefaultMaxAutoRetries());                     --默认最大的连接次数
        putDefaultIntegerProperty(CommonClientConfigKey.MaxAutoRetriesNextServer, getDefaultMaxAutoRetriesNextServer());
        putDefaultBooleanProperty(CommonClientConfigKey.OkToRetryOnAllOperations, getDefaultOkToRetryOnAllOperations());
        putDefaultBooleanProperty(CommonClientConfigKey.FollowRedirects, getDefaultFollowRedirects());
        putDefaultBooleanProperty(CommonClientConfigKey.ConnectionPoolCleanerTaskEnabled, getDefaultConnectionPoolCleanerTaskEnabled());
        putDefaultIntegerProperty(CommonClientConfigKey.ConnIdleEvictTimeMilliSeconds, getDefaultConnectionidleTimeInMsecs());
        putDefaultIntegerProperty(CommonClientConfigKey.ConnectionCleanerRepeatInterval, getDefaultConnectionIdleTimertaskRepeatInMsecs());
        putDefaultBooleanProperty(CommonClientConfigKey.EnableGZIPContentEncodingFilter, getDefaultEnableGzipContentEncodingFilter());
        String proxyHost = ConfigurationManager.getConfigInstance().getString(getDefaultPropName(CommonClientConfigKey.ProxyHost.key()));
     ........
复制代码

    2.Ribbon全局的配置(yaml配置文件)
       下面的配置表示使用Riibon进行远程调用时
            连接超时时间:5s
         请求超时时间:5s
      最大自动重试次数:0次
      下个实例最大请求次数:1次

ribbon:
  ReadTimeout: 5000
  ConnectTimeout: 5000
  MaxAutoRetries: 0
  MaxAutoRetriesNextServer: 1

      3.Ribbon针对单个服务的配置
      下面的配置表示,近针对audit-risk-business这个服务进行配置

audit-risk-business:
    ribbon:
          ReadTimeout: 2000
          ConnectTimeout: 2000
          MaxAutoRetries: 0
          MaxAutoRetriesNextServer: 1   

    4.问题
      Q:全局配置和针对单个服务配置的优先级
      A:全局配置就是第一级别的节点,ribbon作为一级节点。如果两个都配置了 那就是就近原则。先用带audit-risk-business的节点

  Feign的配置   

    1.Feign默认的配置
      首先Feign默认的配置可以查看类

org.springframework.cloud.openfeign.FeignClientFactoryBean

      其中 configureUsingProperties()方法

复制代码
    protected void configureUsingProperties(
            FeignClientProperties.FeignClientConfiguration config,
            Feign.Builder builder) {
        if (config == null) {
            return;
        }

        if (config.getLoggerLevel() != null) {
            builder.logLevel(config.getLoggerLevel());
        }

        if (config.getConnectTimeout() != null && config.getReadTimeout() != null) {
            builder.options(new Request.Options(config.getConnectTimeout(),
                    config.getReadTimeout())); //默认配置
        }    
复制代码

    再查看 Option内部类

复制代码
    /**
     * Creates the new Options instance using the following defaults:
     * <ul>
     * <li>Connect Timeout: 10 seconds</li>
     * <li>Read Timeout: 60 seconds</li>
     * <li>Follow all 3xx redirects</li>
     * </ul>
     */
    public Options() {
      this(10, TimeUnit.SECONDS, 60, TimeUnit.SECONDS, true);
    }
复制代码

    可以看到默认的连接超时时间为10s,请求超时时间为60s

    2.Feign全局配置和针对具体某个服务的配置

复制代码
feign:
  client:
    config:
      #default代表所有服务
      default: 
        #feign客户端建立连接超时时间
        connect-timeout: 10000
        #feign客户端建立连接后读取资源超时时间
        read-timeout: 20000
      #表示当调用audit-risk-business这个服务时,用下面的配置
      audit-risk-business:
        connect-timeout: 10000
        read-timeout: 20000
复制代码

    3.问题
    Q: 同时配置了Feign和Ribbon的超时时间,哪个生效
    A: Feign已经整合Ribbon,如果配置了Feign的超时时间,则Feign的超时时间生效,如果没有配置,则使用Ribbon的超时时间

  Hystrix的配置   

    1.Hystrix全局和默认的配置

复制代码
hystrix:
  command:
    default:  #default全局有效,service id指定应用有效
      execution:
        timeout:
          #如果enabled设置为false,则请求超时交给ribbon控制,为true,则超时作为熔断根据
          enabled: true
        isolation:
          thread:
            timeoutInMilliseconds: 1000 #断路器超时时间,默认1000ms
    AuditService#doTextAudit(TextAuditRequest): //针对具体接口
      execution:
        timeout:
          enabled: true
        isolation:
          thread:
            timeoutInMilliseconds: 1000
    audit-risk-business: //针对具体服务
      execution:
        timeout:
          enabled: true
        isolation:
          thread:
            timeoutInMilliseconds: 1000
复制代码

问题

  Q.Feign/Hystix/Ribbon 配置超时的优先级
  A.Hystrix>Feign>Ribbon
  
  Q.如何开启资源隔离/熔断/降级
  A:Feign对Hystrix的整合后,后续Feign调用的微服务之间的接口会自动开启资源隔离,
    但是要开启熔断使Hystrix的断路器的超时时间生效则必须在启动类添加注解  @EnableCircuitBreaker使断路器配置生效


  

  

posted @   听风是雨  阅读(716)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
/* 看板娘 */
点击右上角即可分享
微信分享提示