关于Redis集群搭建和详解文章

文章结构

1.redis-cluster架构图解释
2.Redis集群的搭建
3.集群测试

1.redis-cluster架构图

这篇文章衔接最近写的一篇关于Redis缓存框架的入门文章。这篇属于高级使用,所涉及的东西也会多一点,所以大家认真阅读。下面笔者来说一下Redis集群的架构图。请看下图。-
在这里插入图片描述
redis-cluster投票:容错
在这里插入图片描述
架构细节:
(1)所有的redis节点彼此互联(PING-PONG机制),内部使用二进制协议优化传输速度和带宽.
(2)节点的fail是通过集群中超过半数的节点检测失效时才生效.
(3)客户端与redis节点直连,不需要中间proxy层.客户端不需要连接集群所有节点,连接集群中任何一个可用节点即可
(4)redis-cluster把所有的物理节点映射到[0-16383]slot上,cluster 负责维护node<->slot<->value
Redis 集群中内置了 16384 个哈希槽,当需要在 Redis 集群中放置一个 key-value 时,redis 先对 key 使用 crc16 算法算出一个结果,然后把结果对 16384 求余数,这样每个 key 都会对应一个编号在 0-16383 之间的哈希槽,redis 会根据节点数量大致均等的将哈希槽映射到不同的节点
在这里插入图片描述

2.Redis集群的搭建

Redis集群中至少应该有三个节点。要保证集群的高可用,需要每个节点有一个备份机。
Redis集群至少需要6台服务器。
因为搭建的需求,需要6台服务器,所以这里笔者为了方便演示,直接用一台服务器搭建一个伪分布式,一台服务器创建6个Redis实例,端口为7001-7006

2.1集群搭建环境

1,准备Redis安装包,根据自己系统需求进行上传到生产环境中。
2,使用ruby脚本搭建集群。需要ruby的运行环境。

安装ruby
yum install ruby
yum install rubygems

在这里插入图片描述
3,安装ruby脚本运行使用的包。根据下面命令进行执行

[root@localhost ~]# gem install redis-3.0.0.gem 
Successfully installed redis-3.0.0

在这里插入图片描述
解压Redis安装包 tar zxvf redis-3.0.0.tar.gz

[root@localhost ~]# cd redis-3.0.0/src
[root@localhost src]# ll *.rb
-rwxrwxr-x. 1 root root 48141 Apr  1  2015 redis-trib.rb

第一步:创建6个redis实例,每个实例运行在不同的端口。需要修改redis.conf配置文件。配置文件中还需要把cluster-enabled yes前的注释去掉。
在这里插入图片描述

第二步:启动每个redis实例。
第三步:使用ruby脚本搭建集群。
在这里插入图片描述
启动脚本
在这里插入图片描述
创建关闭集群的脚本:

[root@localhost redis-cluster]# vim shutdow-all.sh
redis01/redis-cluster-p 7001 shutdown
redis02/redis-cluster-p 7002 shutdown
redis03/redis-cluster-p 7003 shutdown
redis04/redis-cluster-p 7004 shutdown
redis05/redis-cluster-p 7005 shutdown
redis06/redis-cluster-p 7006 shutdown
[root@localhost redis-cluster]# chmod u+x shutdow-all.sh

启动Redis脚本
在这里插入图片描述

使用ruby脚本搭建集群。

./redis-trib.rb create --replicas 1 192.168.216.128:7001 192.168.216.128:7002 192.168.216.128:7003 192.168.216.128:7004 192.168.216.128:7005 192.168.216.128:7006

