开发者社区 > 云原生 > 正文

当调用出现超时时,不能稳定的触发熔断

我针对RT Degrade demo中的例子做了一些修改,前50次调用才会触发sleep,并增加到5s,Thread.sleep(5000),而且增加了success的统计,输出如下。

1549091434879, total:7280, pass:58, block:7271,succcess:9 1549091435884, total:8901, pass:0, block:8901,succcess:0 1549091436887, total:8732, pass:0, block:8732,succcess:0 1549091437890, total:8823, pass:0, block:8823,succcess:0 1549091438891, total:8776, pass:0, block:8776,succcess:0 1549091439896, total:15951, pass:0, block:15902,succcess:49 1549091440899, total:17133, pass:0, block:17133,succcess:0 1549091441902, total:17193, pass:0, block:17193,succcess:0 1549091442903, total:16908, pass:0, block:16908,succcess:0 1549091443908, total:17125, pass:0, block:17125,succcess:0 1549091444910, total:16841, pass:58, block:16784,succcess:58 1549091445912, total:16935, pass:0, block:16935,succcess:0 1549091446916, total:16960, pass:0, block:16959,succcess:0 1549091447919, total:17144, pass:0, block:17144,succcess:0 1549091448919, total:16942, pass:0, block:16944,succcess:0 1549091449921, total:17033, pass:0, block:17031,succcess:0 1549091450924, total:17171, pass:0, block:17172,succcess:0 1549091451926, total:17217, pass:0, block:17216,succcess:0 1549091452930, total:17203, pass:0, block:17203,succcess:0 1549091453933, total:17075, pass:0, block:17075,succcess:0 1549091454938, total:17072, pass:15169, block:1903,succcess:15169 1549091455942, total:17000, pass:17000, block:0,succcess:17000

在使用时,由于网络波动,或者服务端的意外的情况,或者一些锁问题等等,会导致一段时间的调用超时。但Sentinel的熔断策略会导致整体服务长期不可用。 明明第1秒就恢复的问题,导致了20s的不可用。

关于这个问题,有没有好的方法解决?

import com.alibaba.csp.sentinel.Entry; import com.alibaba.csp.sentinel.SphU; import com.alibaba.csp.sentinel.slots.block.BlockException; import com.alibaba.csp.sentinel.slots.block.RuleConstant; import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule; import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager; import com.alibaba.csp.sentinel.util.TimeUtil; import org.junit.Test;

import java.util.ArrayList; import java.util.List; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger;

/** * @author zhaohaifeng * @date 2019-02-02 */ public class SentinelTest {

private static final String KEY = "abc";

private static AtomicInteger pass = new AtomicInteger();
private static AtomicInteger success = 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();
    initDegradeRule();

    for (int i = 0; i < threadCount; i++) {
        Thread entryThread = new Thread(new Runnable() {

            @Override
            public void run() {
                while (true) {
                    Entry entry = null;
                    try {
                        TimeUnit.MILLISECONDS.sleep(5);
                        entry = SphU.entry(KEY);
                        // token acquired
                        int p = pass.incrementAndGet();
                        // sleep 600 ms, as rt
                        if (p < 50) {
                            TimeUnit.MILLISECONDS.sleep(5000);
                        }
                        success.incrementAndGet();
                    } catch (Exception e) {
                        block.incrementAndGet();
                    } finally {
                        total.incrementAndGet();
                        if (entry != null) {
                            entry.exit();
                        }
                    }
                }
            }

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

private static void initDegradeRule() {
    List<DegradeRule> rules = new ArrayList<DegradeRule>();
    DegradeRule rule = new DegradeRule();
    rule.setResource(KEY);
    // set threshold rt, 10 ms
    rule.setCount(10);
    rule.setGrade(RuleConstant.DEGRADE_GRADE_RT);
    rule.setTimeWindow(10);
    rules.add(rule);
    DegradeRuleManager.loadRules(rules);
}

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() {
        long start = System.currentTimeMillis();
        System.out.println("begin to statistic!!!");
        long oldTotal = 0;
        long oldPass = 0;
        long oldBlock = 0;
        long oldSuccess = 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;

            long globalSuccess = success.get();
            long oneSecondSuccess = globalSuccess - oldSuccess;
            oldSuccess = globalSuccess;

            System.out.println(TimeUtil.currentTimeMillis() + ", total:" + oneSecondTotal
                    + ", pass:" + oneSecondPass + ", block:" + oneSecondBlock+",succcess:"+oneSecondSuccess);

            if (seconds-- <= 0) {
                stop = true;
            }
        }

        long cost = System.currentTimeMillis() - start;
        System.out.println("time cost: " + cost + " ms");
        System.out.println("total:" + total.get() + ", pass:" + pass.get()
                + ", block:" + block.get());
        System.exit(0);
    }
}

}

原提问者GitHub用户zhaohaifeng

展开
收起
码字王 2023-05-19 19:04:36 82 0
1 条回答
写回答
取消 提交回答
  • 这就是断路的关键。从断开状态(触发断路)到闭合状态(正常),应该有一个超时(timeWindow)来恢复。你可以根据自己的需求尽量缩短时间窗口

    原回答者GitHub用户sczyh30

    2023-05-19 21:28:05
    赞同 展开评论 打赏
问答分类:
问答地址:

阿里云拥有国内全面的云原生产品技术以及大规模的云原生应用实践,通过全面容器化、核心技术互联网化、应用 Serverless 化三大范式,助力制造业企业高效上云,实现系统稳定、应用敏捷智能。拥抱云原生,让创新无处不在。

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载