错误的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 #最大的等待队列数,超过则拒绝请求
默认的阈值太小,超时时间也很短,所以导致大量请求都判断为超时,错误率升高后触发了熔断。