共计 13380 个字符,预计需要花费 34 分钟才能阅读完成。
欢送拜访我的 GitHub
这里分类和汇总了欣宸的全副原创(含配套源码):https://github.com/zq2599/blog_demos
对于《Spring Cloud Gateway 实战》系列
《Spring Cloud Gateway 实战》是欣宸在 Java 畛域的系列原创,旨在通过我的项目实战与大家一起学习和把握 Spring Cloud Gateway,更好的为理论我的项目服务
本篇概览
作为《Spring Cloud Gateway 实战》的开篇,本文的次要内容如下:
- 基础知识简介
- 确认环境波及到的工具和服务的版本
- 启动 nacos,作为后续实战的注册核心和配置核心
- 创立 maven 父工程,为后续实战的代码和依赖库版本做好治理
- 创立名为 <font color=”blue”>common</font> 的子工程,寄存共用的常量和数据结构
- 创立名为 <font color=”blue”>provider-hello</font> 的 web 利用,用于 gateway 路由的指标
- 运行一个简略的 demo,实现 spring-cloud-gateway 的初体验
对于 Spring Cloud Gateway
- 这是一个基于 Spring 技术栈构建的 API 网关,波及到:Spring5、Spring Boot 2、Reactor 等,指标是为我的项目提供简略高效的 API 路由,以及弱小的扩大能力:平安、监控、弹性计算等
- 官网架构图如下,可见申请到来后,由 <font color=”blue”>Handler Mapping</font> 决定申请对应的实在指标,而后交给 <font color=”blue”>Web Handler</font>,由一系列过滤器 (filter) 执行链式解决,从红色箭头和正文能够发现,申请前后都有过滤器在运行:
版本信息
- 《Spring Cloud Gateway 实战》系列波及的软件和库版本信息如下:
- 本篇实战波及到的次要版本状况如下:
- JDK:1.8.0_291
- IDEA:2021.1.3 (Ultimate Edition)
- maven:3.8.1
- 操作系统:win10 64 位
- springboot:2.4.2
- spring-cloud:2020.0.1
- spring-cloud-alibaba:2021.1
- 更具体的版本匹配关系请参考:https://github.com/alibaba/sp…
经典配置中的外围概念
- 先通过一个典型的简化版配置来理解几个外围概念,假如 Spring Cloud Gateway 利用正在运行,监听 8080 端口,一旦有近程申请来到 8080 端口,上面的配置就会失效了,三个外围概念,以及每个配置的作用,请参考中文正文:
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,地址是:<font color=”blue”>https://github.com/alibaba/na…</font>
- 解压后进入 <font color=”blue”>nacos\bin</font> 目录,执行以下命令启动 nacos:
startup.cmd -m standalone
- 如果您的电脑是 mac 或者 linux,请执行以下命令启动 nacos:
sh startup.sh -m standalone
- 浏览器登录 nacos,地址是 <font color=”blue”>http://localhost:8848/nacos</font>,账号和明码都是 <font color=”red”>nacos</font>
- 登录胜利后显示如下:
源码下载
- 本篇实战中的残缺源码可在 GitHub 下载到,地址和链接信息如下表所示(https://github.com/zq2599/blo…):
名称 | 链接 | 备注 |
---|---|---|
我的项目主页 | https://github.com/zq2599/blo… | 该我的项目在 GitHub 上的主页 |
git 仓库地址(https) | https://github.com/zq2599/blo… | 该我的项目源码的仓库地址,https 协定 |
git 仓库地址(ssh) | git@github.com:zq2599/blog_demos.git | 该我的项目源码的仓库地址,ssh 协定 |
- 这个 git 我的项目中有多个文件夹,本篇的源码在 <font color=”blue”>spring-cloud-tutorials</font> 文件夹下,如下图红框所示:
《Spring Cloud Gateway 实战》系列的父工程
- 新建名为 <font color=”blue”>spring-cloud-tutorials</font> 的 maven 工程,这就是《Spring Cloud Gateway 实战》系列所有源码的父工程就,pom.xml 内容如下,可见这里将 springboot、spring-cloud、spring-cloud-alibaba 库的版本号都曾经确定,今后子工程就无需关注依赖库的版本号了:
<?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 的子工程,寄存共用的常量和数据结构
- 当初创立名为 <font color=”blue”>common</font> 的子工程,整个《Spring Cloud Gateway 实战》系列波及的常量和数据结构都放在这个子工程中,不便其余工程应用
- 新增常量 Constants.java:
package com.bolingcavalry.common; | |
public interface Constants {String HELLO_PREFIX = "Hello World";} |
创立 web 利用,作为服务提供方
- 当初创立名为 <font color=”blue”>provider-hello</font> 的 web 利用,这是个极其一般的 web 利用,提供几个 http 接口服务,咱们在尝试 Spring Cloud Gateway 的基本功能时,都会将申请路由到 <font color=”blue”>provider-hello</font> 上来
- <font color=”blue”>provider-hello</font> 是个一般的 springboot 利用,会在 nacos 进行注册,其 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"> | |
<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 端口是 <font color=”red”>8082</font>,还有一处要留神的是 nacos 服务地址:
server: | |
#服务端口 | |
port: 8082 | |
spring: | |
application: | |
name: provider-hello | |
cloud: | |
nacos: | |
discovery: | |
# nacos 服务地址 | |
server-addr: 127.0.0.1:8848 |
- 启动类 ProviderApplication.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 类 <font color=”blue”>Hello.java</font>,对外提供一个 http 服务:
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,用于查看利用的服务是否失常:
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 疾速体验一下
- 新增名为 <font color=”blue”>hello-gateway</font> 的子工程,pom.xml 如下,重点是依赖了 <font color=”red”>spring-cloud-starter-gateway</font> 库,还有一处要重点小心的:测试库用的是 reactor-test 和 spring-boot-starter-test,这和之前的单元测试很不一样,用的是 webflux:
<?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 的配置文件 <font color=”blue”>application.yml</font>:
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/** |
- 如果要转发到其余域名下,须要创立配置类解决跨域问题:
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); | |
} | |
} |
- 启动类:
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 的用法:
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,失去后果如下,测试通过,证实 <font color=”blue”>hello-gateway</font> 的性能合乎预期,胜利的将申请转发到 <font color=”red”>provider-hello</font> 利用,并且胜利收到响应:
- 至此,《Spring Cloud Gateway 实战》系列的筹备工作曾经实现,而且开发了一个简略的利用体验最根本的 Spring Cloud Gateway 性能,接下来的文章,咱们一起实战更多基本功能。
你不孤独,欣宸原创一路相伴
- Java 系列
- Spring 系列
- Docker 系列
- kubernetes 系列
- 数据库 + 中间件系列
- DevOps 系列
欢送关注公众号:程序员欣宸
微信搜寻「程序员欣宸」,我是欣宸,期待与您一起畅游 Java 世界 …
https://github.com/zq2599/blog_demos
正文完