前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >bucket4j使用实例

bucket4j使用实例

作者头像
code4it
发布2018-09-17 17:23:41
2K0
发布2018-09-17 17:23:41
举报
文章被收录于专栏:码匠的流水账码匠的流水账

本文主要演示一下bucket4j的几个使用实例

maven

代码语言:javascript
复制
        <dependency>
            <groupId>com.github.vladimir-bukhtoyarov</groupId>
            <artifactId>bucket4j-core</artifactId>
            <version>4.0.1</version>
        </dependency>

rate limit

代码语言:javascript
复制
    @Test
    public void testRateLimit(){
        // define the limit 1 time per 10 minute
        Bandwidth limit = Bandwidth.simple(1, Duration.ofMinutes(10));
        // construct the bucket
        Bucket bucket = Bucket4j.builder().addLimit(limit).build();
        IntStream.rangeClosed(1,5)
                .forEach(i -> {
                    executor.submit(() -> {
                        if(bucket.tryConsume(1)){
                            LOGGER.info("acquired");
                        }else{
                            LOGGER.info("blocked");
                        }
                    });
                });
    }
  • 这里使用simple方法构造Bandwidth,进而构建bucket实例

scheduler

代码语言:javascript
复制
    @Test
    public void testAsScheduler(){
        // define the limit 100 times per 1 minute
        Bandwidth limit = Bandwidth.simple(5, Duration.ofMinutes(1));
        // construct the bucket
        Bucket bucket = Bucket4j.builder().addLimit(limit).build();

        // do polling in infinite loop
        while (true) {
            // Consume a token from the token bucket.
            // If a token is not available this method will block until the refill adds one to the bucket.
            try {
                bucket.asScheduler().consume(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            LOGGER.info("do remote call");
        }
    }

输出实例

代码语言:javascript
复制
23:14:46.740 [main] INFO com.example.demo.Bucket4jTest - do remote call
23:14:46.744 [main] INFO com.example.demo.Bucket4jTest - do remote call
23:14:46.744 [main] INFO com.example.demo.Bucket4jTest - do remote call
23:14:46.744 [main] INFO com.example.demo.Bucket4jTest - do remote call
23:14:46.744 [main] INFO com.example.demo.Bucket4jTest - do remote call
23:14:58.749 [main] INFO com.example.demo.Bucket4jTest - do remote call
23:15:10.749 [main] INFO com.example.demo.Bucket4jTest - do remote call
23:15:22.754 [main] INFO com.example.demo.Bucket4jTest - do remote call
23:15:34.757 [main] INFO com.example.demo.Bucket4jTest - do remote call
23:15:46.759 [main] INFO com.example.demo.Bucket4jTest - do remote call
23:15:58.762 [main] INFO com.example.demo.Bucket4jTest - do remote call
23:16:10.765 [main] INFO com.example.demo.Bucket4jTest - do remote call
  • 前面5个token消耗完之后,后续每隔12秒消耗一个token

小结

bucket4j类库是一款优秀的java限流类库,可以用来限流,也可以用作简单调度。

doc

  • Basic usage examples
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-09-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 码匠的流水账 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • maven
  • rate limit
  • scheduler
  • 小结
  • doc
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档