[case35]bucket4j-spring-boot-starter小试牛刀

序本文主要研究一下如何使用bucket4j-spring-boot-starter进行限流maven <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency> <dependency> <groupId>com.github.ben-manes.caffeine</groupId> <artifactId>caffeine</artifactId> </dependency> <dependency> <groupId>javax.cache</groupId> <artifactId>cache-api</artifactId> <version>1.1.0</version> </dependency> <dependency> <groupId>com.github.ben-manes.caffeine</groupId> <artifactId>jcache</artifactId> <version>2.6.2</version> </dependency> <dependency> <groupId>com.giffing.bucket4j.spring.boot.starter</groupId> <artifactId>bucket4j-spring-boot-starter</artifactId> <version>0.1.3</version> </dependency> <dependency> <groupId>com.github.vladimir-bukhtoyarov</groupId> <artifactId>bucket4j-core</artifactId> <version>4.0.0</version> </dependency> <dependency> <groupId>com.github.vladimir-bukhtoyarov</groupId> <artifactId>bucket4j-jcache</artifactId> <version>4.0.0</version> </dependency>配置开启缓存 @Bean public CacheManager cacheManager() { final CachingProvider cachingProvider = Caching.getCachingProvider(); return cachingProvider.getCacheManager(); }application.ymlmanagement: endpoints: web: exposure: include: ““spring: cache: cache-names: - buckets caffeine: spec: maximumSize=1000000,expireAfterAccess=3600sbucket4j: enabled: true filters: - cache-name: buckets filter-method: servlet url: / rate-limits: - bandwidths: - capacity: 5 time: 1 unit: seconds这里设置了名为buckets的缓存,过期时间为1h,容量为1000000设置的rate-limits每10秒5个token验证wrk -t12 -c20 -d10s -T60s –latency http://localhost:8080/actuator访问/actuatorcurl -i http://localhost:8080/actuatorHTTP/1.1 429X-Rate-Limit-Retry-After-Seconds: 0Content-Type: application/json;charset=ISO-8859-1Content-Length: 35Date: Sun, 02 Sep 2018 08:03:51 GMT{ “message”: “Too many requests!” }小结bucket4j-spring-boot-starter使用bucket4j进行限流,适配了springboot1、springboot2以及zuul,通过配置文件配置即可轻松实现限流。docbucket4j-spring-boot-starterbucket4j-spring-boot-starter-examples ...

September 3, 2018 · 1 min · jiezi

bucket4j使用实例

序本文主要演示一下bucket4j的几个使用实例maven <dependency> <groupId>com.github.vladimir-bukhtoyarov</groupId> <artifactId>bucket4j-core</artifactId> <version>4.0.1</version> </dependency>rate limit @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 @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”); } }输出实例23:14:46.740 [main] INFO com.example.demo.Bucket4jTest - do remote call23:14:46.744 [main] INFO com.example.demo.Bucket4jTest - do remote call23:14:46.744 [main] INFO com.example.demo.Bucket4jTest - do remote call23:14:46.744 [main] INFO com.example.demo.Bucket4jTest - do remote call23:14:46.744 [main] INFO com.example.demo.Bucket4jTest - do remote call23:14:58.749 [main] INFO com.example.demo.Bucket4jTest - do remote call23:15:10.749 [main] INFO com.example.demo.Bucket4jTest - do remote call23:15:22.754 [main] INFO com.example.demo.Bucket4jTest - do remote call23:15:34.757 [main] INFO com.example.demo.Bucket4jTest - do remote call23:15:46.759 [main] INFO com.example.demo.Bucket4jTest - do remote call23:15:58.762 [main] INFO com.example.demo.Bucket4jTest - do remote call23:16:10.765 [main] INFO com.example.demo.Bucket4jTest - do remote call前面5个token消耗完之后,后续每隔12秒消耗一个token小结bucket4j类库是一款优秀的java限流类库,可以用来限流,也可以用作简单调度。docBasic usage examples ...

September 2, 2018 · 1 min · jiezi