SpringCloud Alibaba Sentinel 流量控制之流控模式实践总结

官网文档:https://sentinelguard.io/zh-cn/docs/flow-control.html

wiki地址:https://github.com/alibaba/Sentinel/wiki/%E6%B5%81%E9%87%8F%E6%8E%A7%E5%88%B6

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

如下图所示,我们可以针对某个资源添加流控规则,流控模式有直接、关联和链路。

在这里插入图片描述

【1】直接

添加流控规则如下,阈值类型为QPS,单机阈值为1。表示1秒钟内查询1次就是OK,若超过次数1,就直接-快速失败,报默认错误。
在这里插入图片描述

浏览器频繁访问 :http://localhost:8401/testA ,将会抛出异常信息Blocked by Sentinel (flow limiting)。

在这里插入图片描述

需要说明的是,在未做持久化前,每次服务实例的重启都会清空流控规则。

【2】关联

当两个资源之间具有资源争抢或者依赖关系的时候,这两个资源便具有了关联。

比如对数据库同一个字段的读操作和写操作存在争抢,读的速度过高会影响写得速度,写的速度过高会影响读的速度。如果放任读写操作争抢资源,则争抢本身带来的开销会降低整体的吞吐量。

可使用关联限流来避免具有关联关系的资源之间过度的争抢。举例来说,read_db 和 write_db 这两个资源分别代表数据库读写,我们可以给 read_db 设置限流规则来达到写优先的目的:设置 FlowRule.strategy 为 RuleConstant.RELATE 同时设置 FlowRule.ref_identity 为 write_db。这样当写库操作过于频繁时,读数据的请求会被限流。

简单来说就是:当与A关联的资源B达到阀值后,就限流A自己。

如下,我们给资源B设置规则:QPS/1

在这里插入图片描述

我们给资源A修改规则:即只有当B触发限流时,A的QPS/1这个规则才生效

在这里插入图片描述

如下下图所示,如果B未触发限流,那么A的QPS/1是不生效的。
在这里插入图片描述

然后同时请求资源A、资源B,使B达到阈值,此时A也会返回错误信息。

在这里插入图片描述

【3】链路

在这里插入图片描述
简单来讲,资源的链路入口方将会受规则控制,其他的调用不关心。

这里实例版本为:

<!--spring boot 2.2.2-->
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-dependencies</artifactId>
  <version>2.2.2.RELEASE</version>
  <type>pom</type>
  <scope>import</scope>
</dependency>
<!--spring cloud Hoxton.SR1-->
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-dependencies</artifactId>
  <version>Hoxton.SR1</version>
  <type>pom</type>
  <scope>import</scope>
</dependency>
<!--spring cloud alibaba 2.2.0.RELEASE-->
<dependency>
  <groupId>com.alibaba.cloud</groupId>
  <artifactId>spring-cloud-alibaba-dependencies</artifactId>
  <version>2.2.0.RELEASE</version>
  <type>pom</type>
  <scope>import</scope>
</dependency>

添加pom文件

<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-web-servlet</artifactId>
</dependency>

添加配置类:

@Configuration
public class FilterContextConfig {

    @Bean
    public FilterRegistrationBean sentinelFilterRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new CommonFilter());
        registration.addUrlPatterns("/*");
        // 入口资源关闭聚合
        registration.addInitParameter(CommonFilter.WEB_CONTEXT_UNIFY, "false");
        registration.setName("sentinelFilter");
        registration.setOrder(1);
        return registration;
    }
}

定义一个service资源:

@Service
public class JaneService {
    // 定义限流资源
    @SentinelResource("common")
    public String common(){
        return "common";
    }
}

定义controller:

@Autowired
JaneService janeService;

@GetMapping("/testA")
public String testA(){
    janeService.common();
    return "testA";
}

@GetMapping("/testB")
public String testB(){
    janeService.common();
    return "testB";
}

给common添加链路规则:
在这里插入图片描述
这样频繁请求testA将会报错,但是频繁请求testB正常,说明我们的链路控制生效。

在这里插入图片描述

【4】新版本下关于链路的配置

<spring.boot.version>3.1.7</spring.boot.version>
<spring.cloud.version>2022.0.4</spring.cloud.version>
<spring.cloud.alibaba.version>2022.0.0.0-RC2</spring.cloud.alibaba.version>


<!--SpringCloud ailibaba sentinel-datasource-nacos -->
<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
<!--SpringCloud alibaba 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>

在上面版本配置中,yml只需要设置即可: web-context-unify: false

server:
  port: 8401

spring:
  application:
    name: cloudalibaba-sentinel-service
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848         #Nacos服务注册中心地址
    sentinel:
      transport:
        dashboard: localhost:8080 #配置Sentinel dashboard控制台服务地址
        port: 8719 #默认8719端口,假如被占用会自动从8719开始依次+1扫描,直至找到未被占用的端口
      web-context-unify: false # controller层的方法对service层调用不认为是同一个根链路
  • 8
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

流烟默

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

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

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

打赏作者

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

抵扣说明:

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

余额充值