【问题标题】:Spring Cloud Gateway is not finding a route path (404 error)Spring Cloud Gateway 找不到路由路径(404 错误)
【发布时间】:2019-06-10 16:51:25
【问题描述】:

我正在研究 Spring Cloud Gateway 作为专业应用程序的 API 网关解决方案,并且正在学习本教程:http://spring.io/guides/gs/gateway/#scratch

但是我遇到了我无法解决的问题。

这是我的 POM

<groupId>org.springframework</groupId>
<artifactId>gs-gateway</artifactId>
<version>0.1.0</version>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.2.RELEASE</version>
</parent>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Greenwich.RC2</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-gateway</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-contract-stub-runner</artifactId>
        <exclusions>
            <exclusion>
                <artifactId>spring-boot-starter-web</artifactId>
                <groupId>org.springframework.boot</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<properties>
    <java.version>1.8</java.version>
</properties>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

这里是 Application.java

@SpringBootApplication
@RestController
public class Application {

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

@Bean
public RouteLocator myRoutes(RouteLocatorBuilder builder) {
    return builder.routes()
        .route(p -> p
            .path("/get")
            .filters(f -> f.addRequestHeader("Hello", "World"))
            .uri("http://httpbin.org:80"))
        .build();
}
}

运行这个并尝试点击http://localhost:8080/get 给了我一个网关超时。咨询工作中的某个人,我发现我在防火墙后面,需要一个代理。所以我将 Application.java 修改为:

@SpringBootApplication
@RestController
public class Application {

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

@Bean
public RouteLocator myRoutes(RouteLocatorBuilder builder) {
    return builder.routes()
        .route(p -> p.host("internet.proxy.*****.com").and()
            .path("/get")
            .filters(f -> f.addRequestHeader("Hello", "World"))
            .uri("http://httpbin.org:80"))
        .build();
}

现在,我不再遇到网关超时,但现在我收到 404 错误。这是使用 curl http://localhost:8080/get 的输出:

{"timestamp":"2019-01- 
    16T14:24:12.929+0000","path":"/get","status":404,"error":"Not 
Found","message":null}

我可能遗漏了一些简单的东西,但我看不到它。

编辑: 根据上面列出的教程,这应该是我得到的回复:

{
    "args": {},
    "headers": {
        "Accept": "*/*",
        "Connection": "close",
        "Forwarded": 
         "proto=http;host=\"localhost:8080\";for=\"0:0:0:0:0:0:0:1:56207\"",
        "Hello": "World",
        "Host": "httpbin.org",
        "User-Agent": "curl/7.54.0",
        "X-Forwarded-Host": "localhost:8080"
  },
  "origin": "0:0:0:0:0:0:0:1, 73.68.251.70",
  "url": "http://localhost:8080/get"
}

【问题讨论】:

    标签: java spring-cloud-gateway


    【解决方案1】:

    删除代码中的@RestController

    【讨论】:

      【解决方案2】:

      您必须将Host 标头设置为“internet.proxy.*****.com”。

      您连接了 Host 谓词和 Path 谓词。

      curl --header "Host: internet.proxy.test.com" http://localhost:8080/get
      

      【讨论】:

        猜你喜欢
        • 2023-01-21
        • 2020-12-15
        • 1970-01-01
        • 2019-03-02
        • 2019-09-16
        • 2023-04-02
        • 2019-08-03
        • 2019-03-31
        • 2020-06-01
        相关资源
        最近更新 更多