【SpringCloudAlibaba】Sentinel使用

这篇具有很好参考价值的文章主要介绍了【SpringCloudAlibaba】Sentinel使用。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

概述

官网

https://github.com/alibaba/Sentinel
中文:
https://github.com/alibaba/Sentinel/wiki/%E4%BB%8B%E7%BB%8D
https://sentinelguard.io/zh-cn/docs/introduction.html

解决的问题

  1. 服务雪崩
  2. 服务降级
  3. 服务熔断
  4. 服务限流

主要特性

【SpringCloudAlibaba】Sentinel使用,Java,sentinel,redis,数据库

配置

下载可视化控制台

https://github.com/alibaba/Sentinel/releases
【SpringCloudAlibaba】Sentinel使用,Java,sentinel,redis,数据库

POM

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

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: '*'

Sentinel采用的懒加载

流控规则

【SpringCloudAlibaba】Sentinel使用,Java,sentinel,redis,数据库

直接(默认)

  1. 资源名:默认rest路径名
  2. 来源:默认

【SpringCloudAlibaba】Sentinel使用,Java,sentinel,redis,数据库

关联

当与A关联的资源B达到阀值后,就限流A自己(B导致A挂)
B惹事,A挂了
1.预热

  • 公式:阈值除以coldFactor(默认值为3),经过预热时长后才会达到阈值
  • 默认coldFactor为3,即请求 QPS 从 threshold / 3 开始,经预热时长逐渐升至设定的 QPS 阈值。
  • 刚开始不行,后续慢慢OK

应用场景:
如:秒杀系统在开启的瞬间,会有很多流量上来,很有可能把系统打死,预热方式就是把为了保护系统,可慢慢的把流量放进来,慢慢的把阀值增长到设置的阀值。
2.匀速排队

匀速排队,阈值必须设置为QPS

https://github.com/alibaba/Sentinel/wiki/%E6%B5%81%E9%87%8F%E6%8E%A7%E5%88%B6
【SpringCloudAlibaba】Sentinel使用,Java,sentinel,redis,数据库
设置含义:/testA每秒1次请求,超过的话就排队等待,等待的超时时间为20000毫秒。

链路

降级规则

https://github.com/alibaba/Sentinel/wiki/%E7%86%94%E6%96%AD%E9%99%8D%E7%BA%A7

  • Sentinel 熔断降级会在调用链路中某个资源出现不稳定状态时(例如调用超时或异常比例升高),对这个资源的调用进行限制,
    让请求快速失败,避免影响到其它的资源而导致级联错误。

  • 当资源被降级后,在接下来的降级时间窗口之内,对该资源的调用都自动熔断(默认行为是抛出 DegradeException)。

【SpringCloudAlibaba】Sentinel使用,Java,sentinel,redis,数据库

半开的状态系统自动去检测是否请求有异常,
没有异常就关闭断路器恢复使用,
有异常则继续打开断路器不可用。具体可以参考Hystrix

Sentinel的断路器是没有半开状态的

降级策略实战

RT

【SpringCloudAlibaba】Sentinel使用,Java,sentinel,redis,数据库
【SpringCloudAlibaba】Sentinel使用,Java,sentinel,redis,数据库

异常比例

【SpringCloudAlibaba】Sentinel使用,Java,sentinel,redis,数据库
【SpringCloudAlibaba】Sentinel使用,Java,sentinel,redis,数据库
按照上述配置,
单独访问一次,必然来一次报错一次(int age = 10/0),调一次错一次;

开启jmeter后,直接高并发发送请求,多次调用达到我们的配置条件了。
断路器开启(保险丝跳闸),微服务不可用了,不再报错error而是服务降级了。

异常数

时间窗口一定要大于等于60秒。

【SpringCloudAlibaba】Sentinel使用,Java,sentinel,redis,数据库
【SpringCloudAlibaba】Sentinel使用,Java,sentinel,redis,数据库

热点key限流

官网:
https://github.com/alibaba/Sentinel/wiki/%E7%83%AD%E7%82%B9%E5%8F%82%E6%95%B0%E9%99%90%E6%B5%81
何为热点
热点即经常访问的数据,很多时候我们希望统计或者限制某个热点数据中访问频次最高的TopN数据,并对其访问进行限流或者其它操作

兜底方法:
分为系统默认和客户自定义,两种
从HystrixCommand 到@SentinelResource

限流模式只支持QPS模式,固定写死了。(这才叫热点)

  • @SentinelResource注解的方法参数索引,0代表第一个参数,1代表第二个参数,以此类推
  • 单机阀值以及统计窗口时长表示在此窗口时间超过阀值就限流。
  • 上面的抓图就是第一个参数有值的话,1秒的QPS为1,超过就限流,限流后调用dealHandler_testHotKey支持方法。

