Spring Cloud Gateway 路由器总是报 404 错误

时间:2021-04-23 22:24:20

标签: spring-boot spring-cloud spring-cloud-gateway

我有一个 Spring Cloud 项目。该项目由三个主要部分组成:带有 Eureka 的服务发现、带有 Spring Cloud Gateway 的网关和用于 REST API 端点的服务。我想像这样定义 API 端点:

GET
/available

并在网关中使用路径谓词路由它们:

GET
/service-one/available

为了实现这一点,我在网关的 yml 文件中添加了此路由:

spring:
  cloud:
    gateway:                
      routes:
      - id: service_one
        uri: lb://service_one
        predicates:
        - Path=/service-one/**
        filters:
        - StripPrefix=1 

但是当我尝试

// 9000 is the gateway's port number
http://localhost:9000/service-one/available

我收到此错误:

{
    "timestamp": "...",
    "path": "/service-one/available",
    "status": 404,
    "error": "Not Found",
    "message": null,
    "requestId": "..."
}

更多详情:

Spring Boot 版本:2.4.5
Spring Cloud 版本:2020.0.2

  • 发现

pom.xml

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

application.properties:

server.port=8761
spring.application.name=discovery
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

应用启动:

@SpringBootApplication
@EnableEurekaServer
public class DiscoveryApplication {
    // ...
}
  • 网关

pom.xml

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

application.yml:

spring:
  application:
    name: gateway
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true 
          lower-case-service-id: true  
      httpclient:
        wiretap: true
      httpserver:
        wiretap: true                  
      routes:
        ##
        ##
      - id: service_one
        uri: lb://service_one
        predicates:
        - Path=/service-one/**
        filters:
        - StripPrefix=1         
        ##
        ##        
eureka:
  client:
    register-with-eureka: false
  • 服务

pom.xml:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

application.properties:

spring.application.name=service_one
server.port=8080
eureka.client.register-with-eureka=true
eureka.client.service-url.default-zone=http://localhost:8761/eureka

服务端点:

@SpringBootApplication
@EnableEurekaClient
@RestController
public class ServiceOneApplication {
    // ...
    @GetMapping("/available")
    public String available() {
        return "Hi, Service One is available";
    }
}

0 个答案:

没有答案