二、Sentinel 熔断 与 限流

本文详细介绍了Sentinel的安装与使用,包括服务雪崩、服务降级、服务熔断和服务限流等概念,并通过实际工程演示了Sentinel在流控规则、降级规则、热点规则等方面的配置与应用。同时,对比了Sentinel与Hystrix的熔断框架,并探讨了规则持久化,确保应用重启后规则仍然有效。最后,文章提到了Sentinel对Feign的支持以及如何配置Feign的熔断处理。
摘要由CSDN通过智能技术生成

一、Sentinel 熔断 与 限流


1. Sentinel 介绍

在这里插入图片描述

  • 服务雪崩
  • 服务降级
  • 服务熔断
  • 服务限流

2. Sentinel 安装


3. 演示工程


3.1 新建 Module cloudalibaba-sentinel-service8401

3.2 POM
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>cloud-2020</artifactId>
        <groupId>com.qs.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloudalibaba-sentinel-service8401</artifactId>

    <dependencies>
        <!--sentinel-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>
        <!--nacos-discovery-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <!--sentinel-datasource-nacos 后续做持久化用到-->
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-datasource-nacos</artifactId>
        </dependency>

        <!--openfeign-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

        <!--web + actuator-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!--hutool-->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>4.6.3</version>
        </dependency>
        <!--自定义API通用包-->
        <dependency>
            <groupId>com.qs.springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>
        <!--热部署-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

3.3 YML
server:
  port: 8401

spring:
  application:
    name: cloudalibaba-sentinel-service
  cloud:
    nacos:
      discovery:
        # Nacos服务注册中心地址
        server-addr: localhost:8848
    sentinel:
      transport:
        # 配置`Sentinel dashboard`地址
        dashboard: localhost:8080
        # 默认`8719`端口,假如被占用会自动从`8719`开始依次+1扫描,直至找到未被占用的端口
        port: 8719

management:
  endpoints:
    web:
      exposure:
        include: '*'

3.4 主启动
@EnableDiscoveryClient
@SpringBootApplication
public class SentinelMain8401 {

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

3.5 FlowLimitController
@RestController
public class FlowLimitController {

    @GetMapping("testA")
    public String testA() {
        return "testA";
    }

    @GetMapping("testB")
    public String testB() {
        return "testB";
    }
}

3.6 测试

http://localhost:8401/testA
http://localhost:8401/testB
在这里插入图片描述


4. 流控规则

在这里插入图片描述
在这里插入图片描述


4.1 流控模式 > 直接(默认)

在这里插入图片描述


  • 每秒钟请求超过 1 次。
    在这里插入图片描述

4.2 流控模式 > 关联
  • 当关联的资源达到阈值时,就限流自己。
    在这里插入图片描述

  • 配置请求 testB。
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

  • 当与 A 关联的资源 B 达到阀值后,就限流 A 自己(B 惹事,A 挂了)。
    在这里插入图片描述

4.3 流控模式 > 链路
  • 多个请求调用了同一个微服务。

4.4 流控效果 > 快速失败(默认的流控处理)
  • Blocked by Sentinel (Flow Limiting)。
    源码 com.alibaba.csp.sentinel.slots.block.flow.controller.DefaultController

4.5 流控效果 > Warm Up(预热)
  • 流量控制限流 冷启动
  • 公式:阈值 除以 coldFactor(默认值为 3),经过预热时长后才会达到阈值。
  • 默认 coldFactor 为 3,即请求 QPS 从 threshold / 3 开始,经预热时长逐渐升至设定的 QPS 阈值。
    源码 com.alibaba.csp.sentinel.slots.block.flow.controller.WarmUpController
    在这里插入图片描述

