Common abstraction module for API gateway flow control in Sentinel providing GatewayFlowRule and ApiDefinition for gateway-specific traffic management.
—
The API Definition System provides a flexible framework for defining and managing gateway API groups using predicates. This system enables fine-grained control over which requests match specific API definitions for flow control and monitoring purposes.
Core class representing a group of HTTP API patterns with associated predicates for request matching.
/**
* Represents a group of HTTP API patterns for gateway routing and flow control
*/
class ApiDefinition {
/** Default constructor */
ApiDefinition();
/** Constructor with API name */
ApiDefinition(String apiName);
/** Get the API group name */
String getApiName();
/** Set the API group name (fluent interface) */
ApiDefinition setApiName(String apiName);
/** Get the set of predicate items that define this API group */
Set<ApiPredicateItem> getPredicateItems();
/** Set the predicate items for this API group (fluent interface) */
ApiDefinition setPredicateItems(Set<ApiPredicateItem> predicateItems);
}Usage Examples:
import com.alibaba.csp.sentinel.adapter.gateway.common.api.*;
// Create API definition with name
ApiDefinition userApi = new ApiDefinition("user-service");
// Create with fluent interface
ApiDefinition orderApi = new ApiDefinition()
.setApiName("order-service")
.setPredicateItems(Set.of(
new ApiPathPredicateItem()
.setPattern("/api/orders/**")
.setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_PREFIX)
));Base interface for API predicate conditions that determine when requests match an API definition.
/**
* Base interface for API predicate conditions
*/
interface ApiPredicateItem {
// Marker interface for API matching predicates
}Path-based predicate implementation for matching requests by URL path patterns.
/**
* Path-based predicate for API matching using URL patterns
*/
class ApiPathPredicateItem implements ApiPredicateItem {
/** Get the URL pattern for matching */
String getPattern();
/** Set the URL pattern (fluent interface) */
ApiPathPredicateItem setPattern(String pattern);
/** Get the matching strategy (exact/prefix/regex) */
int getMatchStrategy();
/** Set the matching strategy (fluent interface) */
ApiPathPredicateItem setMatchStrategy(int matchStrategy);
}Usage Examples:
// Exact path matching
ApiPathPredicateItem exactMatch = new ApiPathPredicateItem()
.setPattern("/api/users/profile")
.setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_EXACT);
// Prefix matching for all user-related endpoints
ApiPathPredicateItem prefixMatch = new ApiPathPredicateItem()
.setPattern("/api/users")
.setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_PREFIX);
// Regex matching for complex patterns
ApiPathPredicateItem regexMatch = new ApiPathPredicateItem()
.setPattern("/api/users/\\d+/orders")
.setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_REGEX);Groups multiple predicate items together for complex API matching logic.
/**
* Groups multiple predicate items together for complex matching
*/
class ApiPredicateGroupItem implements ApiPredicateItem {
/** Add a predicate item to this group (fluent interface) */
ApiPredicateGroupItem addItem(ApiPredicateItem item);
/** Get all predicate items in this group */
Set<ApiPredicateItem> getItems();
}Usage Examples:
// Group multiple path patterns
ApiPredicateGroupItem userApiGroup = new ApiPredicateGroupItem()
.addItem(new ApiPathPredicateItem()
.setPattern("/api/users/**")
.setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_PREFIX))
.addItem(new ApiPathPredicateItem()
.setPattern("/api/user-profile/**")
.setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_PREFIX));Manager class providing centralized management of API definitions with property-based configuration and observer pattern support.
/**
* Manager for gateway API definitions with property binding and observer pattern
*/
final class GatewayApiDefinitionManager {
/** Register a property for API definition updates */
static void register2Property(SentinelProperty<Set<ApiDefinition>> property);
/** Load a set of API definitions, replacing existing ones */
static boolean loadApiDefinitions(Set<ApiDefinition> apiDefinitions);
/** Get a specific API definition by name */
static ApiDefinition getApiDefinition(String apiName);
/** Get all currently loaded API definitions */
static Set<ApiDefinition> getApiDefinitions();
/** Validate if an API definition is properly configured */
static boolean isValidApi(ApiDefinition apiDefinition);
}Usage Examples:
import com.alibaba.csp.sentinel.adapter.gateway.common.api.*;
import java.util.Set;
// Create multiple API definitions
Set<ApiDefinition> apiDefinitions = Set.of(
new ApiDefinition("user-api")
.setPredicateItems(Set.of(
new ApiPathPredicateItem()
.setPattern("/api/users/**")
.setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_PREFIX)
)),
new ApiDefinition("order-api")
.setPredicateItems(Set.of(
new ApiPathPredicateItem()
.setPattern("/api/orders/**")
.setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_PREFIX)
))
);
// Load API definitions
boolean loaded = GatewayApiDefinitionManager.loadApiDefinitions(apiDefinitions);
// Retrieve specific API definition
ApiDefinition userApi = GatewayApiDefinitionManager.getApiDefinition("user-api");
// Get all loaded definitions
Set<ApiDefinition> allApis = GatewayApiDefinitionManager.getApiDefinitions();
// Validate API definition
ApiDefinition testApi = new ApiDefinition("test-api");
boolean isValid = GatewayApiDefinitionManager.isValidApi(testApi); // false - missing predicatesObserver interface for receiving notifications when API definitions change.
/**
* Observer interface for API definition changes
*/
interface ApiDefinitionChangeObserver {
/** Called when API definitions change */
void onChange(Set<ApiDefinition> apiDefinitions);
}Usage Examples:
// Custom observer implementation
public class MyApiChangeObserver implements ApiDefinitionChangeObserver {
@Override
public void onChange(Set<ApiDefinition> apiDefinitions) {
System.out.println("API definitions updated: " + apiDefinitions.size() + " definitions");
// Handle API definition changes (e.g., refresh caches, update configurations)
}
}Base class for implementing custom API matchers with predicate logic.
/**
* Base class for API matchers with predicate logic
* @param <T> Type of request object to match against
*/
abstract class AbstractApiMatcher<T> implements Predicate<T> {
/** Constructor with API definition */
AbstractApiMatcher(ApiDefinition apiDefinition);
/** Get the API name this matcher handles */
String getApiName();
/** Get the API definition this matcher uses */
ApiDefinition getApiDefinition();
/** Test if the request matches this API (from Predicate interface) */
boolean test(T request);
/** Initialize the matchers - must be implemented by subclasses */
protected abstract void initializeMatchers();
}Usage Examples:
// Custom matcher implementation
public class HttpRequestApiMatcher extends AbstractApiMatcher<HttpServletRequest> {
public HttpRequestApiMatcher(ApiDefinition apiDefinition) {
super(apiDefinition);
}
@Override
protected void initializeMatchers() {
// Initialize custom matching logic based on API definition predicates
}
// test() method is implemented by the base class using initializeMatchers()
}
// Usage
ApiDefinition userApi = new ApiDefinition("user-api")
.setPredicateItems(Set.of(
new ApiPathPredicateItem()
.setPattern("/api/users/**")
.setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_PREFIX)
));
HttpRequestApiMatcher matcher = new HttpRequestApiMatcher(userApi);
boolean matches = matcher.test(httpRequest);The API Definition System supports three URL matching strategies through SentinelGatewayConstants:
isValidApi() before loadingInstall with Tessl CLI
npx tessl i tessl/maven-com-alibaba-csp--sentinel-api-gateway-adapter-common