Gateway
目录
1. 官网地址
2. 概念
2.1. 路由
- 类似nginx配置文件中的location,转发规则路径
spring:
cloud:
gateway:
#设置路由:路由id、路由到微服务的uri、断言
routes:
- id: order_route #路由ID,全局唯一,建议配置服务名
uri: lb://tlmall-order #lb 整合负载均衡器loadbalancer
2.2. 断言
- 类似nginx配置文件中的location,转发规则路径
- 请求url中/order/**的所有请求,去注册中心找tlmall-order服务,转发到它那边。
spring:
application:
name: tlmall-gateway
cloud:
gateway:
#设置路由:路由id、路由到微服务的uri、断言
routes:
- id: order_route #路由ID,全局唯一,建议配置服务名
uri: lb://tlmall-order #lb 整合负载均衡器loadbalancer
predicates:
# 订单服务就得以/order,uri开头
- Path=/order/** # 断言,路径相匹配的进行路由
2.3. 局部过滤器
- 需要在yml显示配置
spring:
cloud:
gateway:
routes:
filters:
- AddRequestHeader=X-Request-color, red #\u6DFB\u52A0\u8BF7\u6C42\u5934
- AddRequestParameter=color, blue # \u6DFB\u52A0\u8BF7\u6C42\u53C2\u6570
2.4. 全局过滤器
- 作用
全局认证校验等等。 - 无需yml配置
- 只需实现接口org.springframework.cloud.gateway.filter.GlobalFilter
- 排序
数字越小,优先级越高。
1:用注解@Order
2:实现接口org.springframework.core.Ordered
3. 为什么需要网关
- 微服务架构,同一个业务模块可能会以不同ip+端口,集群提供服务。
- 如果没有网关
客户端(前端)需要自己硬编码对接多个微服务接口地址,代码维护成本和复杂性变高了,微服务地址改变,前端也需要重新部署。
一些公共功能需要 在多个服务代码重复处理,如日志、认证、监控等。
不好做全局性的措置,如限流,熔断。
各微服务的ip和端口暴露,增加安全隐患。
不好做负载均衡和服务发现,扩展性和稳定性无法保证。
通信协议限制,某些微服务不用http协议,用其他协议,则客户端无法调用,Gateway支持转换为非http协议。
存在跨域问题。
4. 由什么实现
- 由WebFlux + Netty + Reactor 实现的响应式API网关,不在传统servlet容器工作,无法打成war
5. 如何使用
5.1. 新建网关模块
- 独立成为一个微服务应用
- pom引入依赖,需要gateway,注册中心nacos-discovery,配置中心nacos-config,负载均衡器loadbalancer
<!-- gateway网关 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<!--nacos-discovery 注册中心依赖-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- loadbalancer 负载均衡器依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-loadbalancer</artifactId>
</dependency>
<!-- nacos-config 配置中心依赖 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
5.2. 项目结构
5.3. 业务微服务接入网关
- 1:和gateway注册给同一个注册中心
- 2:在网关模块的application.yml中添加业务自己的路由信息
spring:
application:
name: tlmall-gateway
cloud:
gateway:
#设置路由:路由id、路由到微服务的uri、断言
routes:
- id: order_route #路由ID,全局唯一,建议配置服务名
uri: lb://tlmall-order #lb 整合负载均衡器loadbalancer
predicates:
# 订单服务就得以/order,uri开头
- Path=/order/** # 断言,路径相匹配的进行路由
6. 限流
6.1. 基于redis
- 1: pom添加依赖
- 2:yml添加配置
- 3:配置keyResolver,指定限流策略,url限流,参数限流,IP限流等。
6.2. 基于sentinel
- 限流维度
route级别(整个微服务)spring.cloud.gateway.routes.id的值
API级别,利用Sentinel提供的api接口,自定义一些api分组。
6.2.1. 实现步骤
- 1: pom添加依赖
<!-- gateway接入sentinel -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
- 2:yml添加配置
spring:
cloud:
sentinel:
transport:
# sentinel控制台的ip和端口
dashboard: tlmall-sentinel-dashboard:8888
7. 常见问题
7.1. gateway和spring-webmvc依赖冲突
需要排除spring-webmvc
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!