首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring Cloud Gateway和DiscoveryClient路由

Spring Cloud Gateway和DiscoveryClient路由
EN

Stack Overflow用户
提问于 2019-03-13 04:26:40
回答 1查看 4.1K关注 0票数 7

我正在尝试将一个使用spring-cloud-starter-netflix-zuul的网关迁移到Spring Cloud Gateway,我遇到了请求路由的问题。

我阅读了以下有关为DiscoveryClient路由配置谓词和过滤器的文档:

Configuring Predicates and Filters For DiscoveryClient Routes

以下是我的Netflix Zuul配置中的一段代码:

代码语言:javascript
复制
zuul:
  routes:
    account-service: /accounts/**

下面是Spring Cloud Gateway配置中的一段代码,我正在尝试配置一个等价的路由:

代码语言:javascript
复制
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禁止响应:

代码语言:javascript
复制
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.predicatesspring.cloud.gateway.discovery.locator.filters是作为spring.cloud.gateway.routes谓词和筛选器的补充还是替代?

如果不是,我可能会缺少哪些其他配置?

我在Spring Cloud Finchley.SR3/Spring Boot 2.0.8.RELEASE上。

EN

回答 1

Stack Overflow用户

发布于 2019-08-15 13:26:15

Spring Cloud Gateway默认情况下只开启请求路由,您必须显式开启发现支持。试试这个:

代码语言:javascript
复制
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/**访问您的服务

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55130120

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档