Route 的匹配检测器及定制
RoutePredicateFactory 是一组专为路由匹配检测设计的接口,以完成匹配检测处理。对应 predicates 配置。
1、内置的匹配检测器
| 匹配检测器工厂 | 配置前缀 | 说明与示例 |
|---|---|---|
| AfterPredicateFactory | After= | After 时间检测器,ZonedDateTime 格式 ( After=2017-01-20T17:42:47.789-07:00[America/Denver]) |
| BeforePredicateFactory | Before= | After 时间检测器,ZonedDateTime 格式 ( Before=2017-01-20T17:42:47.789-07:00[America/Denver]) |
| CookiePredicateFactory | Cookie= | Cookie 检测器 ( Cookie=token)(Cookie=token, ^user.) |
| HeaderPredicateFactory | Header= | Header 检测器 ( Header=token)(Header=token, ^user.) |
| MethodPredicateFactory | Method= | Method 检测器 ( Method=GET,POST) |
| PathPredicateFactory | Path= | Path 检测器(支持多路径匹配,以","号隔开) ( Path=/demo/**) ,(Path=/demo/**,/hello/**) |
2、配置示例
solon.cloud.gateway:
routes: #!必选
- id: demo
target: "http://localhost:8080" # 或 "lb://user-service"
predicates: #?可选
- "Path=/demo/**"
filters: #?可选
- "StripPrefix=1"
3、定制示例
- Path 检测器定制示例(配置例:
Path=/demo/**)
@Component
public class PathPredicateFactory implements RoutePredicateFactory {
@Override
public String prefix() {
return "Path";
}
@Override
public ExPredicate create(String config) {
return new PathPredicate(config);
}
public static class PathPredicate implements ExPredicate {
private PathRule rule;
/**
* @param config (Path=/demo/**)
* */
public PathPredicate(String config) {
if (Utils.isBlank(config)) {
throw new IllegalArgumentException("PathPredicate config cannot be blank");
}
rule = new PathRule();
rule.include(config);
}
@Override
public boolean test(ExContext ctx) {
return rule.test(ctx.rawPath());
}
}
}