  • 默认 coldFactor 为 3,即请求 QPS 从(threshold / 3)开始,经多少预热时长才逐渐升至设定的 QPS。
  1. 案例,阀值为10 + 预热时长设置5秒。
    在这里插入图片描述
  • 如:秒杀系统在开启的瞬间,会有很多流量上来,很有可能把系统打死。
    预热方式就是为了保护系统,可慢慢的把流量放进来,慢慢的把阀值增长到设置的阀值。

4.6 流控效果 > 排队等待
  • 流量控制
  • 匀速排队,让请求以均匀的速度通过,阀值类型必须设成 QPS,否则无效。
    源码 com.alibaba.csp.sentinel.slots.block.flow.controller.RateLimiterController
    在这里插入图片描述
    在这里插入图片描述

5. 降级规则

  • 熔断降级
  • Sentinel 熔断降级 会在调用链路中某个资源出现不稳定状态时(例如:调用超时 或 异常比例 升高),对这个资源的调用进行限制,让请求快速失败,避免影响到其它的资源而导致级联错误。
  • 当资源被降级后,在接下来的降级时间窗口之内,对该资源的调用都自动熔断(默认行为是抛出 DegradeException)。
  • Sentinel 的断路器是没有半开状态的。
  1. 半开的状态系统自动去检测是否请求有异常,没有异常就关闭断路器恢复使用。
  2. 有异常则继续打开断路器不可用(具体可以参考 Hystrix)。
    在这里插入图片描述
    在这里插入图片描述

5.1 RT 平均响应时间
  • 代码。
    @GetMapping("/testC")
    public String testC() {
        // 线程暂停`1`秒钟
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "testD,测试RT";
    }

  • 配置。
    在这里插入图片描述

  • JMeter 压测。
    在这里插入图片描述
    在这里插入图片描述
  • 结论。
  1. 永远一秒钟打进来 10 个线程(大于 5 个了)调用 testC,我们希望 200 毫秒处理完本次任务。
  2. 如果超过 200 毫秒还没处理完,在未来 1 秒钟的时间窗口内,断路器打开(保险丝跳闸)微服务不可用,保险丝跳闸断电了。
    在这里插入图片描述

5.2 异常比例

在这里插入图片描述
在这里插入图片描述


  • 代码。
@GetMapping("/testD")
public String testD() {
    int age = 10 / 0;
    return "testD,异常比例、异常数";
}

  • 配置。
    在这里插入图片描述

  • JMeter 压测。
    在这里插入图片描述
  • 结论
  1. 开启 JMeter 后,直接高并发发送请求,多次调用达到我们的配置条件了。
  2. 断路器开启(保险丝跳闸),微服务不可用了,不再报错 Error 而是 服务降级 了。
    在这里插入图片描述
  • 停止 JMeter,恢复错误。
    在这里插入图片描述

5.3 异常数

在这里插入图片描述
在这里插入图片描述


  • 代码。
@GetMapping("/testD")
public String testD() {
    int age = 10 / 0;
    return "testD,异常比例、异常数";
}

  • 配置。
    在这里插入图片描述

  • 结论。
  1. 请求 5 次,熔断降级。
    在这里插入图片描述

6. 热点规则

  • 热点参数限流
  • 热点即经常访问的数据,很多时候我们希望 统计 或者 限制,某个热点数据中访问频次最高的 TopN 数据,并对其访问进行限流或者其它操作。

6.1 代码
@GetMapping("/testHotKey")
@SentinelResource(value = "testHotKey", blockHandler = "dealHandler_testHotKey")
public String testHotKey(@RequestParam(value = "p1", required = false) String p1,
                         @RequestParam(value = "p2", required = false) String p2) {
    return "testHotKey";
}


public String dealHandler_testHotKey(String p1, String p2, BlockException exception) {
    return "dealHandler_testHotKey";
}

6.2 配置

在这里插入图片描述


6.3 测试

6.4 参数例外项

7. 系统规则


8. @SentinelResource


8.1 按资源名称限流
  • 代码。
@GetMapping("/byResource")
@SentinelResource(value = "byResource", blockHandler = "handleException")
public CommonResult byResource() {
    // 按资源名称限流测试OK
    return CommonResult.successOfData(new Payment(2020L, "byResource"));
}

public CommonResult handleException(BlockException exception) {
    return CommonResult.failOfCodeAndMsg(ResultCode.BAD_REQUEST.getCode(), "服务不可用");
}


8.2 按照 Url 地址限流
  • 代码。
@GetMapping("/rateLimit/byUrl")
@SentinelResource(value = "byUrl")
public CommonResult byUrl() {
    // 按url限流测试OK
    return CommonResult.successOfData(new Payment(2020L, "按url限流测试OK"));
}

