前言
主要针对目前线上短信被脚本恶意盗刷的情况,用Redis实现滑动窗口限流
示例代码
public void checkCurrentWindowValue(String telNum) { String windowKey = CommonConstant.getNnSmsWindowKey(telNum); //获取当前时间戳 long currentTime = System.currentTimeMillis(); //1小时,默认只能发5次,参数smsWindowMax做成可配置项,配置到Nacos配置中心,可以动态调整 if (RedisUtil.hasKey(windowKey)) { //参数smsWindowTime表示限制的窗口时间 //这里获取当前时间与限制窗口时间之间的短信发送次数 Optional<Long> optional = Optional.ofNullable(RedisUtil.zCount(windowKey, currentTime - smsWindowTime, currentTime)); if (optional.isPresent()) { long count = optional.get(); if (count >= smsWindowMax) { log.error("==========>当前号码:{} 短信发送太频繁,{}", telNum, count); throw new ServiceException(MidRetCode.umid_10060); } } } StringBuilder sb =new StringBuilder(); String windowEle = sb.append(telNum).append(":").append(currentTime).toString(); //添加当前发送元素到zSet中(由于保证元素唯一,这里将元素加上了当前时间戳) RedisUtil.zAdd(windowKey, windowEle, currentTime); //设置2倍窗口Key:windowKey 的过期时间 RedisUtil.expire(windowKey, smsWindowTime*2, TimeUnit.MILLISECONDS); }