前言:大家好,我是小威,24届毕业生,在一家满意的公司实习。本篇文章将详细介绍Sentinel源码实现对分布式系统高可用性和流量控制,后续文章将详细介绍Sentinel的其他知识。
如果文章有什么需要改进的地方还请大佬不吝赐教👏👏。
小威在此先感谢各位大佬啦~~🤞🤞
今天正值Java诞生日,小威为各位大佬准备了小小礼物,请查看文末彩蛋哦😁~
在这里插入图片描述

🏠个人主页:小威要向诸佬学习呀
🧑个人简介:大家好,我是小威,一个想要与大家共同进步的男人😉😉
目前状况🎉:24届毕业生,在一家满意的公司实习👏👏

💕欢迎大家:这里是CSDN,我总结知识的地方,欢迎来到我的博客,我亲爱的大佬😘

在这里插入图片描述

以下正文开始

在这里插入图片描述

Sentinel核心 API

Sentinel 的核心 API 处理流程通过 ProcessorSlotChain 实现。

ProcessorSlotChain 主要是一个处理器链,用于管理和执行所有的流量控制、熔断降级等处理规则,并最终返回结果或者抛出异常。下面我们通过源码的方式详细介绍一下 Sentinel 的 ProcessorSlotChain 实现。

ProcessorSlotChain处理器链

ProcessorSlotChain 是 Sentinel 核心的处理器链,它包含了多个 ProcessorSlot 节点,并负责管理这些节点的运行顺序。同时,ProcessorSlotChain 同时提供了 apply() 方法,用于接收和处理请求。

ProcessorSlotChain 的定义如下:

public class ProcessorSlotChain {
    
    private final List<ProcessorSlot> slots = new ArrayList<>();
    
    public ProcessorSlotChain() {
        this(new ArrayList<>());
    }
    
    public ProcessorSlotChain(List<ProcessorSlot> slotList) {
        if (slotList != null) {
            slots.addAll(slotList);
        }
    }

    // 处理请求
    public Entry process(Context context, ResourceWrapper resourceWrapper, DefaultNode node,
                          int count, boolean prioritized, Object... args) throws Throwable {
        if (context == null) {
            return null;
        }
        return new ProcessorSlotEntry(slots, context, resourceWrapper, node, count, prioritized, args);
    }

    // 添加处理器节点
    public void addLast(ProcessorSlot processorSlot) {
        if (processorSlot != null && !slots.contains(processorSlot)) {
            slots.add(processorSlot);
        }
    }

    // 移除处理器节点
    public void remove(ProcessorSlot processorSlot) {
        slots.remove(processorSlot);
    }

    // 获取处理器节点列表
    public List<ProcessorSlot> getSlots() {
        return Collections.unmodifiableList(slots);
    }
    
}

在 ProcessorSlotChain 中,我们可以看到主要提供了以下几个方法:

  • process(): 处理请求的方法,接收一个 Context 对象和其他相关参数,并返回一个 Entry 对象。
  • addLast(): 向 ProcessorSlotChain 中添加一个 ProcessorSlot 节点。
  • remove(): 从 ProcessorSlotChain 中移除一个 ProcessorSlot 节点。
  • getSlots(): 获取 ProcessorSlotChain 中所有的 ProcessorSlot 节点。

ProcessorSlot接口

ProcessorSlot 接口是 Sentinel 处理器链中的节点接口,每个节点都应该实现这个接口。ProcessorSlot 主要通过其 entry() 方法来执行流量控制、熔断降级等操作,并将处理结果传递给下一个节点。

ProcessorSlot 的定义如下:

public interface ProcessorSlot {
void entry(Context context, ResourceWrapper resourceWrapper, DefaultNode node, int count, boolean prioritized, Object… args) throws Throwable;
}
在 ProcessorSlot 中,我们可以看到主要提供了一个 entry() 方法,这个方法用于进行流量控制等操作,并将处理结果传递给下一个 ProcessorSlot 节点。

ProcessorSlotEntry类

ProcessorSlotEntry 类是 ProcessorSlotChain 的核心实现类,它维护了 ProcessorSlotChain 中所有的 ProcessorSlot 节点,并负责执行整个处理流程。在 ProcessorSlotEntry 中,我们可以看到它首先会根据请求参数创建一个 Context 对象,然后依次执行 ProcessorSlot 节点的 entry() 方法

ProcessorSlotEntry 的定义如下:

public class ProcessorSlotEntry extends Entry {

    private final List<ProcessorSlot> sortedSlots;
    private ProcessorSlot lastExecutedSlot = null;
    private int curIdx = 0;
    
    public ProcessorSlotEntry(List<ProcessorSlot> sortedSlots, Context context,
                              ResourceWrapper resourceWrapper, DefaultNode node,
                              int count, boolean prioritized, Object[] originArgs) {
        super(context, resourceWrapper, node, count, prioritized, originArgs);
        this.sortedSlots = sortedSlots;
    }

