Search in sources :

Example 1 with ContextConfig

use of com.alibaba.csp.sentinel.adapter.reactor.ContextConfig in project Sentinel by alibaba.

the class SentinelGatewayFilter method filter.

@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
    Route route = exchange.getAttribute(ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR);
    Mono<Void> 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, ResourceTypeConstants.COMMON_API_GATEWAY, EntryType.IN, 1, params, new ContextConfig(contextName(routeId), origin))));
    }
    Set<String> 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 SentinelReactorTransformer<>(new EntryConfig(apiName, ResourceTypeConstants.COMMON_API_GATEWAY, EntryType.IN, 1, params)));
    }
    return asyncResult;
}
Also used : AssertUtil(com.alibaba.csp.sentinel.util.AssertUtil) GlobalFilter(org.springframework.cloud.gateway.filter.GlobalFilter) Ordered(org.springframework.core.Ordered) SentinelReactorTransformer(com.alibaba.csp.sentinel.adapter.reactor.SentinelReactorTransformer) GatewayFilterChain(org.springframework.cloud.gateway.filter.GatewayFilterChain) ContextConfig(com.alibaba.csp.sentinel.adapter.reactor.ContextConfig) GatewayParamParser(com.alibaba.csp.sentinel.adapter.gateway.common.param.GatewayParamParser) EntryConfig(com.alibaba.csp.sentinel.adapter.reactor.EntryConfig) ServerWebExchangeUtils(org.springframework.cloud.gateway.support.ServerWebExchangeUtils) SentinelGatewayConstants(com.alibaba.csp.sentinel.adapter.gateway.common.SentinelGatewayConstants) RequestItemParser(com.alibaba.csp.sentinel.adapter.gateway.common.param.RequestItemParser) GatewayApiMatcherManager(com.alibaba.csp.sentinel.adapter.gateway.sc.api.GatewayApiMatcherManager) Set(java.util.Set) Mono(reactor.core.publisher.Mono) WebExchangeApiMatcher(com.alibaba.csp.sentinel.adapter.gateway.sc.api.matcher.WebExchangeApiMatcher) Collectors(java.util.stream.Collectors) ServerWebExchange(org.springframework.web.server.ServerWebExchange) GatewayCallbackManager(com.alibaba.csp.sentinel.adapter.gateway.sc.callback.GatewayCallbackManager) EntryType(com.alibaba.csp.sentinel.EntryType) ResourceTypeConstants(com.alibaba.csp.sentinel.ResourceTypeConstants) GatewayFilter(org.springframework.cloud.gateway.filter.GatewayFilter) Optional(java.util.Optional) Route(org.springframework.cloud.gateway.route.Route) ContextConfig(com.alibaba.csp.sentinel.adapter.reactor.ContextConfig) SentinelReactorTransformer(com.alibaba.csp.sentinel.adapter.reactor.SentinelReactorTransformer) EntryConfig(com.alibaba.csp.sentinel.adapter.reactor.EntryConfig) Route(org.springframework.cloud.gateway.route.Route)

Aggregations

EntryType (com.alibaba.csp.sentinel.EntryType)1 ResourceTypeConstants (com.alibaba.csp.sentinel.ResourceTypeConstants)1 SentinelGatewayConstants (com.alibaba.csp.sentinel.adapter.gateway.common.SentinelGatewayConstants)1 GatewayParamParser (com.alibaba.csp.sentinel.adapter.gateway.common.param.GatewayParamParser)1 RequestItemParser (com.alibaba.csp.sentinel.adapter.gateway.common.param.RequestItemParser)1 GatewayApiMatcherManager (com.alibaba.csp.sentinel.adapter.gateway.sc.api.GatewayApiMatcherManager)1 WebExchangeApiMatcher (com.alibaba.csp.sentinel.adapter.gateway.sc.api.matcher.WebExchangeApiMatcher)1 GatewayCallbackManager (com.alibaba.csp.sentinel.adapter.gateway.sc.callback.GatewayCallbackManager)1 ContextConfig (com.alibaba.csp.sentinel.adapter.reactor.ContextConfig)1 EntryConfig (com.alibaba.csp.sentinel.adapter.reactor.EntryConfig)1 SentinelReactorTransformer (com.alibaba.csp.sentinel.adapter.reactor.SentinelReactorTransformer)1 AssertUtil (com.alibaba.csp.sentinel.util.AssertUtil)1 Optional (java.util.Optional)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 GatewayFilter (org.springframework.cloud.gateway.filter.GatewayFilter)1 GatewayFilterChain (org.springframework.cloud.gateway.filter.GatewayFilterChain)1 GlobalFilter (org.springframework.cloud.gateway.filter.GlobalFilter)1 Route (org.springframework.cloud.gateway.route.Route)1 ServerWebExchangeUtils (org.springframework.cloud.gateway.support.ServerWebExchangeUtils)1