Open aa310958153 opened 2 months ago
正常我的理解只会打印一个helloword /**
初始化流控规则 */ private static void initFlowRules() {
List<FlowRule> rules = new ArrayList<FlowRule>(); FlowRule rule1 = new FlowRule(); rule1.setResource("HelloWorld"); ClusterFlowConfig config = new ClusterFlowConfig(); config.setFallbackToLocalWhenFail(true); config.setSampleCount(10); config.setThresholdType(0); config.setWindowIntervalMs(1000); rule1.setClusterConfig(config); // set limit qps to 20 rule1.setCount(1); rule1.setStrategy(0); rule1.setClusterMode(false); rule1.setControlBehavior(0); rule1.setGrade(RuleConstant.FLOW_GRADE_QPS); rule1.setLimitApp("default"); rules.add(rule1); FlowRuleManager.loadRules(rules);
}
@Test public void flowRule_TEST() throws InterruptedException {
// 配置规则. initFlowRules(); for (int j = 0; j < 4; j++) { new Thread(new Runnable() { int i = 0; @Override public void run() { for (int k = 0; k < 30; k++) { // 1.5.0 版本开始可以直接利用 try-with-resources 特性 自动回收调用 entry.exit() 配合注解使用 @SentinelResource("HelloWorld") try (Entry entry = SphU.entry( "HelloWorld", EntryType.IN)) { // 被保护的逻辑 System.out.println(Thread.currentThread().getId() + "hello world" + (++i)); } catch (BlockException ex) { i++; // 处理被流控的逻辑 System.out.println("blocked!"); } } } }).start(); } Thread.sleep(500000000);
} 打印日志 25hello world1 24hello world1 26hello world1 23hello world1 blocked! blocked! blocked! blocked! blocked! blocked! blocked! blocked! blocked! blocked! blocked! blocked! blocked! blocked! blocked! blocked! blocked!
我也好奇这个问题,源码层面是先执行限流规则判断,再累加统计信息,所以多线程场景下会出现这个问题
找了一个历史的issues:https://github.com/alibaba/Sentinel/issues/59
找了一个历史的issues:#59
非常感谢您的回答,我了解一下
正常我的理解只会打印一个helloword /**
初始化流控规则 */ private static void initFlowRules() {
}
@Test public void flowRule_TEST() throws InterruptedException {
} 打印日志 25hello world1 24hello world1 26hello world1 23hello world1 blocked! blocked! blocked! blocked! blocked! blocked! blocked! blocked! blocked! blocked! blocked! blocked! blocked! blocked! blocked! blocked! blocked!