spring-boot - Spring Cloud Gateway 路由器总是报 404 错误
问题描述
我有一个春季云项目。该项目由三个主要部分组成:带有 REST API 端点的服务发现、带有Eureka
网关Spring Cloud Gateway
和服务。我想像这样定义 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>
应用程序属性:
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";
}
}
解决方案
推荐阅读
- javascript - 如果 Javascript 中缺少对象键,则跳过未定义错误
- varnish - 无法将 Varnish 放在端口 80
- html - 使用 CSS 网格创建 3x3 响应式布局
- php - 如何使用php从字符串中获取年份
- javascript - 单击按钮时如何将多个值传递给模态?
- css - Bootstrap 4 continer-fluid 类不允许具有相对位置的 div
- flutter - 如何在 Flutter 中实现背景模糊的底部模态表?
- azure-keyvault - Azure Key Vault 机密值的最大大小是多少?
- python-3.x - 迭代嵌套字典以查找相似的值,然后添加到具有相同键的字典列表中
- python - 警告:张量流:模型是用形状(20、37、42)构造的输入张量(“input_5:0”,形状=(20、37、42),dtype=float32),但是