江西网站建设公司费用,小学全部课程免费教学软件,网页背景怎么设置,经典企业网站Linux系统Redis的哨兵架构配置 此处基于 Linux系统Redis的主从架构配置 进行哨兵高可用架构的搭建 此案例在一台虚拟机上启动6379和6380和6381三个reids主从实例#xff08;6379为主节点#xff0c;6380和6381为从节点#xff09;#xff0c;以及26379、26380、26381的sent…Linux系统Redis的哨兵架构配置 此处基于 Linux系统Redis的主从架构配置 进行哨兵高可用架构的搭建 此案例在一台虚拟机上启动6379和6380和6381三个reids主从实例6379为主节点6380和6381为从节点以及26379、26380、26381的sentinel哨兵集群 先准备好3份reids.conf配置 6379主节点redis-6379.conf # 端口号设置
port 6379# 持久化数据存储目录
dir ./data/6379/# 将端口号追加命名到pidfile配置的文件
pidfile /var/run/redis_6379.pid
logfile 6379.log6380从节点redis-6380.conf # 端口号设置
port 6380# 持久化数据存储目录
dir ./data/6380/# 将端口号追加命名到pidfile配置的文件
pidfile /var/run/redis_6380.pid
logfile 6380.log# 从6379主redis实例复制数据
replicaof 192.168.3.39 6379# 设置从节点只读
replica-read-only yes6381从节点redis-6381.conf # 端口号设置
port 6381# 持久化数据存储目录
dir ./data/6381/# 将端口号追加命名到pidfile配置的文件
pidfile /var/run/redis_6381.pid
logfile 6381.log# 从6379主redis实例复制数据
replicaof 192.168.3.39 6379# 设置从节点只读
replica-read-only yes启动三个主从节点 src/redis-server redis-6369.conf
src/redis-server redis-6380.conf
src/redis-server redis-6381.conf查看节点是否启动成功 [yunzelocalhost redis-5.0.14]$ ps -ef | grep redis
yunze 3505 1 0 22:12 ? 00:00:02 src/redis-server *:6379
yunze 3512 1 0 22:13 ? 00:00:02 src/redis-server *:6380
yunze 3802 1 0 22:18 ? 00:00:01 src/redis-server *:6381
yunze 4066 2797 0 22:28 pts/0 00:00:00 grep --colorauto redis三个节点启动成功 准备3份哨兵集群的配置 准备3份sentinel.conf配置文件 一定要先准备好所有配置文件再去启动否则如果复制了已启动的sentinel节点的配置文件会导致哨兵集群搭建失败因为sentinel启动之后会在当前节点使用的sentinel配置文件里追加写入 sentinel myid dfb8da08b09e8e97ad4a94cf177a911c654ca464 sentinel节点的myid 不能一样所以尽量先准备好配置文件后再依次启动 cp sentinel.conf sentinel-26379.conf
cp sentinel.conf sentinel-26380.conf
cp sentinel.conf sentinel-26381.conf26379节点sentinel-26379.conf调整配置 port 26379
daemonize yes
pidfile /var/run/redis-sentinel-26379.pid
logfile 26379.log
dir ./data/26379
# ip根据实际情况调整mymaster为主节点的名称
sentinel monitor mymaster 192.168.3.39 6379 226380节点sentinel-23680.conf调整配置 port 26380
daemonize yes
pidfile /var/run/redis-sentinel-26380.pid
logfile 26380.log
dir ./data/26380
# ip根据实际情况调整mymaster为主节点的名称最后的2是指需要有2个以上sentinel节点认为redis主节点失效才是真的失效一般为sentinel总数/21
sentinel monitor mymaster 192.168.3.39 6379 226381节点sentinel-23681.conf调整配置 port 26381
daemonize yes
pidfile /var/run/redis-sentinel-26381.pid
logfile 26381.log
dir ./data/26381
# ip根据实际情况调整mymaster为主节点的名称
sentinel monitor mymaster 192.168.3.39 6379 2启动哨兵集群 src/redis-sentinel sentinel-26379.conf
src/redis-sentinel sentinel-26380.conf
src/redis-sentinel sentinel-26381.conf查看节点是否启动成功 [yunzelocalhost redis-5.0.14]$ ps -ef | grep redis
yunze 3555 1 0 21:28 ? 00:00:00 src/redis-server *:6379
yunze 3560 1 0 21:28 ? 00:00:00 src/redis-server *:6380
yunze 3567 1 0 21:28 ? 00:00:00 src/redis-server *:6381
yunze 3673 1 0 21:28 ? 00:00:00 src/redis-sentinel *:26379 [sentinel]
yunze 3678 1 0 21:28 ? 00:00:00 src/redis-sentinel *:26380 [sentinel]
yunze 3683 1 0 21:28 ? 00:00:00 src/redis-sentinel *:26381 [sentinel]
yunze 3688 3103 0 21:28 pts/0 00:00:00 grep --colorauto redis至此哨兵架构搭建完成 查看哨兵架构节点信息 sentinel都启动成功之后会将整个哨兵集群的基础信息写入到所有sentinel的配置文件里的最下面 查看sentinel-23679.conf配置文件进行确认 sentinel known-replica mymaster 192.168.3.39 6381 # 表示主节点的复制节点及从节点信息
sentinel known-replica mymaster 192.168.3.39 6380 # 表示主节点的复制节点及从节点信息
sentinel known-sentinel mymaster 192.168.3.39 26381 25789bfe6f685c6f35d8710d4df85c344ef8a949
sentinel known-sentinel mymaster 192.168.3.39 26380 7f3308dfa55e6f488fd03f9eed2a8af5141a46c4由上述信息得到6380和6381节点都是从节点则6379就是主节点如果redis主节点挂了则哨兵集群会自动重新选出一个新的reids主节点并修改sentinel配置文件信息 如6379节点redis挂了则sentinel会从6380和6381节点选一个成为主节点假设选举出的新主节点为6381则此时sentinel的配置文件里的集群信息就会变为如下所示 sentinel known-replica mymaster 192.168.3.39 6380
sentinel known-replica mymaster 192.168.3.39 6379
sentinel known-sentinel mymaster 192.168.3.39 26381 25789bfe6f685c6f35d8710d4df85c344ef8a949
sentinel known-sentinel mymaster 192.168.3.39 26380 7f3308dfa55e6f488fd03f9eed2a8af5141a46c4且还会将之前配置的 sentinel monitor mymaster 192.168.3.39 6379 2修改为 sentinel monitor mymaster 192.168.3.39 6381 2而当6379节点重新启动之后哨兵集群会根据sentinel里的集群信息将6379redis节点作为从节点加入到整个集群 使用Spring Boot整合redis进行验证 加入依赖 dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-redis/artifactId/dependencyapplication.yml配置 spring:redis:database: 0timeout: 3000# 哨兵模式sentinel:# redis主节点的名称master: mymasternodes: 192.168.3.39:26379,192.168.3.39:26380,192.168.3.39:26381编写测试代码 项目运行时可关闭掉redis主节点测试哨兵集群自动选举主节点操作redis主节点挂掉后服务会发起10次重新连接之后会重新选举出一个新的主节点继续操作redis数据 import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;/*** author yunze* date 2023/7/31 0031 23:20*/
Slf4j
RestController
RequestMapping(/demo)
public class DemoController {Autowiredprivate StringRedisTemplate stringRedisTemplate;RequestMapping(/test_sentinel)public void testSentinel() {int i 1;while (true) {try {stringRedisTemplate.opsForValue().set(test- i, String.valueOf(i));log.info(设置key{}, test- i);i;Thread.sleep(1000);} catch (Exception e) {e.printStackTrace();log.error(出现异常{}, e.getMessage());}}}
}