首页 > 解决方案 > Spring Boot Zuul API Gateway 返回 404 响应

问题描述

我正在尝试设置 Spring Boot Zuul 2.2,以便我可以向注册到 Eureka 并因此为 Zuul 所知的服务发送请求。

我可以通过 Zuul 访问服务的 Swagger UI,但是当我尝试向服务发送请求时,我得到了404 HTTP status - not found响应。

服务配置如下:

management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: always
eureka:
  client:
    registerWithEureka: true
    fetchRegistry: true
    serviceUrl:
      defaultZone: http://localhost:8761/eureka
  instance:
    preferIpAddress: true

Zuul 应用程序配置如下:

eureka:
  instance:
    preferIpAddress: true
  client:
    registerWithEureka: true
    fetchRegistry: true
spring:
  application:
    name: app-zuul
management:
  endpoints:
    web:
      exposure:
        include: routes,filters
  endpoint:
    health:
      show-details: always

最后,Eureka 配置:

eureka:
  client:
    registerWithEureka: false
    fetchRegistry: false
spring:
  application:
    name: app-eureka
management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: always

我可以通过直接向用户服务发送请求,但是当我尝试请求时http://localhost:3335/api/v1/...会收到一个。404http://localhost:8763/app-user/api/v1/...

我错过了一些配置吗?

标签: javaspringspring-bootnetflix-eurekanetflix-zuul

解决方案


您必须在 zuul 服务器属性中添加 zuul 路由:例如:

zuul:
   routes:
      accounts-web:#app name 
         url: http://localhost:8081 # app url

正确添加路由后,您将能够访问该 URL。

参考:https ://spring.io/guides/gs/routing-and-filtering/

要在路由中使用 eureka 应用程序名称,请添加如下路由:

zuul:
    routes:
        app-user: #app name
            path: /app-user/** #app-url
        app-user:
            serviceId: APP-USER #eureka serviceID

参考:https ://springbootdev.com/2018/02/03/microservices-declare-zuul-routes-with-eureka-serviceid-spring-cloud-zuul-eureka/


推荐阅读