Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the function of sentinel's SentinelGatewayFilter?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what is the function of SentinelGatewayFilter of sentinel". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the role of SentinelGatewayFilter of sentinel"?

Order

This paper mainly studies the SentinelGatewayFilter of sentinel.

SentinelGatewayFilter

Sentinel-1.6.2/sentinel-adapter/sentinel-spring-cloud-gateway-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/gateway/sc/SentinelGatewayFilter.java

Public class SentinelGatewayFilter implements GatewayFilter, GlobalFilter {private final GatewayParamParser paramParser = new GatewayParamParser (new ServerWebExchangeItemParser ()); @ Override public Mono filter (ServerWebExchange exchange, GatewayFilterChain chain) {Route route = exchange.getAttribute (ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR); Mono asyncResult = chain.filter (exchange); if (route! = null) {String routeId = route.getId () Object [] params = paramParser.parseParameterFor (routeId, exchange, r-> r.getResourceMode () = = SentinelGatewayConstants.RESOURCE_MODE_ROUTE_ID); String origin = Optional.ofNullable (GatewayCallbackManager.getRequestOriginParser ()) .map (f-> f.apply (exchange)) .orElse ("") AsyncResult = asyncResult.transform (new SentinelReactorTransformer (new EntryConfig (routeId, EntryType.IN, 1, params, new ContextConfig (contextName (routeId), origin);} Set matchingApis = pickMatchingApiDefinitions (exchange) For (String apiName: matchingApis) {Object [] params = paramParser.parseParameterFor (apiName, exchange, r-> r.getResourceMode () = = SentinelGatewayConstants.RESOURCE_MODE_CUSTOM_API_NAME); asyncResult = asyncResult.transform (new EntryConfig (apiName, EntryType.IN, 1, params));} return asyncResult } private String contextName (String route) {return SentinelGatewayConstants.GATEWAY_CONTEXT_ROUTE_PREFIX + route;} Set pickMatchingApiDefinitions (ServerWebExchange exchange) {return GatewayApiMatcherManager.getApiMatcherMap () .values () .stream () .filter (m-> m.test (exchange)) .map (WebExchangeApiMatcher::getApiName) .filter (Collectors.toSet ());}}

SentinelGatewayFilter implements GatewayFilter and GlobalFilter interfaces; its filter method is mainly to obtain route information, and then transform asyncResult. Here, SentinelReactorTransformer is used.

SentinelReactorTransformer

Sentinel-1.6.2/sentinel-adapter/sentinel-reactor-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/reactor/SentinelReactorTransformer.java

Public class SentinelReactorTransformer implements Function {private final EntryConfig entryConfig; public SentinelReactorTransformer (String resourceName) {this (new EntryConfig (resourceName));} public SentinelReactorTransformer (EntryConfig entryConfig) {AssertUtil.notNull (entryConfig, "entryConfig cannot be null"); this.entryConfig = entryConfig;} @ Override public Publisher apply (Publisher publisher) {if (publisher instanceof Mono) {return new MonoSentinelOperator ((Mono) publisher, entryConfig) } if (publisher instanceof Flux) {return new FluxSentinelOperator ((Flux) publisher, entryConfig);} throw new IllegalStateException ("Publisher type is not supported:" + publisher.getClass (). GetCanonicalName ());}}

SentinelReactorTransformer uses entryConfig to create MonoSentinelOperator or MonoSentinelOperator

MonoSentinelOperator

Sentinel-1.6.2/sentinel-adapter/sentinel-reactor-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/reactor/MonoSentinelOperator.java

Public class MonoSentinelOperator extends MonoOperator {private final EntryConfig entryConfig; public MonoSentinelOperator (Mono

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report