目录
引出
1.Redis的1主2从搭建,主要是redis.conf文件的修改,核心bing允许任何端口访问;
2.Redis的3哨兵搭建,设置好自己的端口,监听的主的ip和端口,运行即可;
3.SpringBoot整合Redis哨兵初步;
redis主从搭建:一主2从
6389
6390
6391
6389Master
准备文件redis.conf
java
[root@localhost software]# mkdir -p 6389/conf 6389/data 6389/log
[root@localhost software]# ls
6380 6381 6389 canal rabbitmq
[root@localhost software]# cd 6389
[root@localhost 6389]# ls
conf data log
[root@localhost 6389]# tree
.
├── conf
├── data
└── log
3 directories, 0 files
拷贝之前的配置文件
java
[root@localhost conf]# pwd
/usr/local/software/6389/conf
[root@localhost conf]# cp ../../6380/conf/redis.conf ./
[root@localhost conf]# ls
redis.conf
[root@localhost conf]#
conf的修改:允许任何端口访问
关掉保护,可随意访问
端口映射6389---6379
为哨兵做的准备,暴露服务器的ip
打开AOF持久化方法
vim文件显示行数冒号 :set number
快速跳转:行号+gg
redis.log日志文件
linux中文件可执行
X 执行
R 读
W 写
RWX 111
运行容器
修改时区,,日志正常:-v /etc/localtime:/etc/localtime \
docker run-i:以交互模式运行容器
-t:为容器重新分配一个伪输入终端
--name :容器名称
--privileged: 设置容器公开权限(默认为true)
-p :映射端口 linux端口: 容器内置端口(mysql默认端口为3306)
-v : linux挂载文件夹/文件和容器内路径的映射
-e: 容器的环境变量(设置mysql默认用户名&密码)
-d: 后台运行容器,并返回容器ID
java
docker run -it \
--name redis_6389 \
--privileged \
-p 6389:6379 \
--network pet_docker_net \
--ip 172.18.12.80 \
-v /etc/localtime:/etc/localtime \
-v /usr/local/software/6389/conf/redis.conf:/usr/local/etc/redis/redis.conf \
-v /usr/local/software/6389/data/:/data \
-v /usr/local/software/6389/log/redis.log:/var/log/redis.log \
-d redis \
/usr/local/etc/redis/redis.conf
查看日志方式tail
cat/tail/head
cat 日志文件名称
tail 日志文件名称 --- 最新的日志内容:最新的日志
head 日志文件
java
[root@localhost log]# tail redis.log |grep version
1:C 12 Aug 2023 11:02:56.436 # Redis version=6.2.6, bits=64, commit=00000000, modified=0, pid=1, just started
[root@localhost log]#
从aof中读取的日志
6390Slave
获取主的内部的ip地址
"IPAddress": "172.18.12.80", 从配置主的端口
运行容器
java
docker run -it \
--name redis_6390 \
--privileged \
-p 6390:6379 \
--network pet_docker_net \
--ip 172.18.12.85 \
-v /etc/localtime:/etc/localtime \
-v /usr/local/software/6390/conf/redis.conf:/usr/local/etc/redis/redis.conf \
-v /usr/local/software/6390/data/:/data \
-v /usr/local/software/6390/log/redis.log:/var/log/redis.log \
-d redis \
/usr/local/etc/redis/redis.conf
6391Slave
java
[root@localhost 6391]# cd conf
[root@localhost conf]# cp ../../6390/conf/redis.conf ./
[root@localhost conf]# pwd
/usr/local/software/6391/conf
[root@localhost conf]# ll
总用量 92
-rwxr-xr-x. 1 root root 93909 8月 12 12:44 redis.conf
[root@localhost conf]# cd ..
[root@localhost 6391]# ls
conf data log
[root@localhost 6391]# cd log/
[root@localhost log]# touch redis.log
[root@localhost log]# ll
总用量 0
-rw-r--r--. 1 root root 0 8月 12 12:45 redis.log
[root@localhost log]# chmod 777 redis.log
[root@localhost log]# ll
总用量 0
-rwxrwxrwx. 1 root root 0 8月 12 12:45 redis.log
运行容器
java
docker run -it \
--name redis_6391 \
--privileged \
-p 6391:6379 \
--network pet_docker_net \
--ip 172.18.12.86 \
-v /etc/localtime:/etc/localtime \
-v /usr/local/software/6391/conf/redis.conf:/usr/local/etc/redis/redis.conf \
-v /usr/local/software/6391/data/:/data \
-v /usr/local/software/6391/log/redis.log:/var/log/redis.log \
-d redis \
/usr/local/etc/redis/redis.conf
搭建成功1主2从
java
[root@localhost log]# docker exec -it redis_6389 bash
root@013a85fa5f85:/data# redis-cli
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=172.18.12.85,port=6379,state=online,offset=784,lag=1
slave1:ip=172.18.12.86,port=6379,state=online,offset=784,lag=1
master_failover_state:no-failover
master_replid:9db2c05f3c084af02e48840cc9aa6467304ecd20
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:784
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:784
127.0.0.1:6379>
创建3哨兵
奇数个哨兵。
三哨兵策略: 1主二从
创建文件夹sentinel
java
[root@localhost conf]# touch sentinel.conf
[root@localhost conf]# vim sentinel.conf
[root@localhost conf]# cat sentinel.conf
# 所以无需担心端口重复使用
# 如果需要在单机
port 26389
# 设定密码认证
# requirepass 123456
# 配置哨兵的监控参数
# 格式:sentinel monitor <master-name> <ip> <redis-port> <quorum>
# master-name是为这个被监控的master起的名字
# ip是被监控的master的IP或主机名。因为Docker容器之间可以使用容器名访问,所以这里写master节点的容器名
# redis-port是被监控节点所监听的端口号
# quorom设定了当几个哨兵判定这个节点失效后,才认为这个节点真的失效了
sentinel monitor redis_6389 192.168.111.130 6389 2
# 连接主节点的密码
# 格式:sentinel auth-pass <master-name> <password>
# sentinel auth-pass local-master 123456
# master在连续多长时间无法响应PING指令后,就会主观判定节点下线,默认是30秒
# 格式:sentinel down-after-milliseconds <master-name> <milliseconds>
sentinel down-after-milliseconds redis_6389 30000
[root@localhost conf]#
创建运行哨兵容器
java
docker run -it \
--name sentinel_26389 \
--privileged \
--network pet_docker_net \
--sysctl net.core.somaxconn=1024 \
--ip 172.18.12.90 \
-p 26389:26389 \
-v /etc/localtime:/etc/localtime \
-v /usr/local/software/sentinel/26389/conf:/user/local/etc/redis/conf/ \
-d redis \
redis-sentinel /user/local/etc/redis/conf/sentinel.conf
java
docker run -it \
--name sentinel_26390 \
--privileged \
--network pet_docker_net \
--sysctl net.core.somaxconn=1024 \
--ip 172.18.12.91 \
-p 26390:26390 \
-v /etc/localtime:/etc/localtime \
-v /usr/local/software/sentinel/26390/conf:/user/local/etc/redis/conf/ \
-d redis \
redis-sentinel /user/local/etc/redis/conf/sentinel.conf
java
docker run -it \
--name sentinel_26391 \
--privileged \
--network pet_docker_net \
--sysctl net.core.somaxconn=1024 \
--ip 172.18.12.92 \
-p 26391:26391 \
-v /etc/localtime:/etc/localtime \
-v /usr/local/software/sentinel/26391/conf:/user/local/etc/redis/conf/ \
-d redis \
redis-sentinel /user/local/etc/redis/conf/sentinel.conf
1主2从搭建成功
查看哨兵日志
问题:脑裂问题
问题:
- 主由于某种网络原因,被哨兵判断为被动下线, 踢下线,2秒钟上线???
- 脑裂问题?
SpringBoot整合Redis哨兵
启动1主2从,3哨兵
[root@localhost log]# docker exec -it redis_6389 bash
root@013a85fa5f85:/data# redis-cli
127.0.0.1:6379> info replication
pom.xml文件
xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.tianju.redis</groupId>
<artifactId>springboot-redis-deep</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<!-- 起步依赖-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.13</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
写配置类,列出哨兵
yml
server:
port: 9099
spring:
redis:
sentinel:
master: redis_6389
nodes: 192.168.111.130:26389,192.168.111.130:26390,192.168.111.130:26391
写service存个值
java
package com.tianju.redis.service.impl;
import com.tianju.redis.service.ISentinelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
@Service
public class SentinelServiceImpl implements ISentinelService {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Override
public void add() {
stringRedisTemplate.opsForValue().set("stu", "pet");
}
}
写测试类
java
package com.tianju.redis.service.impl;
import com.tianju.redis.service.ISentinelService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.*;
@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public class SentinelServiceImplTest {
@Autowired
private ISentinelService sentinelService;
@Test
public void add() {
sentinelService.add();
}
}
总结
1.Redis的1主2从搭建,主要是redis.conf文件的修改,核心bing允许任何端口访问;
2.Redis的3哨兵搭建,设置好自己的端口,监听的主的ip和端口,运行即可;
3.SpringBoot整合Redis哨兵初步;