Integration module providing flow control, traffic shaping, concurrency limiting, circuit breaking and system adaptive overload protection for Spring Cloud Gateway applications
—
Components for parsing and extracting parameters from Spring WebFlux ServerWebExchange objects, enabling parameter-based flow control rules and request analysis.
Parses request items from Spring WebFlux ServerWebExchange, extracting various request attributes for use in Sentinel flow control rules.
/**
* Parses request items from Spring WebFlux ServerWebExchange
* Implements RequestItemParser<ServerWebExchange> interface
*/
public class ServerWebExchangeItemParser implements RequestItemParser<ServerWebExchange> {
/**
* Default constructor
*/
public ServerWebExchangeItemParser();
/**
* Extracts the request path from the exchange
* @param exchange - Server web exchange
* @return String request path value
*/
public String getPath(ServerWebExchange exchange);
/**
* Extracts the remote client IP address from the exchange
* @param exchange - Server web exchange
* @return String remote address or null if not available
*/
public String getRemoteAddress(ServerWebExchange exchange);
/**
* Extracts a specific header value from the request
* @param exchange - Server web exchange
* @param key - Header name to extract
* @return String header value or null if not present
*/
public String getHeader(ServerWebExchange exchange, String key);
/**
* Extracts a URL query parameter value from the request
* @param exchange - Server web exchange
* @param paramName - Parameter name to extract
* @return String parameter value or null if not present
*/
public String getUrlParam(ServerWebExchange exchange, String paramName);
/**
* Extracts a cookie value from the request
* @param exchange - Server web exchange
* @param cookieName - Cookie name to extract
* @return String cookie value or null if not present
*/
public String getCookieValue(ServerWebExchange exchange, String cookieName);
}Usage Examples:
import com.alibaba.csp.sentinel.adapter.gateway.sc.ServerWebExchangeItemParser;
// Create parser instance
ServerWebExchangeItemParser parser = new ServerWebExchangeItemParser();
// Extract request information (typically done within flow control rules)
String path = parser.getPath(exchange);
String clientIp = parser.getRemoteAddress(exchange);
String userAgent = parser.getHeader(exchange, "User-Agent");
String userId = parser.getUrlParam(exchange, "userId");
String sessionId = parser.getCookieValue(exchange, "JSESSIONID");
// Use in custom SentinelGatewayFilter
@Bean
public GlobalFilter customSentinelFilter() {
ServerWebExchangeItemParser customParser = new ServerWebExchangeItemParser();
return new SentinelGatewayFilter(customParser);
}The extracted request parameters can be used in Sentinel flow control rules for fine-grained traffic control:
// Example: Configure parameter-based flow rule using extracted values
ParamFlowRule rule = new ParamFlowRule()
.setResource("api-gateway")
.setParamIdx(0) // First parameter (often path or client IP)
.setCount(10) // Limit to 10 requests
.setDurationInSec(1);
ParamFlowRuleManager.loadRules(Collections.singletonList(rule));You can extend ServerWebExchangeItemParser for custom parameter extraction logic:
public class CustomServerWebExchangeItemParser extends ServerWebExchangeItemParser {
@Override
public String getHeader(ServerWebExchange exchange, String key) {
// Custom header processing logic
String headerValue = super.getHeader(exchange, key);
return processCustomHeader(headerValue);
}
// Add custom extraction methods
public String getCustomAttribute(ServerWebExchange exchange, String attributeName) {
return exchange.getAttribute(attributeName);
}
private String processCustomHeader(String headerValue) {
// Custom processing logic
return headerValue != null ? headerValue.toLowerCase() : null;
}
}All methods are designed to handle null input gracefully:
null when requested information is not availablegetRemoteAddress() returns null if remote address cannot be determinednull for missing data rather than throwing exceptionsInstall with Tessl CLI
npx tessl i tessl/maven-com-alibaba-csp--sentinel-spring-cloud-gateway-adapter