Spring Cloud Gateway的核心包是Spring Cloud Starter Gateway,截止发文maven仓库最新版是3.0.4,如下图:

图中可以看到Spring Cloud Starter Gateway-3.0.4依赖了gateway所需要的包版本,要求的springboot版本是2.5.5,所以只要引入2.5.5版本的springboot和3.0.4版本的Spring Cloud Starter Gateway包就可以了。
pom.xml:(不需要的jar包可以不引入)
<?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.5.5</version>
<relativePath />
</parent>
<groupId>com.zhaohy</groupId>
<artifactId>springcloudGateway</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springcloud-gateway</name>
<description>Demo project for Spring Boot</description>
<packaging>jar</packaging>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--springboot热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<!--JSON依赖 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.58</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>net.sf.flexjson</groupId>
<artifactId>flexjson</artifactId>
<version>3.3</version>
</dependency>
<dependency>
<groupId>nl.bitwalker</groupId>
<artifactId>UserAgentUtils</artifactId>
<version>1.2.4</version>
</dependency>
<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>3.10.2</version>
</dependency>
<!-- httpClient用到的jar包 -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.13</version>
</dependency><dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.12</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpmime -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.12</version>
</dependency>
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.14</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-gateway -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
<version>3.0.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
新建application.yml:
server:
port: 8081
spring:
cloud:
gateway:
routes:
- id: test_route
uri: https://www.baidu.com
predicates:
#访问路径:localhost:8081?url=baidu 代表跳转到www.baidu.com
#url带有baidu的自动跳转到https://www.baidu.com
- Query=url,baidu
- id: path_route
uri: https://www.baidu.com
predicates:
#访问localhost:8081/s跳转到https://www.baidu.com/s
- Path=/s
- id: query_route
uri: https://www.baidu.com
predicates:
#访问路径:localhost:8081?url=green 代表跳转到www.baidu.com
#查询条件带有green的自动跳转到https://www.baidu.com
- Query=green
启动springboot项目,url访问http://localhost:8081/s 就被转发到了https://www.baidu.com/s,url访问http://localhost:8081?url=baidu被转发到了 https://www.baidu.com,url访问http://localhost:8081?green=id,(访问参数里含有green),也被转发到了百度首页。
更多gateway转发规则可以去官网详查:
https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/
或者看这两篇博文觉得也讲的挺细的:
https://www.jianshu.com/p/c8ac84e820cc
https://www.cnblogs.com/crazymakercircle/p/11704077.html
nginx和gateway的区别:
nginx一般作为前端的网关,在最外面一层,对外的,gateway一般作为后台服务器之间不经过前端的用法,对内的。
网友评论