熔断与降级
# 什么是熔断?
在微服务架构中,当调用链路的某个微服务不可用或者响应时间太长,会进行服务熔断,不再有该微服务的调用,快速返回响应信息,当监测到该节点微服务调用响应正常后,恢复调用链路;
# 什么是降级?
当服务器压力剧增情况下,根据当前业务情况及流量对一些服务和页面有策略的降级,来释放服务器资源以保证核心任务正常运行;
限流降级:
达到阈值后,后续请求被降级;
# Hystrix
隔离策略:线程池隔离/信号量隔离;
熔断降级策略:基于失败比率;
@Slf4j
@FilterAspect(id=ROUTER_FILTER_ID,
name = ROUTER_FILTER_NAME,
order = ROUTER_FILTER_ORDER)
public class RouterFilter implements Filter {
@Override
public void doFilter(GatewayContext ctx) throws Exception {
Optional<Rule.HystrixConfig> hystrixConfig = getHystrixConfig(ctx);
if(hystrixConfig.isPresent()){
routeWithHystrix(ctx,hystrixConfig);
}else{
route(ctx,hystrixConfig);
}
}
private static Optional<Rule.HystrixConfig> getHystrixConfig(GatewayContext gatewayContext){
Rule rule = gatewayContext.getRule();
Optional<Rule.HystrixConfig> hystrixConfig = rule.getHystrixConfigs().stream()
.filter(c-> StringUtils.equals(c.getPath(),gatewayContext.getRequest().getPath()))
.findFirst();
return hystrixConfig;
}
private CompletableFuture<Response> route(GatewayContext ctx, Optional<Rule.HystrixConfig> hystrixConfig) {
Request request = ctx.getRequest().build();
CompletableFuture<Response> future = AsyncHttpHelper.getInstance().executeRequest(request);
boolean whenComplete = ConfigLoader.getConfig().isWhenComplete();
if (whenComplete) {
future.whenComplete((response, throwable) -> {
complete(request, response, throwable, ctx,hystrixConfig);
});
} else {
future.whenCompleteAsync((response, throwable) -> {
complete(request, response, throwable, ctx,hystrixConfig);
});
}
return future;
}
private void routeWithHystrix(GatewayContext ctx, Optional<Rule.HystrixConfig> hystrixConfig) {
HystrixCommand.Setter setter = HystrixCommand.Setter.withGroupKey(HystrixCommandGroupKey
.Factory
.asKey(ctx.getUniqueId()))
.andCommandKey(HystrixCommandKey.Factory
.asKey(ctx.getRequest().getPath()))
//线程池大小
.andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter()
.withCoreSize(hystrixConfig.get().getThreadCoreSize()))
.andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
//线程池
.withExecutionIsolationStrategy(HystrixCommandProperties.ExecutionIsolationStrategy.THREAD)
//超时时间
.withExecutionIsolationStrategy(HystrixCommandProperties.ExecutionIsolationStrategy.THREAD)
.withExecutionIsolationThreadInterruptOnTimeout(true)
.withExecutionTimeoutEnabled(true));
new HystrixCommand<Object>(setter){
@Override
protected Object run() throws Exception {
route(ctx,hystrixConfig).get();
return null;
}
@Override
protected Object getFallback(){
ctx.setResponse(hystrixConfig);
ctx.written();
return null;
}
}.execute();
}
// .......
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74