We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
正常我的理解只会打印一个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!
The text was updated successfully, but these errors were encountered:
Sorry, something went wrong.
我也好奇这个问题,源码层面是先执行限流规则判断,再累加统计信息,所以多线程场景下会出现这个问题
找了一个历史的issues:#59
非常感谢您的回答,我了解一下
No branches or pull requests
正常我的理解只会打印一个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!
The text was updated successfully, but these errors were encountered: