惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
爱范儿
爱范儿
WordPress大学
WordPress大学
博客园 - 三生石上(FineUI控件)
J
Java Code Geeks
Vercel News
Vercel News
aimingoo的专栏
aimingoo的专栏
T
Tailwind CSS Blog
罗磊的独立博客
B
Blog
博客园_首页
A
About on SuperTechFans
有赞技术团队
有赞技术团队
V
V2EX
U
Unit 42
I
InfoQ
IT之家
IT之家
博客园 - 司徒正美
阮一峰的网络日志
阮一峰的网络日志
博客园 - 叶小钗
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Stack Overflow Blog
Stack Overflow Blog
The Cloudflare Blog
H
Help Net Security

沐凉`Blog

沐凉·日记 沐凉·日记 沐凉·日记 Flutter开发Linux程序中文乱码解决 | 沐凉`Blog Docker安装Nginx | 沐凉`Blog 沐凉·日记 flutter loading 动画插件(flutter_spinkit 4.1.2) | 沐凉`Blog 关于使用@CrossOrigin遇见的的一些问题和理解 | 沐凉`Blog 沐凉·日记 Linux 常用指令、IntelliJ IDEA 常用快捷键 | 沐凉`Blog 了解23种设计模式-单例模式 | 沐凉`Blog Spring Boot 邮件服务提供者 | 沐凉`Blog 沐凉·日记 Hello World | 沐凉`Blog Ubuntu Server 安装 Java、Tomcat、mysql | 沐凉`Blog
Docker 搭建redis单节点以及一主两从三哨兵模式 | 沐凉`Blog
2019-10-24 · via 沐凉`Blog

docker-compose.yml

1
2
3
4
5
6
7
8
9
10
11
version: '3.1'
services:
redis:
image: redis
container_name: my_redis
restart: always
command: redis-server --requirepass your_password
ports:
- 6379:6379
volumes:
- ./data:/data

使用Spring boot 连接配置application.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
spring:
application:
name: porject-name
redis:
database: 0
host: 127.0.0.1
port: 6379
password: 123456
timeout: 200
jedis:
pool:
max-idle: 8
min-idle: 0
max-wait: -1
max-active: 8

redis 集群部署 一主两从三哨兵

一主两从 docker-compose.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
version: '3.7'
services:
master:
image: redis
container_name: redis-master
restart: always
command: redis-server --port 6379 --requirepass your_password --appendonly yes
ports:
- 6379:6379
volumes:
- ./data:/data

slave1:
image: redis
container_name: redis-slave-1
restart: always
command: redis-server --slaveof redis-master 6379 --requirepass your_password --masterauth your_password --appendonly yes
ports:
- 6380:6379
volumes:
- ./data:/data


slave2:
image: redis
container_name: redis-slave-2
restart: always
command: redis-server --slaveof redis-master 6379 --requirepass your_password --masterauth your_password --appendonly yes
ports:
- 6381:6379
volumes:
- ./data:/data

检查 redis 是否部署成功

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 使用此命令进入docker 容器
docker exec -it redis-master bash
# 使用redis 客户端连接redis
redis-cli
# 认证
auth your_password
# 添加一条数据
set test1 test
# 再取出来
get test1

# 操作全部没问题就可以退出 master
exit
# 在以同样的方式进入 从服务器 测试是否可以拿到刚添加的数据 且不能使用 set 添加数据,说明 一主两从搭建成功
get test1

三哨兵 docker-compose.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
version: '3.7'
services:
sentinel1:
image: redis
container_name: redis-sentinel-1
command: redis-sentinel /usr/local/etc/redis/sentinel.conf
restart: always
ports:
- 26379:26379
volumes:
- ./sentinel1.conf:/usr/local/etc/redis/sentinel.conf

sentinel2:
image: redis
container_name: redis-sentinel-2
command: redis-sentinel /usr/local/etc/redis/sentinel.conf
restart: always
ports:
- 26380:26379
volumes:
- ./sentinel2.conf:/usr/local/etc/redis/sentinel.conf

sentinel3:
image: redis
container_name: redis-sentinel-3
command: redis-sentinel /usr/local/etc/redis/sentinel.conf
restart: always
ports:
- 26381:26379
volumes:
- ./sentinel3.conf:/usr/local/etc/redis/sentinel.conf

三哨兵 配置文件 sentinel.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
port 26379
dir /tmp
# 自定义集群名,其中 192.168.8.188 为 redis-master 的 ip,6379 为 redis-master 的端口,2 为最小投票数(因为有 3 台 Sentinel 所以可以设置成 2)
sentinel monitor mymaster 192.168.8.188 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel auth-pass mymaster your_password
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes

# 复制三份
cp sentinel.conf sentinel1.conf
cp sentinel.conf sentinel2.conf
cp sentinel.conf sentinel3.conf

检查是否可用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 进入sentinel
docker exec -it redis-sentinel-1 bash
# 连接 redis-sentinel
redis-cli -p 26379
# 查看从节点状态
sentinel slaves mymaster

# 查看到每个从节点都连接到主节点,就是正常的
# 31) "master-link-status"
# 32) "ok"

# 可以尝试把主节点停止,可以观察sentinel日志,发现sentinel 会重新选举主节点
docker-compose logs -f
docker stop redis-master

使用Spring boot 连接配置application.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
spring:
application:
name: porject-name
redis:
database: 0
password: your_password
timeout: 200
jedis:
pool:

max-idle: 8

min-idle: 0

max-wait: -1

max-active: 8
sentinel:
master: mymaster
nodes: 127.0.0.1:26379,127.0.0.1:26380,127.0.0.1:26381