redis常用的架构有三种,单例、哨兵、集群,其他的都说过了,这里只简单介绍集群搭建。

单例最简单没什么好说的。

哨兵之前说过,该模式下有哨兵节点监视master和slave,若master宕机可自动将slave转为master,但它也有一个问题,就是不能动态扩充,并且存储大小受每个节点的内存大小限制。

集群模式Redis-Cluster,采用无中心结构,每个节点都和集群内其他节点有连接,数据可以跨主机分布式存储,解决了存储大小受主机限制的问题,Redis集群预分好16384个插槽(slot),每个节点分配一部分slot,当需要在 Redis 集群中放置一个 key-value 时,根据哈希算法决定将key放到哪个slot中,进而找到对应存放数据的主机,查询数据也一样。

集群模式内部同样可以配置主从,例如集群有六个数据节点,可以设置三个主节点,每个主节点对应一个从节点,当一个主节点宕机,可以自动将从节点变成主节点,保证整个集群还能用。但是一个主节点和对应的从节点都宕机后集群将不可用。每个主节点可以配置多个从节点。

集群搭建步骤

1、创建几台虚机,(集群模式最少要三个主节点)例如我们搭建一个三主三从的集群,创建六台虚机,当然要测试的话也可以部署在同一台主机上,使用不同的端口模拟不同主机。

2、在每台主机上安装redis,注意redis配置文件需要注意cluster相关的配置,例如修改cluster-enabled  的值是yes,表示开启集群,其他的超时时间等配置根据实际修改或默认。

3、redis安装完之后不会自己变成集群,还需要执行创建集群的命令,这个每个版本不太一样,redis5使用redis-cli --cluster ,redis3使用redis-trib.rb,下面具体说。

redis5集群创建

redis5的cluster相关命令,此处只写创建,当然还支持添加节点、删除、对solt进行操作等

[root@fvb9wbpl-clustermanager-e0hhllme ~]# redis-cli --cluster help
Cluster Manager Commands:
create host1:port1 ... hostN:portN
--cluster-replicas <arg>
check host:port
--cluster-search-multiple-owners
info host:port
fix host:port
--cluster-search-multiple-owners
reshard host:port
--cluster-from <arg>
--cluster-to <arg>
--cluster-slots <arg>
--cluster-yes
--cluster-timeout <arg>
--cluster-pipeline <arg>
--cluster-replace
rebalance host:port
--cluster-weight <node1=w1...nodeN=wN>
--cluster-use-empty-masters
--cluster-timeout <arg>
--cluster-simulate
--cluster-pipeline <arg>
--cluster-threshold <arg>
--cluster-replace
add-node new_host:new_port existing_host:existing_port
--cluster-slave
--cluster-master-id <arg>
del-node host:port node_id
call host:port command arg arg .. arg
set-timeout host:port milliseconds
import host:port
--cluster-from <arg>
--cluster-copy
--cluster-replace
help For check, fix, reshard, del-node, set-timeout you can specify the host and port of any working node in the cluster.

创建主从集群命令,后面是集群的ip地址和端口,cluster-replicas 1表示一个主节点对应1个从节点,创建成功后会自动分配主从关系

redis-cli --cluster create --cluster-replicas  10.110.30.136: 10.110.30.137: 10.110.30.138: 10.110.30.144: 10.110.30.145: 10.110.30.146:

出现提示后再输入yes,然后开始创建集群

创建成功后查看集群,后面的ip可以是集群中的任意一个节点,结果中的M代表主节点,当M节点宕机后集群会自动把S节点变成M节点,这个可以自行测试

[root@fvb9wbpl-clustermanager-e0hhllme ~]# redis-cli --cluster check 10.110.30.68:
10.110.30.70: (5e49929d...) -> keys | slots | slaves.
10.110.30.69: (6dc594c5...) -> keys | slots | slaves.
10.110.30.112: (2b9a0210...) -> keys | slots | slaves.
[OK] keys in masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 10.110.30.68:)
S: 15a969da394187e239baab1cc57bc6ec810996fe 10.110.30.68:
slots: ( slots) slave
replicates 2b9a0210b3223a1d865324ec32cc70f8ebcbaf65
S: 095777fb0d8ad380e420b9ef465d202a745a14e6 10.110.30.114:
slots: ( slots) slave
replicates 6dc594c5180f655603504ab68591b2e7492c7963
S: 20a33128f5353d36cb084d6ae2bf24c5b4806538 10.110.30.113:
slots: ( slots) slave
replicates 5e49929d47e7bd438ea2e25c3731179f460eb387
M: 5e49929d47e7bd438ea2e25c3731179f460eb387 10.110.30.70:
slots:[-] ( slots) master
additional replica(s)
M: 6dc594c5180f655603504ab68591b2e7492c7963 10.110.30.69:
slots:[-] ( slots) master
additional replica(s)
M: 2b9a0210b3223a1d865324ec32cc70f8ebcbaf65 10.110.30.112:
slots:[-] ( slots) master
additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All slots covered.

