当前位置:网站首页>Sentinel source code analysis Part 6 - sentinel adapter module Chapter 4 zuul2 gateway
Sentinel source code analysis Part 6 - sentinel adapter module Chapter 4 zuul2 gateway
2022-06-30 01:11:00 【Age man】
List of articles
SentinelZuulInboundFilter
- The route extractor extracts resource information for flow restriction
- according to api And other information
- In case of current limiting, return
- structure ZUUL_CTX_SENTINEL_ENTRIES_KEY Information handover OutBound complete sentinel Job site cleaning
public class SentinelZuulInboundFilter extends HttpInboundFilter {
@Override
public Observable<HttpRequestMessage> applyAsync(HttpRequestMessage request) {
Current limiting execution inlet
if (executor != null) {
return Observable.just(request).subscribeOn(Schedulers.from(executor)).flatMap(this::apply);
} else {
return Observable.just(request).flatMap(this::apply);
}
}
application sentinel Current limiting logic
private Observable<HttpRequestMessage> apply(HttpRequestMessage request) {
SessionContext context = request.getContext();
Deque<EntryHolder> holders = new ArrayDeque<>();
String routeId = routeExtractor.apply(request);
String fallBackRoute = routeId;
try {
Pick up the routing id
if (StringUtil.isNotBlank(routeId)) {
structure context
ContextUtil.enter(GATEWAY_CONTEXT_ROUTE_PREFIX + routeId);
Execute current limiting logic
doSentinelEntry(routeId, RESOURCE_MODE_ROUTE_ID, request, holders);
}
Extract matching api Information
Set<String> matchingApis = pickMatchingApiDefinitions(request);
if (!matchingApis.isEmpty() && ContextUtil.getContext() == null) {
ContextUtil.enter(SentinelZuul2Constants.ZUUL_DEFAULT_CONTEXT);
}
Current limiting interception
for (String apiName : matchingApis) {
fallBackRoute = apiName;
doSentinelEntry(apiName, RESOURCE_MODE_CUSTOM_API_NAME, request, holders);
}
return Observable.just(request);
} catch (BlockException t) {
Current limiting occurs
context.put(SentinelZuul2Constants.ZUUL_CTX_SENTINEL_BLOCKED_FLAG, Boolean.TRUE);
context.put(SentinelZuul2Constants.ZUUL_CTX_SENTINEL_FALLBACK_ROUTE, fallBackRoute);
if (fastError) {
context.setShouldSendErrorResponse(true);
context.setErrorEndpoint(blockedEndpointName);
} else {
context.setEndpoint(blockedEndpointName);
}
return Observable.error(t);
} finally {
Give me how to clear the stack filter entry
if (!holders.isEmpty()) {
context.put(SentinelZuul2Constants.ZUUL_CTX_SENTINEL_ENTRIES_KEY, holders);
}
ContextUtil.exit();
}
}
Execute current limiting logic
private void doSentinelEntry(String resourceName, final int resType, HttpRequestMessage input, Deque<EntryHolder> holders) throws BlockException {
Object[] params = paramParser.parseParameterFor(resourceName, input, r -> r.getResourceMode() == resType);
AsyncEntry entry = SphU.asyncEntry(resourceName, ResourceTypeConstants.COMMON_API_GATEWAY, EntryType.IN, params);
holders.push(new EntryHolder(entry, params));
}
}
SentinelZuulOutboundFilter
- adopt Outbound Complete resource cleanup
public class SentinelZuulOutboundFilter extends HttpOutboundFilter {
@Override
public Observable<HttpResponseMessage> applyAsync(HttpResponseMessage input) {
return Observable.just(apply(input));
}
public HttpResponseMessage apply(HttpResponseMessage response) {
SessionContext context = response.getContext();
non-existent entry Directly respond to
if (context.get(SentinelZuul2Constants.ZUUL_CTX_SENTINEL_ENTRIES_KEY) == null) {
return response;
}
There is entry Through exit clear entry
boolean previousBlocked = context.getFilterErrors().stream()
.anyMatch(e -> BlockException.isBlockException(e.getException()));
Deque<EntryHolder> holders = (Deque<EntryHolder>) context.get(SentinelZuul2Constants.ZUUL_CTX_SENTINEL_ENTRIES_KEY);
while (!holders.isEmpty()) {
EntryHolder holder = holders.pop();
if (!previousBlocked) {
Tracer.traceEntry(context.getError(), holder.getEntry());
holder.getEntry().exit(1, holder.getParams());
}
}
return response;
}
}
边栏推荐
- Cantilever beam calculation [matlab code]
- Some thoughts on life
- Crmeb SMS for program configuration of knowledge payment system
- 作文总写不好怎么办?猿辅导:家长要注意这几点
- [mrctf2020]ezpop-1 | PHP serialization
- I, 33 years old, ByteDance test development, unveiled the real income of Beijing "test post"
- VIM编辑器常用指令
- 解决choice金融终端Excel/Wps插件修复visual basic异常
- 2022年最新最详细IDEA关联数据库方式、在IDEA中进行数据库的可视化操作(包含图解过程)
- How did the data center change from "Britney Spears" to "Mrs. bull"?
猜你喜欢

Seata 与三大平台携手编程之夏,百万奖金等你来拿

The first unlucky person watching eth 2.0

UDP servers and clients in go

In depth analysis of a large number of clos on the server_ The root of wait

ResizeKit2.NET大小和分辨率独立

Online sql to CSV tool

Seata and the three platforms are working together in the summer of programming. Millions of bonuses are waiting for you

Analysis of natural frequency and buckling load of cylinder by finite element method

Exercise "product": self made colorful Prompt string display tool (for loop and if condition judgment)

如何拒绝期末复习无用功?猿辅导:找准适合自己的复习方法很重要
随机推荐
我,33岁,字节跳动测试开发,揭开北京“测试岗”的真实收入
网易云音乐内测音乐社交 App“MUS”,通过音乐匹配同频朋友
作文总写不好怎么办?猿辅导:家长要注意这几点
[Simulation Proteus] détection de port 8 bits 8 touches indépendantes
Programmers with a monthly salary of less than 30K must recite the interview stereotype. I will eat it first!
Sfdp 超级表单开发平台 V6.0.4 正式发布
Too voluminous ~ eight part essay, the strongest king of interview!
Shell spec date format
MySQL function
numpy的索引和图片的索引一样吗?
UDP servers and clients in go
如何在IDEA中自定義模板、快速生成完整的代碼?
Crmeb SMS for program configuration of knowledge payment system
If the amount exceeds 6 digits after the decimal point, only 6 digits will be reserved, and if it is less than 6 digits, it will remain the same - Basic accumulation
【Proteus仿真】8比特端口檢測8獨立按鍵
练习副“产品”:自制七彩提示字符串展示工具(for循环、if条件判断)
2020-12-03
SFDP super form development platform v6.0.4 was officially released
优秀的测试/开发程序员与普通的程序员对比......
如何查看一个文件夹下所有文件的大小?