在实现限流和熔断时,每次重启应用都会导致Dashboard中的规则丢失,这显然是无法满足正常使用的。这时因为Sentinel默认将规则存储在内存中,因此客户端一旦重启,Sentinel的数据规则就会丢失。
Sentinel提供了4种持久化机制:

  • 本地文件
  • Apollo
  • Nacos
  • Zookeeper

那么这一节就利用Nacos来将规则进行持久化处理,保证每次重启应用后都能保留设置的规则。
本文仅以接口限流为例,熔断的操作方式与限流无异,因此不做赘述。

使用Nacos存储限流规则

前提:启动NacosSentinel Dashboard
第一步:创建模块alibaba-sentinel-datasource-nacos
第二步:修改pom.xml,引入Sentinel和Nacos存储拓展:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <artifactId>spring-cloud-learn</artifactId>
  7. <groupId>com.example</groupId>
  8. <version>0.0.1-SNAPSHOT</version>
  9. </parent>
  10. <modelVersion>4.0.0</modelVersion>
  11. <artifactId>alibaba-sentinel-datasource-nacos</artifactId>
  12. <properties>
  13. <java.version>1.8</java.version>
  14. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  15. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  16. <spring-boot.version>2.3.7.RELEASE</spring-boot.version>
  17. <spring-cloud-alibaba.version>2.2.2.RELEASE</spring-cloud-alibaba.version>
  18. </properties>
  19. <dependencyManagement>
  20. <dependencies>
  21. <dependency>
  22. <groupId>org.springframework.boot</groupId>
  23. <artifactId>spring-boot-dependencies</artifactId>
  24. <version>${spring-boot.version}</version>
  25. <type>pom</type>
  26. <scope>import</scope>
  27. </dependency>
  28. <dependency>
  29. <groupId>com.alibaba.cloud</groupId>
  30. <artifactId>spring-cloud-alibaba-dependencies</artifactId>
  31. <version>${spring-cloud-alibaba.version}</version>
  32. <type>pom</type>
  33. <scope>import</scope>
  34. </dependency>
  35. </dependencies>
  36. </dependencyManagement>
  37. <dependencies>
  38. <dependency>
  39. <groupId>org.springframework.boot</groupId>
  40. <artifactId>spring-boot-starter-web</artifactId>
  41. </dependency>
  42. <dependency>
  43. <groupId>com.alibaba.cloud</groupId>
  44. <artifactId>spring-cloud-alibaba-sentinel-datasource</artifactId>
  45. </dependency>
  46. <dependency>
  47. <groupId>com.alibaba.csp</groupId>
  48. <artifactId>sentinel-datasource-nacos</artifactId>
  49. <version>1.8.1</version>
  50. </dependency>
  51. <dependency>
  52. <groupId>com.alibaba.cloud</groupId>
  53. <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
  54. </dependency>
  55. <dependency>
  56. <groupId>org.projectlombok</groupId>
  57. <artifactId>lombok</artifactId>
  58. <optional>true</optional>
  59. </dependency>
  60. <dependency>
  61. <groupId>org.springframework.boot</groupId>
  62. <artifactId>spring-boot-starter-test</artifactId>
  63. <scope>test</scope>
  64. <exclusions>
  65. <exclusion>
  66. <groupId>org.junit.vintage</groupId>
  67. <artifactId>junit-vintage-engine</artifactId>
  68. </exclusion>
  69. </exclusions>
  70. </dependency>
  71. </dependencies>
  72. </project>

第三步:修改application.properties,添加配置信息:

  1. spring.application.name=alibaba-sentinel-datasource-nacos
  2. server.port=8003
  3. # sentinel dashboard
  4. spring.cloud.sentinel.transport.dashboard=localhost:9260
  5. # sentinel datasource: nacos
  6. spring.cloud.sentinel.datasource.ds.nacos.server-addr=localhost:8848
  7. spring.cloud.sentinel.datasource.ds.nacos.username=nacos
  8. spring.cloud.sentinel.datasource.ds.nacos.password=nacos
  9. spring.cloud.sentinel.datasource.ds.nacos.data-id=${spring.application.name}-sentinel
  10. spring.cloud.sentinel.datasource.ds.nacos.group-id=DEFAULT_GROUP
  11. spring.cloud.sentinel.datasource.ds.nacos.data-type=json
  12. spring.cloud.sentinel.datasource.ds.nacos.rule-type=flow