示例:

class TestController{
	// 此处value的值是资源名可以为abc都行与之后dashboard中配置的资源名对应就可以
	@GetMapping("/testHotKey")
    @SentinelResource(value = "testHotKey",blockHandler = "deal_testHotKey")
    public String testHotKey(@RequestParam(value = "p1",required = false) String p1,
                             @RequestParam(value = "p2",required = false) String p2)
    {
        //int age = 10/0;
        return "------testHotKey";
    }
    public String deal_testHotKey (String p1, String p2, BlockException exception)
    {
        return "------deal_testHotKey,o(╥﹏╥)o";  //sentinel系统默认的提示:Blocked by Sentinel (flow limiting)
    }
}

【SpringCloudAlibaba】Sentinel使用,Java,sentinel,redis,数据库

高级选项:参数例外项

前提条件
注意:热点参数的注意点,参数必须是基本类型或者String

当p1等于5的时候,阈值变为200
【SpringCloudAlibaba】Sentinel使用,Java,sentinel,redis,数据库

其他

@SentinelResource
处理的是Sentinel控制台配置的违规情况,有blockHandler方法配置的兜底处理;

RuntimeException
int age = 10/0,这个是java运行时报出的运行时异常RunTimeException,@SentinelResource不管

总结
@SentinelResource主管配置出错,运行出错该走异常走异常

// 有fallback解决后面会细看
 @SentinelResource(value = "testHotKey",blockHandler = "deal_testHotKey" fallBack="")

系统规则

官网:
https://github.com/alibaba/Sentinel/wiki/%E7%B3%BB%E7%BB%9F%E8%87%AA%E9%80%82%E5%BA%94%E9%99%90%E6%B5%81
仅对入口流量生效

  • Load 自适应(仅对 Linux/Unix-like 机器生效):系统的 load1 作为启发指标,进行自适应系统保护。当系统 load1 超过设定的启发值,且系统当前的并发线程数超过估算的系统容量时才会触发系统保护(BBR 阶段)。系统容量由系统的 maxQps * minRt 估算得出。设定参考值一般是 CPU cores * 2.5。
  • CPU usage(1.5.0+ 版本):当系统 CPU 使用率超过阈值即触发系统保护(取值范围 0.0-1.0),比较灵敏。
  • 平均 RT:当单台机器上所有入口流量的平均 RT 达到阈值即触发系统保护,单位是毫秒。
  • 并发线程数:当单台机器上所有入口流量的并发线程数达到阈值即触发系统保护。
  • 入口 QPS:当单台机器上所有入口流量的 QPS 达到阈值即触发系统保护。
    【SpringCloudAlibaba】Sentinel使用,Java,sentinel,redis,数据库

@SentinelResource

按资源名称限流+后续处理

【SpringCloudAlibaba】Sentinel使用,Java,sentinel,redis,数据库

按照Url地址限流+后续处理

【SpringCloudAlibaba】Sentinel使用,Java,sentinel,redis,数据库

面临的问题

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

客户自定义限流处理逻辑

@GetMapping("/rateLimit/customerBlockHandler")
@SentinelResource(value = "customerBlockHandler",
        blockHandlerClass = CustomerBlockHandler.class,//异常处理类
        blockHandler = "handlerException2")//异常处理方法
public CommonResult customerBlockHandler()
{
    return new CommonResult(200,"按客戶自定义",new Payment(2020L,"serial003"));
}
public class CustomerBlockHandler
{
    public static CommonResult handlerException(BlockException exception)
    {
        return new CommonResult(4444,"按客戶自定义,global handlerException----1");
    }
    public static CommonResult handlerException2(BlockException exception)
    {
        return new CommonResult(4444,"按客戶自定义,global handlerException----2");
    }
}

【SpringCloudAlibaba】Sentinel使用,Java,sentinel,redis,数据库

服务熔断功能

sentinel整合ribbon+openFeign+fallback

fallback管运行异常(管java)
blockHandler管控制台配置违规(管dashboard中配置)

@SentinelResource(value = "fallback",
fallback = "handlerFallback",
blockHandler = "blockHandler")

若 blockHandler 和 fallback 都进行了配置,则被限流降级而抛出 BlockException 时只会进入 blockHandler 处理逻辑。
exceptionsToIgnore

//exceptionsToIgnore 忽略该种异常,sentinel不进行流量拦截
@SentinelResource(value = "fallback",
fallback = "handlerFallback",blockHandler = "blockHandler", 
exceptionsToIgnore = {IllegalArgumentException.class})

OpenFeign

POM

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

激活Sentinel对Feign的支持

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

带@FeignClient注解的业务接口

/**
 * 使用 fallback 方式是无法获取异常信息的,
 * 如果想要获取异常信息,可以使用 fallbackFactory参数
 */
