Integration module providing flow control, traffic shaping, concurrency limiting, circuit breaking and system adaptive overload protection for Spring Cloud Gateway applications
npx @tessl/cli install tessl/maven-com-alibaba-csp--sentinel-spring-cloud-gateway-adapter@1.8.0Sentinel Spring Cloud Gateway Adapter provides comprehensive integration between Alibaba Sentinel and Spring Cloud Gateway, enabling flow control, traffic shaping, concurrency limiting, circuit breaking, and system adaptive overload protection for reactive Spring Cloud Gateway applications. The adapter leverages the Sentinel Reactor Adapter to provide seamless integration with Spring WebFlux and reactive streams.
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-spring-cloud-gateway-adapter</artifactId>
<version>1.8.8</version>
</dependency>import com.alibaba.csp.sentinel.adapter.gateway.sc.SentinelGatewayFilter;
import com.alibaba.csp.sentinel.adapter.gateway.sc.exception.SentinelGatewayBlockExceptionHandler;
import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.GatewayCallbackManager;
import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.BlockRequestHandler;@Configuration
public class GatewayConfiguration {
private final List<ViewResolver> viewResolvers;
private final ServerCodecConfigurer serverCodecConfigurer;
public GatewayConfiguration(ObjectProvider<List<ViewResolver>> viewResolversProvider,
ServerCodecConfigurer serverCodecConfigurer) {
this.viewResolvers = viewResolversProvider.getIfAvailable(Collections::emptyList);
this.serverCodecConfigurer = serverCodecConfigurer;
}
@Bean
@Order(-1)
public SentinelGatewayBlockExceptionHandler sentinelGatewayBlockExceptionHandler() {
return new SentinelGatewayBlockExceptionHandler(viewResolvers, serverCodecConfigurer);
}
@Bean
@Order(-1)
public GlobalFilter sentinelGatewayFilter() {
return new SentinelGatewayFilter();
}
}Sentinel Spring Cloud Gateway Adapter is built around several key components:
Main components for integrating Sentinel with Spring Cloud Gateway, providing the primary filter and exception handling mechanisms for reactive flow control.
public class SentinelGatewayFilter implements GatewayFilter, GlobalFilter, Ordered {
public SentinelGatewayFilter();
public SentinelGatewayFilter(int order);
public SentinelGatewayFilter(RequestItemParser<ServerWebExchange> serverWebExchangeItemParser);
public SentinelGatewayFilter(int order, RequestItemParser<ServerWebExchange> requestItemParser);
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain);
public int getOrder();
}
public class SentinelGatewayBlockExceptionHandler implements WebExceptionHandler {
public SentinelGatewayBlockExceptionHandler(List<ViewResolver> viewResolvers, ServerCodecConfigurer serverCodecConfigurer);
public Mono<Void> handle(ServerWebExchange exchange, Throwable ex);
}Components for parsing and extracting parameters from Spring WebFlux ServerWebExchange objects, enabling parameter-based flow control rules.
public class ServerWebExchangeItemParser implements RequestItemParser<ServerWebExchange> {
public ServerWebExchangeItemParser();
public String getPath(ServerWebExchange exchange);
public String getRemoteAddress(ServerWebExchange exchange);
public String getHeader(ServerWebExchange exchange, String key);
public String getUrlParam(ServerWebExchange exchange, String paramName);
public String getCookieValue(ServerWebExchange exchange, String cookieName);
}Customizable handlers for processing blocked requests, supporting JSON responses, HTML responses, and redirects with flexible callback management.
@FunctionalInterface
public interface BlockRequestHandler {
Mono<ServerResponse> handleRequest(ServerWebExchange exchange, Throwable t);
}
public final class GatewayCallbackManager {
public static BlockRequestHandler getBlockHandler();
public static void setBlockHandler(BlockRequestHandler blockHandler);
public static void resetBlockHandler();
public static Function<ServerWebExchange, String> getRequestOriginParser();
public static void setRequestOriginParser(Function<ServerWebExchange, String> requestOriginParser);
public static void resetRequestOriginParser();
}System for managing custom API definitions that extend beyond simple route-based resource identification, enabling fine-grained control over resource matching.
public final class GatewayApiMatcherManager {
public static Map<String, WebExchangeApiMatcher> getApiMatcherMap();
public static Optional<WebExchangeApiMatcher> getMatcher(String apiName);
public static Set<ApiDefinition> getApiDefinitionSet();
}
public class WebExchangeApiMatcher extends AbstractApiMatcher<ServerWebExchange> {
public WebExchangeApiMatcher(ApiDefinition apiDefinition);
}Path matching utilities providing support for exact, Ant-style pattern, and regular expression-based path matching for flexible resource identification.
public final class RouteMatchers {
public static com.alibaba.csp.sentinel.util.function.Predicate<ServerWebExchange> all();
public static com.alibaba.csp.sentinel.util.function.Predicate<ServerWebExchange> antPath(String pathPattern);
public static com.alibaba.csp.sentinel.util.function.Predicate<ServerWebExchange> exactPath(String path);
public static com.alibaba.csp.sentinel.util.function.Predicate<ServerWebExchange> regexPath(String pathPattern);
}The adapter requires the following provided dependencies:
spring-cloud-gateway-core) - Core Spring Cloud Gateway componentsspring-webflux) - Reactive web frameworksentinel-api-gateway-adapter-common) - Common gateway adapter utilitiessentinel-reactor-adapter) - Reactive Sentinel integrationSentinelGatewayFilter as a @Bean with @Order(-1)SentinelGatewayBlockExceptionHandler as a @Bean with @Order(-1)Configure custom block request handlers via GatewayCallbackManager.setBlockHandler() to customize how blocked requests are handled.
Use GatewayApiMatcherManager in conjunction with the common adapter's GatewayApiDefinitionManager to define custom API resources beyond route-based matching.
Set custom request origin parsers via GatewayCallbackManager.setRequestOriginParser() to extract request origins for origin-based flow control rules.