Lettuce Client Util
package com.zjw.utils;
import io.lettuce.core.RedisClient;
import io.lettuce.core.RedisURI;
import io.lettuce.core.api.StatefulRedisConnection;
import java.time.Duration;
public class LettuceSentinelRedisUtil {
private static RedisURI redisUri = null;
private static RedisClient client;
static {
String[] sentinels = "127.0.0.1:26371,127.0.0.1:26372,127.0.0.1:26373".split(",");
RedisURI.Builder builder = null;
if(sentinels.length > 0) {
String[] hostAndPort = sentinels[0].split(":");
builder = RedisURI.Builder.sentinel(hostAndPort[0], Integer.parseInt(hostAndPort[1]),
"my_redis_sentinel");
}
for (int i = 1; i < sentinels.length; i++) {
String[] hostAndPort = sentinels[i].split(":");
builder.withSentinel(hostAndPort[0], Integer.parseInt(hostAndPort[1]));
}
if(builder!=null) {
builder.withTimeout(Duration.ofMillis(200));
builder.withPassword("123456".toCharArray());
builder.withDatabase(0);
redisUri = builder.build();
}
client = RedisClient.create(redisUri);
}
public static RedisClient getClient() {
return client;
}
public static StatefulRedisConnection<String, String> getConn() {
return client.connect();
}
}
ScrollWindowLimiter
package com.zjw.utils;
import io.lettuce.core.Range;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.sync.RedisCommands;
import java.util.Date;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
public class ScrollWindowLimiter {
private static final StatefulRedisConnection<String, String> conn = LettuceSentinelRedisUtil.getConn();
private static final RedisCommands<String, String> commands = conn.sync();
private static final long intervalTime = 1000L;
public static boolean tryAccess(String bucket, Integer qps) {
long currentTime = new Date().getTime();
long lower = currentTime/1000 * 1000;
long upper = currentTime/1000 * 1000 + 1000;
boolean exists = commands.exists(bucket) > 0;
if (exists) {
Range<Long> currentWindow = Range.create(lower, upper);
Range<Long> beforeWindow = Range.create(0L, lower);
Long count = commands.zcount(bucket, currentWindow);
System.out.println(count);
commands.zremrangebyscore(bucket, beforeWindow);
if (count != null && count > qps) {
return false;
} else {
commands.zadd(bucket, currentTime, UUID.randomUUID().toString());
return true;
}
} else {
commands.zadd(bucket, currentTime, UUID.randomUUID().toString());
return true;
}
}
public static void main(String[] args) throws InterruptedException {
Random r = new Random();
for (; ; ) {
boolean b = tryAccess("jobmd_job4resume", 10);
System.out.println("滚动窗口限流-是否通过:" + b);
int sleepTime = r.nextInt(170);
TimeUnit.MILLISECONDS.sleep(sleepTime);
}
}
}
SlidingWindowLimiter
package com.zjw.utils;
import io.lettuce.core.Range;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.sync.RedisCommands;
import java.util.Date;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
public class SlidingWindowLimiter {
private static final StatefulRedisConnection<String, String> conn = LettuceSentinelRedisUtil.getConn();
private static final RedisCommands<String, String> commands = conn.sync();
private static final long intervalTime = 1000L;
public static boolean tryAccess(String bucket, Integer qps) {
long currentTime = new Date().getTime();
boolean exists = commands.exists(bucket) > 0;
if (exists) {
Range<Long> inWindowRange = Range.create(currentTime - intervalTime, currentTime);
Range<Long> outWindowRange = Range.create(0L, currentTime - intervalTime);
Long count = commands.zcount(bucket, inWindowRange);
System.out.println(count);
commands.zremrangebyscore(bucket, outWindowRange);
if (count != null && count > qps) {
return false;
} else {
commands.zadd(bucket, currentTime, UUID.randomUUID().toString());
return true;
}
} else {
commands.zadd(bucket, currentTime, UUID.randomUUID().toString());
return true;
}
}
public static void main(String[] args) throws InterruptedException {
Random r = new Random();
for (; ; ) {
boolean b = tryAccess("jobmd_job4job", 10);
System.out.println("滑动窗口-是否通过:" + b);
int sleepTime = r.nextInt(170);
TimeUnit.MILLISECONDS.sleep(sleepTime);
}
}
}
TokenLimiter
package com.zjw.utils;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.sync.RedisCommands;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
public class TokenLimiter {
private static final StatefulRedisConnection<String, String> conn = LettuceSentinelRedisUtil.getConn();
private static final RedisCommands<String, String> commands = conn.sync();
private static final Timer timer = new Timer();
private static final ConcurrentHashMap<String, Integer> config = new ConcurrentHashMap<>();
static {
timer.schedule(new TimerTask() {
@Override
public void run() {
for (Map.Entry<String, Integer> entry : config.entrySet()) {
for (int i = 0; i < entry.getValue() / 4; i++) {
if (commands.llen(entry.getKey()) < entry.getValue()) {
commands.rpush(entry.getKey(), UUID.randomUUID().toString());
}
}
}
}
}, 0, 250);
}
public static void setConfig(String bucket, Integer qps) {
config.put(bucket, qps);
}
public static void delConfig(String bucket) {
config.remove(bucket);
}
public static boolean tryAccess(String bucket) {
Object result = commands.lpop(bucket);
return result != null;
}
public static void main(String[] args) throws InterruptedException {
TokenLimiter.setConfig("jobmd_resume4job", 10);
Random r = new Random();
for (; ; ) {
boolean b = TokenLimiter.tryAccess("jobmd_resume4job");
System.out.println("令牌桶限流-是否通过:" + b);
int sleepTime = r.nextInt(170);
TimeUnit.MILLISECONDS.sleep(sleepTime);
}
}
}