Spring Cloud - Hystrix
文章目录
服务容错和Hystrix
一旦下游服务C因某些原因变得不可用,积压了大量请求,服务B的请求线程也随之阻塞。线程资源逐渐耗尽,使得服务B也变得不可用。紧接着,服务A也变为不可用,整个调用链路被拖垮(雪崩)
主要为了解决雪崩效应
功能
-
服务降级
- 双十一,网站被挤爆了
- 网络开小差了
- 区分业务,优先核心服务,非核心服务不可用或弱可用
- HystrixCommand注解指定
- fallbackMethod(回退函数)中具体实现降级逻辑
-
服务熔断
- 开启熔断
在固定时间窗口内,接口调用超时比率达到一个阈值,会开启熔断。进入熔断状态后,后续对该服务接口的调用不再经过网络,直接执行本地的默认方法,达到服务降级的效果。
- 熔断恢复
熔断不可能是永久的。当经过了规定时间之后,服务将从熔断状态回复过来,再次接受调用方的远程调用。
- 开启熔断
- 依赖隔离
- 监控(Hystrix DashBord)
使用
-
导入pom
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency>
-
开启注解
@EnableCircuitBreaker-
或者替换为
@EnableSpringCloud
-
-
对应方法上面使用注解(这个方法必须写)
@HystrixCommand(fallbackMethod = "fallback")-
设置全局
@DefaultProperties(defaultFallback = "defaultFallBack")
- 设置默认fallback和超时时间
-
@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...
状态机
容错:重试机制,第一次不成功再重试一次就成功了
故障问题:使用断路器模式,故障达到一定的值,断路器断闸
熔断的三个状态
close(关闭状态)
服务没有故障时,熔断器所处的状态,对调用方的调用不做任何限制。
open(熔断器打开状态)
在固定时间窗口内(Hystrix默认是10秒),接口调用出错比率达到一个阈值(Hystrix默认为50%),会进入熔断开启状态。进入熔断状态后,后续对该服务接口的调用不再经过网络,直接执行本地的fallback方法。
half open (半熔断)
在进入熔断开启状态一段时间之后(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的使用
-
配置
feign: hystrix: enabled: true - 修改对应client增加fallback,类注解
@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) { } } }- 更改扫描包的路径,能够读取到client
可视化配置
导入pom
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
开启注解
@EnableHystrixDashboard
访问http://localhost:8899/hystrix
问题
使用了zuul会让类懒加载,所以第一次访问会超时,这时候我们需要把配置直接放到zuul里面

评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论