maven版本
com.alibaba.cloud spring-cloud-starter-alibaba-sentinel 2.2.6.RELEASE 代码如下,简单demo测试限流无效
public class SentinelExample2 {
private static void initFlowRules() { List rules = new ArrayList<>(); FlowRule rule = new FlowRule(); rule.setResource("HelloWorld1"); rule.setGrade(RuleConstant.FLOW_GRADE_QPS); // Set limit QPS to 10. rule.setCount(10); rules.add(rule); FlowRuleManager.loadRules(rules); }
public static void main(String[] args) { // 配置规则. initFlowRules();
ExecutorService exec = Executors.newCachedThreadPool();
CountDownLatch countDownLatch = new CountDownLatch(1);
for (int i = 0; i < 500; i++) {
Runnable run = () -> {
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
Entry entry = null;
// 务必保证finally会被执行
try {
// 资源名可使用任意有业务语义的字符串
entry = SphU.entry("HelloWorld1");
// 被保护的业务逻辑
// do something...
System.out.println("hello world");
} catch (BlockException e1) {
// 资源访问阻止,被限流或被降级
// 进行相应的处理操作
System.out.println("blocked!");
} finally {
if (entry != null) {
entry.exit();
}
}
};
exec.execute(run);
}
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
countDownLatch.countDown();
}
请问是哪里出问题了?麻烦解答,谢谢
原提问者GitHub用户wuhaoqiang1
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
限流是qps=10,每秒允许10个请求执行。 而这500个线程是立刻运行并马上执行完毕的,估计是整个时间窗口太短 + 单个执行时间太快的原因,来不及统计。
在SphU.entry之前,加上:
try { Thread.sleep(ThreadLocalRandom.current().nextInt(300, 3000)); } catch (InterruptedException e) { e.printStackTrace(); }
再观察输出试试
原回答者GitHub用户cdfive
阿里云拥有国内全面的云原生产品技术以及大规模的云原生应用实践,通过全面容器化、核心技术互联网化、应用 Serverless 化三大范式,助力制造业企业高效上云,实现系统稳定、应用敏捷智能。拥抱云原生,让创新无处不在。