[root@localhost redis-cluster]# ./redis-trib.rb create --replicas 1 192.168.25.153:7001 192.168.25.153:7002 192.168.25.153:7003 192.168.25.153:7004 192.168.25.153:7005 192.168.25.153:7006
Creating cluster
Connecting to node 192.168.25.153:7001: OK
Connecting to node 192.168.25.153:7002: OK
Connecting to node 192.168.25.153:7003: OK
Connecting to node 192.168.25.153:7004: OK
Connecting to node 192.168.25.153:7005: OK
Connecting to node 192.168.25.153:7006: OK
Performing hash slots allocation on 6 nodes…
Using 3 masters:
192.168.25.153:7001
192.168.25.153:7002
192.168.25.153:7003
Adding replica 192.168.25.153:7004 to 192.168.25.153:7001
Adding replica 192.168.25.153:7005 to 192.168.25.153:7002
Adding replica 192.168.25.153:7006 to 192.168.25.153:7003
M: 2e48ae301e9c32b04a7d4d92e15e98e78de8c1f3 192.168.25.153:7001
slots:0-5460 (5461 slots) master
M: 8cd93a9a943b4ef851af6a03edd699a6061ace01 192.168.25.153:7002
slots:5461-10922 (5462 slots) master
M: 2935007902d83f20b1253d7f43dae32aab9744e6 192.168.25.153:7003
slots:10923-16383 (5461 slots) master
S: 74f9d9706f848471583929fc8bbde3c8e99e211b 192.168.25.153:7004
replicates 2e48ae301e9c32b04a7d4d92e15e98e78de8c1f3
S: 42cc9e25ebb19dda92591364c1df4b3a518b795b 192.168.25.153:7005
replicates 8cd93a9a943b4ef851af6a03edd699a6061ace01
S: 8b1b11d509d29659c2831e7a9f6469c060dfcd39 192.168.25.153:7006
replicates 2935007902d83f20b1253d7f43dae32aab9744e6
Can I set the above configuration? (type ‘yes’ to accept): yes
Nodes configuration updated Assign a different config epoch to each node
Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join…
Performing Cluster Check (using node 192.168.25.153:7001)
M: 2e48ae301e9c32b04a7d4d92e15e98e78de8c1f3 192.168.25.153:7001
slots:0-5460 (5461 slots) master
M: 8cd93a9a943b4ef851af6a03edd699a6061ace01 192.168.25.153:7002
slots:5461-10922 (5462 slots) master
M: 2935007902d83f20b1253d7f43dae32aab9744e6 192.168.25.153:7003
slots:10923-16383 (5461 slots) master
M: 74f9d9706f848471583929fc8bbde3c8e99e211b 192.168.25.153:7004
slots: (0 slots) master
replicates 2e48ae301e9c32b04a7d4d92e15e98e78de8c1f3
M: 42cc9e25ebb19dda92591364c1df4b3a518b795b 192.168.25.153:7005
slots: (0 slots) master
replicates 8cd93a9a943b4ef851af6a03edd699a6061ace01
M: 8b1b11d509d29659c2831e7a9f6469c060dfcd39 192.168.25.153:7006
slots: (0 slots) master
replicates 2935007902d83f20b1253d7f43dae32aab9744e6
[OK] All nodes agree about slots configuration.

Check for open slots…
Check slots coverage…
[OK] All 16384 slots covered.
[root@localhost redis-cluster]#

到这里搭建成功!

测试

    @Test
    public void jedisClusterTest() throws Exception {
        Set<HostAndPort> setJedis = new HashSet<>();
        setJedis.add(new HostAndPort("192.168.216.131", 7001));
        setJedis.add(new HostAndPort("192.168.216.131", 7002));
        setJedis.add(new HostAndPort("192.168.216.131", 7003));
        setJedis.add(new HostAndPort("192.168.216.131", 7004));
        setJedis.add(new HostAndPort("192.168.216.131", 7005));
        setJedis.add(new HostAndPort("192.168.216.131", 7006));
        JedisCluster jedisCluster = new JedisCluster(setJedis);
        jedisCluster.set("test1", "123");
        String test1 = jedisCluster.get("test1");
        System.out.println("test1 = " + test1);
        jedisCluster.close();
    }

在这里插入图片描述
文章结束,转载请带原链接!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Coding工匠

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

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

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

打赏作者

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

抵扣说明:

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

余额充值