Java_api訪問Redis5.05集羣

代碼:

package com.cn.until;

import redis.clients.jedis.*;

import java.util.LinkedHashSet;
import java.util.Set;


public class RedisClusterUtil {

    private static volatile JedisCluster cluster;

    private RedisClusterUtil() {
    }

    public static JedisCluster getInstance() {
        if (cluster == null) {
            synchronized (RedisClusterUtil.class) {
                if (cluster == null) {
                    Set<HostAndPort> nodes = new LinkedHashSet<>();
                    nodes.add(new HostAndPort("master", 7001));
                    nodes.add(new HostAndPort("master", 7002));
                    nodes.add(new HostAndPort("master", 7003));
                    nodes.add(new HostAndPort("master", 8001));
                    nodes.add(new HostAndPort("master", 8002));
                    nodes.add(new HostAndPort("master", 8003));
                    cluster = new JedisCluster(nodes, 5000, 3000, 10, "123456", new JedisPoolConfig());
                    return cluster;
                }
            }
        }
        return cluster;
    }

    public static void main(String[] args) {
        JedisCluster cluster = RedisClusterUtil.getInstance();
        cluster.set("aaa","AAA");
        cluster.set("str","STR");
        System.out.println(cluster.get("aaa"));
        System.out.println(cluster.get("str"));
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章