使用redis进行用户接口访问时间次数限制
来源:互联网 发布:做网站 知乎 编辑:程序博客网 时间:2024/05/22 17:05
假设一个用户(用IP判断)每分钟访问某一个服务接口的次数不能超过10次
import org.apache.commons.lang3.StringUtils;import org.slf4j.Logger;import redis.clients.jedis.Jedis;import redis.clients.jedis.Response;import redis.clients.jedis.Transaction;/** * * <p>Title:</p> */public class RateLimit {private static final Logger logger = LogUtil.get();private static final String RATE_LIMIT = "RATELIMIT";/** * @Title: allow @Description: 进行流量控制,允许访问返回true 不允许访问返回false * @param: @param key 放入redis的key,放入前要在key之前添加前缀 前缀配置在eds.properties中的 redis.prefix * @param: @param timeOut 超时时间单位秒 * @param: @param count 超时时间内允许访问的次数 * @param: @param type 不同类型的数据 * @param: @return * @param: @throws * Exception @return: boolean @throws */ public static boolean allow(String type,String key, int timeOut, int count) { // Boolean useFc = Boolean.valueOf(EdsPropertiesUtil.getInstance().getProperty("flowControl.use")); // // 若不使用流量控制直接返回true // if (!useFc) { // return true; // } boolean result = false; Jedis jedis = null; StringBuffer keyBuff = new StringBuffer(RATE_LIMIT); keyBuff.append("_").append(type).append(":").append(key); key = keyBuff.toString(); try { jedis = new Jedis(ConfigurationUtil.getRedisHost(), Integer.valueOf(ConfigurationUtil.getRedisPort())); if (StringUtils.isNoneEmpty(ConfigurationUtil.getRedisPassWord())) { jedis.auth(ConfigurationUtil.getRedisPassWord()); } jedis.connect(); Long newTimes = null; Long pttl = jedis.pttl(key); if (pttl > 0) { newTimes = jedis.incr(key); if (newTimes > count) { logger.info("key:{},超出{}秒内允许访问{}次的限制,这是第{}次访问", new Object[] { key, timeOut, count, newTimes }); } else { result = true; } } else if (pttl == -1 || pttl == -2 || pttl == 0) { Transaction tx = jedis.multi(); Response<Long> rsp1 = tx.incr(key); tx.expire(key, timeOut); tx.exec(); newTimes = rsp1.get(); if (newTimes > count) { logger.info("key:{},{}秒内允许访问{}次,第{}次访问", new Object[] { key, timeOut, count, newTimes }); } else { result = true; } } if (result) { logger.debug("key:{},访问次数{}", new Object[] { key, newTimes }); } } catch (Exception e) { logger.error("流量控制发生异常", e); e.printStackTrace(); // 当发生异常时 允许访问 result = true; } finally { jedis.close(); } return result; } }ConfigurationUtil 为配置文件中的值
方法调用:
// 限制器,限制在60秒之内最多登录5次
if (RateLimit.allow("RECOMMENDCODE",accountCode, 60, 5)) { //处理业务 }else{ //返回失败 }
阅读全文
2 0
- 使用redis进行用户接口访问时间次数限制
- 接口访问次数限制
- PHP结合Redis来限制用户或者IP某个时间段内访问的次数
- PHP中Yii2框架用redis实现限制接口访问次数
- nginx+lua 限制接口访问次数
- 在java项目中,如何限制每个用户访问接口的次数?
- 使用lua脚本编写访问次数限制
- Redis 过期时间、访问限制与缓存
- php 结合redis 限制用户登入 密码错误次数
- redis限制单ip、单用户的访问次数
- redis限制单ip、单用户的访问次数
- 如果用JS限制用户一天内访问的次数。
- 使用redis模拟每天用户可以进行某操作的次数。
- 使用cookie记录用户访问次数
- Java通过redis实现接口ip访问频率限制
- vc 限制软件的使用次数或时间
- vc 限制软件的使用次数或时间
- 拦截器限制用户访问次数和验证用户登录代码
- Activiti Modeler整合之后,部署之后流程图片显示乱码问题分析与解决
- Nginx多级代理,获取客户端真实请求IP以及每级代理IP
- spring与struts2的区别
- Learning URDF Step by Step
- PDO获取结果集
- 使用redis进行用户接口访问时间次数限制
- c++—引用。。。
- linux常用命令
- bat入门
- feurio中文版下载地址附使用教程
- windows 环境下在anaconda 3中安装python2和python3两个环境(python2和python3共存)
- 机器学习实战笔记-决策树
- XListView实现上拉加载下拉刷新,注意事项
- java.util.concurrent.locks