hystrix
pom.xml
<!--hystrix 服务安全管理: 服务降级 服务熔断 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
线程池隔离、服务降级
@RestController
@RequestMapping("consumer")
@DefaultProperties(defaultFallback = "BackMethod")//hystrix 注解设置超时方法
public class ConsumerController {
@GetMapping("{id}")
@HystrixCommand //hystrix 注解
public String queryBuId(@PathVariable("id") int id){
String url = "http://user-service/user/"+id;
String user = restTemplate.getForObject(url , String.class);
return user;
}
public String BackMethod(){//超时友好提示信息方法
return "sorry,the server is busying";
}
}
application.yml文件配置超时时长
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 3000 # 设置hystrix的超时时间为3000ms
服务熔断
关闭状态
打开状态
半打开状态
默认开启