Spring Zuul将URL映射到`serviceId`,而不是`route``path`

问题描述 投票:0回答:1

我有一个Spring REST服务和一个Spring Zuul服务。他们注册了Spring-Eureka服务。

这是Zuul配置:

zuul:
  routes:
    rest:
      path:/rest/**
      serviceId:service-rest

我预计任何对/rest/*的调用都会路由到REST服务。但是只有/service/rest/*将被转移到REST服务。

例如:

curl -i http://localhost:5555/rest/hello # response was 404

curl http://localhost:5555/service-rest/hello # response was 200

似乎Spring Zuul将URL映射到serviceId,而不是映射到path

如何配置Zuul使用path但不使用serviceID

Zuul代码是:

@EnableZuulProxy
@SpringCloudApplication
public class ZuulApplication {

    public static void main(String[] args) {
        SpringApplication.run(ZuulApplication.class, args);
    }
}

Zuul配置:

spring.application.name=api-gateway
server.port=5555

zuul:
  routes:
    rest:
      path:/rest/**
      serviceId:service-rest

eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/

REST服务是简单的Spring Web项目。其配置如下:

server.port=8001
spring.application.name=service-rest

eureka.client.serviceUrl.defaultZone: http://localhost:8761/eureka/
spring spring-boot netflix-zuul spring-cloud-netflix
1个回答
0
投票

默认情况下,Zuul将删除传递给它的URL的前缀,有关源代码,请参阅here

所以在你的情况下,/rest/部分被从http://localhost:5555/rest/hello剥离,变成http://localhost:5555/hello,导致404 Not Found错误,因为Eureka不知道一个名为“你好”的服务。

将您的Zuul配置更改为:

zuul:
  routes:
    rest:
      path:/rest/**
      serviceId:service-rest
      stripPrefix: false
© www.soinside.com 2019 - 2024. All rights reserved.