redis3集群创建

redis3不支持cluster,3版本创建集群使用redis-trib.rb,当然还需要ruby等不在这里介绍。

redis-trib.rb的参数跟cluster类似,了解了上面的下面这个一看就懂,基本一样的

[root@mucuijwb-clustermanager-o9sprluh bin]# /usr/local/bin/redis-trib.rb help
Usage: redis-trib <command> <options> <arguments ...> create host1:port1 ... hostN:portN
--replicas <arg>
check host:port
info host:port
fix host:port
--timeout <arg>
reshard host:port
--from <arg>
--to <arg>
--slots <arg>
--yes
--timeout <arg>
--pipeline <arg>
rebalance host:port
--weight <arg>
--auto-weights
--use-empty-masters
--timeout <arg>
--simulate
--pipeline <arg>
--threshold <arg>
add-node new_host:new_port existing_host:existing_port
--slave
--master-id <arg>
del-node host:port node_id
set-timeout host:port milliseconds
call host:port command arg arg .. arg
import host:port
--from <arg>
--copy
--replace
help (show this help) For check, fix, reshard, del-node, set-timeout you can specify the host and port of any working node in the cluster.

创建主从集群命令,参数含义跟cluster的一样

/usr/local/bin/redis-trib.rb create --replicas  10.110.30.74: 10.110.30.137: 10.110.30.138: 10.110.30.144: 10.110.30.145: 10.110.30.146:

检查集群

[root@mucuijwb-clustermanager-o9sprluh bin]# /usr/local/bin/redis-trib.rb check 10.110.30.74:
>>> Performing Cluster Check (using node 10.110.30.74:)
M: 7483d09dadda2ecebd9fdbd6b1f186769e00904d 10.110.30.74:
slots:- ( slots) master
additional replica(s)
M: 76ecc2cf2ddc07fe7f2fda53f2f5318198321761 10.110.30.72:
slots:- ( slots) master
additional replica(s)
M: 0f60c9c6595726d391805f5acfaf5be13496346a 10.110.30.73:
slots:- ( slots) master
additional replica(s)
S: 6f56bbe811be2ff97ef654777ae48c3ae5d83824 10.110.30.111:
slots: ( slots) slave
replicates 76ecc2cf2ddc07fe7f2fda53f2f5318198321761
S: 8c3bb1ed5e531665dfedf126df5121b76dc604d4 10.110.30.110:
slots: ( slots) slave
replicates 7483d09dadda2ecebd9fdbd6b1f186769e00904d
S: 543c3b42c93ccac1728eaa5d5c9a43938f922890 10.110.30.108:
slots: ( slots) slave
replicates 0f60c9c6595726d391805f5acfaf5be13496346a
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All slots covered.

集群连接

客户端连接集群模式可以直接连接redis节点,不需要再经过中间节点转发,客户端连接集群,需要加 -c 参数,否则插入数据会报错

[root@fvb9wbpl-rediscluster-vddntq84 ~]# redis-cli -c
127.0.0.1:> dbsize
(integer)
127.0.0.1:>

连接其他主机

[root@fvb9wbpl-rediscluster-vddntq84 ~]# redis-cli -c -h 10.110.30.113 -p
10.110.30.113:> dbsize
(integer)
10.110.30.113:>

插入数据和查询数据,注意对插槽slot的分配操作,可以看出来是分布式存储的

[root@fvb9wbpl-rediscluster-vddntq84 ~]# redis-cli -c -h 10.110.30.113 -p
10.110.30.113:> set foo bar
-> Redirected to slot [] located at 10.110.30.112:
OK10.110.30.112:> set c d
-> Redirected to slot [] located at 10.110.30.69:
OK
10.110.30.69:>
10.110.30.69:> get foo
-> Redirected to slot [] located at 10.110.30.112:
"bar"
10.110.30.112:> get c
-> Redirected to slot [] located at 10.110.30.69:
"d"
10.110.30.69:>

