1. Introduction to Gateway
The Gateway project provides an API Gateway built on top of the Spring ecosystem, including: Spring 5, Spring Boot 2, and Project Reactor. Spring Cloud Gateway aims to provide a simple and efficient way to route to APIs and provide them with cross-cutting concerns such as security, monitoring/metrics and resiliency.
Official website link through train gateway
Features of Spring Cloud Gateway:
- Built on Spring Framework 5, Project Reactor and Spring Boot 2.0
- Ability to match routes on any request attribute.
- Route-based conditional constructor Predicates and filter filter
- Circuit breaker integration.
- 集成 Spring Cloud DiscoveryClient
- Conditional constructors Predicates and filters for easy-to-write routes
- request rate limit
- route rewrite
So why do I need to use gateway gateway?
The main reason is that in order to facilitate the use of the same https secure domain name when developing the applet corresponding to the Love Talk service and the previous completed project service, the codes of the two functions are written in one project, resulting in the modification of one function at a time. At the same time, it will affect the use of another service, and it does not achieve the concept of microservices performing their own duties. Therefore, it is planned to reconstruct the project by using the gateway gateway, and finally realize that a domain name is forwarded to different backend services according to the route.
2. Create a Gateway project
2.1 Create a new springboot project on idea
2.2 After selecting the relevant parameters, click Next
2.3 Check dependencies
2.4 The complete project structure is as follows, and then we create a new yml file
This is the complete pom.xml file
<?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.6.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.yinfeng</groupId>
<artifactId>gateway</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>gateway</name>
<description>gateway</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>2021.0.1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-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>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
3. Configure forwarding rules
Configure it in the yml file we created earlier
spring:
application:
name: gateway
cloud:
gateway:
routes:
- id: test
# 转发的地址
uri: http://127.0.0.1:8888
predicates:
# 配置url路径匹配规则
- Path=/test/**
server:
port: 8081
4. Test it
Start a backend service test first
After starting the gateway gateway service
, access our gateway domain name and test route through PostMan
It can be seen that our request can be normally forwarded to the interface of the test service, and the response of the interface has also been received, and our goal has finally been completed.
5. Summary
It is not easy to go out to work. I hope all brothers can find their favorite jobs, and the Year of the Tiger will be full! I also hope that brothersFollow, Like, Favorite, CommentA wave of support, thank you very much!