Search in sources :

Example 1 with SentinelReactorTransformer

use of com.alibaba.csp.sentinel.adapter.reactor.SentinelReactorTransformer in project incubator-shenyu by apache.

the class SentinelPlugin method doExecute.

@Override
protected Mono<Void> doExecute(final ServerWebExchange exchange, final ShenyuPluginChain chain, final SelectorData selector, final RuleData rule) {
    final ShenyuContext shenyuContext = exchange.getAttribute(Constants.CONTEXT);
    assert shenyuContext != null;
    String resourceName = CacheKeyUtils.INST.getKey(rule);
    SentinelHandle sentinelHandle = GsonUtils.getInstance().fromJson(rule.getHandle(), SentinelHandle.class);
    sentinelHandle.checkData(sentinelHandle);
    return chain.execute(exchange).doOnSuccess(v -> {
        HttpStatus status = exchange.getResponse().getStatusCode();
        if (status == null || !status.is2xxSuccessful()) {
            exchange.getResponse().setStatusCode(null);
            throw new SentinelFallbackException(status == null ? HttpStatus.INTERNAL_SERVER_ERROR : status);
        }
    }).transform(new SentinelReactorTransformer<>(resourceName)).onErrorResume(throwable -> fallbackHandler.fallback(exchange, UriUtils.createUri(sentinelHandle.getFallbackUri()), throwable));
}
Also used : SentinelReactorTransformer(com.alibaba.csp.sentinel.adapter.reactor.SentinelReactorTransformer) ShenyuContext(org.apache.shenyu.plugin.api.context.ShenyuContext) HttpStatus(org.springframework.http.HttpStatus) SentinelHandle(org.apache.shenyu.common.dto.convert.rule.SentinelHandle)

Example 2 with SentinelReactorTransformer

use of com.alibaba.csp.sentinel.adapter.reactor.SentinelReactorTransformer 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

SentinelReactorTransformer (com.alibaba.csp.sentinel.adapter.reactor.SentinelReactorTransformer)2 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 AssertUtil (com.alibaba.csp.sentinel.util.AssertUtil)1 Optional (java.util.Optional)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 SentinelHandle (org.apache.shenyu.common.dto.convert.rule.SentinelHandle)1 ShenyuContext (org.apache.shenyu.plugin.api.context.ShenyuContext)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