redis cluster(集群)模式的创建方式的更多相关文章

  1. Redis cluster集群模式的原理

    redis cluster redis cluster是Redis的分布式解决方案,在3.0版本推出后有效地解决了redis分布式方面的需求 自动将数据进行分片,每个master上放一部分数据 提供内 ...

  2. Azure Redis Cache (5) Redis Cache Cluster集群模式

    <Windows Azure Platform 系列文章目录> Redis Cluster 3.0之后的版本,已经支持Redis Cluster集群模式,Redis Cluster采用无中 ...

  3. centos6下redis cluster集群部署过程

    一般来说,redis主从和mysql主从目的差不多,但redis主从配置很简单,主要在从节点配置文件指定主节点ip和端口,比如:slaveof 192.168.10.10 6379,然后启动主从,主从 ...

  4. Ubuntu 16.04下Redis Cluster集群搭建(官方原始方案)

    前提:先安装好Redis,参考:http://www.cnblogs.com/EasonJim/p/7599941.html 说明:Redis Cluster集群模式可以做到动态增加节点和下线节点,使 ...

  5. Docker 搭建 Redis Cluster 集群环境

    使用 Docker 搭建 Redis Cluster,最重要的环节就是容器通信的问题,这一块我们在之前的文章中已经给大家解决了<Docker 网络模式详解及容器间网络通信>,本篇文章主要练 ...

  6. 搭建分布式 Redis Cluster 集群与 Redis 入门

    目录 Redis 集群搭建 Redis 是啥 集群(Cluster) Redis Cluster 说明 Redis Cluster 节点 Redis Cluster 集群模式 不能保证一致性 创建和使 ...

  7. Redis系列5:深入分析Cluster 集群模式

    Redis系列1:深刻理解高性能Redis的本质 Redis系列2:数据持久化提高可用性 Redis系列3:高可用之主从架构 Redis系列4:高可用之Sentinel(哨兵模式) 1 背景 前面我们 ...

  8. K8S部署Redis Cluster集群(三主三从模式) - 部署笔记

    一.Redis 介绍 Redis代表REmote DIctionary Server是一种开源的内存中数据存储,通常用作数据库,缓存或消息代理.它可以存储和操作高级数据类型,例如列表,地图,集合和排序 ...

  9. docker下创建redis cluster集群

    概述 在Redis中,集群的解决方案有三种 主从复制 哨兵机制 Cluster Redis Cluster是Redis的分布式解决方案,在 3.0 版本正式推出. 准备工作 1.确定本机IP地址 2. ...

随机推荐

  1. MySQL数据库相关资料

    python 全栈开发,Day60(MySQL的前戏,数据库概述,MySQL安装和基本管理,初识MySQL语句) python 全栈开发,Day61(库的操作,表的操作,数据类型,数据类型(2),完整 ...

  2. 【SQL】在数据库中发起http请求的小改进

    市面上常见的是用MSXML2.ServerXMLHTTP这个类,但这个类在发起异步请求时并不可靠,就是当send后并不一定会发出这个请求.这里推荐改用Microsoft.XMLHTTP,如果只是简单的 ...

  3. Visual Studio Code 调试 SpringBoot

    Build Anything with Spring Boot:Spring Boot is the starting point for building all Spring-based appl ...

  4. [非原创]Office 2019 增强版 批处理激活 亲测成功

    忘了原创网址了,在 https://www.52pojie.cn/ 上看到的,这里我备忘一下,希望知道原创网址的朋友告诉我一下,谢谢! 将下面批处理脚本存成.bat文件后,以管理员方式运行: @ech ...

  5. yso中URLDNS的pop链分析(重新分析整理)

    #发现之前对这个链关注的点有点问题,重新分析了一下 由于最近面试的过程中被问到了yso中URLDNS这个pop链的工作原理,当时面试因为是谈到shiro的怎么检测和怎么攻击时谈到了这个.其实在实战中用 ...

  6. 洛谷 题解 P1353 【[USACO08JAN]跑步Running】

    动态规划 状态 dp[i][j]表示第i分钟疲劳值为j的最大值 初始 全部都为一个最小值"0" 转移 考虑休息和走 如果当前疲劳值比时间要大,显然不可能出现这种情况 如果比时间小 ...

  7. 在UPW中使用VLC解码媒体

    VLC支持的格式很全,学会如何使用后,完全可以自己给自己写一个简单的万能播放器了. 源码来自github:https://github.com/kakone/VLC.MediaElement.git( ...

  8. UWP笔记-消息弹窗自动淡出

    为了让用户有个更好的UI交互,可以增加自动淡出的消息弹窗,例如:网易云音乐UWP,切换播放模式时,出现的类似消息提示. 右键项目,添加用户控件 UserControlDemo.xaml: <Us ...

  9. 【AtCoder】ARC059

    为啥这场ARC那么水--一个点就切完了 ARC059 C - いっしょ / Be Together 枚举就行 #include <bits/stdc++.h> #define fi fir ...

  10. Git在IDEA工具中快捷拉取代码

    在拥有GitLab账号之后, 进入IDEA中,点击vcs菜单-->Checkout from Version Control-->Git 随后会出现一个弹框,输入git上的项目地址点击CL ...