redis 集群、哨兵访问代码

微笑、不失礼 提交于 2019-11-30 03:53:30

之前有用过redis集群,现在做一个总结


import java.util.HashSet;
import java.util.Set;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisSentinelPool;

public class RedisCluster {
    public static void  cluster() {
        Set<HostAndPort> nodes=new HashSet<HostAndPort>();
        HostAndPort node7001=new HostAndPort("192.168.3.153", 7001);
        HostAndPort node7002=new HostAndPort("192.168.3.153", 7002);
        HostAndPort node7003=new HostAndPort("192.168.3.153", 7003);
        HostAndPort node7004=new HostAndPort("192.168.3.153", 7004);
        HostAndPort node7005=new HostAndPort("192.168.3.153", 7005);
        HostAndPort node7006=new HostAndPort("192.168.3.153", 7006);
        nodes.add( node7001 );
        nodes.add( node7002 );
        nodes.add( node7003 );
        nodes.add( node7004 );
        nodes.add( node7005 );
        nodes.add( node7006 );
        JedisPoolConfig pool=new JedisPoolConfig();
        JedisCluster cluster=new JedisCluster( nodes,1000,1000,6,"123456",pool);
         cluster.set("aa", "abc");
        System.out.println(cluster.get("aa"));  ;
        try {
            cluster.close();
        } catch ( Exception e) {
            e.printStackTrace();
        }
    }
    public static void sentinel() {
        String masterName="mymaster";
        //sentinel地址集合
        Set<String>set=new HashSet<String>();
        set.add("192.168.3.153:12001");
        set.add("192.168.3.153:12002");
        set.add("192.168.3.153:12003");
        JedisPoolConfig pool=new JedisPoolConfig();
        pool.setMaxIdle(10); 
        pool.setMaxTotal(10); 
        pool.setMaxWaitMillis(10);
        pool.setJmxEnabled(true);
        JedisSentinelPool jSentinelPool =new JedisSentinelPool(masterName,set,pool,10,"123456"); 
        jSentinelPool.getResource().auth("123456");
        jSentinelPool.getResource().set("aa", "111111111111111");
 System.out.println( jSentinelPool.getResource().get("aa"));
 jSentinelPool.close();
    }
}

 

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!