sentinel初次剖析

1. 阿里sentinel源码研究深入

1.1. 前言

  • 之前使用了一次sentinel,已经可以初步使用它的限流和降级功能,对于其中的原理比较好奇,因此想花时间研究并记录下来,备忘

1.2. 源码

1.2.1. 流控降级监控等的构建

  • 首先客户端而言,我关注的是我写的代码SphU.entry,这明显是很关键的方法,下图的内容就是这里构建的
    -Sentinel工作主流程就包含在上面一个方法里,通过链式调用的方式,经过了建立树状结构,保存统计簇点,异常日志记录,实时数据统计,负载保护,权限认证,流量控制,熔断降级等Slot

  • 进入链式方法的入口为CtSph类,try方法大括号内
Entry e = new CtEntry(resourceWrapper, chain, context);
        try {
            chain.entry(context, resourceWrapper, null, count, prioritized, args);
        } catch (BlockException e1) {
            e.exit(count, args);
            throw e1;
        } catch (Throwable e1) {
            // This should not happen, unless there are errors existing in Sentinel internal.
            RecordLog.info("Sentinel unexpected exception", e1);
        }

1.2.2. 修改控制台规则是如何通知客户端的?

  • 看sentinel-transport-simple-http包中的HttpEventTask类,它开启了一个线程,转么用来做为socket连接,控制台通过socket请求通知客户端,从而更新客户端规则,更改规则核心代码如下
// Find the matching command handler.
            CommandHandler<?> commandHandler = SimpleHttpCommandCenter.getHandler(commandName);
            if (commandHandler != null) {
                CommandResponse<?> response = commandHandler.handle(request);
                handleResponse(response, printWriter, outputStream);
            } else {
                // No matching command handler.
                badRequest(printWriter, "Unknown command `" + commandName + '`');
            }

通过命令模式,commandName为setRules时,更新规则

1.2.3. 既然它建立连接用的socket,为什么不用netty呢?

  • 带着这个疑问,我本想在issues里找下,突然发现它的源码中有个sentinel-transport-netty-http这个包和sentinel-transport-simple-http处于同级,官方的例子用的simple-http,但明显它也准备了netty-http,于是我替换成了netty-http,运行后效果和原先一样,至于效率上有没有提升,我就不清楚了^_^

1.2.4. 流量规则如何检查?

  • 该规则检查类为FlowRuleChecker,在core核心包中,核心检查方法如下
    private static boolean passLocalCheck(FlowRule rule, Context context, DefaultNode node, int acquireCount,
                                          boolean prioritized) {
        Node selectedNode = selectNodeByRequesterAndStrategy(rule, context, node);
        if (selectedNode == null) {
            return true;
        }
        return rule.getRater().canPass(selectedNode, acquireCount, prioritized);
    }

1.2.5. 熔断降级如何判断?

  • 判断类为DegradeRuleManager,在core核心包,核心内容如下,再深入就是它判断的算法了,感兴趣的自己去看如下的passCheck
public static void checkDegrade(ResourceWrapper resource, Context context, DefaultNode node, int count)
        throws BlockException {
   

        Set<DegradeRule> rules = degradeRules.get(resource.getName());
        if (rules == null) {
   
            return;
        }

        for (DegradeRule rule : rules) {
   
            if (!rule.passCheck(context, node
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
双向的TCP流量过滤软件,它允许您添加自定义正则表达式(正则表达式)过滤器。预置过滤包括:HTTP头信息,POST和GET数据,域名或即使*过滤*在任何连接传递的数据。 为了对付新的威胁,TCP过滤器包括一个强大的威胁检测引擎,用于检测和阻止黑洞,网络攻击,恶意URL和其他基于Web的威胁。 本软件经常更新数据库,为了您的安全。 [10-01-2012] v1.4.0.0 Added "Password Protect Websites" Password is saved encrypted Added "Status" TAB Added "Execute Action after X minutes of idle activity" (RegEx Rules) Added "Domain:" info in alert dialog (RegEx Rules) Added "URL:" info in alert dialog (RegEx Rules) Improved "Threats Detection Engine (TDE)" Optimized UI Added "Actions" links in Status TAB Added right-click option "Set Password" on Domains->Protected TAB Added right-click option "Options" on RegEx Rules TAB Block download of executable files Block download of PDF files with JavaScript code Block download of PDF files Block download of Java (JAR) files Block download of Wordpad (RTF) files Block download of Video (AVI, FLV, MPG, MOV) files Block download of Flash (SWF) files Block download of ZIP and RAR files Block download of Microsoft Word and Excel files Block a website by TLD Disable task manager when in stealth mode Added "Menu"->"Disable Task Manager" Added "Menu"->"Enable Task Manager" Disable cmd dos prompt when in stealth mode Lock all cdroms when in stealth mode Added "Menu"->"Disable CMD Dos Prompt" Added "Menu"->"Enable CMD Dos Prompt" Added "Menu"->"Lock CD-ROMs" Added "Menu"->"UnLock CD-ROMs" Block download of JavaScript (JS) files Block IRC traffic Block FTP traffic Added "Rules"->"ADS" TAB to manage regexes to block ADS links Added "Threats"->Process Behavioral Analysis (Block connections of suspicious processes) Added "Block all unknown websites" (allow only whitelisted domains) Block IMs traffic (MSN Messenger, Y! Messenger) Updated "Reset Settings" Disabled "Threats Detection Engine (TDE)" (will be available in final version) Minor fixes and optimizations

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值