简体   繁体   English

Spring 云网关 - 简单路由或回退不起作用

[英]Spring Cloud Gateway - Simple Routing or fallback doesnt work

I am new to Spring cloud gateway implementation.我是 Spring 云网关实施的新手。 Just by trying a simple tutorial the code doesnt seem to work.只是通过尝试一个简单的教程,代码似乎不起作用。 Please see the following steps and code which I have done.请参阅我完成的以下步骤和代码。

  1. Use spring initializer to create a gateway project使用 spring 初始化器创建网关项目

Pom.xml密码.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR7</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <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>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
  1. Added Simple Route.添加了简单路线。 -> Got an error for ServerCodecConfigurer. -> ServerCodecConfigurer 出错。
  2. Resolved the error, ran the project.解决了错误,运行了项目。

Application.java应用.java

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.http.codec.ServerCodecConfigurer;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
    
    @Bean
    public ServerCodecConfigurer serverCodecConfigurer() {
        return ServerCodecConfigurer.create();
    }
    
    @Bean
    public RouteLocator myRoutes(RouteLocatorBuilder builder) {
        return builder.routes()
                .route(p -> p
                        .path("/get")
                        .filters(f -> f.addRequestHeader("Hello", "Value"))
                        .uri("http://httpbin.org:80"))
                .build();
    }

}

  1. Ran the command: $curl --dump-header - http://localhost:9000/get运行命令: $curl --dump-header - http://localhost:9000/get

Response回复

HTTP/1.1 404
Vary: Origin
Vary: Access-Control-Request-Method
Vary: Access-Control-Request-Headers
Content-Type: application/json
Transfer-Encoding: chunked
Date: Tue, 18 Aug 2020 21:16:39 GMT

{"timestamp":"2020-08-18T21:16:39.260+00:00","status":404,"error":"Not Found","message":"","path":"/get"}

Expected Response预期回应

HTTP/1.1 200 OK
Date: Wed, 19 Aug 2020 08:21:29 GMT
Content-Type: application/json
Content-Length: 254
Connection: keep-alive
Server: gunicorn/19.9.0
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true

{
  "args": {},
  "headers": {
    "Accept": "*/*",
    "Host": "httpbin.org",
    "User-Agent": "curl/7.55.1",
    "X-Amzn-Trace-Id": "Root=1-5f3ce109-63eb231a030488be4168fc7e"
  },
  "origin": "0.0.0.0",
  "url": "http://httpbin.org/get"
}

Using the Greenwich.SR2 Cloud Version and other project, the route seems to work but Hystrix still doesnt work.使用 Greenwich.SR2 Cloud Version 和其他项目,路由似乎有效,但 Hystrix 仍然无效。 Using the following sample project: https://github.com/spring-guides/gs-gateway使用以下示例项目: https://github.com/spring-guides/gs-gateway

Any help on implementing the cloud gateway with Hystrix and Eureka would be helpful.任何有关使用 Hystrix 和 Eureka 实施云网关的帮助都会有所帮助。 Have tried a lot of combinations and it still doesnt seem to work.尝试了很多组合,但它似乎仍然不起作用。

building a simple gateway from start.spring.io and your mRoutes() bean works and I get the expected response.从 start.spring.io 构建一个简单的网关,您的mRoutes() bean 工作,我得到了预期的响应。 hystrix-dashboard is requires servlets and when I add it I get your ServerCodecConfigurer problem. hystrix-dashboard需要 servlets,当我添加它时,我遇到了ServerCodecConfigurer问题。

If you check your logs you will see:如果你检查你的日志,你会看到:

2020-08-19 16:36:58.943  WARN 157108 --- [           main] GatewayClassPathWarningAutoConfiguration : 

**********************************************************

Spring MVC found on classpath, which is incompatible with Spring Cloud Gateway at this time. Please remove spring-boot-starter-web dependency.

**********************************************************

Remove the spring-cloud-starter.netflix-hystrix-dashboard dependency as it is not compatible.删除spring-cloud-starter.netflix-hystrix-dashboard依赖项,因为它不兼容。

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

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