Spring Cloud Gateway实战之一:初探

欢迎访问我的GitHub

这里分类和汇总了欣宸的全部原创(含配套源码):github.com/zq2599/blog…

关于《Spring Cloud Gateway实战》系列

《Spring Cloud Gateway实战》是欣宸在Java领域的系列原创,旨在通过项目实战与大家一起学习和掌握Spring Cloud Gateway,更好的为实际项目服务

本篇概览

作为《Spring Cloud Gateway实战》的开篇,本文的主要内容如下:

  1. 基础知识简介
  2. 确认环境涉及到的工具和服务的版本
  3. 启动nacos,作为后续实战的注册中心和配置中心
  4. 创建maven父工程,为后续实战的代码和依赖库版本做好管理
  5. 创建名为common的子工程,存放共用的常量和数据结构
  6. 创建名为provider-hello的web应用,用于gateway路由的目标
  7. 运行一个简单的demo,完成spring-cloud-gateway的初体验

关于Spring Cloud Gateway

  • 这是一个基于Spring技术栈构建的API网关,涉及到:Spring5、Spring Boot 2、Reactor等,目标是为项目提供简单高效的API路由,以及强大的扩展能力:安全、监控、弹性计算等
  • 官方架构图如下,可见请求到来后,由Handler Mapping决定请求对应的真实目标,然后交给Web Handler,由一系列过滤器(filter)执行链式处理,从红色箭头和注释可以发现,请求前后都有过滤器在运行:

在这里插入图片描述

版本信息

  • 《Spring Cloud Gateway实战》系列涉及的软件和库版本信息如下:
  • 本篇实战涉及到的主要版本情况如下:
  1. JDK:1.8.0_291
  2. IDEA:2021.1.3 (Ultimate Edition)
  3. maven:3.8.1
  4. 操作系统:win10 64位
  5. springboot:2.4.2
  6. spring-cloud:2020.0.1
  7. spring-cloud-alibaba:2021.1

经典配置中的核心概念

  • 先通过一个典型的简化版配置来了解几个核心概念,假设Spring Cloud Gateway应用正在运行,监听8080端口,一旦有远程请求来到8080端口,下面的配置就会生效了,三个核心概念,以及每个配置的作用,请参考中文注释:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
yml复制代码spring:
cloud:
gateway:
# 核心概念1:路由,一个路由代表一个处理逻辑,
# 该逻辑里面包含三个元素:匹配条件(是否该此路由处理)、真实处理地址、过滤器
routes:
# id要确保唯一性
- id: add_request_header_route
# 真实处理地址,请求一旦确定是当前路由处理,就会转发到这个地址去
uri: https://example.org
# 核心概念2:谓语或者断言,作用是判断请求是否由当前路由处理
predicates:
# 这是断言的一种,检查请求的Cookie中mycookie的值是否等于mycookievalue
- Cookie=mycookie,mycookievalue
# 核心概念3:过滤器,请求前和请求后都可以有过滤器处理请求响应数据
filters:
# 这个过滤器的作用是在请求header中添加一个键值对,值等于"aaabbbccc"
- AddRequestHeader=X-Request-Red, aaabbbccc
  • 上述配置信息中的predicates是简化版配置,和完整配置对比效果如下,简单的说就是把一行拆成了三项:name、args.name、args.regexp

在这里插入图片描述

  • 理论知识点到为止,咱们还是尽快动手吧

启动nacos-2.0.3

  • 整个《pring Cloud Gateway实战》系列,我们会涉及到多个服务,这就不可避免的会用到注册中心和配置中心,这里我选择了nacos,它可以很好地承担注册中心和配置中心的角色,接下来介绍如何部署和启动nacos
  • 下载nacos,地址是:github.com/alibaba/nac…
  • 解压后进入nacos\bin目录,执行以下命令启动nacos:
1
shell复制代码startup.cmd -m standalone
  • 如果您的电脑是mac或者linux,请执行以下命令启动nacos:
1
shell复制代码sh startup.sh -m standalone

在这里插入图片描述

源码下载

  • 本篇实战中的完整源码可在GitHub下载到,地址和链接信息如下表所示(github.com/zq2599/blog…%EF%BC%9A)