@FeignClient(value = "nacos-payment-provider",fallback = PaymentFallbackService.class)//调用中关闭9003服务提供者

PaymentFallbackService

@Component
public class PaymentFallbackService implements PaymentService
{
    @Override
    public CommonResult<Payment> paymentSQL(Long id)
    {
        return new CommonResult<>(444,"服务降级返回,没有该流水信息",new Payment(id, "errorSerial......"));
    }
}

Controller

//==================OpenFeign
@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);
}

主启动

@EnableFeignClients

多种熔断框架比较

【SpringCloudAlibaba】Sentinel使用,Java,sentinel,redis,数据库

规则持久化

配置

POM

<!--SpringCloud ailibaba sentinel-datasource-nacos -->
<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-datasource-nacos</artifactId>
</dependency>

YML

spring:
  application:
    name: cloudalibaba-sentinel-service
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848 #Nacos服务注册中心地址
    sentinel:
      transport:
        dashboard: localhost:8080 #配置Sentinel dashboard地址
        port: 8719
      datasource:
        ds1:
          nacos:
            server-addr: localhost:8848
            dataId: cloudalibaba-sentinel-service
            groupId: DEFAULT_GROUP
            data-type: json
            rule-type: flow

添加Nacos业务规则配置

【SpringCloudAlibaba】Sentinel使用,Java,sentinel,redis,数据库

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

刷新sentinel
重启服务后可能多次调用接口才会通过持久化验证

其他示例

摘自官网

自适应限流的示例

https://github.com/alibaba/Sentinel/blob/master/sentinel-demo/sentinel-demo-basic/src/main/java/com/alibaba/csp/sentinel/demo/system/SystemGuardDemo.java文章来源地址https://www.toymoban.com/news/detail-687296.html

public class SystemGuardDemo {

    private static AtomicInteger pass = new AtomicInteger();
    private static AtomicInteger block = new AtomicInteger();
    private static AtomicInteger total = new AtomicInteger();

    private static volatile boolean stop = false;
    private static final int threadCount = 100;

    private static int seconds = 60 + 40;

    public static void main(String[] args) throws Exception {

        tick();
        initSystemRule();

        for (int i = 0; i < threadCount; i++) {
            Thread entryThread = new Thread(new Runnable() {
                @Override
                public void run() {
                    while (true) {
                        Entry entry = null;
                        try {
                            entry = SphU.entry("methodA", EntryType.IN);
                            pass.incrementAndGet();
                            try {
                                TimeUnit.MILLISECONDS.sleep(20);
                            } catch (InterruptedException e) {
                                // ignore
                            }
                        } catch (BlockException e1) {
                            block.incrementAndGet();
                            try {
                                TimeUnit.MILLISECONDS.sleep(20);
                            } catch (InterruptedException e) {
                                // ignore
                            }
                        } catch (Exception e2) {
                            // biz exception
                        } finally {
                            total.incrementAndGet();
                            if (entry != null) {
                                entry.exit();
                            }
                        }
                    }
                }

            });
            entryThread.setName("working-thread");
            entryThread.start();
        }
    }

    private static void initSystemRule() {
        SystemRule rule = new SystemRule();
        // max load is 3
        rule.setHighestSystemLoad(3.0);
        // max cpu usage is 60%
        rule.setHighestCpuUsage(0.6);
        // max avg rt of all request is 10 ms
        rule.setAvgRt(10);
        // max total qps is 20
        rule.setQps(20);
        // max parallel working thread is 10
        rule.setMaxThread(10);

        SystemRuleManager.loadRules(Collections.singletonList(rule));
    }

    private static void tick() {
        Thread timer = new Thread(new TimerTask());
        timer.setName("sentinel-timer-task");
        timer.start();
    }

    static class TimerTask implements Runnable {
        @Override
        public void run() {
            System.out.println("begin to statistic!!!");
            long oldTotal = 0;
            long oldPass = 0;
            long oldBlock = 0;
            while (!stop) {
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                }
                long globalTotal = total.get();
                long oneSecondTotal = globalTotal - oldTotal;
                oldTotal = globalTotal;

                long globalPass = pass.get();
                long oneSecondPass = globalPass - oldPass;
                oldPass = globalPass;

                long globalBlock = block.get();
                long oneSecondBlock = globalBlock - oldBlock;
                oldBlock = globalBlock;

                System.out.println(seconds + ", " + TimeUtil.currentTimeMillis() + ", total:"
                    + oneSecondTotal + ", pass:"
                    + oneSecondPass + ", block:" + oneSecondBlock);
                if (seconds-- <= 0) {
                    stop = true;
                }
            }
            System.exit(0);
        }
    }
}

