1、添加依赖包
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
2、在启动类添加注解支持
@EnableHystrix
3、配置熔断时间
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 2000 #默认3秒
4、编写熔断处理方案
@GetMapping("s")
@HystrixCommand(fallbackMethod ="fallbackObj" ) //熔断就执行fallbackObj方法
public Object object(){
String s = restTemplate.getForObject("http://USER-SERVICE/user", String.class);
return s;
}
public Object fallbackObj(){
return "当前访问人数过多,请从新访问";
}