zoukankan      html  css  js  c++  java
  • 基于redis+lua实现的分布式限流

    public class Console {

    public static void main(String[] args) {
        Config config = new Config();
        config.setLockWatchdogTimeout(10000);
        config.useSingleServer().setAddress("redis://127.0.0.1:6379");
        RedissonClient redissonClient = Redisson.create(config);
    
        List<Object> keys = new ArrayList<>();
        keys.add("txk");
        Object[] values=new Object[]{1};
        Object eval = redissonClient.getScript().eval(READ_WRITE, RedisLua.LIMIT_LUA_STRING, RScript.ReturnType.INTEGER, keys, values);
        System.out.println(eval);
    
    }
    
    
    static class RedisLua {
    
        public static final String LIMIT_LUA_STRING;
    
        static {
            StringBuilder limitLuaString =new StringBuilder();
            limitLuaString.append(" local key = KEYS[1]");
            limitLuaString.append("
    local limit = tonumber(ARGV[1])");
            limitLuaString.append("
    local curentLimit = tonumber(redis.call('get', key) or "0")");
            limitLuaString.append("
    if curentLimit + 1 > limit then");
            limitLuaString.append("
    return 0");
            limitLuaString.append("
    else");
            limitLuaString.append("
     redis.call("INCRBY", key, 1)");
            limitLuaString.append("
    redis.call("EXPIRE", key, ARGV[2])");
            limitLuaString.append("
    return curentLimit + 1");
            limitLuaString.append("
    end");
            LIMIT_LUA_STRING=limitLuaString.toString();
        }
    }
    

    }

  • 相关阅读:
    OpenCV--图像特征(harris角点检测)
    pycharm处理命令行参数
    OpenCV--文档扫描OCR识别
    OpenCV--信用卡数字识别
    OpenCV--傅里叶变换
    OpenCV--直方图
    OpenCV--模板匹配
    OpenCV--图像轮廓
    OpenCV--图像金字塔
    51Nod-1288 汽油补给 ST表 贪心 模拟
  • 原文地址:https://www.cnblogs.com/merciless/p/14201332.html
Copyright © 2011-2022 走看看