Integration module providing flow control, traffic shaping, concurrency limiting, circuit breaking and system adaptive overload protection for Spring Cloud Gateway applications
—
Core integration components providing the main filter and exception handling mechanisms for integrating Sentinel with Spring Cloud Gateway in reactive environments.
Main reactive filter that integrates Sentinel flow control with Spring Cloud Gateway request processing pipeline.
/**
* Main filter that integrates Sentinel flow control with Spring Cloud Gateway
* Implements GatewayFilter, GlobalFilter, and Ordered interfaces
*/
public class SentinelGatewayFilter implements GatewayFilter, GlobalFilter, Ordered {
/**
* Default constructor with highest precedence order
*/
public SentinelGatewayFilter();
/**
* Constructor with custom order
* @param order - Filter execution order
*/
public SentinelGatewayFilter(int order);
/**
* Constructor with custom request item parser
* @param serverWebExchangeItemParser - Custom parser for request parameters
*/
public SentinelGatewayFilter(RequestItemParser<ServerWebExchange> serverWebExchangeItemParser);
/**
* Full constructor with custom order and parser
* @param order - Filter execution order
* @param requestItemParser - Custom parser for request parameters
*/
public SentinelGatewayFilter(int order, RequestItemParser<ServerWebExchange> requestItemParser);
/**
* Filter method for request processing with Sentinel integration
* @param exchange - Server web exchange
* @param chain - Gateway filter chain
* @return Mono<Void> representing completion
*/
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain);
/**
* Returns filter execution order
* @return int order value
*/
public int getOrder();
}Usage Examples:
// Basic usage with default configuration
@Bean
@Order(-1)
public GlobalFilter sentinelGatewayFilter() {
return new SentinelGatewayFilter();
}
// Custom order
@Bean
@Order(100)
public GlobalFilter sentinelGatewayFilter() {
return new SentinelGatewayFilter(100);
}
// Custom request parser
@Bean
@Order(-1)
public GlobalFilter sentinelGatewayFilter() {
CustomRequestItemParser parser = new CustomRequestItemParser();
return new SentinelGatewayFilter(parser);
}WebExceptionHandler implementation that processes Sentinel block exceptions in Spring Cloud Gateway reactive environment.
/**
* Exception handler for Sentinel block exceptions in reactive environment
* Implements WebExceptionHandler interface
*/
public class SentinelGatewayBlockExceptionHandler implements WebExceptionHandler {
/**
* Constructor with Spring WebFlux components
* @param viewResolvers - List of view resolvers for rendering responses
* @param serverCodecConfigurer - Server codec configurer for message writers
*/
public SentinelGatewayBlockExceptionHandler(List<ViewResolver> viewResolvers, ServerCodecConfigurer serverCodecConfigurer);
/**
* Handles exceptions, specifically Sentinel block exceptions
* @param exchange - Server web exchange
* @param ex - Exception to handle
* @return Mono<Void> representing completion
*/
public Mono<Void> handle(ServerWebExchange exchange, Throwable ex);
}Usage Examples:
@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);
}
}Both SentinelGatewayFilter and SentinelGatewayBlockExceptionHandler should typically be configured with @Order(-1) to ensure they execute early in the Spring Cloud Gateway processing pipeline.
The filter automatically treats all route IDs (defined in Spring properties) and custom API definitions (from GatewayApiDefinitionManager) as Sentinel resources for flow control.
Both components are designed for reactive programming with Spring WebFlux and follow Spring Cloud Gateway patterns, returning Mono<Void> for asynchronous processing.
SentinelGatewayFilter applies Sentinel protection to requestsBlockException is thrownSentinelGatewayBlockExceptionHandler catches the exceptionBlockRequestHandler processes the blocked requestInstall with Tessl CLI
npx tessl i tessl/maven-com-alibaba-csp--sentinel-spring-cloud-gateway-adapter