What is the function of sentinel's SentinelGatewayFilter?
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