springcloud整合sentinel,限流策略持久化到nacos,详细配置案例
目录
1.组件下载和启动
(1)sentinel-dashboard下载
(2)nacos下载
(3)jmeter下载
(4)redis下载(与流控关系不大,与项目启动有关)
2.本微服务项目启动三个子应用:gateway(网关)、auth(鉴权)和system(业务);
(1)gateway服务(流控的统一入口)
①pom.xml
②bootstrap.yml配置
③ruoyi-gateway-dev.yml
④启动类RuoYiGatewayApplication加入配置,否则在sentinel-dashboard主界面左侧无“API管理”菜单
⑤新增流控规则实时查询接口类“FlowRulesController”
⑥在sentinel-dashboard界面查看或者配置流控信息
⑦jmeter测试限流
⑧共享配置 application-dev.yml
(2)auth服务(鉴权)
①pom.xml
②bootstrap.yml配置
③ruoyi-auth-dev.yml
④启动类RuoYiAuthApplication无需处理
⑤登录系统后,sentinel-dashboard监控如下
(3)system服务(业务)
①pom.xml
②bootstrap.yml配置
③ruoyi-system-dev.yml
④启动类RuoYiSystemApplication显示API分组
⑤前端请求业务接口后,sentinel控制台显示
3.总结
1.组件下载和启动
(1)sentinel-dashboard下载
https://github.com/alibaba/Sentinel/releaseshttps://github.com/alibaba/Sentinel/releases
本案例使用sentinel-dashboard-1.8.4版本,
启动sentinel-dashboard服务,
java -Dserver.port=8718 -Dcsp.sentinel.dashboard.server=localhost:8718 -Dproject.name=sentinel-dashboard -Dcsp.sentinel.api.port=8719 -jar D:\sentinel-dashboard-1.8.4.jar
(2)nacos下载
发布历史 | Nacos 官网https://nacos.io/download/release-history/
本案例使用nacos-server-2.3.2版本,比较新的版本已经取消了登录功能,此处免密登录。
双击“nacos\bin\startup.cmd”,启动nacos服务,
(3)jmeter下载
Apache JMeter - Download Apache JMeterhttps://jmeter.apache.org/download_jmeter.cgi
本例使用apache-jmeter-5.5,
修改:apache-jmeter-5.5\bin\jmeter.properties,解决返回值中文乱码问题。
(4)redis下载(与流控关系不大,与项目启动有关)
https://github.com/MicrosoftArchive/redis/releaseshttps://github.com/MicrosoftArchive/redis/releases本案例使用Redis-x64-3.2.100
2.本微服务项目启动三个子应用:gateway(网关)、auth(鉴权)和system(业务);
最顶级pom.xml配置如下,主要是依赖包的版本控制;
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.ruoyi</groupId><artifactId>ruoyi</artifactId><version>3.6.4</version><name>ruoyi</name><url>http://www.ruoyi.vip</url><description>若依微服务系统</description><properties><ruoyi.version>3.6.4</ruoyi.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>17</java.version><spring-boot.version>3.3.3</spring-boot.version><spring-cloud.version>2023.0.3</spring-cloud.version><spring-cloud-alibaba.version>2023.0.1.2</spring-cloud-alibaba.version><spring-boot-admin.version>3.3.3</spring-boot-admin.version><mybatis-spring.version>3.0.3</mybatis-spring.version><swagger.fox.version>3.0.0</swagger.fox.version><swagger.core.version>1.6.2</swagger.core.version><kaptcha.version>2.3.3</kaptcha.version><pagehelper.boot.version>2.1.0</pagehelper.boot.version><druid.version>1.2.23</druid.version><dynamic-ds.version>4.3.1</dynamic-ds.version><commons.io.version>2.13.0</commons.io.version><velocity.version>2.3</velocity.version><fastjson.version>2.0.43</fastjson.version><jjwt.version>0.9.1</jjwt.version><minio.version>8.2.2</minio.version><poi.version>4.1.2</poi.version><springdoc.version>2.5.0</springdoc.version><transmittable-thread-local.version>2.14.4</transmittable-thread-local.version></properties><!-- 依赖声明 --><dependencyManagement><dependencies><!-- SpringCloud 微服务 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency><!-- SpringCloud Alibaba 微服务 --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-dependencies</artifactId><version>${spring-cloud-alibaba.version}</version><type>pom</type><scope>import</scope></dependency><!-- SpringBoot 依赖配置 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>${spring-boot.version}</version><type>pom</type><scope>import</scope></dependency><!-- Springdoc webmvc 依赖配置 --><dependency><groupId>org.springdoc</groupId><artifactId>springdoc-openapi-starter-webmvc-ui</artifactId><version>${springdoc.version}</version></dependency><!-- 验证码 --><dependency><groupId>pro.fessional</groupId><artifactId>kaptcha</artifactId><version>${kaptcha.version}</version></dependency><!-- pagehelper 分页插件 --><dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper-spring-boot-starter</artifactId><version>${pagehelper.boot.version}</version><exclusions><exclusion><artifactId>mybatis-spring</artifactId><groupId>org.mybatis</groupId></exclusion></exclusions></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>${mybatis-spring.version}</version></dependency><!-- io常用工具类 --><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>${commons.io.version}</version></dependency><!-- excel工具 --><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>${poi.version}</version></dependency><!-- 代码生成使用模板 --><dependency><groupId>org.apache.velocity</groupId><artifactId>velocity-engine-core</artifactId><version>${velocity.version}</version></dependency><!-- JSON 解析器和生成器 --><dependency><groupId>com.alibaba.fastjson2</groupId><artifactId>fastjson2</artifactId><version>${fastjson.version}</version></dependency><!-- JWT --><dependency><groupId>io.jsonwebtoken</groupId><artifactId>jjwt</artifactId><version>${jjwt.version}</version></dependency><!-- 线程传递值 --><dependency><groupId>com.alibaba</groupId><artifactId>transmittable-thread-local</artifactId><version>${transmittable-thread-local.version}</version></dependency><!-- 核心模块 --><dependency><groupId>com.ruoyi</groupId><artifactId>ruoyi-common-core</artifactId><version>${ruoyi.version}</version></dependency><!-- 接口模块 --><dependency><groupId>com.ruoyi</groupId><artifactId>ruoyi-common-swagger</artifactId><version>${ruoyi.version}</version></dependency><!-- 安全模块 --><dependency><groupId>com.ruoyi</groupId><artifactId>ruoyi-common-security</artifactId><version>${ruoyi.version}</version></dependency><!-- 数据脱敏 --><dependency><groupId>com.ruoyi</groupId><artifactId>ruoyi-common-sensitive</artifactId><version>${ruoyi.version}</version></dependency><!-- 权限范围 --><dependency><groupId>com.ruoyi</groupId><artifactId>ruoyi-common-datascope</artifactId><version>${ruoyi.version}</version></dependency><!-- 多数据源 --><dependency><groupId>com.ruoyi</groupId><artifactId>ruoyi-common-datasource</artifactId><version>${ruoyi.version}</version></dependency><!-- 分布式事务 --><dependency><groupId>com.ruoyi</groupId><artifactId>ruoyi-common-seata</artifactId><version>${ruoyi.version}</version></dependency><!-- 日志记录 --><dependency><groupId>com.ruoyi</groupId><artifactId>ruoyi-common-log</artifactId><version>${ruoyi.version}</version></dependency><!-- 缓存服务 --><dependency><groupId>com.ruoyi</groupId><artifactId>ruoyi-common-redis</artifactId><version>${ruoyi.version}</version></dependency><!-- 系统接口 --><dependency><groupId>com.ruoyi</groupId><artifactId>ruoyi-api-system</artifactId><version>${ruoyi.version}</version></dependency></dependencies></dependencyManagement><modules><module>ruoyi-auth</module><module>ruoyi-gateway</module><module>ruoyi-visual</module><module>ruoyi-modules</module><module>ruoyi-api</module><module>ruoyi-common</module></modules><packaging>pom</packaging><dependencies><!-- bootstrap 启动器 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-bootstrap</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.11.0</version><configuration><parameters>true</parameters><source>${java.version}</source><target>${java.version}</target><encoding>${project.build.sourceEncoding}</encoding></configuration></plugin></plugins><pluginManagement><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>${spring-boot.version}</version><executions><execution><goals><goal>repackage</goal></goals></execution></executions></plugin></plugins></pluginManagement></build><repositories><repository><id>public</id><name>aliyun nexus</name><url>https://maven.aliyun.com/repository/public</url><releases><enabled>true</enabled></releases></repository></repositories><pluginRepositories><pluginRepository><id>public</id><name>aliyun nexus</name><url>https://maven.aliyun.com/repository/public</url><releases><enabled>true</enabled></releases><snapshots><enabled>false</enabled></snapshots></pluginRepository></pluginRepositories></project>
(1)gateway服务(流控的统一入口)
①pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><groupId>com.ruoyi</groupId><artifactId>ruoyi</artifactId><version>3.6.4</version></parent><modelVersion>4.0.0</modelVersion><artifactId>ruoyi-gateway</artifactId><description>ruoyi-gateway网关模块</description><dependencies><!-- SpringCloud Gateway --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId></dependency><!-- 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><!-- Sentinel Datasource Nacos --><dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-datasource-nacos</artifactId></dependency><!-- SpringBoot Actuator --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><!-- SpringCloud Loadbalancer --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-loadbalancer</artifactId></dependency><!--验证码 --><dependency><groupId>pro.fessional</groupId><artifactId>kaptcha</artifactId></dependency><!-- RuoYi Common Redis--><dependency><groupId>com.ruoyi</groupId><artifactId>ruoyi-common-redis</artifactId></dependency><!-- Springdoc --><dependency><groupId>org.springdoc</groupId><artifactId>springdoc-openapi-starter-webflux-ui</artifactId><version>${springdoc.version}</version></dependency></dependencies><build><finalName>${project.artifactId}</finalName><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><executions><execution><goals><goal>repackage</goal></goals></execution></executions></plugin></plugins></build></project>
②bootstrap.yml配置
# Tomcat
server:port: 8080# Spring
spring: application:# 应用名称name: ruoyi-gatewayprofiles:# 环境配置active: devcloud:nacos:discovery:# 服务注册地址server-addr: 127.0.0.1:8848config:# 配置中心地址server-addr: 127.0.0.1:8848# 配置文件格式file-extension: yml# 共享配置shared-configs:- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}sentinel:# 取消控制台懒加载eager: truetransport:# 控制台地址dashboard: 127.0.0.1:8718# nacos配置持久化datasource:# 流控规则ds1:nacos:server-addr: 127.0.0.1:8848dataId: sentinel-ruoyi-gatewaygroupId: DEFAULT_GROUPdata-type: jsonrule-type: gw-flow# 接口限流规则ds2:nacos:server-addr: 127.0.0.1:8848dataId: api-groupgroupId: DEFAULT_GROUPdata-type: jsonrule-type: gw-api-group
③ruoyi-gateway-dev.yml
spring:data:redis:host: localhostport: 6379password: cloud:gateway:discovery:locator:lowerCaseServiceId: trueenabled: trueroutes:# 认证中心- id: ruoyi-authuri: lb://ruoyi-authpredicates:- Path=/auth/**filters:# 验证码处理- CacheRequestFilter- ValidateCodeFilter- StripPrefix=1# 代码生成- id: ruoyi-genuri: lb://ruoyi-genpredicates:- Path=/code/**filters:- StripPrefix=1# 定时任务- id: ruoyi-joburi: lb://ruoyi-jobpredicates:- Path=/schedule/**filters:- StripPrefix=1# 系统模块- id: ruoyi-systemuri: lb://ruoyi-systempredicates:- Path=/system/**filters:- StripPrefix=1# 文件服务- id: ruoyi-fileuri: lb://ruoyi-filepredicates:- Path=/file/**filters:- StripPrefix=1
# 安全配置
security:# 验证码captcha:enabled: truetype: math# 防止XSS攻击xss:enabled: trueexcludeUrls:- /system/notice# 不校验白名单ignore:whites:- /auth/logout- /auth/login- /auth/register- /*/v2/api-docs- /*/v3/api-docs- /csrf- /system/api- /system/rule# springdoc配置
springdoc:webjars:# 访问前缀prefix:
④启动类RuoYiGatewayApplication加入配置,否则在sentinel-dashboard主界面左侧无“API管理”菜单
// sentinel显示API分组菜单
System.setProperty("csp.sentinel.app.type", "1");
⑤新增流控规则实时查询接口类“FlowRulesController”
用于实时查询通过sentinel-dashboard界面配置后的API分组信息和流控信息,然后可以回填到nacos对应的配置文件里,即可实现流控规则的持久化。
package com.ruoyi.gateway.controller;import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition;
import com.alibaba.csp.sentinel.adapter.gateway.common.api.GatewayApiDefinitionManager;
import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule;
import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayRuleManager;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.Set;@RestController
public class FlowRulesController {@GetMapping("/rule")public Set<GatewayFlowRule> getGatewayFlowRules(){return GatewayRuleManager.getRules() ;}@GetMapping("/api")public Set<ApiDefinition> getApiGroupRules(){return GatewayApiDefinitionManager.getApiDefinitions();}
}
当在sentinel-dashboard界面配置好分组定义和限流策略后,将上面接口查到的实时限流内容,保存到nacos对应的json配置文件里,实现持久化,后面服务重启也不会丢失了,sentinel-dashboard永远显示的是最新的限流配置,包括nacos数据源里的配置和sentinel-dashboard界面的配置之和。
⑥在sentinel-dashboard界面查看或者配置流控信息
⑦jmeter测试限流
⑧共享配置 application-dev.yml
spring:autoconfigure:exclude: com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure
# feign 配置
feign:sentinel:enabled: trueokhttp:enabled: truehttpclient:enabled: falseclient:config:default:connectTimeout: 10000readTimeout: 10000compression:request:enabled: truemin-request-size: 8192response:enabled: true# 暴露监控端点
management:endpoints:web:exposure:include: '*'
(2)auth服务(鉴权)
①pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><groupId>com.ruoyi</groupId><artifactId>ruoyi</artifactId><version>3.6.4</version></parent><modelVersion>4.0.0</modelVersion><artifactId>ruoyi-auth</artifactId><description>ruoyi-auth认证授权中心</description><dependencies><!-- 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><!-- SpringBoot Web --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- SpringBoot Actuator --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><!-- RuoYi Common Security--><dependency><groupId>com.ruoyi</groupId><artifactId>ruoyi-common-security</artifactId></dependency></dependencies><build><finalName>${project.artifactId}</finalName><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><executions><execution><goals><goal>repackage</goal></goals></execution></executions></plugin></plugins></build></project>
②bootstrap.yml配置
# Tomcat
server: port: 9200# Spring
spring: application:# 应用名称name: ruoyi-authprofiles:# 环境配置active: devcloud:nacos:discovery:# 服务注册地址server-addr: 127.0.0.1:8848config:# 配置中心地址server-addr: 127.0.0.1:8848# 配置文件格式file-extension: yml# 共享配置shared-configs:- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}sentinel:# 取消控制台懒加载eager: truetransport:# 控制台地址dashboard: 127.0.0.1:8718
③ruoyi-auth-dev.yml
spring:data:redis:host: localhostport: 6379password:
④启动类RuoYiAuthApplication无需处理
⑤登录系统后,sentinel-dashboard监控如下
簇点链路的访问路径可以作为gateway服务配置限流API分组路径的参考,auth服务本身不做流控。
(3)system服务(业务)
①pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://maven.apache.org/POM/4.0.0"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><groupId>com.ruoyi</groupId><artifactId>ruoyi-modules</artifactId><version>3.6.4</version></parent><modelVersion>4.0.0</modelVersion><artifactId>ruoyi-modules-system</artifactId><description>ruoyi-modules-system系统模块</description><dependencies><!-- 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><!-- SpringBoot Actuator --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><!-- Mysql Connector --><dependency><groupId>com.mysql</groupId><artifactId>mysql-connector-j</artifactId></dependency><!-- RuoYi Common DataSource --><dependency><groupId>com.ruoyi</groupId><artifactId>ruoyi-common-datasource</artifactId></dependency><!-- RuoYi Common DataScope --><dependency><groupId>com.ruoyi</groupId><artifactId>ruoyi-common-datascope</artifactId></dependency><!-- RuoYi Common Log --><dependency><groupId>com.ruoyi</groupId><artifactId>ruoyi-common-log</artifactId></dependency><!-- RuoYi Common Swagger --><dependency><groupId>com.ruoyi</groupId><artifactId>ruoyi-common-swagger</artifactId></dependency></dependencies><build><finalName>${project.artifactId}</finalName><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><executions><execution><goals><goal>repackage</goal></goals></execution></executions></plugin></plugins></build></project>
②bootstrap.yml配置
# Tomcat
server:port: 9201# Spring
spring: application:# 应用名称name: ruoyi-systemprofiles:# 环境配置active: devcloud:nacos:discovery:# 服务注册地址server-addr: 127.0.0.1:8848config:# 配置中心地址server-addr: 127.0.0.1:8848# 配置文件格式file-extension: yml# 共享配置shared-configs:- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}sentinel:# 取消控制台懒加载eager: truetransport:# 控制台地址dashboard: 127.0.0.1:8718
③ruoyi-system-dev.yml
# spring配置
spring:data:redis:host: localhostport: 6379password: datasource:druid:stat-view-servlet:enabled: trueloginUsername: ruoyiloginPassword: 123456dynamic:druid:initial-size: 5min-idle: 5maxActive: 20maxWait: 60000connectTimeout: 30000socketTimeout: 60000timeBetweenEvictionRunsMillis: 60000minEvictableIdleTimeMillis: 300000validationQuery: SELECT 1 FROM DUALtestWhileIdle: truetestOnBorrow: falsetestOnReturn: falsepoolPreparedStatements: truemaxPoolPreparedStatementPerConnectionSize: 20filters: stat,slf4jconnectionProperties: druid.stat.mergeSql\=true;druid.stat.slowSqlMillis\=5000datasource:# 主库数据源master:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/ry-cloud?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8username: rootpassword: root# 从库数据源# slave:# username: # password: # url: # driver-class-name: # mybatis配置
mybatis:# 搜索指定包别名typeAliasesPackage: com.ruoyi.system# 配置mapper的扫描,找到所有的mapper.xml映射文件mapperLocations: classpath:mapper/**/*.xml# springdoc配置
springdoc:gatewayUrl: http://localhost:8080/${spring.application.name}api-docs:# 是否开启接口文档enabled: trueinfo:# 标题title: '系统模块接口文档'# 描述description: '系统模块接口描述'# 作者信息contact:name: RuoYiurl: https://ruoyi.vip
④启动类RuoYiSystemApplication显示API分组
// sentinel显示API分组菜单System.setProperty("csp.sentinel.app.type", "1");
⑤前端请求业务接口后,sentinel控制台显示
请求链路可以作为gate限流时,API管理分组的路径参考,业务服务本身不作限流处理。
3.总结
springcloud项目的限流主要分为两种粒度,一是路由服务级别,二是API接口级别;统一通过gateway服务配置限流策略,其他需要限流的服务,只需要保证将自身的服务集成到sentinel和sentinel-dashboard即可,让sentinel控制台感知到API接口被访问即可。
gateway服务的pom.xml必须有依赖:
<!-- 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><!-- Sentinel Datasource Nacos -->
<dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
其他业务服务限流只需下面依赖:
<!-- SpringCloud Alibaba Sentinel -->
<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>