我正在使用 Spring boot、Spring Cloud Gateway Server MVC、thymeleaf、Java 11、Maven 设计应用程序,但我是 Spring Cloud Gateway 的新手
我的 Spring 应用程序包含以下依赖项
Pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.17</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>spring-cloud-gateway-server-mvc</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>11</java.version>
<spring-cloud.version>2021.0.8</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
<version>3.1.8</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-gateway-mvc</artifactId>
<version>3.1.8</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper</artifactId>
<version>9.0.82</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-afterburner</artifactId>
<version>2.14.2</version>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</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>
网关配置.java
package com.commonapplication.controller;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class GatewayConfiguration {
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder){
return builder.routes().
route("example_route",r->r
.path("/demo1/**")
.uri("http://localhost:8090/demo1Service1"))
.build();
}
应用程序.属性
logging.level.org.springframework.web=debug
logging.level.org.springframework.cloud.gateway.handler.RoutePredicateHandlerMapping= TRACE
spring.main.web-application-type=reactive
CommonApplication.java
package com.commonapplication;
@SpringBootApplication
public class CommonApplication {
public static void main(String[] args) {
SpringApplication.run(CommonApplication.class, args);
}
}
StartUpController.java
package com.commonapplication.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
@Controller
public class StartUpController {
@RequestMapping(value="/index",method=RequestMethod.GET)
public String index() {
return "index";
}
}
代码包含 src->resources->template 中的index.html 文件。 当从浏览器点击“localhost:8080/index”时。 index.html 已正确加载。此 UI 包含一个按钮。单击此按钮后,我的应用程序将点击“localhost:8080/demo1/service1”。路由是在 GatewayConfiguration.java 文件中为路径“demo1/**”定义的,以映射到在 localhost:8090 上运行的应用程序。 运行 localhost:8090 的映射应用程序的代码是
应用程序.属性
server.port=8090
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
pom.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.7.17</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example.demo1</groupId>
<artifactId>demo1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo1</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
<spring-cloud.version>2021.0.8</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper</artifactId>
<version>9.0.82</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Demo1Application.java
package com.example.demo1;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Demo1Application {
public static void main(String[] args) {
SpringApplication.run(Demo1Application.class, args);
}
}
Demo1Controller.java
package com.example.demo1.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
@RestController
public class Demo1Controller {
@RequestMapping(value="/demo1Index",method= RequestMethod.GET)
public String index(ModelMap map) {
return "demo1Index";
}
@RequestMapping(value="/demo1Service1",method= RequestMethod.GET)
@ResponseBody
public String demoS1() {
System.out.println("Demo 1 is working.");
return "demo1 is working";
}
}
点击按钮后,我会在日志中看到以下几行:
2023-11-24 13:45:45.223 DEBUG 12936 --- [nio-8080-exec-5] o.s.c.g.h.RoutePredicateHandlerMapping : Mapping [Exchange: GET http://localhost:8080/demo1/service1?_=1700813739548] to Route{id='example_route', uri=http://localhost:8090/demo1Service1, order=0, predicate=Paths: [/demo1/**], match trailing slash: true, gatewayFilters=[], metadata={}}
2023-11-24 13:45:45.223 DEBUG 12936 --- [nio-8080-exec-5] o.s.c.g.h.RoutePredicateHandlerMapping : [19d35c9c] Mapped to org.springframework.cloud.gateway.handler.FilteringWebHandler@23adefa8
2023-11-24 13:45:45.827 DEBUG 12936 --- [ctor-http-nio-2] o.s.w.s.adapter.HttpWebHandlerAdapter : [19d35c9c] Completed 404 NOT_FOUND
我无法理解为什么我的网关找不到我在 8090 上运行的服务。我已经参考了很多示例,但每个示例都包含 eureka 服务器的使用。我是 Spring Cloud Gateway 的新手,所以我不确定这个应用程序是否可以在没有 eureka 服务器的情况下工作。另外,这里我使用 Spring Cloud Gateway Server MVC 而不是 Spring Cloud Gateway
在 Spring-cloud-gateway-server jar 中调试后发现错误。 将 GatewayConfiguration.java 中的路径更改为 .path("/demo1Service") 并将 uri 更改为 .uri("http://localhost:8090")。现在,当我点击 url htt://localhost:8080/demo1Service 时,它已成功路由到 http://localhost:8090/demo1Service。