【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-discoverynacos-configalibaba-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

相关推荐
为java加瓦3 小时前
Spring 方法注入机制深度解析:Lookup与Replace Method原理与应用
java·数据库·spring
无名客03 小时前
SpringCloud中的网关(Gateway)的作用是什么?
spring·spring cloud·gateway
hrrrrb5 小时前
【Spring Security】Spring Security 概念
java·数据库·spring
小信丶5 小时前
Spring 中解决 “Could not autowire. There is more than one bean of type“ 错误
java·spring
hello 早上好12 小时前
深入 Spring 依赖注入底层原理
数据库·sql·spring
Chan1618 小时前
流量安全优化:基于 Sentinel 实现网站流量控制和熔断
java·spring boot·安全·sentinel·intellij-idea·进程
cxyxiaokui00119 小时前
🔍 为什么我的日志在事务回滚后也没了?——揭秘 REQUIRES_NEW 的陷阱
java·后端·spring
跟着珅聪学java19 小时前
spring boot 整合 activiti 教程
android·java·spring
Java水解19 小时前
Spring JDBC与KingbaseES深度集成:构建高性能国产数据库应用实战
后端·spring
低音钢琴21 小时前
【SpringBoot从初学者到专家的成长15】MVC、Spring MVC与Spring Boot:理解其差异与联系
spring boot·spring·mvc