hystrix 有三种状态:
熔断关闭:接口发生短路则走fallback方法 如果连续几次之后则会开启熔断状态
熔断开启:所有请求不走接口 直接走fallback方法 熔断开启一段时间之后会打开半熔断状态
半熔断状态:允许少量请求走半熔断状态 如果响应成功则关闭熔断 如果响应失败则继续开启熔断
1.添加pom.xml文件
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>2.2.7.RELEASE</version>
</dependency>
2.添加yml文件
#熔断分为三种状态
#熔断开启、熔断关闭、半熔断状态 熔断开启状态所有请求直接走fallback;
#半熔断状态 允许少量请求访问 如果成功则关闭熔断如果失败则继续开启熔断
feign:
circuitbreaker:
enabled: true
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 1000
strategy: SEMAPHORE # 设置执行策略为信号量
circuitBreaker:
requestVolumeThreshold: 3 # 设置触发断路器的最小请求次数
sleepWindowInMilliseconds: 50000 # 断路器打开后的暂停时间
errorThresholdPercentage: 50 # 设置触发断路器的错误百分比
3.启动类添加注解 @EnableHystrix
4.feign注解修改
@FeignClient(value = "song-auth",fallback = AuthServiceFallback.class)
public interface AuthService {
@PostMapping("/userInfo/getUserInfo")//访问路径
String findById();//对应远程调用controller接口
}
5.添加熔断返回类
@Component
public class AuthServiceFallback implements AuthService {
@Override
public String findById() {
return "服务调用失败";
}
}