Redis集群搭建

    科技2022-08-09  99

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

    一、Redis集群的特点

    Redis集群提供了以下好处:

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

    Redis集群的不足:

    ·多键操作是不被支持的多键的Redis事务是不被支持的。lua脚本不被支持。由于集群方案出现较晚,很多公司已经采用了其他的集群方案,而代 理或者客户端分片的方案想要迁移至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如何分配这六个节点?一个集群至少要有三个主节点。

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

    四、在集群中录入值

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

    五、查询集群中的值

    六、集群的Jedis开发

    Processed: 0.010, SQL: 8