最近工作中遇到一个问题,公司运营团队(约100人)要对每日的订单进行审核,一般早上单量比较集中,但是到了下午单量就会变少,所以这边客服为了保证能够实时的进行有订单进来,一直会按着F5刷新页面,有时候确实没单子,但是100人同时一直按着F5不松手,导致服务器压力很大,观察日志一个客服评价每秒钟请求量达到3次。一个服务接口5分钟的请求量达到8w,多么可怕的一个数字。
所以针对这个接口,利用redis,在后台做了限流处理。大致逻辑是:
1.针对某一个客服,5秒钟请求量达到5次,跳转到休息页,倒计时5秒钟重新回到审单页面。
2.否则的话正常执行。
具体限流代码
//加一个限流
Long inc = 0L;
boolean exsits = jimRedisService.exsits(RxConstant.DISTRIBUTED_RECEIVE_LIMIT_NUM + WebHelper.getPin());
if (exsits) {
inc = jimRedisService.inc(RxConstant.DISTRIBUTED_RECEIVE_LIMIT_NUM + WebHelper.getPin());
logger.info("inc->num:pin:{},inrcNum:{}", WebHelper.getPin(), inc);
} else {
jimRedisService.setEx(RxConstant.DISTRIBUTED_RECEIVE_LIMIT_NUM + WebHelper.getPin(), "1", 3, TimeUnit.SECONDS);
}
if (inc > 5) {
try {
jimRedisService.del(RxConstant.DISTRIBUTED_RECEIVE_LIMIT_NUM + WebHelper.getPin());
model.addAttribute("timestamp", System.currentTimeMillis());
//转到超时限制页
return "frewarn";
} catch (Exception e) {
logger.error("limit num error:{}", e);
}
}
frewarn.jsp页面
<style type="text/css">
.changeFont {
color: red;
font-size: 18px;
}
</style>
<div class="row" style="padding-top: 10%">
<div class="col-md-4 col-md-offset-4" style="width: 250px">
<span id="spanTime"></span>
<div class="alert alert-danger" role="alert">
<strong>刷新太频繁了,休息一下再工作吧!</strong><br/><br/>
<a href="/distributeCategoryList?t=${timestamp}"><b>转到列表页</b></a>
</div>
</div>
</div>
<script type="text/javascript">
window.onload = function () {
//1.首先声明seconds
var seconds = 3;
//2.声明定时器
var timer = null;
//3.开启定时器
timer = setInterval(show, 1000);
//开启定时器后要执行的函数
function show() {
if (seconds == 0) {
clearInterval(timer);//清除定时器
// window.location.href = "http://test.rx.man.jd.com/distributeCategoryList";//跳转到百度首页
window.location = "/distributeCategoryList?t=" + new Date().getTime();//跳转到百度首页
return;
}
//将不断变化的秒数显示在页面上
document.getElementById('spanTime').innerHTML = '<span class="changeFont">' + seconds + '</span>' + "秒后返回审核页面!"; //这里添加的class仅仅是为了改变 变化的秒数的样式
seconds--;
}
};
</script>