CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-com-alibaba-csp--sentinel-spring-cloud-gateway-adapter

Integration module providing flow control, traffic shaping, concurrency limiting, circuit breaking and system adaptive overload protection for Spring Cloud Gateway applications

Pending
Overview
Eval results
Files

request-processing.mddocs/

Request Processing

Components for parsing and extracting parameters from Spring WebFlux ServerWebExchange objects, enabling parameter-based flow control rules and request analysis.

Capabilities

ServerWebExchangeItemParser

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);
}

Integration with Flow Control Rules

Parameter-Based Rules

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));

Custom Parameter Extraction

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;
    }
}

Request Analysis Capabilities

Path Analysis

  • Extracts clean request path without query parameters
  • Useful for path-based routing and flow control rules
  • Returns normalized path value from Spring Cloud Gateway route processing

Client Identification

  • Extracts remote client IP address from request
  • Handles proxy scenarios through Spring WebFlux's remote address resolution
  • Returns null if remote address cannot be determined

Header Processing

  • Extracts any HTTP header by name
  • Case-insensitive header name matching
  • Returns first header value if multiple values exist

Query Parameter Extraction

  • Extracts URL query parameters by name
  • Handles URL-encoded parameter values
  • Returns first parameter value if multiple values exist

Cookie Management

  • Extracts cookie values by cookie name
  • Integrates with Spring WebFlux cookie handling
  • Provides null-safe cookie value extraction

Error Handling

Null Safety

All methods are designed to handle null input gracefully:

  • Return null when requested information is not available
  • Do not throw exceptions for missing headers, parameters, or cookies
  • Safe to use in flow control rule conditions

Exception Scenarios

  • Network Issues: getRemoteAddress() returns null if remote address cannot be determined
  • Missing Data: All extraction methods return null for missing data rather than throwing exceptions
  • Malformed Requests: Parser handles malformed requests gracefully without disrupting gateway processing

Install with Tessl CLI

npx tessl i tessl/maven-com-alibaba-csp--sentinel-spring-cloud-gateway-adapter

docs

api-management.md

block-handling.md

core-integration.md

index.md

request-processing.md

route-matching.md

tile.json