0
# Core Integration
1
2
Core integration components providing the main filter and exception handling mechanisms for integrating Sentinel with Spring Cloud Gateway in reactive environments.
3
4
## Capabilities
5
6
### SentinelGatewayFilter
7
8
Main reactive filter that integrates Sentinel flow control with Spring Cloud Gateway request processing pipeline.
9
10
```java { .api }
11
/**
12
* Main filter that integrates Sentinel flow control with Spring Cloud Gateway
13
* Implements GatewayFilter, GlobalFilter, and Ordered interfaces
14
*/
15
public class SentinelGatewayFilter implements GatewayFilter, GlobalFilter, Ordered {
16
/**
17
* Default constructor with highest precedence order
18
*/
19
public SentinelGatewayFilter();
20
21
/**
22
* Constructor with custom order
23
* @param order - Filter execution order
24
*/
25
public SentinelGatewayFilter(int order);
26
27
/**
28
* Constructor with custom request item parser
29
* @param serverWebExchangeItemParser - Custom parser for request parameters
30
*/
31
public SentinelGatewayFilter(RequestItemParser<ServerWebExchange> serverWebExchangeItemParser);
32
33
/**
34
* Full constructor with custom order and parser
35
* @param order - Filter execution order
36
* @param requestItemParser - Custom parser for request parameters
37
*/
38
public SentinelGatewayFilter(int order, RequestItemParser<ServerWebExchange> requestItemParser);
39
40
/**
41
* Filter method for request processing with Sentinel integration
42
* @param exchange - Server web exchange
43
* @param chain - Gateway filter chain
44
* @return Mono<Void> representing completion
45
*/
46
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain);
47
48
/**
49
* Returns filter execution order
50
* @return int order value
51
*/
52
public int getOrder();
53
54
}
55
```
56
57
**Usage Examples:**
58
59
```java
60
// Basic usage with default configuration
61
@Bean
62
@Order(-1)
63
public GlobalFilter sentinelGatewayFilter() {
64
return new SentinelGatewayFilter();
65
}
66
67
// Custom order
68
@Bean
69
@Order(100)
70
public GlobalFilter sentinelGatewayFilter() {
71
return new SentinelGatewayFilter(100);
72
}
73
74
// Custom request parser
75
@Bean
76
@Order(-1)
77
public GlobalFilter sentinelGatewayFilter() {
78
CustomRequestItemParser parser = new CustomRequestItemParser();
79
return new SentinelGatewayFilter(parser);
80
}
81
```
82
83
### SentinelGatewayBlockExceptionHandler
84
85
WebExceptionHandler implementation that processes Sentinel block exceptions in Spring Cloud Gateway reactive environment.
86
87
```java { .api }
88
/**
89
* Exception handler for Sentinel block exceptions in reactive environment
90
* Implements WebExceptionHandler interface
91
*/
92
public class SentinelGatewayBlockExceptionHandler implements WebExceptionHandler {
93
/**
94
* Constructor with Spring WebFlux components
95
* @param viewResolvers - List of view resolvers for rendering responses
96
* @param serverCodecConfigurer - Server codec configurer for message writers
97
*/
98
public SentinelGatewayBlockExceptionHandler(List<ViewResolver> viewResolvers, ServerCodecConfigurer serverCodecConfigurer);
99
100
/**
101
* Handles exceptions, specifically Sentinel block exceptions
102
* @param exchange - Server web exchange
103
* @param ex - Exception to handle
104
* @return Mono<Void> representing completion
105
*/
106
public Mono<Void> handle(ServerWebExchange exchange, Throwable ex);
107
}
108
```
109
110
**Usage Examples:**
111
112
```java
113
@Configuration
114
public class GatewayConfiguration {
115
116
private final List<ViewResolver> viewResolvers;
117
private final ServerCodecConfigurer serverCodecConfigurer;
118
119
public GatewayConfiguration(ObjectProvider<List<ViewResolver>> viewResolversProvider,
120
ServerCodecConfigurer serverCodecConfigurer) {
121
this.viewResolvers = viewResolversProvider.getIfAvailable(Collections::emptyList);
122
this.serverCodecConfigurer = serverCodecConfigurer;
123
}
124
125
@Bean
126
@Order(-1)
127
public SentinelGatewayBlockExceptionHandler sentinelGatewayBlockExceptionHandler() {
128
return new SentinelGatewayBlockExceptionHandler(viewResolvers, serverCodecConfigurer);
129
}
130
}
131
```
132
133
## Integration Notes
134
135
### Filter Order
136
Both `SentinelGatewayFilter` and `SentinelGatewayBlockExceptionHandler` should typically be configured with `@Order(-1)` to ensure they execute early in the Spring Cloud Gateway processing pipeline.
137
138
### Resource Management
139
The filter automatically treats all route IDs (defined in Spring properties) and custom API definitions (from `GatewayApiDefinitionManager`) as Sentinel resources for flow control.
140
141
### Reactive Integration
142
Both components are designed for reactive programming with Spring WebFlux and follow Spring Cloud Gateway patterns, returning `Mono<Void>` for asynchronous processing.
143
144
### Exception Handling Flow
145
1. `SentinelGatewayFilter` applies Sentinel protection to requests
146
2. If a request is blocked, a `BlockException` is thrown
147
3. `SentinelGatewayBlockExceptionHandler` catches the exception
148
4. The configured `BlockRequestHandler` processes the blocked request
149
5. An appropriate response is returned to the client