什么是gateway?
Gateway:是Spring官方基于Spring Spring Boot和Project Reactor等技术开发的网关,Gateway旨在为微服务架构提供一种简单而有效的统一的API路由管理方式。Gateway作为Spring Cloud生态系中的网关,目标是替代ZUUL,其不仅提供统一的路由方式,并且基于Filter链的方式提供了关基本的功能,例如:安全,监控/埋点,和限流等。
一、路由转发功能:
1.引入依赖,注意与自己项目版本对应避免冲突,我这里使用2.0.4版本,具体可参考下文
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
<version>2.0.4.RELEASE</version>
</dependency>
2022.x 分支
适配 Spring Boot 3.0,Spring Cloud 2022.x 版本及以上的 Spring Cloud Alibaba 版本按从新到旧排列如下表(最新版本用*标记): (注意,该分支 Spring Cloud Alibaba 版本命名方式进行了调整,未来将对应 Spring Cloud 版本,前三位为 Spring Cloud 版本,最后一位为扩展版本,比如适配 Spring Cloud 2022.0.0 版本对应的 Spring Cloud Alibaba 第一个版本为:2022.0.0.0,第个二版本为:2022.0.0.1,依此类推)
Spring Cloud Alibaba Version | Spring Cloud Version | Spring Boot Version |
2022.0.0.0* | Spring Cloud 2022.0.0 | 3.0.2 |
2022.0.0.0-RC2 | Spring Cloud 2022.0.0 | 3.0.2 |
2022.0.0.0-RC1 | Spring Cloud 2022.0.0 | 3.0.0 |
2021.x 分支
适配 Spring Boot 2.4,Spring Cloud 2021.x 版本及以上的 Spring Cloud Alibaba 版本按从新到旧排列如下表(最新版本用*标记):
Spring Cloud Alibaba Version | Spring Cloud Version | Spring Boot Version |
2021.0.5.0* | Spring Cloud 2021.0.5 | 2.6.13 |
2021.0.4.0 | Spring Cloud 2021.0.4 | 2.6.11 |
2021.0.1.0 | Spring Cloud 2021.0.1 | 2.6.3 |
2021.1 | Spring Cloud 2020.0.1 | 2.4.2 |
2.2.x 分支
适配 Spring Boot 为 2.4,Spring Cloud Hoxton 版本及以下的 Spring Cloud Alibaba 版本按从新到旧排列如下表(最新版本用*标记):
Spring Cloud Alibaba Version | Spring Cloud Version | Spring Boot Version |
2.2.10-RC1* | Spring Cloud Hoxton.SR12 | 2.3.12.RELEASE |
2.2.9.RELEASE | Spring Cloud Hoxton.SR12 | 2.3.12.RELEASE |
2.2.8.RELEASE | Spring Cloud Hoxton.SR12 | 2.3.12.RELEASE |
2.2.7.RELEASE | Spring Cloud Hoxton.SR12 | 2.3.12.RELEASE |
2.2.6.RELEASE | Spring Cloud Hoxton.SR9 | 2.3.2.RELEASE |
2.2.1.RELEASE | Spring Cloud Hoxton.SR3 | 2.2.5.RELEASE |
2.2.0.RELEASE | Spring Cloud Hoxton.RELEASE | 2.2.X.RELEASE |
2.1.4.RELEASE | Spring Cloud Greenwich.SR6 | 2.1.13.RELEASE |
2.1.2.RELEASE | Spring Cloud Greenwich | 2.1.X.RELEASE |
2.0.4.RELEASE(停止维护,建议升级) | Spring Cloud Finchley | 2.0.X.RELEASE |
1.5.1.RELEASE(停止维护,建议升级) | Spring Cloud Edgware | 1.5.X.RELEASE |
这里附上原文参考地址:版本说明 · alibaba/spring-cloud-alibaba Wiki · GitHub
2.编写yml配置文件(具体配置可参考官网:Spring Cloud Gateway 中文文档 (springdoc.cn))
server:
port: 9999
spring:
application:
name: gateway-service
cloud: #配置spring cloud相关属性
gateway: #配置gateway相关属性
routes: # 网关路由配置
- id: gateway_service # 路由id,自定义,只要唯一即可
uri: https://localhost:8081 # 路由的目标地址
predicates: # 路由断言,也就是判断请求是否符合路由规则的条件
- Path=/order/** # 按照路径匹配,可自行增加
3.编写boostrap文件,也可见官网配置(该文件会在启动时优先加载)
server:
ssl:
enabled: true #启用SSL支持,将使用HTTPS协议
key-alias: xxx #密钥库中的默认密钥别名。换成自己的
key-store-password: xxx #密钥库密码
key-store: classpath:xxx #这里指定了p12格式的密钥库文件
key-store-type: PKCS12 #密钥格式
#不验证后端服务的SSL证书,这在开发测试阶段常用来跳过证书验证
spring:
cloud:
gateway:
httpclient:
ssl:
useInsecureTrustManager: true
4.启动服务
5.通过apifox进行接口测试
①编辑网关地址(注意是https)
②请求转发,由于path配置的是/order/**所以这里用order/xx,类推自行配置
③可正确转发请求,路由转发配置成功,至于为什么没数据那是因为博主传的参没结果哦~
二、过滤器功能
1.该功能包含诸多情形,具体见下图:
2.实现该功能只需在配置文件中routes属性下添加filters属性即可,根据个人使用情形查看官网Spring Cloud Gateway 中文文档 (springdoc.cn))使用方式自行选择使用何种,如请求头添加信息、添加参数等等。这里不做过多赘述。