本文基于 spring cloud gateway 2.0.1
1、简介
RouteDefinitionLocator 是路由定义定位器的顶级接口,它的主要作用就是读取路由的配置信息(org.springframework.cloud.gateway.route.RouteDefinition)。它有五种不同的实现类,如图
2、RouteDefinitionLocator
org.springframework.cloud.gateway.route.RouteDefinitionLocator ,路由定义定位器接口,只有一个方法,用来获取路由定义列表的方法。
public interface RouteDefinitionLocator {
Flux<RouteDefinition> getRouteDefinitions();
}
通过 RouteDefinitionLocator 的类图,可以看出该接口有多个实现类:
- PropertiesRouteDefinitionLocator:基于属性配置
- DiscoveryClientRouteDefinitionLocator:基于服务发现
- CompositeRouteDefinitionLocator:组合方式
- CachingRouteDefinitionLocator:缓存方式
- 其中还有一个接口 RouteDefinitionRepository 继承自RouteDefinitionLocator,用于对路由定义的操作(保存、删除路由定义)
2.1、RouteDefinition
RouteDefinition 作为GatewayProperties中的属性,在网关启动的时候读取配置文件中的相关配置信息
@Validated
public class RouteDefinition {
@NotEmpty
private String id = UUID.randomUUID().toString();
@NotEmpty
@Valid
private List<PredicateDefinition> predicates = new ArrayList();
@Valid
private List<FilterDefinition> filters = new ArrayList();
@NotNull
private URI uri;
private int order = 0;
public RouteDefinition() {
}
public RouteDefinition(String text) {
int eqIdx = text.indexOf(61);
if (eqIdx <= 0) {
throw new ValidationException("Unable to parse RouteDefinition text '" + text + "', must be of the form name=value");
} else {
this.setId(text.substring(0, eqIdx));
String[] args = StringUtils.tokenizeToStringArray(text.substring(eqIdx + 1), ",");
this.setUri(URI.create(args[0]));
for(int i = 1; i < args.length; ++i) {
this.predicates.add(new PredicateDefinition(args[i]));
}
}
}
----------------------省略----------------------------
}
在 RouteDefinition 中,主要有五个属性:
id:路由id,默认为uuid
predicates:PredicateDefinition 路由断言定义列表
filt