一旦下游服务C因某些缘由变得不可用,积压了大量请求,服务B的请求线程也随之阻塞。线程资源逐渐耗尽,使得服务B也变得不可用。紧接着,服务A也变为不可用,整个调用链路被拖垮(雪崩)html
主要为了解决雪崩效应java
服务降级git
服务熔断github
在固定时间窗口内,接口调用超时比率达到一个阈值,会开启熔断。进入熔断状态后,后续对该服务接口的调用再也不通过网络,直接执行本地的默认方法,达到服务降级的效果。spring
熔断不多是永久的。当通过了规定时间以后,服务将从熔断状态回复过来,再次接受调用方的远程调用。网络
导入pomapp
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency>
开启注解@EnableCircuitBreaker
ide
或者替换为@EnableSpringCloud
函数
对应方法上面使用注解(这个方法必须写)post
@HystrixCommand(fallbackMethod = "fallback")
设置全局
@DefaultProperties(defaultFallback = "defaultFallBack")
@RestController @DefaultProperties(defaultFallback = "defaultFallBack") public class HystrixController { //@HystrixCommand(fallbackMethod = "fallback") @HystrixCommand(commandProperties = { @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000") }) @GetMapping("/getProductInfoList") public String getProductInfo() { RestTemplate restTemplate = new RestTemplate(); return restTemplate.postForObject("http://127.0.0.1:9083/product/listForOrder", Collections.singletonList("157875196366160022"), String.class); } private String fallback() { return "太拥挤了,请稍后再试~"; } private String defaultFallBack() { return "默认提示:太拥挤了,请稍后重试~"; } }
会自动实现依赖隔离,进行容错保护
参数参考:http://zyouwei.com/%E6%8A%80%...
circuitBreaker
断路器
circuitBreaker.requestVolumeThreshold
circuitBreaker.sleepWindowInMilliseconds
circuitBreaker.errorThresholdPercentage
https://martinfowler.com/blik...
状态机
容错:重试机制,第一次不成功再重试一次就成功了
故障问题:使用断路器模式,故障达到必定的值,断路器断闸
服务没有故障时,熔断器所处的状态,对调用方的调用不作任何限制。
在固定时间窗口内(Hystrix默认是10秒),接口调用出错比率达到一个阈值(Hystrix默认为50%),会进入熔断开启状态。进入熔断状态后,后续对该服务接口的调用再也不通过网络,直接执行本地的fallback方法。
在进入熔断开启状态一段时间以后(Hystrix默认是5秒),熔断器会进入半熔断状态。所谓半熔断就是尝试恢复服务调用,容许有限的流量调用该服务,并监控调用成功率。若是成功率达到预期,则说明服务已恢复,进入熔断关闭状态;若是成功率仍旧很低,则从新进入熔断关闭状态
``
@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "10"), //时间窗口,统计时间范围 @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "1000"), //错误百分比 @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "60")
必须在对应的方法上使用注解 @HystrixCommand
hystrix: command: default: execution: isolation: thread: timeoutInMilliseconds: 3000 # circuitBreaker: # enabled: true
给特定方法设置:commandKey
配置
feign: hystrix: enabled: true
@Component
不要忘记了@FeignClient(name = "product",fallback =ProductClient.FallBackClass.class ) public interface ProductClient { @PostMapping("/product/listForOrder") List<ProductInfoOutPut> listForOrder(List<String> productIdList); @PostMapping("/product/decreaseStock") void decreaseStock(List<DecreaseStockInput> CartDTO); @Component class FallBackClass implements ProductClient{ @Override public List<ProductInfoOutPut> listForOrder(List<String> productIdList) { return null; } @Override public void decreaseStock(List<DecreaseStockInput> CartDTO) { } } }
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId> </dependency>
@EnableHystrixDashboard
访问http://localhost:8899/hystrix
使用了zuul会让类懒加载,因此第一次访问会超时,这时候咱们须要把配置直接放到zuul里面