使用Nacos存储限流规则

Sentinel自身就支持了多种不同的数据源来持久化规则配置,目前包括以下几种方式:

  • 文件配置
  • Nacos配置
  • ZooKeeper配置
  • Apollo配置

    准备工作

    下面我们将同时使用到Nacos和Sentinel Dashboard,所以可以先把Nacos和Sentinel Dashboard启动起来。
    默认配置下启动后,它们的访问地址(后续会用到)为:

  • Nacos:http://localhost:8848/

  • Sentinel Dashboard:http://localhost:8081/

    应用配置

    第一步:在Spring Cloud应用的pom.xml中引入Spring Cloud Alibaba的Sentinel模块和Nacos存储扩展:
    1. <dependencies>
    2. <dependency>
    3. <groupId>org.springframework.boot</groupId>
    4. <artifactId>spring-boot-starter-web</artifactId>
    5. </dependency>
    6. <dependency>
    7. <groupId>org.springframework.cloud</groupId>
    8. <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
    9. </dependency>
    10. <dependency>
    11. <groupId>com.alibaba.csp</groupId>
    12. <artifactId>sentinel-datasource-nacos</artifactId>
    13. <version>1.5.2</version>
    14. </dependency>
    15. </dependencies>
    第二步:在Spring Cloud应用中添加配置信息: ```xml spring.application.name=alibaba-sentinel-datasource-nacos server.port=8003

sentinel dashboard

spring.cloud.sentinel.transport.dashboard=localhost:8081

sentinel datasource nacos

spring.cloud.sentinel.datasource.ds.nacos.server-addr=localhost:8848

注意这里的dataId也就是nacos上面配置的dataId是加了-sentinel的

spring.cloud.sentinel.datasource.ds.nacos.dataId=${spring.application.name}-sentinel spring.cloud.sentinel.datasource.ds.nacos.groupId=DEFAULT_GROUP 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.groupId:nacos中存储规则的groupId
- spring.cloud.sentinel.datasource.ds.nacos.dataId:nacos中存储规则的dataId
- spring.cloud.sentinel.datasource.ds.nacos.rule-type:该参数是spring cloud alibaba升级到0.2.2之后增加的配置,用来定义存储的规则类型。所有的规则类型可查看枚举类:org.springframework.cloud.alibaba.sentinel.datasource.RuleType,每种规则的定义格式可以通过各枚举值中定义的规则对象来查看,比如限流规则可查看:com.alibaba.csp.sentinel.slots.block.flow.FlowRule

这里对于dataId使用了${spring.application.name}变量,这样可以根据应用名来区分不同的规则配置。<br />**注意**:由于版本迭代关系,Github Wiki中的文档信息不一定适用所有版本。比如:在这里适用的0.2.1版本中,并没有spring.cloud.sentinel.datasource.ds2.nacos.rule-type这个参数。所以,可以通过查看org.springframework.cloud.alibaba.sentinel.datasource.config.DataSourcePropertiesConfiguration和org.springframework.cloud.alibaba.sentinel.datasource.config.NacosDataSourceProperties两个类来分析具体的配置内容,会更为准确。<br />**第三步**:创建应用主类,并提供一个rest接口,比如:
```java
@SpringBootApplication
public class SentinelNacosApplication {

    public static void main(String[] args) {
        SpringApplication.run(SentinelNacosApplication.class, args);
    }

    @RestController
    static class TestController {
        @GetMapping("/server")
        public String hello(){
            return "hello";
        }
    }
}

第四步:Nacos中创建限流规则的配置,比如:Sentinel使用Nacos存储规则 - 图1

[
    {
        "resource": "/server",
        "limitApp": "default",
        "grade": 1,
        "count": 5,
        "strategy": 0,
        "controlBehavior": 0,
        "clusterMode": false
    }
]

可以看到上面配置规则是一个数组类型,数组中的每个对象是针对每一个保护资源的配置对象,每个对象中的属性解释如下:

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

第五步:启动应用。如果一些顺利,可以看到类似下面的日志,代表已经成功从Nacos加载了一条限流规则:

2019-04-16 14:24:42.919  INFO 89484 --- [           main] o.s.c.a.s.c.SentinelDataSourceHandler    : [Sentinel Starter] DataSource ds-sentinel-nacos-datasource start to loadConfig
2019-04-16 14:24:42.938  INFO 89484 --- [           main] o.s.c.a.s.c.SentinelDataSourceHandler    : [Sentinel Starter] DataSource ds-sentinel-nacos-datasource load 1 FlowRule

通过快速访问localhost:8003/server接口,超过阈值时会返回Blocked by Sentinel (flow limiting)

此时,在Sentinel Dashboard中就可以看到当前我们启动的alibaba-sentinel-datasource-nacos服务。点击左侧菜单中的流控规则,可以看到已经存在一条记录了,具体如下:
Sentinel使用Nacos存储规则 - 图2