    @Override
    public void exit(int count, Object... args) throws Throwable {
        // 执行下一个节点
        if (lastExecutedSlot != null && curIdx < sortedSlots.size()) {
            lastExecutedSlot.exit(context, resourceWrapper, node, count, args);
            lastExecutedSlot = null;
        }
    }

    @Override
    public void fireEntry() throws Throwable {
        lastExecutedSlot = null;
        if (curIdx < sortedSlots.size()) {
            ProcessorSlot slot = sortedSlots.get(curIdx++);
            lastExecutedSlot = slot;
            slot.entry(context, resourceWrapper, node, count, prioritized, originArgs);
        }
    }
}

在 ProcessorSlotEntry 中,我们可以看到主要提供了以下几个方法:

  • exit(): 退出当前 ProcessorSlot 节点,并执行下一个 ProcessorSlot 节点。
  • fireEntry(): 进入当前 ProcessorSlot 节点,并执行 ProcessorSlot 中的 entry() 方法。

Sentinel API 处理流程

通过 ProcessorSlotChain、ProcessorSlot 和 ProcessorSlotEntry 这几个核心类的协作,完成了 Sentinel API 处理流程。

具体而言,当客户端向 Sentinel 发送请求后,API 接口会首先进入 ProcessorSlotChain 的第一个 ProcessorSlot 点,即 EntranceNode。

EntranceNode 主要负责对请求进行统计和流量控制,并根据请求的情况调用下一个 ProcessorSlot 节点。

在 ProcessorSlotChain 中,后续的 ProcessorSlot 节点依次进行流控、熔断降级等操作,并将处理结果传递给下一个 ProcessorSlot 节点进行处理。

最后,ProcessorSlotChain 的最后一个节点,即 ExitNode,会将请求结果返回给客户端,并统计监控数据。通过这个处理流程,Sentinel 实现了对分布式系统的高可用性和流量控制。

图书推荐

Java诞生日,推荐Java“此生错过必遗憾”系列书单:

Java28岁了,当打之年,并且还会打很多年。
为即将或正在使用Java的你推荐Java“此生错过必遗憾”系列书单。看看你还缺哪本?请补齐。优惠购书链接就在文中,拿好不谢。

在这里插入图片描述

Java四大名著”之一Core Java最新版,一键打包全套2册!建议入门小白和准备升级到Java17的开发者购买。本书根据Java17新特性全面升级!赠送作者亲授视频课+海量代码集。

限时秒杀链接:点击购买

在这里插入图片描述

“Java四大名著”之一Core Java次新版,一键打包全套2册!建议实际生产环境仍在使用Java8、Java11开发且暂时没有升级版本打算的开发者购买。本书基于Java9-11编写,赠送作者亲授视频课+海量代码集。

限时秒杀链接:点击购买

在这里插入图片描述

“Java四大名著”之一,需要有一定编程基础的人才可阅读,即使很多内容还无法理解,但每次读完一定会有所收获。本书单最前面推荐的《Java核心技术》侧重技术,而《Java编程思想》侧重于“思想”,本书为你剖析Java中各个内容的设计理念。这是一本伴随我们技术成长的好书,买一本放在旁边,摸着就有底气。

限时秒杀链接:点击购买

在这里插入图片描述

Java之父James Gosling:
“我很希望我10年前就能拥有这本书。有人可能认为我不需要任何Java方面的书籍,但是我需要这本书。”

“Java四大名著”之一,适合已经掌握Java核心技术的程序员,想更加深入地了解Java编程语言的开发者阅读。针对如何编写高效、设计优良的程序提出了最实用、最权威的指导方针,通过90条简短、独立的经验法则,探索新的设计模式和语言习惯用法,帮你更加有效地使用Java编程语言及其基本类库,指引你少走弯路。这些经验规则涵盖了大多数开发人员每天所面临的问题的解决方案。是Java开发人员案头上的一本不可或缺的参考书。

限时秒杀链接:点击购买

粉丝福利:评论区任意留言可参与活动抽奖(可评论最多五条,抽取四名欧皇)

好了,本篇文章就先分享到这里了,后续将会继续介绍sentinel详细的其他方面的知识,感谢大佬认真读完支持咯~
在这里插入图片描述

文章到这里就结束了,如果有什么疑问的地方请指出,诸佬们一起讨论😁
希望能和诸佬们一起努力,今后我们顶峰相见🍻
再次感谢各位小伙伴儿们的支持🤞

在这里插入图片描述

Logo

苏州本地的技术开发者社区,在这里可以交流本地的好吃好玩的,可以交流技术,可以交流招聘等等,没啥限制。

更多推荐