我们在使用后台微服务的时候,各个服务之前会有很多请求和交叉业务。这里会引起雪崩、超时等异常处理。SpringCloud Hystrix服务降级、容错机治理使 hystrix 有很好的支持,引入后实现断路器功能。
1:pom 引入jar包
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
2:添加注解
Application中增加 @EnableCircuitBreaker 开启hystrix功能
3:配置文件配置
注意:feign中的hystrix的enabled属性要设置true
hystrix:
command:
transferApprove: # 这里是要设置超时时间的方法,如新增其他方法需要增加此节点信息。
execution:
isolation:
thread:
timeoutInMilliseconds: 6000 #默认连接超时时间是1秒
4:增加代码
代码注意fastOrBatchFallback的出参和入参要和设置了@