简体   繁体   English

Spring Cloud Gateway找不到路由路径(404错误)

[英]Spring Cloud Gateway is not finding a route path (404 error)

I am investigating Spring Cloud Gateway as an API gateway solution for a professional application and am running through this tutorial: http://spring.io/guides/gs/gateway/#scratch . 我正在研究Spring Cloud Gateway作为用于专业应用程序的API网关解决方案,并且正在本教程中进行操作: http : //spring.io/guides/gs/gateway/#scratch

However I am running into issues I cannot solve. 但是,我遇到了无法解决的问题。

Here is my POM 这是我的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>

and here is the Application.java 这是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();
}
}

Running this and trying to hit http://localhost:8080/get was giving me a gateway timeout. 运行此命令并尝试访问http:// localhost:8080 / get ,这给了我一个网关超时。 Consulting with someone at work I discovered that I am behind a firewall and needed a proxy. 向工作人员咨询后,我发现自己在防火墙后面,需要代理。 So I modified the Application.java to this: 所以我将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();
}

Now, I am no longer getting a gateway time out but now I am getting a 404 error. 现在,我不再让网关超时,但现在却遇到404错误。 Here is the output using curl http://localhost:8080/get : 这是使用curl http:// localhost:8080 / get的输出

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

I am probably missing something simple but I cannot see it. 我可能缺少一些简单的东西,但看不到。

EDIT: according to the tutorial listed above this should be the response I get back: 编辑:根据上面列出的教程,这应该是我得到的响应:

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

在代码中删除@RestController

You have to set Host header to "internet.proxy.*****.com". 您必须将Host标头设置为“ internet.proxy。*****。com”。

You concatenated Host predicate and Path predicate. 您将主机谓词和路径谓词串联在一起。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM