Spring Cloud Gateway提供了一个在Spring生态系统之上构建的API网关,包括:Spring 5,Spring Boot 2和Project Reactor。Spring Cloud Gateway旨在提供一种简单而有效的路由方式,并为它们提供一些网关基本功能,例如:安全,监控/指标和弹性。
一. 版本说明
Spring Boot版本:2.2.5.RELEASE
Spring Cloud版本:Hoxton.SR3
如无特殊说明,Spring Cloud的所有例程都将采用上面的版本。
二. 网关的使用
1. 新建父项目
1.1 使用简单Maven创建父项目:
选择Maven,选择jdk版本,单击“Next”
输入项目名称,及位置,单击“Finish”
1.2. 创建好后,打开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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.cherry</groupId>
<artifactId>springcloudproject</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>14</java.version>
<spring-cloud.version>Hoxton.SR3</spring-cloud.version>
<springboot.version>2.2.5.RELEASE</springboot.version>
</properties>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${springboot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
1.3. 删除src文件夹
2. 创建网关子项目
2.1 在父项目下面,使用Spring Initializr新建网关子项目gateway,选择依赖Gateway,简略贴图如下:
建好后,修改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>
<artifactId>springcloudproject</artifactId>
<groupId>com.cherry</groupId>
<version>1.0-SNAPSHOT</version>
<!-- <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/> <!– lookup parent from repository –>-->
</parent>
<groupId>com.cherry</groupId>
<artifactId>gateway</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>gateway</name>
<description>Demo project for Spring Boot</description>
<!--<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR3</spring-cloud.version>
</properties>-->
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</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>
2.2 修改配置文件application.properties后缀为yml(即文件名改为application.yml),对网关进行配置
这里我们将csdn博客作为服务提供方
server:
port: 9001
spring:
application:
name: gateway
cloud:
gateway:
routes:
- id: gateway-service
uri: https://blog.csdn.net
predicates:
- Path=/huanzi833
2.3 执行run,springboot内置Tomcat启动,端口9001
2.4 浏览器输入地址http://localhost:9001/huanzi833
如果你觉得本文章还算有用,请关注并点赞^_^,之后还会陆续上其他关于Spring Cloud的内容,谢谢
--------------------------
10 分钟教会你 Spring Boot 集成 Thymeleaf、MyBatis 完成产品的增删改查