【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

相关推荐
编程、小哥哥1 小时前
netty之Netty与SpringBoot整合
java·spring boot·spring
wxin_VXbishe6 小时前
springboot合肥师范学院实习实训管理系统-计算机毕业设计源码31290
java·spring boot·python·spring·servlet·django·php
OEC小胖胖6 小时前
Spring Boot + MyBatis 项目中常用注解详解(万字长篇解读)
java·spring boot·后端·spring·mybatis·web
落落落sss7 小时前
MybatisPlus
android·java·开发语言·spring·tomcat·rabbitmq·mybatis
潘多编程15 小时前
Java中的状态机实现:使用Spring State Machine管理复杂状态流转
java·开发语言·spring
_阿伟_15 小时前
SpringMVC
java·spring
杨半仙儿还未成仙儿21 小时前
Spring框架:Spring Core、Spring AOP、Spring MVC、Spring Boot、Spring Cloud等组件的基本原理及使用
spring boot·spring·mvc
攸攸太上1 天前
Spring Gateway学习
java·后端·学习·spring·微服务·gateway
无理 Java1 天前
【技术详解】SpringMVC框架全面解析:从入门到精通(SpringMVC)
java·后端·spring·面试·mvc·框架·springmvc
gobeyye1 天前
spring loC&DI 详解
java·spring·rpc