  • 配置。
    在这里插入图片描述


8.3 上面兜底方案面临的问题
  1. 系统默认的,没有体现我们自己的业务要求。
  2. 依照现有条件,我们自定义的处理方法又和业务代码耦合在一块,不直观。
  3. 每个业务方法都添加一个兜底的,那代码膨胀加剧。
  4. 全局统一的处理方法没有体现。

8.4 客户自定义限流处理逻辑
  • CustomerBlockHandler
public class CustomerBlockHandler {

    public static CommonResult handleException(BlockException exception) {
        return CommonResult.successOfData("客户自定义限流处理逻辑,handleException");
    }

    public static CommonResult handleException2(BlockException exception) {
        return CommonResult.successOfData("客户自定义限流处理逻辑,handleException2");
    }

}

  • RateLimitController
@GetMapping("/rateLimit/customerBlockHandler")
@SentinelResource(value = "customerBlockHandler",
        blockHandlerClass = CustomerBlockHandler.class, blockHandler = "handleException2")
public CommonResult customerBlockHandler() {
    return CommonResult.successOfData("按客户自定义限流处理逻辑");
}

  • 配置。
    在这里插入图片描述


9. 服务熔断


9.1 Ribbon 系列

9.1.1 新建 Module cloudalibaba-provider-payment9003/9004

9.1.2 POM
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>cloud-2020</artifactId>
        <groupId>com.qs.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloudalibaba-provider-payment9003</artifactId>

    <dependencies>
        <!--nacos-discovery-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

        <!--web + actuator-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!--自定义API通用包-->
        <dependency>
            <groupId>com.qs.springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>
        <!--热部署-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

9.1.3 YML
server:
  port: 9003

spring:
  application:
    name: nacos-payment-provider
  cloud:
    nacos:
      discovery:
        # 配置`Nacos`地址
        server-addr: localhost:8848

management:
  endpoints:
    web:
      exposure:
        include: '*'

9.1.4 主启动
@SpringBootApplication
@EnableDiscoveryClient
public class PaymentMain9003 {

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

9.1.5 PaymentController
@RestController
public class PaymentController {

    @Value("${server.port}")
    private String serverPort;

    private static HashMap<Long, Payment> hashMap = new HashMap<>();

    static {
        hashMap.put(1L, new Payment(1L, "28a8c1e3bc2742d8848569891fb42181"));
        hashMap.put(2L, new Payment(2L, "bba8c1e3bc2742d8848569891ac32182"));
        hashMap.put(3L, new Payment(3L, "6ua8c1e3bc2742d8848569891xt92183"));
    }

    @GetMapping(value = "/paymentSQL/{id}")
    public CommonResult paymentSQL(@PathVariable("id") Long id) {
        Payment payment = hashMap.get(id);
        return new CommonResult(ResultCode.SUCCESS.getCode(), serverPort, payment);
    }
}

9.1.6 测试

http://localhost:9003/paymentSQL/1
http://localhost:9004/paymentSQL/1


9.1.7 新建 Module cloudalibaba-consumer-nacos-order84

9.1.8 POM
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>cloud-2020</artifactId>
        <groupId>com.qs.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloudalibaba-consumer-nacos-order84</artifactId>

