SpringCloud Alibaba 实战教程6-sentinel限流持久化配置nacos

2020-12-22 15:05:41 浏览数 (1)

一、引入pom

代码语言:javascript复制
<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-datasource-nacos</artifactId>
</dependency>

二、配置文件添加sentinel数据源

代码语言:javascript复制
server:
  port: 8020
spring:
  application:
    name: order-service
  cloud:
    nacos:
      config:
        server-addr: 192.168.2.6:8001
        group: DEFAULT_GROUP
        prefix: order-service
        file-extension: yaml
        namespace: 9bb83945-f4a3-4839-8d3b-363f1b78db36
      discovery:
        server-addr: 192.168.2.6:8001
    sentinel:
      transport:
        dashboard: 192.168.2.6:8858 #配置Sentinel dashboard地址
        # 默认就是8719端口,如果占用会依次 1开始扫描
        # 取消延迟加载
        eager: true
      datasource:
        ds1:
          nacos:
            server-addr: 192.168.2.6:8001
            namespace: 9bb83945-f4a3-4839-8d3b-363f1b78db36
            dataId: ${spring.application.name}-sentinel
            group: DEFAULT_GROUP
            data-type: json
            rule-type: flow

三、nacos中添加规则

代码语言:javascript复制
[
    {
        "resource": "getByOrderNo",
        "limitApp": "default",
        "grade": 1,
        "count": 3,
        "strategy": 0,
        "controlBehavior": 0,
        "clusterMode": false
    }
]

resource:资源名称 limitApp:来源应用 grade:阀值类型,0-线程数,1-qps count:单机阀值 strategy:流控模式,0-直接,1-关联,2-链路 controlBehavior:流控效果,0-快速失败,1-warm up,2-排队等待 clusterMode:是否集群

四、查看sentinel流控规则

五、接口调用测试

0 人点赞