redis-Jedis连接集群

Posted lijianda

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了redis-Jedis连接集群相关的知识,希望对你有一定的参考价值。

关闭防火墙或把端口加入防火墙

一、通过代码

@Test
public void testJedisCluster() throws Exception 
    //创建一连接,JedisCluster对象,在系统中是单例存在
    Set<HostAndPort> nodes = new HashSet<>();
    nodes.add(new HostAndPort("127.0.0.1", 7001));
    nodes.add(new HostAndPort("127.0.0.1", 7002));
    nodes.add(new HostAndPort("127.0.0.1", 7003));
    nodes.add(new HostAndPort("127.0.0.1", 7004));
    nodes.add(new HostAndPort("127.0.0.1", 7005));
    nodes.add(new HostAndPort("127.0.0.1", 7006));
    JedisCluster cluster = new JedisCluster(nodes);
    //执行JedisCluster对象中的方法,方法和redis一一对应。
    cluster.set("cluster-test", "my jedis cluster test");
    String result = cluster.get("cluster-test");
    System.out.println(result);
    //程序结束时需要关闭JedisCluster对象
    cluster.close();

 

二、通过spring(ioc控制反转)

1、配置XXX.xml

<!-- redis集群 -->
<bean id="jedisCluster" class="redis.clients.jedis.JedisCluster">
    <constructor-arg index="0">
        <set>
            <bean class="redis.clients.jedis.HostAndPort">
                <constructor-arg index="0" value="192.168.101.3"></constructor-arg>
                <constructor-arg index="1" value="7001"></constructor-arg>
            </bean>
            <bean class="redis.clients.jedis.HostAndPort">
                <constructor-arg index="0" value="192.168.101.3"></constructor-arg>
                <constructor-arg index="1" value="7002"></constructor-arg>
            </bean>
            <bean class="redis.clients.jedis.HostAndPort">
                <constructor-arg index="0" value="192.168.101.3"></constructor-arg>
                <constructor-arg index="1" value="7003"></constructor-arg>
            </bean>
            <bean class="redis.clients.jedis.HostAndPort">
                <constructor-arg index="0" value="192.168.101.3"></constructor-arg>
                <constructor-arg index="1" value="7004"></constructor-arg>
            </bean>
            <bean class="redis.clients.jedis.HostAndPort">
                <constructor-arg index="0" value="192.168.101.3"></constructor-arg>
                <constructor-arg index="1" value="7005"></constructor-arg>
            </bean>
            <bean class="redis.clients.jedis.HostAndPort">
                <constructor-arg index="0" value="192.168.101.3"></constructor-arg>
                <constructor-arg index="1" value="7006"></constructor-arg>
            </bean>
        </set>
    </constructor-arg>
    <constructor-arg index="1" ref="jedisPoolConfig"></constructor-arg>
</bean>

2、测试代码

private ApplicationContext applicationContext;
    @Before
    public void init() 
        applicationContext = new ClassPathXmlApplicationContext(
                "classpath:applicationContext.xml");
    

    // redis集群
    @Test
    public void testJedisCluster() 
        JedisCluster jedisCluster = (JedisCluster) applicationContext
                .getBean("jedisCluster");
        jedisCluster.set("name", "zhangsan");
        String value = jedisCluster.get("name");
        System.out.println(value);
    

 

以上是关于redis-Jedis连接集群的主要内容,如果未能解决你的问题,请参考以下文章

Redis-Jedis使用Scan实现redis前缀key查找

maven怎么连接hadoop集群

多节点 cassandra 集群:连接错误:('无法连接到任何服务器')

用jedis连接Redis集群

Hazelcast 客户端与整个集群断开连接

linux集群系列 --- LVS之负载均衡集群 --- 持久连接