电子商务网站开发基础,专业购物网站建设哪家好,电子商务网站建设哪家好,wordpress学习pdf文章目录 1. RuoYi 实现了对服务的限流2. 网关与普通服务的限流区别2.1. 引入的组件有差别2.2. 配置文件有差别 3. 注意事项 备注#xff1a; 1、RuoYi 网关默认只在 nacos 配置中心的 Sentinel 限流配置中配置了对“服务限流”#xff0c;而没有详细控制到限流的 URL。 2、各… 文章目录 1. RuoYi 实现了对服务的限流2. 网关与普通服务的限流区别2.1. 引入的组件有差别2.2. 配置文件有差别 3. 注意事项 备注 1、RuoYi 网关默认只在 nacos 配置中心的 Sentinel 限流配置中配置了对“服务限流”而没有详细控制到限流的 URL。 2、各个服务虽然引入了 Sentinel 相关组件但是并没有对各个具体服务做具体的 URL 限流配置 3、如果用户需要对 URL 限流请自行在服务中配置 Sentinel dashboard 连接即可。 本文主要介绍如何用Sentinel控制网关流控和网关服务与普通服务流控的不同点。
1. RuoYi 实现了对服务的限流
在 pom.xml 引入组件
!-- SpringCloud Alibaba Nacos --
dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-nacos-discovery/artifactId
/dependency!-- SpringCloud Alibaba Nacos Config --
dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-nacos-config/artifactId
/dependency!-- SpringCloud Alibaba Sentinel --
dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-sentinel/artifactId
/dependency!-- SpringCloud Alibaba Sentinel Gateway --
dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-alibaba-sentinel-gateway/artifactId
/dependency!-- 限流规则持久化到nacos --
dependencygroupIdcom.alibaba.csp/groupIdartifactIdsentinel-datasource-nacos/artifactId
/dependency配置 bootstrap.yml 文件
spring:application:name: sentinel-servicecloud: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:8848dataId: sentinel-ruoyi-gatewaygroupId: DEFAULT_GROUPdata-type: json# 网关的流控类型才是gw-flow普通是flowrule-type: gw-flowsentinel-ruoyi-gateway 文件的详细内容
在 nacos 的 sentinel-ruoyi-gateway文件中配置如如下内容完成了对 4 个微服务的“总体流控”。
[{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. 引入的组件有差别
普通需要限流服务引入的组件
!-- SpringCloud Alibaba Nacos --
dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-nacos-discovery/artifactId
/dependency!-- SpringCloud Alibaba Nacos Config --
dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-nacos-config/artifactId
/dependency!-- SpringCloud Alibaba Sentinel --
dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-sentinel/artifactId
/dependency!-- 限流规则持久化到nacos --
dependencygroupIdcom.alibaba.csp/groupIdartifactIdsentinel-datasource-nacos/artifactId
/dependency在pom.xml中添加相关依赖这里我们使用Nacos作为注册中心所以需要同时添加Nacos的依赖
有 nacos-discovery、nacos-config、alibaba-sentinel3 个组件。
网关需要限流服务引入的组件
!-- SpringCloud Alibaba Nacos --
dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-nacos-discovery/artifactId
/dependency!-- SpringCloud Alibaba Nacos Config --
dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-nacos-config/artifactId
/dependency!-- SpringCloud Alibaba Sentinel --
dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-sentinel/artifactId
/dependency!-- SpringCloud Alibaba Sentinel Gateway --
dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-alibaba-sentinel-gateway/artifactId
/dependency!-- 限流规则持久化到nacos --
dependencygroupIdcom.alibaba.csp/groupIdartifactIdsentinel-datasource-nacos/artifactId
/dependency比普通服务的限流多了一个 sentinel-gateway组件。
2.2. 配置文件有差别
下面的配置是连接 Sentinel dashboard 控制台这个配置是一样的。
spring:application:name: sentinel-servicecloud:nacos:discovery:server-addr: localhost:8848 #配置Nacos地址sentinel:transport:dashboard: localhost:8080 #配置sentinel dashboard地址port: 8719普通服务的 nacos 持久化配置
spring:cloud:sentinel:datasource:ds1:nacos:server-addr: localhost:8848dataId: ${spring.application.name}-sentinelgroupId: DEFAULT_GROUPdata-type: jsonrule-type: flow网关服务的 nacos 持久化配置
spring:cloud:sentinel: # 把流控规则nacos配置持久化datasource:ds1:nacos:server-addr: 127.0.0.1:8848dataId: sentinel-ruoyi-gatewaygroupId: DEFAULT_GROUPdata-type: json# 网关的流控类型才是gw-flow普通是flowrule-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