[TOC] >[warning] 端口变更提醒: v3.1端口是8087 | v3.2 端口改为 9000 ## jeecg-cloud-sentinel ![](https://img.kancloud.cn/70/d8/70d8fb095a6de35b9ae668356223ba02_560x671.png) ### 启动sentinel项目 ``` com.alibaba.csp.sentinel.dashboard.JeecgSentinelDashboardApplication ``` ![](https://img.kancloud.cn/c6/fd/c6fd364dc0292c089791d87f50cf81f1_479x185.png) 访问地址 [http://localhost:9000](http://localhost:9000) 账号密码:sentinel/sentinel ![](https://img.kancloud.cn/a7/9b/a79be1a5f8ec3aca0dbc412b000c294b_1146x669.png) **说明:第一次访问,内容是空。** ![](https://img.kancloud.cn/20/ff/20ff90d10cb1b357de441651393bebc8_1920x848.png) 访问任意接口,就会出现应用 ![](https://img.kancloud.cn/09/69/0969aeca22df698ee75937e701d189c0_2536x1123.png) ### 项目集成 #### 网关集成sentinel 1.jeecg-cloud-gateway中添加sentinel依赖 ~~~ <!--sentinel 限流熔点降级--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> </dependency> <!-- sentinel集成nacos作为数据源 --> <dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-datasource-nacos</artifactId> </dependency> <!--- sentinel流控链路不生效 --> <dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-web-servlet</artifactId> </dependency> ~~~ 添加sentinel配置 ~~~ #Sentinel配置 sentinel: transport: dashboard: jeecg-boot-sentinel:9000 # 支持链路限流 web-context-unify: false filter: enabled: false # 取消Sentinel控制台懒加载 eager: false datasource: #流控规则 flow: # 指定数据源名称 # 指定nacos数据源 nacos: server-addr: @config.server-addr@ # 指定配置文件 dataId: ${spring.application.name}-flow-rules # 指定分组 groupId: SENTINEL_GROUP # 指定配置文件规则类型 rule-type: flow # 指定配置文件数据格式 data-type: json #降级规则 degrade: nacos: server-addr: @config.server-addr@ dataId: ${spring.application.name}-degrade-rules groupId: SENTINEL_GROUP rule-type: degrade data-type: json #系统规则 system: nacos: server-addr: @config.server-addr@ dataId: ${spring.application.name}-system-rules groupId: SENTINEL_GROUP rule-type: system data-type: json #授权规则 authority: nacos: server-addr: @config.server-addr@ dataId: ${spring.application.name}-authority-rules groupId: SENTINEL_GROUP rule-type: authority data-type: json #热点参数 param-flow: nacos: server-addr: @config.server-addr@ dataId: ${spring.application.name}-param-flow-rules groupId: SENTINEL_GROUP rule-type: param-flow data-type: json #网关流控规则 gw-flow: nacos: server-addr: @config.server-addr@ dataId: ${spring.application.name}-flow-rules groupId: SENTINEL_GROUP rule-type: gw-flow data-type: json #API流控规则 gw-api-group: nacos: server-addr: @config.server-addr@ dataId: ${spring.application.name}-api-rules groupId: SENTINEL_GROUP rule-type: gw-api-group data-type: json ~~~ #### 非网关模块集成 以system模块为例说明如何限流某个接口 1.jeecg-cloud-system 模块中需加入如下依赖,目的是使用@SentinelResource注解 ~~~ <!-- sentinel限流熔断降级 --> <dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-core</artifactId> <version>${sentinel-core.version}</version> </dependency> ~~~ 2.jeecg-cloud-system-start 引入sentinel配置 如下所示 ~~~ server: #微服务端口 port: 7001 spring: application: name: jeecg-system cloud: #Sentinel配置 sentinel: transport: dashboard: localhost:9000 datasource: #流控规则 flow: # 指定数据源名称 # 指定nacos数据源 nacos: server-addr: @config.server-addr@ # 指定配置文件 dataId: ${spring.application.name}-flow-rules # 指定分组 groupId: SENTINEL_GROUP # 指定配置文件规则类型 rule-type: flow # 指定配置文件数据格式 data-type: json #降级规则 degrade: nacos: server-addr: @config.server-addr@ dataId: ${spring.application.name}-degrade-rules groupId: SENTINEL_GROUP rule-type: degrade data-type: json #系统规则 system: nacos: server-addr: @config.server-addr@ dataId: ${spring.application.name}-system-rules groupId: SENTINEL_GROUP rule-type: system data-type: json #授权规则 authority: nacos: server-addr: @config.server-addr@ dataId: ${spring.application.name}-authority-rules groupId: SENTINEL_GROUP rule-type: authority data-type: json #热点参数 param-flow: nacos: server-addr: @config.server-addr@ dataId: ${spring.application.name}-param-flow-rules groupId: SENTINEL_GROUP rule-type: param-flow data-type: json ~~~ ### 规则配置 #### 流控配置 [配置说明](https://github.com/alibaba/Sentinel/wiki/%E7%BD%91%E5%85%B3%E9%99%90%E6%B5%81) 1.通过RouteID(路由名称)限制整个服务模块的接口 ![](https://img.kancloud.cn/94/7a/947a0553225f0d50714ee4b7ee477165_1208x758.png) 2.打开接口文档进行测试,点击system模块中任意接口 ![](https://img.kancloud.cn/60/b5/60b51b7a602c02aeb9971c8712c5201a_1522x656.gif) 2.通过API分组限制某一个或者某一组接口 ![](https://img.kancloud.cn/f3/bc/f3bcdd9624e8f9de43d9ddf1edd10b10_1196x554.png) ![](https://img.kancloud.cn/1b/b5/1bb56976f9947777334ed5eaff65e908_1200x710.png) 测试同上 #### 熔断配置 [配置说明](https://github.com/alibaba/Sentinel/wiki/%E7%86%94%E6%96%AD%E9%99%8D%E7%BA%A7) ![](https://img.kancloud.cn/9f/8a/9f8a8a95e5777f7b9c5e0975342bf979_1194x611.png) Sentinel 提供以下几种熔断策略: * 慢调用比例 (`SLOW_REQUEST_RATIO`):选择以慢调用比例作为阈值,需要设置允许的慢调用 RT(即最大的响应时间),请求的响应时间大于该值则统计为慢调用。当单位统计时长(`statIntervalMs`)内请求数目大于设置的最小请求数目,并且慢调用的比例大于阈值,则接下来的熔断时长内请求会自动被熔断。经过熔断时长后熔断器会进入探测恢复状态(HALF-OPEN 状态),若接下来的一个请求响应时间小于设置的慢调用 RT 则结束熔断,若大于设置的慢调用 RT 则会再次被熔断。 * 异常比例 (`ERROR_RATIO`):当单位统计时长(`statIntervalMs`)内请求数目大于设置的最小请求数目,并且异常的比例大于阈值,则接下来的熔断时长内请求会自动被熔断。经过熔断时长后熔断器会进入探测恢复状态(HALF-OPEN 状态),若接下来的一个请求成功完成(没有错误)则结束熔断,否则会再次被熔断。异常比率的阈值范围是`[0.0, 1.0]`,代表 0% - 100%。 * 异常数 (`ERROR_COUNT`):当单位统计时长内的异常数目超过阈值之后会自动进行熔断。经过熔断时长后熔断器会进入探测恢复状态(HALF-OPEN 状态),若接下来的一个请求成功完成(没有错误)则结束熔断,否则会再次被熔断。 快速点击触发熔断 ![](https://img.kancloud.cn/a3/cd/a3cdc7598732c647b6348fbba70079d4_1220x710.png) **注:gateway层面只能针对网关进行路由熔断降级。 不能针对@SentinelResource(value ="test")中的tes限流降级** #### 系统规则 [配置说明](https://github.com/alibaba/Sentinel/wiki/%E7%B3%BB%E7%BB%9F%E8%87%AA%E9%80%82%E5%BA%94%E9%99%90%E6%B5%81) 系统保护规则是从应用级别的入口流量进行控制,从单台机器的 load、CPU 使用率、平均 RT、入口 QPS 和并发线程数等几个维度监控应用指标,让系统尽可能跑在最大吞吐量的同时保证系统整体的稳定性。 系统保护规则是应用整体维度的,而不是资源维度的,并且**仅对入口流量生效**。入口流量指的是进入应用的流量(`EntryType.IN`),比如 Web 服务或 Dubbo 服务端接收的请求,都属于入口流量。 系统规则支持以下的模式: * **Load 自适应**(仅对 Linux/Unix-like 机器生效):系统的 load1 作为启发指标,进行自适应系统保护。当系统 load1 超过设定的启发值,且系统当前的并发线程数超过估算的系统容量时才会触发系统保护(BBR 阶段)。系统容量由系统的`maxQps * minRt`估算得出。设定参考值一般是`CPU cores * 2.5`。 * **CPU usage**(1.5.0+ 版本):当系统 CPU 使用率超过阈值即触发系统保护(取值范围 0.0-1.0),比较灵敏。 * **平均 RT**:当单台机器上所有入口流量的平均 RT 达到阈值即触发系统保护,单位是毫秒。 * **并发线程数**:当单台机器上所有入口流量的并发线程数达到阈值即触发系统保护。 * **入口 QPS**:当单台机器上所有入口流量的 QPS 达到阈值即触发系统保护。 一下配置表示全局所有入口每秒只能通过1次 ![](https://img.kancloud.cn/0a/09/0a09a6cb00c67047fd10192dcf3df40f_1201x597.png) 测试如下 ![](https://img.kancloud.cn/b2/07/b2078da09f1d579e461283488754009a_1234x704.png) #### 热点规则 何为热点?热点即经常访问的数据。很多时候我们希望统计某个热点数据中访问频次最高的 Top K 数据,并对其访问进行限制。比如: * 商品 ID 为参数,统计一段时间内最常购买的商品 ID 并进行限制 * 用户 ID 为参数,针对一段时间内频繁访问的用户 ID 进行限制 热点参数限流会统计传入参数中的热点参数,并根据配置的限流阈值与模式,对包含热点参数的资源调用进行限流。热点参数限流可以看做是一种特殊的流量控制,仅对包含热点参数的资源调用生效。 [配置说明](https://github.com/alibaba/Sentinel/wiki/%E7%83%AD%E7%82%B9%E5%8F%82%E6%95%B0%E9%99%90%E6%B5%81) 以sysetm模块职务列表为例说明 1.添加 @SentinelResource(value = "position")注解 value名字就是资源名如下图 ![](https://img.kancloud.cn/b0/35/b035629da69ab4813c3ce1d26393fc2f_1070x462.png) 2.添加热点规则 ![](https://img.kancloud.cn/ba/5a/ba5a3bc51923c90b63b29418c9fecfd2_1214x674.png) 或者通过热点规则菜单添加 ![](https://img.kancloud.cn/5a/f8/5af89cb4290652f0cacb0cbb2af3e480_1215x645.png) 3.测试热点规则,此处设置参数索引为0,仅做测试,具体场景需要结合真实业务场景 ![](https://img.kancloud.cn/bd/79/bd7969e1c2708d745bc88e26f24f943a_1126x691.png) 4.编写热点规则拦截器,系统内置规则代码如下(当不满足业务规则时需自行修改拦截规则) ``` /** * sentinel ip授权规则拦截器(黑名单白名单),改规则支持参数和IP拦截模式,当参数为空时走ip拦截模式 * * @author zyf */ @Component public class DefaultRequestOriginParser implements RequestOriginParser { @Override public String parseOrigin(HttpServletRequest request) { //基于请求参数,origin对应授权规则中的流控应用名称,也可通过getHeader传参 String origin = request.getParameter("origin"); if (StringUtils.isNotEmpty(origin)) { return origin; } else { //当参数为空使用ip拦截模式 String ip = IpUtils.getIpAddr(request); return ip; } } } ``` #### 授权规则 很多时候,我们需要根据调用来源来判断该次请求是否允许放行,这时候可以使用 Sentinel 的来源访问控制(黑白名单控制)的功能。来源访问控制根据资源的请求来源(`origin`)限制资源是否通过,若配置白名单则只有请求来源位于白名单内时才可通过;若配置黑名单则请求来源位于黑名单时不通过,其余的请求通过。 规则配置 来源访问控制规则(`AuthorityRule`)非常简单,主要有以下配置项: * `resource`:资源名,即限流规则的作用对象。 * `limitApp`:对应的黑名单/白名单,不同 origin 用`,`分隔,如`appA,appB`。 * `strategy`:限制模式,`AUTHORITY_WHITE`为白名单模式,`AUTHORITY_BLACK`为黑名单模式,默认为白名单模式。 1.添加授权规则 ![](https://img.kancloud.cn/14/4f/144f1d0ab3e13b787fab87cd5cc5f4fc_1202x646.png) ![](https://img.kancloud.cn/63/e0/63e0c70ca31afd3586c5d37520c9c74a_990x503.png) 2.测试授权规则 当origin=app1时如下图 ![](https://img.kancloud.cn/51/00/5100cf8987dbbadc45d75f96bed765ae_1207x758.png) 当origin=app2时如下图 ![](https://img.kancloud.cn/d8/7e/d87ec6c9b42b3719d822a14ad1d64378_913x611.png) **docker 部署脚本:** `bladex/sentinel-dashboard`, 默认账号密码为 \[sentinel sentinel\] ``` version: '3.5' services: sentinel-dashboard: image: bladex/sentinel-dashboard:1.8.0 container_name: sentinel-dashboard #account and password: [sentinel sentinel] #修改默认密码为 123456 environment: - sentinel.dashboard.auth.password=123456 restart: always ports: - 8858:8858 ``` * 官方文档:[https://sentinelguard.io/zh-cn/docs/dashboard.html](https://sentinelguard.io/zh-cn/docs/dashboard.html) * 推荐学习视频: [Sentinel视频教程](https://www.bilibili.com/video/BV12A411E7aX?p=16)