ITPub博客

首页 > IT职业 > IT生活 > 通过redis限制每秒钟只能处理n个请求

通过redis限制每秒钟只能处理n个请求

IT生活 作者:lslzf 时间:2021-09-09 10:28:50 0 删除 编辑

// 同一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/ ,如需转载,请注明出处,否则将追究法律责任。

下一篇: 测试用。。。
请登录后发表评论 登录
全部评论

注册时间:2019-04-15

  • 博文量
    102
  • 访问量
    870