jedis连接redis集群_redis集群问题

2022-11-04 15:44:15 浏览数 (1)

启动redis集群

搭建redis集群,搭建具体过程如下 http://blog.csdn.net/lucky_ly/article/details/78703102 启动redis集群。

测试集群搭建在虚拟机的Linux系统下,客户机访问采用的windows环境下。 默认情况下redis只允许本机进行访问的。但我们为了方便测试,将redis.conf的bind 改为对应的Linux系统ip,这里为192.168.48.138

使用jedisCluster

代码语言:javascript复制
package com.redis;

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

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

public class RedisClusterTest { 
   
    public static void main(String[] args){
        //创建ip地址集合
        Set<HostAndPort> ips = new HashSet<HostAndPort>();
        ips.add(new HostAndPort("192.168.48.138", 7000));
        ips.add(new HostAndPort("192.168.48.138", 7001));
        ips.add(new HostAndPort("192.168.48.138", 7002));
        ips.add(new HostAndPort("192.168.48.138", 7003));
        ips.add(new HostAndPort("192.168.48.138", 7004));
        ips.add(new HostAndPort("192.168.48.138", 7005));

        JedisCluster cluster = new JedisCluster(ips);
        cluster.set("redisCluster", "pretty good");
        System.out.println(cluster.get("redisCluster"));
    }
}

使用spring装配JedisCluster

代码语言:javascript复制
<bean class="redis.clients.jedis.JedisCluster" id="jedisCluster">
<constructor-arg>
<set>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.48.138"/>
<constructor-arg name="port" value="7000"/>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.48.138"/>
<constructor-arg name="port" value="7001"/>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.48.138"/>
<constructor-arg name="port" value="7002"/>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.48.138"/>
<constructor-arg name="port" value="7003"/>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.48.138"/>
<constructor-arg name="port" value="7004"/>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.48.138"/>
<constructor-arg name="port" value="7005"/>
</bean>
</set>
</constructor-arg>
</bean> 
代码语言:javascript复制
package ly;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import redis.clients.jedis.JedisCluster;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring.xml")
public class JedisTest {
@Autowired
private  JedisCluster jedisCluster;
@Test
public void clusterTest(){
jedisCluster.set("goodMan", "ly");
System.out.println(jedisCluster.get("one"));
}
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/182051.html原文链接:https://javaforall.cn

0 人点赞