到了这里,关于【SpringCloudAlibaba】Sentinel使用的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • springCloudAlibaba集成sentinel实战(超详细)

    Sentinel是阿里开源的项目,提供了流量控制、熔断降级、系统负载保护等多个维度来保障服务之间的稳定性。 分布式系统的流量防卫兵: 随着微服务的普及,服务调用的稳定性变得越来越重要。Sentinel以“流量”为切入点,在流量控制、断路、负载保护等多个领域开展工作,

    2024年04月15日
    浏览(33)
  • 4.springcloudalibaba sentinel v1.8.6版本服务搭建

    前面完成了gateway项目部署并且测试,现在部署搭建sentinel服务并且测试。 下载地址 这里选择的是目前最新的sentinel版本 直接下载启动jar包,使用命令安装服务 使用设置的账号密码登录如下图所示启动成功 完整pom如下 完整的配置如下 启动服务后,查看控制台,发现server服务

    2024年02月07日
    浏览(25)
  • 基于SpringCloudAlibaba+Sentinel的分布式限流设计

    胡弦,视频号2023年度优秀创作者,互联网大厂P8技术专家,Spring Cloud Alibaba微服务架构实战派(上下册)和RocketMQ消息中间件实战派(上下册)的作者,资深架构师,技术负责人,极客时间训练营讲师,四维口袋KVP最具价值技术专家,技术领域专家团成员,2021电子工业出版社年度优

    2024年04月22日
    浏览(25)
  • SpringCloudAlibaba微服务实战系列(三)Sentinel1.8.0+流控

    Sentinel被称为分布式系统的流量防卫兵,是阿里开源流量框架,从 服务限流、降级、熔断等 多个纬度保护服务。Sentinel同时提供了简洁易用的控制台,可以看到接入应用的秒级数据,并可以在控制台设置一些规则保护应用。它比Hystrix支持的范围广泛,如Spring Cloud、Dubbo、gRP

    2024年02月16日
    浏览(29)
  • SpringCloudAlibaba Gateway(三)-整合Sentinel功能路由维度、API维度进行流控

    ​ 前面使用过Sentinel组件对服务提供者、服务消费者进行流控、限流等操作。除此之外,Sentinel还支持对Gateway、Zuul等主流网关进行限流。 ​ 自sentinel1.6.0版开始,Sentinel提供了Gateway的适配模块,能针对路由(route)和自定义API分组两个维度进行限流。 路由维度是指配置文件中的

    2024年02月10日
    浏览(28)
  • SpringCloudAlibaba微服务实战系列(四)Sentinel熔断降级、异常fallback、block细致处理

    接着上篇文章的内容,在Sentinel中如何进行降级和熔断呢? 降级规则 在Sentinel中降级主要有三个策略:RT、异常比例、异常数,也是针对某个资源的设置。而在 1.8.0+ 版本后RT改为了 慢调用比例 需要设置允许的慢调用 RT(即最大的响应时间),请求的响应时间大于该值则统计

    2024年02月16日
    浏览(34)
  • SpringCloudAlibaba微服务实战系列(五)Sentinel1.8.5+Nacos持久化

    前面介绍Sentinel的流控、熔断降级等功能,同时Sentinel应用也在面临着一个问题:我们在Sentinel后台管理界面中配置了一堆流控、降级规则,但是Sentinel一重启,这些规则全部消失了。那么我们就要考虑Sentinel的持久化问题。 Sentinel为我们提供了几种持久化的解决方案: 存储到

    2024年02月10日
    浏览(41)
  • 使用 Docker Compose 部署 Redis Sentinel 高可用架构

    在现代应用中,无法容忍系统中断或数据丢失。Redis 作为一种高性能的内存数据库,被广泛应用于缓存、会话管理等场景。然而,即使我们拥有可伸缩的 Redis Cluster 集群,也需要考虑在主节点故障时自动切换到从节点的机制。这时候 Redis Sentinel 就派上用场了。高可用性是分布

    2024年02月13日
    浏览(34)
  • 【Redis学习】Redis哨兵(sentinel)

    定义 吹哨人巡查监控后台master主机是否故障,如果故障了根据 投票数 自动将某一个从库转换为新主库,继续对外服务。 作用: 监控redis运行状态,包括master和slave 当master down机,能自动将slave切换成新master 哨兵可以类比为无人值守的运维。 功能 主从监控 监控主从redis库运

    2023年04月09日
    浏览(35)
  • Redis 哨兵(sentinel)

    1.1 吹哨人巡查监控后台master主机是否故障,如果故障了根据投票数自动将某一个从库转换为新主库,继续对外服务 1.2 作用 俗称,无人值守运维 哨兵的作用: 1、监控redis运行状态,包括master和slave 2、 当master down机,能自动将slave切换成新master 1.3 官网理论, https://redis.io/doc

    2024年02月09日
    浏览(29)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包