其中:

  • spring.cloud.sentinel.transport.dashboard:Sentinel Dashboard的访问地址
  • spring.cloud.sentinel.datasource.ds.nacos.server-addr: Nacos的访问地址
  • spring.cloud.sentinel.datasource.ds.nacos.username:Nacos的访问用户名
  • spring.cloud.sentinel.datasource.ds.nacos.password:Nacos的访问密码
  • spring.cloud.sentinel.datasource.ds.nacos.group-id:Nacos存储规则的Group
  • spring.cloud.sentinel.datasource.ds.nacos.data-id:Nacos存储规则的Data ID
  • spring.cloud.sentinel.datasource.ds.nacos.data-type:Nacos存储规格的配置格式
  • spring.cloud.sentinel.datasource.ds.nacos.rule-type: Nacos存储的规则类型

第四步:创建应用入口类,定义一个REST接口:

  1. package com.demo;
  2. import lombok.extern.slf4j.Slf4j;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.web.bind.annotation.GetMapping;
  6. import org.springframework.web.bind.annotation.RestController;
  7. @Slf4j
  8. @SpringBootApplication
  9. public class AlibabaSentinelDatasourceNacosApplication {
  10. public static void main(String[] args) {
  11. SpringApplication.run(AlibabaSentinelDatasourceNacosApplication.class, args);
  12. }
  13. @RestController
  14. static class EchoController {
  15. @GetMapping("echo")
  16. public String echo() {
  17. return "echo controller";
  18. }
  19. }
  20. }

第五步:进入Nacos控制台,创建限流规则,如下图:
image.png
其中:

  • Data IDalibaba-sentinel-datasource-nacos-sentinel,与配置文件的spring.cloud.sentinel.datasource.ds.nacos.data-id一致
  • 配置格式JSON
  • 配置内容

    1. [
    2. {
    3. "resource": "/echo",
    4. "limitApp": "default",
    5. "grade": 1,
    6. "count": 5,
    7. "strategy": 0,
    8. "controlBehavior": 0,
    9. "clusterMode": false
    10. }
    11. ]

    其中:

  • resource:资源名,即限流规则的作用对象

  • limitApp:流控针对的调用来源,若为 default 则不区分调用来源
  • grade:限流阈值类型(QPS 或并发线程数);0代表根据并发数量来限流,1代表根据QPS来进行流量控制
  • count:限流阈值
  • strategy:调用关系限流策略
  • controlBehavior:流量控制效果(直接拒绝、Warm Up、匀速排队)
  • clusterMode:是否为集群模式

第六步:启动应用,可以看到控制台中输出了Nacos的记载信息:

  1. 2021-04-20 16:25:22.120 INFO 75095 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'spring.cloud.sentinel-com.alibaba.cloud.sentinel.SentinelProperties' of type [com.alibaba.cloud.sentinel.SentinelProperties] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
  2. 2021-04-20 16:25:22.122 INFO 75095 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'com.alibaba.cloud.sentinel.custom.SentinelAutoConfiguration' of type [com.alibaba.cloud.sentinel.custom.SentinelAutoConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

通过curl访问一次/echo接口,打开Sentinel Dashboard,可以看到流控规则中出现了一条记录,如下图:
image.png
这时前往Nacos配置中心,修改限流阈值为3,点击发布,再刷新Sentinel Dashboard,可以看到流控规则的阈值自动刷新了。
image.png
修改完成后,使用Postman Collection Runner请求接口,检查限流有没有生效:
image.png

说明

  • Sentinel控制台中修改规则:仅存在于服务的内存中,不会修改Nacos中的配置值,重启后恢复原来的值。
  • Nacos控制台中修改规则:服务的内存中规则会更新,Nacos中持久化规则也会更新,重启后依然保持。

    代码示例

  • Github:

  • Gitee: