wget https://codeload.github.com/redis/redis/tar.gz/refs/tags/7.2.3
解压
tar -xzvf 7.2.3
切换到src目录
make
创建目录
mkdir master-6379 slave-6380 slave-6381 sentinel-26379 sentinel-26380 sentinel-26381
拷贝redis.conf到master-6379目录
cp redis.conf ../master-6379
配置节点
4.1 主节点配置
#允许所有网络访问,根据需要指定
bind 0.0.0.0
# 保护模式设置为no,允许其他主机访问
protected-mode no
#设置密码
requirepass 123456
# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
# When Redis is supervised by upstart or systemd, this parameter has no impact.
daemonize yes
pidfile /var/run/redis_6379.pid
#设置logfile
logfile /var/log/redis_6379
#主从同步master密码
masterauth 123456
4.2 从节点配置
复制主节点配置到从节点目录,修改配置文件;以下为修改点
port 6380
pidfile /var/run/redis_6380.pid
logfile /var/log/redis_6380
#replicaof <masterip> <masterport>
replicaof 10.211.55.19 6379
port 6381
pidfile /var/run/redis_6381.pid
logfile /var/log/redis_6381
#replicaof <masterip> <masterport>
replicaof 10.211.55.19 6379
4.3 哨兵节点配置
如下为哨兵节点配置,三个哨兵节点需要修改端口,pidfile和logfile配置
protected-mode no
#设置端口
port 26379
pidfile /var/run/redis-sentinel-26379.pid
logfile /var/log/redis-sentinel-26379
#设置监听主节点
sentinel monitor mymaster 10.211.55.19 6379 2
#配置密码
sentinel auth-pass mymaster 123456
启动数据节点
[root@10 src]# ./redis-server ../../master-6379/redis.conf
[root@10 src]# ./redis-server ../../slave-6380/redis.conf
[root@10 src]# ./redis-server ../../slave-6381/redis.conf
启动哨兵
[root@10 src]# ./redis-sentinel ../../sentinel-26379/sentinel.conf
[root@10 src]# ./redis-sentinel ../../sentinel-26380/sentinel.conf
[root@10 src]# ./redis-sentinel ../../sentinel-26381/sentinel.conf
#集群链接,默认端口,密码123456
./redis-cli -a 123456
[root@10 src]# ./redis-cli -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=10.211.55.19,port=6380,state=online,offset=2998,lag=1
slave1:ip=10.211.55.19,port=6381,state=online,offset=2998,lag=1
master_failover_state:no-failover
master_replid:93a66d0147afbcf864e4d86abaf093d4c5fb4a03
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:3137
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:3137
停用主节点,集群仍然可用
引入依赖及SpringBoot配置
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
spring.redis.password=123456
spring.redis.sentinel.master=mymaster
spring.redis.sentinel.nodes=10.211.55.19:26379,10.211.55.19:26380,10.211.55.19:26381
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。