微服务学习18——hystrix的依赖隔离
(1)线程池隔离
(2)Hystrix自动实现了依赖隔离
配置熔断:
@HystrixCommand(commandProperties = {
@HystrixProperty(name = "circuitBreaker.enabled",value = "true"), // 设置熔断
@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "10"),
@HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "10000"),
@HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "60")
})
@GetMapping("/getProductInfoList")
public String getProductInfoList(@RequestParam("number") Integer number) {
if(number % 2 == 0) {
return "success";
}
RestTemplate restTemplate = new RestTemplate();
return restTemplate.postForObject("http://127.0.0.1:8082/product/listForOrder", Arrays.asList("157875196366160022"), String.class);
}
其中,Circuit Breaker:断路器
断路器的作用就是及时的切断错误的电路。如果发现错误率较高,就采用断路处理。
微服务处理容错一般有两种方式:
(1)重试机制;对于预期的短暂时间可以解决的问题,重试是可以解决的;
(2)断路器模式;对于重试不能解决,且要较长时间才能解决的问题,就可以采用断路器模式。
circuitBreaker.sleepWindowInMilliseconds:表示休眠时间窗
requestVolumeThreshold:表示断路器的最小请求数
errorThresholdPercentage:表示设置断路器的最小错误百分比条件。