春季云网关 – 简单路由或回退不起作用

huangapple go评论90阅读模式
英文:

Spring Cloud Gateway - Simple Routing or fallback doesnt work

问题

我刚接触Spring Cloud Gateway实现。仅尝试了一个简单的教程,代码似乎无法正常工作。请查看我所完成的以下步骤和代码。

  1. 使用Spring Initializer创建一个网关项目

Pom.xml

<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">
	<!-- 其他配置 -->
</project>
  1. 添加简单路由 -> 遇到了ServerCodecConfigurer错误。
  2. 解决了错误,运行了项目。

Application.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. 运行命令: $curl --dump-header - http://localhost:9000/get

响应

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

期望响应

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

使用Greenwich.SR2云版本和其他项目,路由似乎工作,但Hystrix仍然无法工作。使用以下示例项目:
<https://github.com/spring-guides/gs-gateway>

关于如何实现带有Hystrix和Eureka的云网关,任何帮助都将很有帮助。已尝试了许多组合,但似乎仍然无法工作。

英文:

I am new to Spring cloud gateway implementation. 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

Pom.xml

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

	&lt;properties&gt;
		&lt;java.version&gt;1.8&lt;/java.version&gt;
		&lt;spring-cloud.version&gt;Hoxton.SR7&lt;/spring-cloud.version&gt;
	&lt;/properties&gt;

	&lt;dependencies&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
			&lt;artifactId&gt;spring-boot-starter-actuator&lt;/artifactId&gt;
		&lt;/dependency&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
			&lt;artifactId&gt;spring-cloud-starter-gateway&lt;/artifactId&gt;
		&lt;/dependency&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
			&lt;artifactId&gt;spring-cloud-starter-netflix-eureka-client&lt;/artifactId&gt;
		&lt;/dependency&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
			&lt;artifactId&gt;spring-cloud-starter-netflix-hystrix&lt;/artifactId&gt;
		&lt;/dependency&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
			&lt;artifactId&gt;spring-cloud-starter-netflix-hystrix-dashboard&lt;/artifactId&gt;
		&lt;/dependency&gt;

		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
			&lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
			&lt;scope&gt;test&lt;/scope&gt;
			&lt;exclusions&gt;
				&lt;exclusion&gt;
					&lt;groupId&gt;org.junit.vintage&lt;/groupId&gt;
					&lt;artifactId&gt;junit-vintage-engine&lt;/artifactId&gt;
				&lt;/exclusion&gt;
			&lt;/exclusions&gt;
		&lt;/dependency&gt;
	&lt;/dependencies&gt;

	&lt;dependencyManagement&gt;
		&lt;dependencies&gt;
			&lt;dependency&gt;
				&lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
				&lt;artifactId&gt;spring-cloud-dependencies&lt;/artifactId&gt;
				&lt;version&gt;${spring-cloud.version}&lt;/version&gt;
				&lt;type&gt;pom&lt;/type&gt;
				&lt;scope&gt;import&lt;/scope&gt;
			&lt;/dependency&gt;
		&lt;/dependencies&gt;
	&lt;/dependencyManagement&gt;

	&lt;build&gt;
		&lt;plugins&gt;
			&lt;plugin&gt;
				&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
				&lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
			&lt;/plugin&gt;
		&lt;/plugins&gt;
	&lt;/build&gt;
&lt;/project&gt;
  1. Added Simple Route. -> Got an error for ServerCodecConfigurer.
  2. Resolved the error, ran the project.

Application.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 -&gt; p
						.path(&quot;/get&quot;)
						.filters(f -&gt; f.addRequestHeader(&quot;Hello&quot;, &quot;Value&quot;))
						.uri(&quot;http://httpbin.org:80&quot;))
				.build();
	}

}

  1. Ran the command: $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

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

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

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

Using the Greenwich.SR2 Cloud Version and other project, the route seems to work but Hystrix still doesnt work. Using the following sample project:
<https://github.com/spring-guides/gs-gateway>

Any help on implementing the cloud gateway with Hystrix and Eureka would be helpful. Have tried a lot of combinations and it still doesnt seem to work.

答案1

得分: 3

从start.spring.io构建一个简单的网关,并且您的mRoutes() bean正常工作且我得到了预期的响应。hystrix-dashboard需要servlets,当我添加它时,我遇到了您的ServerCodecConfigurer问题。

如果您检查日志,您会看到:

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


在类路径上发现了Spring MVC,这与当前的Spring Cloud Gateway不兼容。请删除spring-boot-starter-web依赖。


移除spring-cloud-starter-netflix-hystrix-dashboard依赖,因为它不兼容。

英文:

building a simple gateway from start.spring.io and your mRoutes() bean works and I get the expected response. hystrix-dashboard is requires servlets and when I add it I get your ServerCodecConfigurer problem.

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.

huangapple
  • 本文由 发表于 2020年8月19日 16:27:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/63482951.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定