Jedis访问Redis集群

Jedis访问Redis集群

环境

  • centos6.5中配置了6个redis服务,ip映射到centos01上,端口分别是7001~7006
    • centos01 7001
    • centos01 7002
    • centos01 7003
    • centos01 7004
    • centos01 7005
    • centos01 7006
  • idea中新建maven工程,pom依赖文件引入jedis

     

    <dependencies>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency>
    </dependencies>
    

开启集群

  • 开启集群命令,开启相应的配置文件
    • redis-server 7001/redis.conf
    • redis-server 7002/redis.conf
    • redis-server 7003/redis.conf
    • redis-server 7004/redis.conf
    • redis-server 7005/redis.conf
    • redis-server 7006/redis.conf
  • 检查是否开启集群

连接测试

 

public static void main(String[] args) throws Exception {
    HostAndPort hp1 = new HostAndPort("centos01", 7001);
    HostAndPort hp2 = new HostAndPort("centos01", 7002);
    HostAndPort hp3 = new HostAndPort("centos01", 7003);
    HostAndPort hp4 = new HostAndPort("centos01", 7004);
    HostAndPort hp5 = new HostAndPort("centos01", 7005);
    HostAndPort hp6 = new HostAndPort("centos01", 7006);

    Set hs = new HashSet();
    hs.add(hp1);
    hs.add(hp2);
    hs.add(hp3);
    hs.add(hp4);
    hs.add(hp5);
    hs.add(hp6);

    JedisCluster jc = new JedisCluster(hs);
    jc.set("a", "test");
    jc.close();
}
  • centos查看key为a的值

以上操作都没问题,那么测试成功

常见的错误

  • java.net.ConnectException: Connection refused: connect
  • cluster is down
    • 连接失败--通常是redis集群没有完全起起来
    • 解决方案-重新启动shutdown的redis服务
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

乘风御浪云帆之上

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值