1.添加依赖包
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<!-- hystrix 熔断器依赖包 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
<!-- hystrix 熔断器监控依赖包 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
2.启动方法
在启类上添加以下注解
@EnableHystrixDashboard
@EnableCircuitBreaker
@EnableFeignClients
3.配置
feign: #开启 feign 熔断器功能
hystrix:
enabled: true
ribbon: # 负载组件默认配置
ConnectTimeout: 500 #创建请求链接超时时间
ReadTimeout: 1000 #请求超时时间
OkToRetryOnAllOperations: true #对所有操作请求进行重试
MaxAutoRetries: 0 #对当前实例的重试次数
MaxAutoRetriesNextServer: 3 #切换实例的重试次数
TOKEN-SERVICE: # TOKEN-SERVICE 服务负载均衡配置 @
ribbon:
ConnectTimeout: 500
ReadTimeout: 500
OkToRetryOnAllOperations: true
MaxAutoRetries: 0
MaxAutoRetriesNextServer: 3
hystrix: #熔断器配置
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 5100 #设置超时时间
circuitBreaker:
requestVolumeThreshold: 5 #(默认值20)滚动时间窗(默认10秒),是最小统计请求数。(如果10秒内仅有19个请求,就算19个请求都失败了,断路器也不会打开)
errorThresholdPercentage: 50 #(默认值50) 在滚动时间窗内,请求大于 requestVolumeThreshold 的前提下。错误率高达 50% 则打开断路器
TokenServiceApi#getToken(String): #具体方法熔断器配置
execution:
isolation:
thread:
timeoutInMilliseconds: 5000 #设置超时时间
threadpool: #隔离连接池配置
TOKEN-SERVICE:
coreSize: 10 # TOKEN-SERVICE 服务的线程数量
hystrix 熔断器监控 url : /hystrix