Hystrix配置错误导致高并发场景下Gateway网关调用其他服务时迅速被降级

        错误的Gateway的yml配置如下:

spring:
  autoconfigure:
    exclude:
      - com.bjx.core.util.SpringContextUtil
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true
      globalcors:
        corsConfigurations:
          '[/**]':
            allowCredentials: true
            allowedOrigins: "*"
            allowedMethods: "*"
            allowedHeaders: "*"
      routes:
        - id: saas-manage-auth-api
          uri: lb://saas-manage-auth-api
          predicates:
            - Path=/auth/**
          filters:
            - name: Hystrix
              args:
                name: default
                fallbackUri: 'forward:/fallback/saas-manage-auth-api'
      loadbalancer:
        use404: true
      default-filters:
        - StripPrefix=1

hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 10000
  shareSecurityContext: true

ribbon:
  eager-load:
    enabled: true

logging:
  level:
    com.alibaba.cloud.nacos.client: error
    com.alibaba.nacos.client.config.impl.ClientWorker: warn

         压力测试的时候发现,异常率非常的高,30并发异常率高达70%。为了排查这个问题,直接跳过了网关去压测业务服务,发现异常率为0,初步判断问题出在网关上。

        当发生这种情况时,业务服务没有报错,网关日志显示的是降级接口输出的内容,那么查看hystrix的源码后,将配置修改如下:

        

spring:
  autoconfigure:
    exclude:
      - com.bjx.core.util.SpringContextUtil
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true
      globalcors:
        corsConfigurations:
          '[/**]':
            allowCredentials: true
            allowedOrigins: "*"
            allowedMethods: "*"
            allowedHeaders: "*"
      routes:
        - id: saas-manage-auth-api
          uri: lb://saas-manage-auth-api
          predicates:
            - Path=/auth/**
          filters:
            - name: Hystrix
              args:
                name: default
                fallbackUri: 'forward:/fallback/saas-manage-auth-api'
      loadbalancer:
        use404: true
      default-filters:
        - StripPrefix=1

hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            #服务超时时间,单位毫秒,默认1000
            timeoutInMilliseconds: 60000
      circuitBreaker:
        # 断路器检测的基础请求值,只有时间窗口内的请求数达到这个阈值时,才会判定错误率,否则比如只有一两个请求,即便都失败了,也不会打开断路器,因为基数太少了,默认20
        requestVolumeThreshold: 5000 
        # 错误百分比,超过就会短路,默认值50
        errorThresholdPercentage: 75
  threadpool:
    default:
      coreSize: 10
      maximumSize: 400
      maxQueueSize: 10000
  shareSecurityContext: true

ribbon:
  eager-load:
    enabled: true

logging:
  level:
    com.alibaba.cloud.nacos.client: error
    com.alibaba.nacos.client.config.impl.ClientWorker: warn

 重点就在这几个参数上

command:
    default:
      execution:
        isolation:
          thread:
            #服务超时时间,单位毫秒,默认1000
            timeoutInMilliseconds: 60000
      circuitBreaker:
        # 断路器检测的基础请求值,只有时间窗口内的请求数达到这个阈值时,才会判定错误率,否则比如只有一两个请求,即便都失败了,也不会打开断路器,因为基数太少了,默认20
        requestVolumeThreshold: 5000 
        # 错误百分比,超过就会短路,默认值50
        errorThresholdPercentage: 75

threadpool:
    default:
      coreSize: 10 #核心线程数
      maximumSize: 400 #最大线程数
      maxQueueSize: 10000 #最大的等待队列数,超过则拒绝请求

默认的阈值太小,超时时间也很短,所以导致大量请求都判断为超时,错误率升高后触发了熔断。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值