【RuoYi-Cloud项目研究】【ruoyi-gateway模块】Spring Gatewaye和Sentinel实现网关流控

文章目录

  • [1. RuoYi 实现了对服务的限流](#1. RuoYi 实现了对服务的限流)
  • [2. 网关与普通服务的限流区别](#2. 网关与普通服务的限流区别)
    • [2.1. 引入的组件有差别](#2.1. 引入的组件有差别)
    • [2.2. 配置文件有差别](#2.2. 配置文件有差别)
  • [3. 注意事项](#3. 注意事项)

备注:

1、RuoYi 网关默认只在 nacos 配置中心的 Sentinel 限流配置中配置了对"服务限流",而没有详细控制到限流的 URL。

2、各个服务虽然引入了 Sentinel 相关组件但是并没有对各个具体服务做具体的 URL 限流配置;

3、如果用户需要对 URL 限流请自行在服务中配置 Sentinel dashboard 连接即可。

本文主要介绍如何用Sentinel控制网关流控,和网关服务与普通服务流控的不同点。

1. RuoYi 实现了对服务的限流

  • 在 pom.xml 引入组件
xml 复制代码
<!-- SpringCloud Alibaba Nacos -->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

<!-- SpringCloud Alibaba Nacos Config -->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>

<!-- SpringCloud Alibaba Sentinel -->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>

<!-- SpringCloud Alibaba Sentinel Gateway -->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
</dependency>

<!-- 限流规则持久化到nacos -->
<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
  • 配置 bootstrap.yml 文件
yaml 复制代码
spring:
  application:
    name: sentinel-service
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848 #配置Nacos地址
    sentinel:
      transport:
        dashboard: localhost:8080 #配置sentinel dashboard地址
        port: 8719
      # 把流控规则nacos配置持久化
      datasource:
        ds1:
          nacos:
            server-addr: 127.0.0.1:8848
            dataId: sentinel-ruoyi-gateway
            groupId: DEFAULT_GROUP
            data-type: json
            # 网关的流控类型才是gw-flow,普通是flow
            rule-type: gw-flow
  • sentinel-ruoyi-gateway 文件的详细内容

在 nacos 的 sentinel-ruoyi-gateway文件中,配置如如下内容,完成了对 4 个微服务的"总体流控"。

yaml 复制代码
[
    {
        "resource": "ruoyi-auth",
        "count": 500,
        "grade": 1,
        "limitApp": "default",
        "strategy": 0,
        "controlBehavior": 0
    },
	{
        "resource": "ruoyi-system",
        "count": 1000,
        "grade": 1,
        "limitApp": "default",
        "strategy": 0,
        "controlBehavior": 0
    },
	{
        "resource": "ruoyi-gen",
        "count": 200,
        "grade": 1,
        "limitApp": "default",
        "strategy": 0,
        "controlBehavior": 0
    },
	{
        "resource": "ruoyi-job",
        "count": 300,
        "grade": 1,
        "limitApp": "default",
        "strategy": 0,
        "controlBehavior": 0
    }
]

2. 网关与普通服务的限流区别

限流是网关的重要职责之一,"对网关限流"跟"服务的URL限流"并不是完全一样,主要体现在以下几个方面。

2.1. 引入的组件有差别

  • 普通需要限流服务引入的组件
xml 复制代码
<!-- SpringCloud Alibaba Nacos -->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

<!-- SpringCloud Alibaba Nacos Config -->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>

<!-- SpringCloud Alibaba Sentinel -->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>

<!-- 限流规则持久化到nacos -->
<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-datasource-nacos</artifactId>
</dependency>

在pom.xml中添加相关依赖,这里我们使用Nacos作为注册中心,所以需要同时添加Nacos的依赖

有 nacos-discovery、nacos-config、alibaba-sentinel3 个组件。

  • 网关需要限流服务引入的组件
xml 复制代码
<!-- SpringCloud Alibaba Nacos -->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

<!-- SpringCloud Alibaba Nacos Config -->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>

<!-- SpringCloud Alibaba Sentinel -->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>

<!-- SpringCloud Alibaba Sentinel Gateway -->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
</dependency>

<!-- 限流规则持久化到nacos -->
<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-datasource-nacos</artifactId>
</dependency>

比普通服务的限流多了一个 sentinel-gateway组件。

2.2. 配置文件有差别

下面的配置是连接 Sentinel dashboard 控制台,这个配置是一样的。

yaml 复制代码
spring:
  application:
    name: sentinel-service
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848 #配置Nacos地址
    sentinel:
      transport:
        dashboard: localhost:8080 #配置sentinel dashboard地址
        port: 8719
  • 普通服务的 nacos 持久化配置
yaml 复制代码
spring:
  cloud:
    sentinel:
      datasource:
        ds1:
          nacos:
            server-addr: localhost:8848
            dataId: ${spring.application.name}-sentinel
            groupId: DEFAULT_GROUP
            data-type: json
            rule-type: flow
  • 网关服务的 nacos 持久化配置
yaml 复制代码
spring:
  cloud:
    sentinel:      
      # 把流控规则nacos配置持久化
      datasource:
        ds1:
          nacos:
            server-addr: 127.0.0.1:8848
            dataId: sentinel-ruoyi-gateway
            groupId: DEFAULT_GROUP
            data-type: json
            # 网关的流控类型才是gw-flow,普通是flow
            rule-type: gw-flow

注意点:通过Spring Cloud Alibaba Sentinel 数据源模块,网关流控规则数据源类型是 gw-flow 而不是 flow。

3. 注意事项

应该是各个服务管理自己 uri 的流控;网关总体管理各个服务的流控。

资料参考:只需三步实现Gateway结合Sentinel实现无侵入网关限流,注意避坑!

避坑点1:通过Spring Cloud Alibaba接入sentinel需要将spring.cloud.sentinel.filter.enabled 配置项置为 false(网关流控默认粒度为route和自定义API分组维度,不支持URL粒度)

避坑点 2:通过Spring Cloud Alibaba Sentinel 数据源模块,网关流控规则数据源类型是 gw-flow 而不是flow

相关推荐
Bingo_BIG4 天前
Java Spring框架:单表的查询、新增、修改、删除、导入、导出
java·spring·前后端
paopaokaka_luck4 天前
智慧社区综合服务小程序(人脸识别、AI问答、Echarts图形化分析)
前端·javascript·spring boot·spring·数据分析·echarts
牧瀬クリスだ4 天前
YAML配置详解:SpringBoot实战指南
java·spring·yml
需要8264 天前
JVM 内存区域与对象创建:一次 GC 从哪来
java·jvm·spring boot·spring·servlet·tomcat
paopaokaka_luck5 天前
考研政治刷题小程序(AI推荐题目、ECharts数据分析、章节练习与专项训练、模拟考试、错题本与收藏夹、学习打卡与目标管理、题库和组卷维护)
前端·javascript·spring boot·spring·数据分析·echarts
步行cgn5 天前
Spring 基于 XML 的自动装配:byType 详解
java·后端·spring
边境悍匪5 天前
蜗牛学苑 Java 智能体学习 Day46|贯穿项目 2 思维导图复盘
java·开发语言·vue.js·学习·spring
步行cgn5 天前
Spring c 命名空间注入详解
java·后端·spring
需要8265 天前
MySQL 索引 B+ 树与“查询为什么变慢了
java·spring boot·spring·spring cloud·tomcat
vx-程序开发5 天前
【计算机毕设】django校园跑腿服务系统83141
java·数据库·vue.js·spring boot·spring·elasticsearch·django