首页 > IT职业 > IT生活 > 通过redis限制每秒钟只能处理n个请求
// 同一xx设置同时只可以操作8个并发的限制
if (xxx) {
String prefix = sipAccountInfo.getSipAccount();
String str = prefix.substring(0, prefix.indexOf("_"));
String today = MyDateUtils.formatLocalDateTime(LocalDateTime.now());
String concurrencyCalledCountKey = RedisKeyCenter.getConcurrencyCalledCountByGatewayKey(str, today);
Long result;
result = redisService.incrementKey(concurrencyCalledCountKey, 1);
result = result == null ? 0 : result;
if (result == 1) {
redisService.expire(concurrencyCalledCountKey, 1, TimeUnit.MINUTES);
}
while (result >= 8) {
logger.debug());
Thread.sleep(1000);
String newTime = MyDateUtils.formatLocalDateTime(LocalDateTime.now());
String countKey = RedisKeyCenter.getConcurrencyCalledCountByGatewayKey(str, newTime);
result = redisService.incrementKey(countKey, 1);
result = result == null ? 0 : result;
if (result == 1) {
redisService.expire(countKey, 1, TimeUnit.MINUTES);
}
}
}
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/1070/viewspace-2822537/ ,如需转载,请注明出处,否则将追究法律责任。