当前位置:网站首页>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;
}
}
边栏推荐
- 英伟达Jetson Nano的初步了解
- The unity editor randomly generates objects. After changing the scene, the problem of object loss is solved
- How to seamlessly transition from traditional microservice framework to service grid ASM
- Time flies that year
- Developers, why does the maturity of container technology herald the arrival of cloud native era?
- 数据中台咋就从“小甜甜”变成了“牛夫人”?
- Programmers with a monthly salary of less than 30K must recite the interview stereotype. I will eat it first!
- Quick Pow: 如何快速求幂
- 传统微服务框架如何无缝过渡到服务网格 ASM
- PHP wechat merchant transfer to change initiating merchant transfer API
猜你喜欢

Nested call and chained access of functions in handwritten C language

R语言线性回归模型拟合诊断异常值分析家庭燃气消耗量和卡路里实例带自测题

如何拒绝期末复习无用功?猿辅导:找准适合自己的复习方法很重要

R language linear regression model fitting diagnosis outliers analysis of domestic gas consumption and calorie examples with self-test questions

如何查看一个文件夹下所有文件的大小?

What if you can't write your composition well? Ape counseling: parents should pay attention to these points

Go 中的 UDP 服务器和客户端
![[concurrent programming] if you use channel to solve concurrency problems?](/img/6c/76bcc9b63117bda57937a49cfd8696.jpg)
[concurrent programming] if you use channel to solve concurrency problems?

How to customize templates and quickly generate complete code in idea?
![[cloud native] kernel security in container scenario](/img/cc/828a8f246b28cb02b7efa1bdd8dee4.png)
[cloud native] kernel security in container scenario
随机推荐
How to customize templates and quickly generate complete code in idea?
Seata et les trois plateformes travaillent main dans la main pour programmer Summer, un million de bonus vous attend
Analysis of natural frequency and buckling load of cylinder by finite element method
R language linear regression model fitting diagnosis outliers analysis of domestic gas consumption and calorie examples with self-test questions
Ml: introduction to confidence interval (the difference and relationship between precision / accuracy / accuracy), use method, and detailed introduction to case application
Cantilever beam calculation [matlab code]
Some thoughts on life
Application of pointer in STC89C52 single chip microcomputer and demonstration of practical cases
[cloud native] kernel security in container scenario
Time does not spare
如何查看一个文件夹下所有文件的大小?
Crmeb SMS for program configuration of knowledge payment system
Preliminary understanding of NVIDIA Jetson nano
Experience of C language course design: open source sharing of "push box" course design works
R语言线性回归模型拟合诊断异常值分析家庭燃气消耗量和卡路里实例带自测题
Stimulus reports reporting tool, stimulus creates and builds reports
IDEA中的常用设置
Video to image -cv2 Videocapture() usage
Time flies that year
[Simulation Proteus] détection de port 8 bits 8 touches indépendantes