    <dependencies>
        <!--nacos-discovery-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <!--sentinel-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>
        <!--web + actuator-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!--自定义API通用包-->
        <dependency>
            <groupId>com.qs.springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>
        <!--热部署-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

9.1.9 YML
server:
  port: 84

spring:
  application:
    name: nacos-order-consumer
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
    sentinel:
      transport:
        # 配置`Sentinel dashboard`地址
        dashboard: localhost:8080
        # 默认`8719`端口,假如被占用会自动从`8719`开始依次`+1`扫描,直至找到未被占用的端口
        port: 8719

# 消费者将要去访问的微服务名称(注册成功进`nacos`的微服务提供者)
service-url:
  nacos-user-service: http://nacos-payment-provider

9.1.10 主启动
@EnableDiscoveryClient
@SpringBootApplication
public class OrderNacosMain84 {

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

9.1.11 ApplicationContextConfig
@Configuration
public class ApplicationContextConfig {

    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }
}

9.1.12 CircleBreakerController
@RestController
@Slf4j
public class CircleBreakerController {

    private static final String SERVICE_URL = "http://nacos-payment-provider";
    @Resource
    private RestTemplate restTemplate;

    @RequestMapping("/consumer/fallback/{id}")
    @SentinelResource(value = "fallback") // 没有任何配置
    public CommonResult<Payment> fallback(@PathVariable Long id) {
        CommonResult<Payment> result = restTemplate.getForObject(SERVICE_URL + "/paymentSQL/" + id, CommonResult.class, id);

        if (id == 4) {
            throw new IllegalArgumentException("IllegalArgumentException,非法参数异常");
        } else if (result.getData() == null) {
            throw new NullPointerException("NullPointerException,空指针异常");
        }

        return result;
    }

}

9.1.13 测试

http://localhost:84/consumer/fallback/1
http://localhost:84/consumer/fallback/4 error
http://localhost:84/consumer/fallback/5 error


9.1.14 配置 fallback 负责业务异常
@RestController
@Slf4j
public class CircleBreakerController {

    private static final String SERVICE_URL = "http://nacos-payment-provider";
    @Resource
    private RestTemplate restTemplate;

    @RequestMapping("/consumer/fallback/{id}")
    // 没有任何配置
//    @SentinelResource(value = "fallback") 
	// 配置`fallback`负责业务异常
    @SentinelResource(value = "fallback", fallback = "handlerFallback") 
    public CommonResult<Payment> fallback(@PathVariable Long id) {
        CommonResult<Payment> result = restTemplate.getForObject(SERVICE_URL + "/paymentSQL/" + id, CommonResult.class, id);

        if (id == 4) {
            throw new IllegalArgumentException("IllegalArgumentException,非法参数异常");
        } else if (result.getData() == null) {
            throw new NullPointerException("NullPointerException,空指针异常");
        }

        return result;
    }

    public CommonResult handlerFallback(@PathVariable Long id, Throwable e) {
        Payment payment = new Payment(id, "null");
        return new CommonResult(ResultCode.INTERNAL_SERVER_ERROR.getCode(), "处理异常" + e.getMessage(), payment);
    }

}

http://localhost:84/consumer/fallback/1
http://localhost:84/consumer/fallback/4 error
http://localhost:84/consumer/fallback/5 error


9.1.15 配置 blockHandler 负责在 Sentinel 里面配置的降级限流
@RestController
@Slf4j
public class CircleBreakerController {

    private static final String SERVICE_URL = "http://nacos-payment-provider";
    @Resource
    private RestTemplate restTemplate;

    @RequestMapping("/consumer/fallback/{id}")
    // 没有任何配置
//    @SentinelResource(value = "fallback") 
	// 配置`fallback`负责业务异常
//    @SentinelResource(value = "fallback", fallback = "handlerFallback")
	// 配置`blockHandler`负责在`Sentinel`里面配置的降级限流 
    @SentinelResource(value = "fallback", blockHandler = "blockHandler") 
    public CommonResult<Payment> fallback(@PathVariable Long id) {
        CommonResult<Payment> result = restTemplate.getForObject(SERVICE_URL + "/paymentSQL/" + id, CommonResult.class, id);

        if (id == 4) {
            throw new IllegalArgumentException("IllegalArgumentException,非法参数异常");
        } else if (result.getData() == null) {
            throw new NullPointerException("NullPointerException,空指针异常");
        }

        return result;
    }

