我将wiki中的案例代码拷贝到我的IDEA中,然后稍作修改,具体如下:
public static void main(String[] args) { String resourceName = "helloWorld"; initFlowRules(resourceName);
    long begin = System.currentTimeMillis();
    long end = begin + 1000;
    int success = 0;
    int fail = 0;
    while (System.currentTimeMillis() < end) {
        Entry entry = null;
        try {
            entry = SphU.entry(resourceName);
            success++;
        } catch (BlockException e1) {
            fail++;
        } finally {
            if (entry != null) {
                entry.exit();
            }
        }
    }
    System.out.println("Begin time: " + begin);
    System.out.println("End time: " + end);
    System.out.println("Success: " + success);
    System.out.println("Fail: " + fail);
    System.exit(0);
}
private static void initFlowRules(String name){
    List<FlowRule> rules = new ArrayList<>();
    FlowRule rule = new FlowRule();
    rule.setResource(name);
    rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
    // Set limit QPS to 20.
    rule.setCount(20);
    rules.add(rule);
    FlowRuleManager.loadRules(rules);
}
初始化FlowRule时,设置的qps为20,我期待程序运行结束后,success输出的值也应该为20, 然而实际运行过程中,日志显示success的值 有时为20,有时为40.
Begin time: 1533132761258 End time: 1533132762258 Success: 40 Fail: 140633
原提问者GitHub用户xflonga
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
Sentinel 指标统计的滑动窗口中每个小窗口的初始时间都是经过对齐的(间隔默认 500 ms),而不是当前时间,因此会存在时间偏差导致多通过的情况。
比如流量的时间范围(取后四位):1258 - 2258
理想的情况是:
瞬间20个请求 这段时间无法通过 ↓ ↓ | o | | 1258 1758 2258 (windowStart)
但实际的窗口是:
瞬间20个请求 这段时间无法通过 第一个窗口过期,因此又可以进入 20 个 ↓ ↓ ↓ | o | | o | 1000 1500 2000 2500 (windowStart)
原回答者GitHub用户sczyh30
阿里云拥有国内全面的云原生产品技术以及大规模的云原生应用实践,通过全面容器化、核心技术互联网化、应用 Serverless 化三大范式,助力制造业企业高效上云,实现系统稳定、应用敏捷智能。拥抱云原生,让创新无处不在。