Java Code Examples for org.redisson.api.RedissonClient#getRateLimiter()
The following examples show how to use
org.redisson.api.RedissonClient#getRateLimiter() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RateLimiterTest.java From blog with BSD 2-Clause "Simplified" License | 6 votes |
public static void main(String[] args) { Config config = new Config(); config.useSingleServer().setAddress("redis://localhost:6379"); RedissonClient client = Redisson.create(config); RRateLimiter rateLimiter = client.getRateLimiter("rate_limiter"); rateLimiter.trySetRate(RateType.OVERALL, 1, 5, RateIntervalUnit.SECONDS); ExecutorService executorService = Executors.newFixedThreadPool(10); for (int i = 0; i < 10; i++) { executorService.submit(() -> { try { rateLimiter.acquire(); System.out.println("时间:" + System.currentTimeMillis() + ",线程" + Thread.currentThread().getId() + "进入数据区:" + System.currentTimeMillis()); } catch (Exception e) { e.printStackTrace(); } }); } }
Example 2
Source File: RateLimiterExamples.java From redisson-examples with Apache License 2.0 | 6 votes |
public static void main(String[] args) throws InterruptedException { // connects to 127.0.0.1:6379 by default RedissonClient redisson = Redisson.create(); RRateLimiter limiter = redisson.getRateLimiter("myLimiter"); // one permit per 2 seconds limiter.trySetRate(RateType.OVERALL, 1, 2, RateIntervalUnit.SECONDS); CountDownLatch latch = new CountDownLatch(2); limiter.acquire(1); latch.countDown(); Thread t = new Thread(() -> { limiter.acquire(1); latch.countDown(); }); t.start(); t.join(); latch.await(); redisson.shutdown(); }