Redis 主从,哨兵,集群实战(四)
下载地址及版本说明
Redis 各版本下载地址: http://download.redis.io/releases/
版本说明:一般来说版本号第二位,偶数是稳定版本,奇数是在开发中的版本
本文基于Redis 版本为:3.2.12
理论依据文章为:
https://blog.csdn.net/sanri1993/article/details/101599701
https://blog.csdn.net/sanri1993/article/details/101620171
主从搭建
一主多从结构
结构
master 6379
slave 6380 -> 6379
slave 6381 -> 6379
搭建过程
- 配置节点
master 6379 redis6379.conf
port 6379
protected-mode no
daemonize yesslave 6380 redis6380.conf
port 6380
protected-mode no
daemonize yes
slaveof localhost 6379slave 6381 redis6381.conf
port 6381
protected-mode no
daemonize yes
slaveof localhost 6379- 启动
./redis-server conf/redis6379.conf
./redis-server conf/redis6380.conf
./redis-server conf/redis6381.conf - 查看是否启动成功
# 先看进程是否启动
netstat -tlnp | grep -E "6379|6380|6381"
# 查看拓扑结构
127.0.0.1:6379> info Replication
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=6380,state=online,offset=10459,lag=0
slave1:ip=127.0.0.1,port=6381,state=online,offset=10459,lag=0树状主从结构
结构
master 6379
slave 6380 -> 6379
slave 6381 -> 6380
搭建过程
和一主多从有变化的配置,其它环节一致
slave 6381 redis6381.conf
port 6381
protected-mode no
daemonize yes
slaveof localhost 6380哨兵环境搭建
现切换成一主多从结构 ,在一主多从的情况下,搭建三个哨兵
结构
sentinel 26379 sentinel 26380 sentinel 26381
master 6379
slave 6380 -> 6379
slave 6381 -> 6379
搭建过程
- 配置哨兵节点
sentinel 26379
port 26379
daemonize yes
sentinel monitor mymaster 127.0.0.1 6379 2sentinel 26380
port 26380
daemonize yes
sentinel monitor mymaster 127.0.0.1 6379 2sentinel 26381
port 26381
daemonize yes
sentinel monitor mymaster 127.0.0.1 6379 2- 启动哨兵
./redis-sentinel conf/sentinel26379.conf
./redis-sentinel conf/sentinel26380.conf
./redis-sentinel conf/sentinel26381.conf- 查看是否启动成功
# 查看进程是否启动成功
ps aux | grep sentinel
# 关闭主节点,看是否会选举一台从节点成为主节点
127.0.0.1:6379> shutdown
# 重启之前的主节点,一段时间后,查看是否成为 slave 节点集群环境搭建
结构
cluster master ->cluster slave
master 6379 -> slave6389
master 6380 -> slave6390
master 6381 -> slave6391
搭建过程
- 配置节点配置信息
master 6379
port 6379
protected-mode no
daemonize yes
cluster-enabled yes
cluster-node-timeout 15000
# . 相对于命令运行路径,最好写绝对路径
cluster-config-file ./nodes-6379.conf其它 master slave 只需要修改端口号即可,然后使用 ./redis-server <configfile> 启动
- 建立集群通信
# 将集群的每一个节点建立通信
192.168.108.128:6379>cluster meet ip port
# 查询集群节点
192.168.108.128:6379>cluster nodes - 映射数据槽
redis集群有16384个哈希槽,要把所有数据映射到16384槽,需要批量设置槽
# 查询集群状态
192.168.108.128:6381> cluster info
cluster_state:fail
# 显示为 fail 是还没有映射槽,还不能提供服务
redis-cli -h 192.168.108.128 -p 6379 cluster addslots {0..5461}
redis-cli -h 192.168.108.128 -p 6380 cluster addslots {5462..10922}
redis-cli -h 192.168.108.128 -p 6381 cluster addslots {10923..16383}- 配置主从,在从节点上操作,复制哪一个 master
192.168.152.128:6389> cluster replicate 9b7b0c22f95eb01fb9935ad4b04d396c7f99e881
192.168.152.128:6390> cluster replicate 5351c088472467ae485ed519cea271efda646bfa
192.168.152.128:6391> cluster replicate e718f126278072e1e180c3e518d73e0bc877b3dc- 测试集群是否正常工作
# 登录上之后,执行不同的 set 操作,看能否跳转至其它节点执行命令
./redis-cli -c SpringBoot 连接 redis 集群
- 引入 spring-boot-starter-data-redis
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>- 配置节点信息
spring.redis.cluster.nodes=192.168.108.128:6379,192.168.108.128:6380,192.168.108.128:6381一点小推广
创作不易,希望可以支持下我的开源软件,及我的小工具,欢迎来 gitee 点星,fork ,提 bug 。
Excel 通用导入导出,支持 Excel 公式
博客地址:https://blog.csdn.net/sanri1993/article/details/100601578
gitee:https://gitee.com/sanri/sanri-excel-poi
使用模板代码 ,从数据库生成代码 ,及一些项目中经常可以用到的小工具
博客地址:https://blog.csdn.net/sanri1993/article/details/98664034
gitee:https://gitee.com/sanri/sanri-tools-maven
相关文章