【问题标题】:Zuul and forwarding url path to another url pathZuul 并将 url 路径转发到另一个 url 路径
【发布时间】:2019-06-11 10:06:48
【问题描述】:

我使用 spring zuul 来管理 API_Gateway。但是我的基本问题是无法将请求转发到特定路径。我的服务中存在多个不可更改的地址,我将从特定地址转发。我更喜欢不使用“Zuulfilter”,但使用它并没有用。

用户请求:(没关系)

浏览器请求:gateway.com/api1/user → gateway → apihost.com/api1/user

大摇大摆的请求:(失败了)

浏览器请求:gateway.com/api1/swagger → gateway → apihost.com/swagger-ui.html

我使用 service_discovery (eureka) 并且我的 application.yml 文件配置是:

Zuul:
 routes:
   swagger:
     path: /api1/swagger
     service_id: api1
     url: forward:/swagger-ui.html#/
   users:
     path: /api1/user/**
     service_id: api1
     stripPrefix: false

但我在调试模式下看到:

2019-01-17 11:28:46.437 DEBUG 77407 --- [nio-8181-exec-4] s.b.a.e.w.s.WebMvcEndpointHandlerMapping : Did not find handler method for [/swagger-ui.html#/api1/swagger]
2019-01-17 11:28:46.437 DEBUG 77407 --- [nio-8181-exec-4] a.e.w.s.ControllerEndpointHandlerMapping : Looking up handler method for path /swagger-ui.html#/api1/swagger
2019-01-17 11:28:46.437 DEBUG 77407 --- [nio-8181-exec-4] a.e.w.s.ControllerEndpointHandlerMapping : Did not find handler method for [/swagger-ui.html#/api1/swagger]

我还添加了一个路由过滤器(Zuulfilter),用于直接从服务调用地址,但响应不同。无论如何使用 Zuul 配置来完成转发(关于我使用服务发现)如果不存在开发 servlet 过滤器的示例代码吗?

【问题讨论】:

    标签: java spring-boot netflix-zuul api-gateway


    【解决方案1】:

    我的朋友 Eureka 用于服务发现,而 Zuul 是 API 网关。 在 Zuul 客户端的 application.yaml 中添加如下所示的配置。

    试试这个方法:-

    server:
      port: 9999
    
    
    eureka:         #tells about the Eureka server details and its refresh time
      instance:
        leaseRenewalIntervalInSeconds: 1
        leaseExpirationDurationInSeconds: 2
      client:
        serviceUrl:
          defaultZone: http://127.0.0.1:8761/eureka/
    
    spring:
      application:
        name: zuul-server
    
    
        zuul:
          #Service will be mapped under the /api URI
    
          routes:    
           service1:
              service-id: id4
              path: /trade/**     
              url: http://localhost:8091
              strip-prefix: false
    
            service2:
    
              service-id: id5
              path:  /**
              url: http://localhost:8096
              strip-prefix: false
    

    【讨论】:

    • 谢谢,但我使用的是 Eureka(服务发现),无法直接设置 URL。
    • 请看更新的答案,我们可以直接设置url,这不是Zuul的目的吗?? @耶稣
    • 我知道,但我的服务 IP 地址不固定。并将 URL 路径从 eureka_host_port/api1/swagger 更改为 eureka_host_port/swagger-ui.html#/。此示例主机和端口相同,但路径(/api1/swgger ≠ /swagger-ui.html#/)不同。
    猜你喜欢
    • 2017-11-09
    • 1970-01-01
    • 1970-01-01
    • 2011-06-02
    • 1970-01-01
    • 2020-06-28
    • 2012-03-09
    • 1970-01-01
    • 2021-04-11
    相关资源
    最近更新 更多