我正在尝试将一个使用spring-cloud-starter-netflix-zuul的网关迁移到Spring Cloud Gateway,我遇到了请求路由的问题。
我阅读了以下有关为DiscoveryClient路由配置谓词和过滤器的文档:
Configuring Predicates and Filters For DiscoveryClient Routes
以下是我的Netflix Zuul配置中的一段代码:
zuul:
routes:
account-service: /accounts/**下面是Spring Cloud Gateway配置中的一段代码,我正在尝试配置一个等价的路由:
spring:
cloud:
gateway:
routes:
- id: account-service-route
uri: lb://account-service
predicates:
- Path=/accounts/**我正在使用Spring Cloud Eureka作为我的Discovery Server (在一个单独的微服务中),并且我目前没有Configuring Predicates and Filters For DiscoveryClient Routes中列出的任何配置
如果我向/accounts/***发出请求,我会收到404响应。如果我将Spring Cloud Gateway的配置更改为以下内容,并向/account-service/***发出相同的请求,则会收到403禁止响应:
spring:
cloud:
gateway:
routes:
- id: account-service-route
uri: lb://account-service
predicates:
- Path=/account-service/**我怀疑这与Spring Cloud Gateway在DiscoveryClient路由配置方面的默认行为有关,但我在日志中没有看到足够的日志来为我指明正确的方向。
所以,我的问题是:当使用Spring Cloud Gateway和Spring Cloud Eureka时,是否需要创建Configuring Predicates and Filters For DiscoveryClient Routes中列出的配置条目?
如果是这样的话,有人可以参考我的路由示例提供更多关于需要配置的说明/清晰度吗?如果我遗漏了什么,我很抱歉,但从我所读到的内容来看,我并不清楚这个配置到底需要什么。例如,spring.cloud.gateway.discovery.locator.predicates和spring.cloud.gateway.discovery.locator.filters是作为spring.cloud.gateway.routes谓词和筛选器的补充还是替代?
如果不是,我可能会缺少哪些其他配置?
我在Spring Cloud Finchley.SR3/Spring Boot 2.0.8.RELEASE上。
发布于 2019-08-15 13:26:15
Spring Cloud Gateway默认情况下只开启请求路由,您必须显式开启发现支持。试试这个:
spring:
application:
name: gateway
cloud:
gateway:
discovery:
locator:
enabled: true # default false
routes:
- id: account-service-route
uri: lb://account-service #
predicates:
- Path=/account/**现在,您应该能够通过http://<gateway>/account-service/account/**访问您的服务
https://stackoverflow.com/questions/55130120
复制相似问题