【问题标题】:Spring Cloud Gateway routing doesn't workSpring Cloud Gateway 路由不起作用
【发布时间】:2021-11-19 12:54:28
【问题描述】:

我正在使用 Spring Cloud(Hoxton.SR10) 和 Spring Boot (2.2.6.RELEASE)

我在 Eureka 服务器 8761 中注册了我的服务

我有一个网关服务来管理路由(目前没有任何安全措施)

# profil DEV
---
spring:
  profiles: dev

  cloud:
    gateway:
      routes:
        - id: pr-api-id
          uri: http://localhost:8086/
          predicates:
           - Path=/api/**

这是 pom.xml

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-actuator</artifactId>
</dependency>
<!-- Spring cloud eureka client -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

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

当我打电话给localhost:9092/api/v0(我在localhost:8086/v0 上有一个工作正常的服务)应该将localhost:8086/v0du 的相同结果返回给spring cloud gateway的路由

但我收到 http 错误 404

This application has no explicit mapping for /error, so you are seeing this as a fallback.
Mon Sep 27 12:46:20 CEST 2021
There was an unexpected error (type=Not Found, status=404).
No message available

网关主类 java

@SpringBootApplication
@EnableEurekaClient
public class GatewayApplication {

【问题讨论】:

  • 根据您的帖子,网关正在将请求发送到 localhost:8086/api/v0。网关默认不修改请求。您需要更改路径。 StripPrefix 1 过滤器应该可以工作
  • 感谢您的回复,但它不起作用路线:-id:pr-api-id uri:localhost:8086 谓词:-Path=/api/** 过滤器:-StripPrefix=1

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


【解决方案1】:

我终于通过在我的配置文件中添加 cors-configuration 解决了我的问题。

spring:
   cloud:
          gateway:
      default-filters:
        - DedupeResponseHeader=Access-Control-Allow-Origin
      globalcors:
        cors-configurations:
          "[/**]":
            allowCredentials: true
            allowedOrigins: "*"
            allowedHeaders: "Origin, X-Requested-With, Content-Type, Accept, Content-Length, TOKEN, Authorization"
            allowedMethods: "GET, POST, PATCH, PUT, DELETE, OPTIONS"
            maxAge: 3628800

【讨论】:

    猜你喜欢
    • 2021-03-27
    • 2020-04-01
    • 2021-06-27
    • 2019-08-03
    • 2019-03-31
    • 2023-01-21
    • 2019-10-29
    • 2018-11-14
    • 1970-01-01
    相关资源
    最近更新 更多