Still don't know the entry point of microservices? Take a look here, hands-on with the actual gateway component Gateway, and send it into the soul~

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:

  1. Built on Spring Framework 5, Project Reactor and Spring Boot 2.0
  2. Ability to match routes on any request attribute.
  3. Route-based conditional constructor Predicates and filter filter
  4. Circuit breaker integration.
  5. 集成 Spring Cloud DiscoveryClient
  6. Conditional constructors Predicates and filters for easy-to-write routes
  7. request rate limit
  8. 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

insert image description here

2.2 After selecting the relevant parameters, click Next

insert image description here

2.3 Check dependencies

insert image description here

2.4 The complete project structure is as follows, and then we create a new yml file

insert image description here
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

insert image description here
insert image description here

After starting the gateway gateway service
insert image description here
, access our gateway domain name and test route through PostMan

insert image description here

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!

Guess you like

Origin blog.csdn.net/a1774381324/article/details/124074749