    public CommonResult handlerFallback(@PathVariable Long id, Throwable e) {
        Payment payment = new Payment(id, "null");
        return new CommonResult(ResultCode.INTERNAL_SERVER_ERROR.getCode(), "处理异常" + e.getMessage(), payment);
    }

    public CommonResult blockHandler(@PathVariable Long id, BlockException blockException) {
        Payment payment = new Payment(id, "null");
        return new CommonResult(ResultCode.SERVICE_UNAVAILABLE.getCode(), "降级限流" + blockException.getMessage(), payment);
    }

}


9.1.16 配置 fallbackblockHandler

9.1.17 配置 exceptionsToIgnore 忽略属性
@SentinelResource(value = "fallback", fallback = "handlerFallback", blockHandler = "blockHandler",
			// 配置`exceptionsToIgnore`忽略属性
            exceptionsToIgnore = {IllegalArgumentException.class}) 
// `IllegalArgumentException`被忽略

9.2 Feign 系列

9.2.1 修改 Module cloudalibaba-consumer-nacos-order84

9.2.2 POM
<!--openfeign-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

9.2.3 YML
# 激活Sentinel对Feign的支持
feign:
  sentinel:
    enabled: true

9.2.4 PaymentService
@FeignClient(value = "nacos-payment-provider", fallback = PaymentFallbackService.class)
public interface PaymentService {

    @GetMapping(value = "/paymentSQL/{id}")
    CommonResult<Payment> paymentSQL(@PathVariable("id") Long id);
}

9.2.5 PaymentFallbackService
@Component
public class PaymentFallbackService implements PaymentService {

    @Override
    public CommonResult paymentSQL(Long id) {
        return new CommonResult(ResultCode.SERVICE_UNAVAILABLE.getCode(), "服务降级", new Payment(id, "errorSerial"));
    }
}

9.2.6 OpenFeignController
@RestController
@Slf4j
public class OpenFeignController {

    @Resource
    private PaymentService paymentService;

    @GetMapping(value = "/consumer/openfeign/{id}")
    public CommonResult<Payment> paymentSQL(@PathVariable("id") Long id) {
        if (id == 4) {
            throw new RuntimeException("没有该id");
        }
        return paymentService.paymentSQL(id);
    }

}

9.2.7 主启动
@EnableFeignClients

9.2.8 测试

http://localhost:84/consumer/openfeign/1

  • 测试 84 调用 9003,此时故意关闭 9003 微服务提供者,看 84 消费侧自动降级,不会被耗死。

10. 熔断框架比较

在这里插入图片描述


11. 规则持久化

  • 一旦我们重启应用,Sentinel 规则将消失,生产环境需要将配置规则进行持久化。
  • 将限流配置规则持久化进 Nacos 保存,只要刷新 8401 某个 Rest 地址,Sentinel 控制台的流控规则就能看到,只要 Nacos 里面的配置不删除,针对 8401 上 Sentinel 上的流控规则持续有效。

11.1 修改 Module cloudalibaba-sentinel-service8401

11.2 POM
<!--datasource-nacos-->
<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-datasource-nacos</artifactId>
</dependency>

11.3 YML(添加 Nacos 数据源配置)
spring:
  cloud:
    sentinel:
      # 规则持久化
      datasource:
        ds1:
          nacos:
            server-addr: localhost:8848
            dataId: ${spring.application.name}
            groupId: DEFAULT_GROUP
            data-type: json
            rule-type: flow

11.4 Nacos 添加业务规则配置
  • resource:资源名称。
  • limitApp:来源应用。
  • grade:阈值类型(0表示线程数,1表示QPS)。
  • count:单机阈值。
  • strategy:流控模式(0表示直接,1表示关联,2表示链路)。
  • controlBehavior:流控效果(0表示快速失败,1表示Warm Up,2表示排队等待)。
  • clusterMode:是否集群。
    在这里插入图片描述

11.5 测试

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

骑士梦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值