Spring Cloud Gateway的配置由RouteDefinitionLocator的集合驱动。

默认情况下,PropertiesRouteDefinitionLocator使用Spring Boot的@ConfigurationProperties机制加载属性。
上面的配置示例都使用了一个使用位置参数而不是命名参数的快捷符号。 以下两个例子是等效的:

对于网关的一些用法,配置文件足够使用,但是对于生产环境使用外部配置(例如数据库)会更好。未来的里程牌版本中,将具有基于Spring Data Repositories的RouteDefinitionLocator实现,例如:Redis,MongoDB和Cassandra。
为了允许在Java中进行简单配置,在RouteLocatorBuilder bean中定义了一个fluent API。

此样式还允许更多自定义谓词断言。 RouteDefinitionLocator bean定义的谓词使用逻辑and组合。 通过使用流畅的Java API,您可以在Predicateclass上使用and(),or()和negate()运算符。
DiscoveryClient Route Definition Locator
可以将网关配置创建路由基于服务,且该服务可以注册在DiscoveryClient兼容的注册中心。
要启用此功能,请设置spring.cloud.gateway.discovery.locator.enabled = true并确保DiscoveryClient实现位于类路径上并已启用(例如Netflix Eureka,Consul或Zookeeper)。
Configuring Predicates and Filters For DiscoveryClient Routes
默认情况下,Gateway为通过DiscoveryClient创建的路由定义单个断言和过滤器。
默认谓词是使用模式/ serviceId / **定义的路径断言,其中serviceId是DiscoveryClient中服务的id。
默认过滤器是重写路径过滤器,其中包含regex /serviceId/(?<remaining>.*)和替换/ $ {remaining}。 这只是在向下游发送请求之前从路径中剥离服务ID。
如果您想自定义DiscoveryClient路由使用的断言 and/or 过滤器,可以通过设置spring.cloud.gateway.discovery.locator.predicates [x]和spring.cloud.gateway.discovery.locator.filters[Y]来实现。 执行此操作时,如果要保留该功能,则需要确保包含上面的默认断言和过滤器。 下面是一个这样的例子:

网友评论