名称 链接 备注
项目主页 github.com/zq2599/blog… 该项目在GitHub上的主页
git仓库地址(https) github.com/zq2599/blog… 该项目源码的仓库地址,https协议
git仓库地址(ssh) git@github.com:zq2599/blog_demos.git 该项目源码的仓库地址,ssh协议
  • 这个git项目中有多个文件夹,本篇的源码在spring-cloud-tutorials文件夹下,如下图红框所示:

在这里插入图片描述

《Spring Cloud Gateway实战》系列的父工程

  • 新建名为spring-cloud-tutorials的maven工程,这就是《Spring Cloud Gateway实战》系列所有源码的父工程就,pom.xml内容如下,可见这里将springboot、spring-cloud、spring-cloud-alibaba库的版本号都已经确定,今后子工程就无需关注依赖库的版本号了:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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>
<modules>
<module>hello-gateway</module>
<module>provider-hello</module>
<module>common</module>
</modules>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.2</version>
<relativePath/>
</parent>

<groupId>com.bolingcavalry</groupId>
<artifactId>spring-cloud-tutorials</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<java.version>1.8</java.version>
<spring-cloud.version>2020.0.1</spring-cloud.version>
<spring-cloud-alibaba.version>2021.1</spring-cloud-alibaba.version>
</properties>

<packaging>pom</packaging>
<description>Demo project for Spring Cloud </description>

<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>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.14.9</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.7</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.16</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>

创建名为common的子工程,存放共用的常量和数据结构

  • 现在创建名为common的子工程,整个《Spring Cloud Gateway实战》系列涉及的常量和数据结构都放在这个子工程中,方便其他工程使用
  • 新增常量Constants.java:
1
2
3
4
5
java复制代码package com.bolingcavalry.common;

public interface Constants {
String HELLO_PREFIX = "Hello World";
}

创建web应用,作为服务提供方

  • 现在创建名为provider-hello的web应用,这是个极其普通的web应用,提供几个http接口服务,咱们在尝试Spring Cloud Gateway的基本功能时,都会将请求路由到provider-hello上来
  • provider-hello是个普通的springboot应用,会在nacos进行注册,其pom.xml内容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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">
<parent>
<artifactId>spring-cloud-tutorials</artifactId>
<groupId>com.bolingcavalry</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>provider-hello</artifactId>
<packaging>jar</packaging>

<dependencies>

<dependency>
<groupId>com.bolingcavalry</groupId>
<artifactId>common</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!--nacos:用于服务注册与发现-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>

<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<!-- 如果父工程不是springboot,就要用以下方式使用插件,才能生成正常的jar -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.bolingcavalry.provider.ProviderApplication</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
  • 工程的配置文件application.yml如下,web端口是8082,还有一处要注意的是nacos服务地址:
1
2
3
4
5
6
7
8
9
10
11
12
yml复制代码server:
#服务端口
port: 8082

spring:
application:
name: provider-hello
cloud:
nacos:
discovery:
# nacos服务地址
server-addr: 127.0.0.1:8848
  • 启动类ProviderApplication.java
1
2
3
4
5
6
7
8
9
10
11
12
java复制代码package com.bolingcavalry.provider;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}
  • 普通的Controller类Hello.java,对外提供一个http服务:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
java复制代码package com.bolingcavalry.provider.controller;

import com.bolingcavalry.common.Constants;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("/hello")
public class Hello {

private String dateStr(){
return new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date());
}

/**
* 返回字符串类型
* @return
*/
@GetMapping("/str")
public String helloStr() {
return Constants.HELLO_PREFIX + ", " + dateStr();
}
}
  • 新增测试类HelloTest.java,用于检查应用的服务是否正常:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
java复制代码package com.bolingcavalry.provider.controller;

import com.bolingcavalry.common.Constants;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;

import static org.hamcrest.Matchers.containsString;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@SpringBootTest
@AutoConfigureMockMvc
@Slf4j
class HelloTest {

@Autowired
private MockMvc mvc;

@Test
void hello() throws Exception {
String responseString = mvc.perform(MockMvcRequestBuilders.get("/hello/str").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().string(containsString(Constants.HELLO_PREFIX)))
.andDo(print())
.andReturn()
.getResponse()
.getContentAsString();

log.info("response in junit test :\n" + responseString);
}
}
  • 执行单元测试(此时nacos是否启动无所谓,只是不启动的话控制台会有一些错误信息,但是没有影响),如下,测试通过表示服务是正常的:

在这里插入图片描述

开发一个简单的demo,完成spring-cloud-gateway的初体验

  • 前面做了那么多准备,接下来咱们会投入到Spring Cloud Gateway的开发中,先写个简单的demo快速体验一下
  • 新增名为hello-gateway的子工程,pom.xml如下,重点是依赖了spring-cloud-starter-gateway库,还有一处要重点小心的:测试库用的是reactor-test和spring-boot-starter-test,这和之前的单元测试很不一样,用的是webflux:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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">
<parent>
<artifactId>spring-cloud-tutorials</artifactId>
<groupId>com.bolingcavalry</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>hello-gateway</artifactId>

<dependencies>
<dependency>
<groupId>com.bolingcavalry</groupId>
<artifactId>common</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>

<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
  • 下面是重点,Spring Cloud Gateway的配置文件application.yml:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
yml复制代码server:
#服务端口
port: 8081
spring:
application:
name: hello-gateway
cloud:
gateway:
routes:
- id: path_route
# 匹配成功后,会被转发到8082端口,至于端口后面的path,会直接使用原始请求的
# 例如http://127.0.0.1:8081/hello/str,会被转发到http://127.0.0.1:8082/hello/str
uri: http://127.0.0.1:8082
predicates:
# 根据请求路径中带有"/hello/",就算匹配成功
- Path=/hello/**
  • 如果要转发到其他域名下,需要创建配置类解决跨域问题:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
java复制代码package com.bolingcavalry.hellogateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.util.pattern.PathPatternParser;

@Configuration
public class CorsConfig {

@Bean
public CorsWebFilter corsFilter() {
CorsConfiguration config = new CorsConfiguration();
config.addAllowedMethod("*");
config.addAllowedOrigin("*");
config.addAllowedHeader("*");

UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());
source.registerCorsConfiguration("/**", config);

return new CorsWebFilter(source);
}
}
  • 启动类:
1
2
3
4
5
6
7
8
9
10
11
java复制代码package com.bolingcavalry.hellogateway;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class HelloGatewayApplication {
public static void main(String[] args) {
SpringApplication.run(HelloGatewayApplication.class,args);
}
}
  • 最后是单元测试类,请注意,由于Spring Cloud Gateway使用了webflux技术栈,因此不能用常见的MockMvc来模拟请求,几个注解也值得注意,另外也要注意WebTestClient的expectStatus、expectBody等API的用法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
java复制代码package com.bolingcavalry.hellogateway;

import com.bolingcavalry.common.Constants;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.reactive.server.WebTestClient;
import static org.junit.jupiter.api.Assertions.assertTrue;

@SpringBootTest
@ExtendWith(SpringExtension.class)
@AutoConfigureWebTestClient
public class HelloTest {

@Autowired
private WebTestClient webClient;

@Test
void testHelloPredicates() {
webClient.get()
.uri("/hello/str")
.accept(MediaType.APPLICATION_JSON)
.exchange()
// 验证状态
.expectStatus().isOk()
// 验证结果,注意结果是字符串格式
.expectBody(String.class).consumeWith(result -> assertTrue(result.getResponseBody().contains(Constants.HELLO_PREFIX)));
}
}
  • 请确保provider-hello应用已经启动,再运行上面创建的HelloTest.java,得到结果如下,测试通过,证明hello-gateway的功能符合预期,成功的将请求转发到provider-hello应用,并且成功收到响应:

在这里插入图片描述

  • 至此,《Spring Cloud Gateway实战》系列的准备工作已经完成,而且开发了一个简单的应用体验最基本的Spring Cloud Gateway功能,接下来的文章,咱们一起实战更多基本功能。

你不孤单,欣宸原创一路相伴

  1. Java系列
  2. Spring系列
  3. Docker系列
  4. kubernetes系列
  5. 数据库+中间件系列
  6. DevOps系列

欢迎关注公众号:程序员欣宸

微信搜索「程序员欣宸」,我是欣宸,期待与您一同畅游Java世界…
github.com/zq2599/blog…

本文转载自: 掘金

开发者博客 – 和开发相关的 这里全都有

0%