Redis集群搭建

今天,观看尚硅谷Redis视频,讲到Redis集群搭建,为了避免以后遗忘,所以简单记录一下。

一、Redis集群的特点

Redis集群提供了以下好处:

  1. 实现扩容
  2. 分摊压力
  3. 无中心配置相对简单

Redis集群的不足:

  1. ·多键操作是不被支持的
  2. 多键的Redis事务是不被支持的。lua脚本不被支持。
  3. 由于集群方案出现较晚,很多公司已经采用了其他的集群方案,而代 理或者客户端分片的方案想要迁移至redis cluster,需要整体迁移而 不是逐步过渡,复杂度较大。

二、Redis集群搭建步骤

1、首先需要创建 6 个,redis.conf个文件,在这里,我在/opt/myRedis目录下创建了以下6个conf文件

在这里插入配置文件图片描述
2、在创建的配置文件中写入以下内容

include /opt/myRedis/redis.conf
pidfile /var/run/redis6379.pid
port 6379
dbfilename dump6379.rdb
#集群配置
cluster-enabled yes                  #打开集群模式
cluster-config-file nodes-6379.conf  #设置节点配置文件名
cluster-node-timeout 15000           #设置节点失联事件,超过该时间(毫秒),集群自动进行主从切换

3、启动redis

[root@localhost myRedis]# redis-server redis6379.conf
[root@localhost myRedis]# redis-server redis6380.conf
[root@localhost myRedis]# redis-server redis6381.conf
[root@localhost myRedis]# redis-server redis6389.conf
[root@localhost myRedis]# redis-server redis6390.conf
[root@localhost myRedis]# redis-server redis6391.conf

[root@localhost myRedis]# ll
总用量 160
-rw-r--r--. 1 root root   805 10月  5 11:18 nodes-6379.conf
-rw-r--r--. 1 root root   805 10月  5 11:18 nodes-6380.conf
-rw-r--r--. 1 root root   805 10月  5 11:18 nodes-6381.conf
-rw-r--r--. 1 root root   805 10月  5 11:18 nodes-6389.conf
-rw-r--r--. 1 root root   805 10月  5 11:18 nodes-6390.conf
-rw-r--r--. 1 root root   805 10月  5 11:18 nodes-6391.conf

[root@localhost myRedis]# ps -ef | grep redis
hh4uop    3589  3524  0 09:19 pts/1    00:00:00 vim redis
root      4111  3990  0 09:41 pts/4    00:00:00 vim redis.conf
root      4311  3990  0 09:57 pts/4    00:00:15 redis-sentinel *:26379 [sentinel]
root      5075     1  0 10:49 ?        00:00:07 redis-server *:6380 [cluster]
root      5196     1  0 10:55 ?        00:00:07 redis-server *:6381 [cluster]
root      5409     1  0 11:10 ?        00:00:05 redis-server *:6379 [cluster]
root      5514     1  0 11:15 ?        00:00:05 redis-server *:6389 [cluster]
root      5521     1  0 11:16 ?        00:00:05 redis-server *:6390 [cluster]
root      5527     1  0 11:16 ?        00:00:04 redis-server *:6391 [cluster]
root      6067  3267  0 12:04 pts/0    00:00:00 grep --color=auto redis

4、输入以下命令

[root@localhost /]# cd /opt
[root@localhost opt]# cd /opt/redis-6.0.8/src
[root@localhost src]# ./redis-cli --cluster  create 
10.132.22.220:6379 
10.132.22.220:6380 
10.132.22.220:6381 
10.132.22.220:6389 
10.132.22.220:6390 
10.132.22.220:6391 --cluster-replicas 1

在这里插入图片描述输入yes

在这里插入图片描述5、以集群的方式进入客户端

[root@localhost myRedis]# redis-cli -c -p 6379

6、通过cluster nodes命令查看集群信息

在这里插入图片描述发现有三个主服务器,三个从服务器

至此,搭建完成

三、redis cluster如何分配这六个节点?一个集群至少要有三个主节点。

  1. 一个集群至少要有三个主节点
  2. 选项–replicas 1表示我们希望为集群中的每介主节点创 建一个从节点。
  3. 分配原则尽量保证每个主数据库运行在不同的IP地址,每 个从库和主库不在一个IP地址上。

四、在集群中录入值

  1. 在redis-cli每次录入、查询键值,redis都会计算出该key应该送往的插槽,如果不是该客户端对应服务器的插槽,redis会报错,并告知应前往的redis实例地址和端口
  2. redis-cli客户端提供了-C参数实现自动重定向。 如redis-cli -c-p 6379登入后,再录入、查询键值对可以自动重定向
  3. 不在一个slot下的键值,是不能使用mget,mset等多键操作。
  4. 可以通过{来定义组的概念,从而使key中f内相同内容的键值对放到 一个slot中去。

五、查询集群中的值

在这里插入图片描述

六、集群的Jedis开发

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值