Spring Cloud Gateway 简明教程(01)-概念简介与Hello World 示例

一. Spring Cloud Gateway 简介

Spring Cloud Gateway 是在 Spring MVC 基础之上用于构建 API 网关的项目,其目标是提供一个简单、有效的方式实现 API 的路由和一些其他跨领域的功能,比如安全、监控/指标、弹性服务等。关于网关设计的要点,可以参考耗子叔专栏中的文章管理设计篇之“网关模式”

1. 主要概念
  • Route(路由):每条 Route 代表一条 API 的路由规则,主要由四部分组成:路由 ID,目的地址,一组匹配器(Predicate)和一组过滤器(Filter)。
  • Predicate(匹配器):可以基于时间、权重、请求信息等进行请求匹配。一条路由的所有 Predicate 匹配都通过时该条路由才算匹配成功。
  • Filter(过滤器):用于对请求和响应做响应的处理。
2. 工作流程

下面两幅图展示了 Spring Cloud Gateway 的工作流程:

在这里插入图片描述

可以看到,当一个请求过来时主要有下面几步:

  • 接收请求
  • 遍历路由进行匹配器匹配,匹配成功则开始执行 filter
  • PreFilter 处理请求
  • 转发请求到 Service(即匹配成功的 Route 所配置的 uri)
  • PostFilter 处理响应
  • 返回响应

二. Hello World 示例

上面是基本的概念介绍,下面我们创建一个简单的示例项目。

假设我们有一个教务系统,有教师服务 teacher-service和学生服务 student-service 两个后台服务,现在我们需要用 Spring Cloud Gateway 作为网关进行请求代理和转发。

1. 创建后台服务

教师后台服务

  • Controller
@SpringBootApplication
public class TeacherServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(TeacherServiceApplication.class, args);
    }

}

@RestController
public class TeacherController {

    @GetMapping("/teacher/message")
    public String message() {
        return "This is Teacher Service";
    }
}
  • application.yml 配置文件
server:
  port: 8081
  • pom 文件
<?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.2.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.school.teacher</groupId>
    <artifactId>teacher-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>teacher-service</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</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>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

学生后台服务

  • Controller

@SpringBootApplication
public class StudentServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(StudentServiceApplication.class, args);
    }

}

@RestController
public class StudentService {

    @GetMapping("/student/message")
    public String message() {
        return "This is Student Service";
    }
}

  • application.yml 配置文件
server:
  port: 8082
  • pom 文件
<?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.2.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.school.student</groupId>
    <artifactId>student-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>student-service</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</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>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

创建完成并启动服务后,可以分别访问对应服务的 Controller 进行测试,都整对的话访问情况如下:

教师服务

学生服务

2. 创建网关服务

后端服务创建好之后,再来创建网关服务并配置路由其

  • Java 代码

@SpringBootApplication
public class SchoolGatewayApplication {

    public static void main(String[] args) {
        SpringApplication.run(SchoolGatewayApplication.class, args);
    }

}
  • pom 文件
<?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.2.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.school.gateway</groupId>
    <artifactId>school-gateway</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>school-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>

  • 路由配置

Spring Cloud Gateway 的路由可以在代码、配置文件中进行配置。代码如下:

  • application.yml 文件配置
server:
  port: 8080

spring:
  cloud:
    gateway:
      routes:
      # 按路径匹配
      # 以 /teacher 开头的请求将会被路由到 http://localhost:8081 
      - id: teacher-route
        uri: http://localhost:8081
        predicates:
          - Path=/teacher/**
      # 以 /student 开头的请求将会被路由到 http://localhost:8082
      - id: student-route
        uri: http://localhost:8082
        predicates:
          - Path=/student/**

Java 代码配置


@Configuration
public class RouteConfig {

    @Bean
    public RouteLocator gatewayRoutes(RouteLocatorBuilder builder) {
        return builder.routes()
                .route(r -> r.path("/teacher/**")
                        .uri("http://localhost:8081/")
                        .id("teacher-route"))

                .route(r -> r.path("/student/**")
                        .uri("http://localhost:8082/")
                        .id("student-route"))
                .build();
    }
}


配置完成后启动网关服务,然后通过网关访问后端服务,访问情况如下:


可以看到通过网关可以正常访问后端服务了,网关会根据请求路由将请求路由到对应的后端服务。有了直观的使用之后,就可以对 Spring Cloud Gateway 做更详细的探索了。下一篇主要介绍下 Predicate 匹配器的主要匹配规则使用。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章