14. DiscoveryClient
路由定义定位器
您可以将网关配置为基于在兼容的服务注册表中注册的服务创建路由。DiscoveryClient
要启用此功能,请设置并确保实现(例如 Netflix Eureka、Consul 或 Zookeeper)位于 Classpath 上并启用。spring.cloud.gateway.discovery.locator.enabled=true
DiscoveryClient
14.1. 为 DiscoveryClient
路由配置谓词和过滤器
默认情况下,网关为使用 .DiscoveryClient
默认谓词是使用 pattern 定义的路径谓词,其中 is
来自 ./serviceId/**
serviceId
DiscoveryClient
默认过滤器是带有 regex 和 replacement 的重写路径过滤器。
这会在将请求发送到下游之前从路径中剥离服务 ID。/serviceId/?(?<remaining>.*)
/${remaining}
如果要自定义路由使用的谓词或筛选条件,请设置 和 。
执行此操作时,如果要保留该功能,则需要确保包含前面显示的默认谓词和筛选条件。
以下示例显示了这是什么样子的:DiscoveryClient
spring.cloud.gateway.discovery.locator.predicates[x]
spring.cloud.gateway.discovery.locator.filters[y]
例 70.application.properties
spring.cloud.gateway.discovery.locator.predicates[0].name: Path spring.cloud.gateway.discovery.locator.predicates[0].args[pattern]: "'/'+serviceId+'/**'" spring.cloud.gateway.discovery.locator.predicates[1].name: Host spring.cloud.gateway.discovery.locator.predicates[1].args[pattern]: "'**.foo.com'" spring.cloud.gateway.discovery.locator.filters[0].name: CircuitBreaker spring.cloud.gateway.discovery.locator.filters[0].args[name]: serviceId spring.cloud.gateway.discovery.locator.filters[1].name: RewritePath spring.cloud.gateway.discovery.locator.filters[1].args[regexp]: "'/' + serviceId + '/?(?<remaining>.*)'" spring.cloud.gateway.discovery.locator.filters[1].args[replacement]: "'/${remaining}'"