本次以一台虚拟机进行搭建,小编也是搭建了一星期,主从没啥问题,就是故障转移不行,根本原因就是Docker网络的问题,redis和哨兵不在一个网段中。很多教学都是用Host,但是不知道现在不能启动成功,所以还是要在一个网络中!

一、前言
redis在我们企业级开发中是很常见的,但是单个redis不能保证我们的稳定使用,所以我们要建立一个集群。
redis有两种高可用的方案:
- High availability with Redis Sentinel
- Scaling with Redis Cluster
第一个就是我们本次的要搭建的,就是高可用的哨兵,主redis挂掉,哨兵会进行投票进行故障转移!第二个就是分片集群,哨兵的一个缺点就是只能存在一个master节点,写的效率太低。分片集群就是解决哨兵的问题,可以水平扩展,提高redis的性能!
哨兵最低配是三哨兵,以奇数递增。分片集群最低配是三主三从。本次以一台虚拟机进行搭建,小编也是搭建了一星期,主从没啥问题,就是故障转移不行,根本原因就是docker网络的问题,redis和哨兵不在一个网段中。很多教学都是用host,但是不知道现在不能启动成功,所以还是要在一个网络中!
本次一体验为主,太多知识性的问题,大家可以去官网看一下。
redis官网
二、docker和docker compose安装
docker和docker compose安装使用【最新版】
三、启动redis主从
1、创建一个redis-sentinel-test文件夹
mkdir redis-sentinel-test
2、在里面创建两个文件夹
cd redis-sentinel-test/
mkdir redis
mkdir sentinel
3、在redis创建compose文件
必须以docker-compose.yml命名,本次测试redis就不挂载目录到宿主机了,需要的可以使用volumes挂载到宿主机!
4、编辑compose文件
这里为了测试方便,就不设置密码了!
protected-mode no:关闭就可以其他地方连接使用了。
slave-announce-ip:使用宿主机的ip。
version: "4.1"
services:
master:
image: redis:7.0.4
container_name: redis-master
command: bash -c "redis-server --protected-mode no --slave-announce-ip 192.168.84.143 --slave-announce-port 6379"
ports:
- 6379:6379
slave1:
image: redis:7.0.4
container_name: redis-slave-1
ports:
- 6380:6379
command: bash -c "redis-server --protected-mode no --slaveof redis-master 6379 --slave-announce-ip 192.168.84.143 --slave-announce-port 6380"
slave2:
image: redis:7.0.4
container_name: redis-slave-2
ports:
- 6381:6379
command: bash -c "redis-server --protected-mode no --slaveof redis-master 6379 --slave-announce-ip 192.168.84.143 --slave-announce-port 6381"
5、启动redis主从
compose更新了,启动由原来的-变成了空格。

6、重点提醒
我们启动后,docker compose会自动创建一个网络,就是以文件夹的名称+_default命名!
我们在编写sentinel的compose文件时,==要使用这个默认的网络==,不然就不在一个网段,故障转移无法切换!!

7、查看主从状态
进入主redis:
e77为容器id:
docker exec -it e77 /bin/bash
redis-cli

查看状态信息:

8、测试主从


四、启动三个哨兵
1、切换到sentinel文件夹
2、创建sentinel.conf文件
我们去官网找一个最低配的文件:
redis官网
因为这是两个实例,咱们只需要一个,所有只需要前四行即可:
protected-mode no
sentinel monitor mymaster 192.168.84.143 6379 2
sentinel down-after-milliseconds mymaster 60000
sentinel failover-timeout mymaster 180000
sentinel parallel-syncs mymaster 1
第一行:Redis 监控一个名为mymaster的redis集群,我们可以随意写;后面就是ip,我们宿主机的ip即可,端口为主redis的端口;2为哨兵投票的票数,当主redis宕机,三个哨兵必须两个哨兵都投票的redis才会变为主!!
第二行:Sentinel判断实例进入主观下线所需的时间,毫秒单位。
第三行:在指定的时间内未能完成failover故障转移,则任务故障转移失败。
第四行:限制在一次故障转移之后,每次向新的主节点同时发起复制操作节点个数,越大效率越慢。

创建sentinel1.conf文件,把上面四行粘贴进来!

复制两份,不需任何修改:
cp sentinel1.conf sentinel2.conf
cp sentinel1.conf sentinel3.conf
3、编写conpose文件
version: "4.2"
services:
sentinel1:
image: redis:7.0.4
container_name: redis-sentinel-1
ports:
- 26379:26379
command: redis-sentinel /usr/local/etc/redis/sentinel.conf
volumes:
- /mydata/redis-sentinel-test/sentinel/sentinel1.conf:/usr/local/etc/redis/sentinel.conf
sentinel2:
image: redis:7.0.4
container_name: redis-sentinel-2
ports:
- 26380:26379
command: redis-sentinel /usr/local/etc/redis/sentinel.conf
volumes:
- /mydata/redis-sentinel-test/sentinel/sentinel2.conf:/usr/local/etc/redis/sentinel.conf
sentinel3:
image: redis:7.0.4
container_name: redis-sentinel-3
ports:
- 26381:26379
command: redis-sentinel /usr/local/etc/redis/sentinel.conf
volumes:
- /mydata/redis-sentinel-test/sentinel/sentinel3.conf:/usr/local/etc/redis/sentinel.conf
networks:
default:
name: redis_default
external: true
networks:是呼应上面说的sentinel要和redis在一个网络里,这样才可以完成故障转移!
4、查看文件树
如果没有可以安装一下:

5、启动哨兵

6、查看哨兵信息
进入哨兵容器:
docker exec -it c8 /bin/bash
连接哨兵:

==一主二从三哨兵完成==

7、故障转移测试
我们把master给停掉:
8、故障转移日志

9、查看哨兵状态

此时主节点已自动切换为6381端口。
10、重启`6379`服务
我们发现刚刚新加入的6379会以从节点加入到主节点中!

五、整合SpringBoot
1、导入依赖
小编的springboot版本为:2.3.7.RELEASE。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
2、yml配置
server:
port: 8084
spring:
redis:
sentinel:
# sentinel.conf里的集群名称
master: mymaster
# 我们只需要连哨兵即可,哨兵内部会帮我们找到redis
nodes:
- 192.168.84.143:26379
- 192.168.84.143:26380
- 192.168.84.143:26381
3、json序列化配置
/**
* @author wangzhenjun
* @date 2022/8/18 16:37
*/
@Configuration
public class RedisConfig {
@Bean
@SuppressWarnings(value = { "unchecked", "rawtypes" })
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory)
{
RedisTemplate<Object, Object> template = new RedisTemplate<>();
template.setConnectionFactory(connectionFactory);
Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class);
// 使用StringRedisSerializer来序列化和反序列化redis的key值
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(serializer);
// Hash的key也采用StringRedisSerializer的序列化方式
template.setHashKeySerializer(new StringRedisSerializer());
template.setHashValueSerializer(serializer);
template.afterPropertiesSet();
return template;
}
}
4、新建controller测试
/**
* @author wangzhenjun
* @date 2022/8/18 17:48
*/
@RestController
public class TestController {
@Autowired
private RedisTemplate redisTemplate;
@GetMapping("/test")
public void saveRedis(){
redisTemplate.opsForValue().set("name","看到我就成功了");
}
5、项目结构

6、测试


7、查看redis是否有值


六、总结
小编经过一个星期的搭建终于完成了 ,最大的问题就是网络问题,最后终于解决了!!看了很多视频和教学,有的太模糊,小编特地花一天时间整体一个无坑版,希望能够帮到后来人!!