redis集羣搭建+lua腳本的使用

詳細參考這篇文章(windows)java

https://blog.csdn.net/qiuyufeng/article/details/70474001node

 

1、使用JAVA代碼操做redis集羣redis

複製代碼
public static void main(String[] args) throws Exception {
    JedisPoolConfig poolConfig = new JedisPoolConfig();
    // 最大鏈接數  
        poolConfig.setMaxTotal(1);
    // 最大空閒數  
        poolConfig.setMaxIdle(1);
    // 最大容許等待時間,若是超過這個時間還未獲取到鏈接,則會報JedisException異常:  
    // Could not get a resource from the pool  
        poolConfig.setMaxWaitMillis(1000);
    Set<HostAndPort> nodes = new LinkedHashSet<HostAndPort>();
        nodes.add(new HostAndPort("127.0.0.1", 6379));
        nodes.add(new HostAndPort("127.0.0.1", 6380));
        nodes.add(new HostAndPort("127.0.0.1", 6381));
        nodes.add(new HostAndPort("127.0.0.1", 6382));
        nodes.add(new HostAndPort("127.0.0.1", 6383));
        nodes.add(new HostAndPort("127.0.0.1", 6384));
    JedisCluster cluster = new JedisCluster(nodes, poolConfig);
    String name = cluster.get("name");
        System.out.println(name);
        cluster.set("age", "18");
        System.out.println(cluster.get("age"));
        try {
        cluster.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
複製代碼

 

2、使用JAVA代碼操做lua腳本windows

  一、編寫lua腳本lua

  

redis.call(\"SET\",KEYS[1],ARGV[1]);\n"
            + "redis.call(\"SET\",KEYS[2],ARGV[2]);

 

  二、java代碼spa

  

複製代碼
public static void main(String[] args) throws Exception {
    JedisPoolConfig poolConfig = new JedisPoolConfig();
    // 最大鏈接數
        poolConfig.setMaxTotal(1);
    // 最大空閒數
        poolConfig.setMaxIdle(1);
    // 最大容許等待時間,若是超過這個時間還未獲取到鏈接,則會報JedisException異常:
    // Could not get a resource from the pool
        poolConfig.setMaxWaitMillis(1000);
    Set<HostAndPort> nodes = new LinkedHashSet<HostAndPort>();
        nodes.add(new HostAndPort("127.0.0.1", 6379));
        nodes.add(new HostAndPort("127.0.0.1", 6380));
        nodes.add(new HostAndPort("127.0.0.1", 6381));
        nodes.add(new HostAndPort("127.0.0.1", 6382));
        nodes.add(new HostAndPort("127.0.0.1", 6383));
        nodes.add(new HostAndPort("127.0.0.1", 6384));
    JedisCluster cluster = new JedisCluster(nodes, poolConfig);
    String lua = "redis.call(\"SET\",KEYS[1],ARGV[1]);\n"
            + "redis.call(\"SET\",KEYS[2],ARGV[2]);";

    String[] p = {"{a}a1","{a}a2","a","b"};
    Object eval = cluster.eval(lua, 2, p);
        try {
        cluster.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
複製代碼

須要注意的時,redis集羣執行lua操做的時候,要求key值必需要在同一個solt上面,爲了達到這個目的,能夠在key值中增長「{xx}」內容,這樣redis在計算hash槽的時候會按{}內的內容計算hash值;.net

能夠參考這篇文章https://blog.csdn.net/jing956899449/article/details/53338282code

相關文章
相關標籤/搜索