spring云网关路由器总是给出404错误

6ovsh4lw  于 2021-07-26  发布在  Java
关注(0)|答案(0)|浏览(274)

我有一个springcloud项目。这个项目由三个主要部分组成:一个带有 Eureka ,一个具有 Spring Cloud Gateway 以及RESTAPI端点的服务。我想这样定义api端点:

GET
/available

并使用网关中的路径 predicate 路由它们:

GET
/service-one/available

为了实现这一点,我在gateway的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>

应用程序属性:

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>

应用程序.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>

应用程序属性:

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";
    }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题