版本介绍
SpringBoot 版本 2.1.6.RELEASE
SpringCloud 版本 Greenwich.SR1
开启注解
@EnableCircuitBreaker
依赖包
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<!-- jar包版本管理 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
核心配置
# 链接
[https://github.com/Netflix/Hystrix/wiki/Configuration#execution.isolation.thread.timeoutInMilliseconds](https://github.com/Netflix/Hystrix/wiki/Configuration#execution.isolation.thread.timeoutInMilliseconds)
# 配置类
com.netflix.hystrix.HystrixCommandProperties
# 设置超时时间
execution.isolation.thread.timeoutInMilliseconds
# 设置熔断
circuitBreaker.enabled
# 时间滚动中最小请求参数,只有在一个统计窗口内处理的请求数量达到这个阈值,才会进行熔断与否的判断
circuitBreaker.requestVolumeThreshold
# 休眠时间统计窗口
circuitBreaker.sleepWindowInMilliseconds
# 错误百分比,判断熔断的阈值,默认值50,表示在一个统计窗口内有50%的请求处理失败,会触发熔断
circuitBreaker.errorThresholdPercentage
自己的配置
eureka:
instance:
# 开启IP注册
prefer-ip-address: true
# 实例ID, 即页面status
instance-id: ${eureka.instance.ip-address}:${server.port}
client:
service-url:
defaultZone: "http://eureka-server:8671/eureka/,\
http://eureka-server1:8671/eureka/"
# 应用名称
spring:
application:
name: order-service
feign:
httpclient:
enabled: false
# 单位毫秒:默认2000毫秒
connection-timeout: 2000
# 线程池大小
max-connections: 200
okhttp:
enabled: true
hystrix:
enabled: true
ribbon:
ConnectTimeout: 10_000
ReadTimeout: 5_000
# 每个实例重试次数, 设置为0防止重试
MaxAutoRetries: 0
# 重试的时候实例切换次数, , 设置为0防止重试
MaxAutoRetriesNextServer: 0
# com.netflix.hystrix.HystrixCommandProperties
# com.netflix.hystrix.contrib.javanica.conf.HystrixPropertiesManager
hystrix:
# com.netflix.hystrix.HystrixThreadPoolProperties
threadpool:
default:
# 线程池大小,默认10个
coreSize: 10
# 最大队列m,默认-1, SynchronousQueue 没必要开启
maxQueueSize: 100
# 队列拒绝阈值, 默认5,动态控制最大队列
queueSizeRejectionThreshold: 100
command:
default:
execution:
isolation:
thread:
# 设置熔断超时时间, 默认1000毫秒, 应该大于ribbon超时时间+可读时间
timeoutInMilliseconds: 15_500
circuitBreaker:
# 时间滚动中最小请求参数,只有在一个统计窗口内处理的请求数量达到这个阈值,熔断器才进行错误率的计算
requestVolumeThreshold: 20
# 熔断器中断请求5秒后会进入半打开状态,放部分流量过去重试
sleepWindowInMilliseconds: 5000
# 错误百分比,判断熔断的阈值,默认值50,表示在一个统计窗口内有50%的请求处理失败,会触发熔断
errorThresholdPercentage: 100
metrics:
rollingStats:
# 统计窗口持续时间, 默认值为10000毫秒
timeInMilliseconds: 10_000
# getUserById:
# execution:
# isolation:
# thread:
# # 设置熔断超时时间, 默认1000毫秒
management:
endpoints:
web:
# 默认配置
# base-path: /actuator
exposure:
# 开启Hystrix Dashboard监控必须配置如下
include: [hystrix.stream]
# timeoutInMilliseconds: 6000
网上查找相关资料很多,感觉默认配置基本上够用了,所以感觉不推荐修改其默认值
当然超时时间最好设置一下不然会有问题,ribbon超时时间+ribbon可读时间必须小于Hystrix超时时间,不然出发不了熔断Fallbak方法
通过源码和测试,一般提高并发需要提高这个参数
如果设置Hystrix线程池线程池大小为10(hystrix.threadpool.default.coreSize=10)时,
意味瞬间并发请求最大为10,超过的任务则会纳入最大队列
(hystrix.threadpool.default.maxQueueSize,默认队列为SynchronousQueue)
此时会触发ribbon连接超时时间ribbon.ConnectTimeout,当hystrix纳入队列的超时时间超过ribbon的连接时间,
则会抛异常java.util.concurrent.TimeoutException: null
通过设置ribbon.MaxAutoRetries和ribbon.MaxAutoRetriesNextServer可禁止重试
下面图片是自己网上找的有时间测测
image.png
网友评论