使用DiscoveryClient Route Definition Locator,您可以配置网关根据注册在与DiscoveryClient兼容的服务注册中心上的服务创建路由。
要启用此功能,请设置spring.cloud.gateway.discovery.locator.enabled=true,并确保在类路径上存在并启用了DiscoveryClient实现(例如Netflix Eureka、Consul或Zookeeper)。
1.默认情况下,Spring Cloud Gateway为使用DiscoveryClient创建的路由定义了断言和过滤器。
通过DiscoveryClient创建的路由,默认情况下,网关为其定义了一个断言和一个过滤器。
默认的断言是一个路径断言,使用模式 /serviceId/**,其中 serviceId 是来自 DiscoveryClient 的服务的 ID。
默认的过滤器是一个重写路径过滤器,使用正则表达式 /serviceId/?(?<remaining>.*) 和替换规则 /${remaining}。这在请求发送到下游之前,将从路径中剥离服务 ID。
如果您想自定义 DiscoveryClient 路由所使用的断言或过滤器,请设置 spring.cloud.gateway.discovery.locator.predicates[x] 和 spring.cloud.gateway.discovery.locator.filters[y]。在这样做时,如果您希望保留默认功能,需要确保包含前面显示的默认断言和过滤器。以下示例展示了如何进行配置:
application.properties
spring.cloud.gateway.discovery.locator.predicates[0].