Docker安装Redis+Sentinel哨兵集群,SpringBoot连接Redis集群配置

一、准备工作

  1. 两台Centos7服务器(虚拟机即可)
    • 192.168.32.131(主)
    • 192.168.32.129(从)
  2. 两台服务器安装Docker
  3. Docker下载Redis镜像

二、Redis配置主从节点

1、131服务器安装Redis主节点

创建Redis配置文件和数据挂载目录

jsx 复制代码
mkdir -p /data/redisMaster

cd /data/redisMaster

touch myredis.conf

vim myredis.conf

myredis.conf配置文件内容:

jsx 复制代码
# 允许任何IP访问
bind 0.0.0.0
# 关闭保护模式
protected-mode no
# 设置端口号
port 6380
timeout 0
save 900 1
save 300 10
save 60 10000
rdbcompression yes
dbfilename dump.rdb
dir /data
appendonly yes
appendfsync everysec

# 设置Redis密码
# requirepass 123456

# 设置外部访问IP,如果不加,其他IP访问会报错
slave-announce-ip 192.168.32.131

Docker启动Redis主节点命令:

jsx 复制代码
docker run --restart=always \
--log-opt max-size=100m \
--log-opt max-file=2 \
-p 6380:6380 \
--name redisMaster \
-v /data/redisMaster/myredis.conf:/etc/redis/redis.conf \
-v /data/redisMaster/data:/data \
-d redis redis-server /etc/redis/redis.conf

2、131服务器安装Redis从节点1

大体步骤与主节点类似,配置文件略有不同,请仔细观看

jsx 复制代码
mkdir -p /data/redisSlave1

cd /data/redisSlave1

touch myredis.conf

vim myredis.conf
jsx 复制代码
bind 0.0.0.0
protected-mode no
port 6381
timeout 0
save 900 1
save 300 10
save 60 10000
rdbcompression yes
dbfilename dump.rdb
dir /data
appendonly yes
appendfsync everysec
slave-announce-ip 192.168.32.131
# 设置主节点IP和端口号
replicaof 192.168.32.131 6380
jsx 复制代码
docker run --restart=always \
--log-opt max-size=100m \
--log-opt max-file=2 \
-p 6381:6381 \
--name redisSlave1 \
-v /data/redisSlave1/myredis.conf:/etc/redis/redis.conf \
-v /data/redisSlave1/data:/data \
-d redis redis-server /etc/redis/redis.conf

3、131服务器安装Redis从节点2

大体步骤与主节点类似,配置文件略有不同,请仔细观看

jsx 复制代码
mkdir -p /data/redisSlave2

cd /data/redisSlave2

touch myredis.conf

vim myredis.conf
jsx 复制代码
bind 0.0.0.0
protected-mode no
port 6382
timeout 0
save 900 1
save 300 10
save 60 10000
rdbcompression yes
dbfilename dump.rdb
dir /data
appendonly yes
appendfsync everysec
slave-announce-ip 192.168.32.131
# 设置主节点IP和端口号
replicaof 192.168.32.131 6380
jsx 复制代码
docker run --restart=always \
--log-opt max-size=100m \
--log-opt max-file=2 \
-p 6382:6382 \
--name redisSlave2 \
-v /data/redisSlave2/myredis.conf:/etc/redis/redis.conf \
-v /data/redisSlave2/data:/data \
-d redis redis-server /etc/redis/redis.conf

4、129服务器安装Redis从节点3

大体步骤与主节点类似,配置文件略有不同,请仔细观看

jsx 复制代码
mkdir -p /data/redisSlave3

cd /data/redisSlave3

touch myredis.conf

vim myredis.conf
jsx 复制代码
bind 0.0.0.0
protected-mode no
port 6383
timeout 0
save 900 1
save 300 10
save 60 10000
rdbcompression yes
dbfilename dump.rdb
dir /data
appendonly yes
appendfsync everysec
slave-announce-ip 192.168.32.129
# 设置主节点IP和端口号
replicaof 192.168.32.131 6380
jsx 复制代码
docker run --restart=always \
--log-opt max-size=100m \
--log-opt max-file=2 \
-p 6383:6383 \
--name redisSlave3 \
-v /data/redisSlave3/myredis.conf:/etc/redis/redis.conf \
-v /data/redisSlave3/data:/data \
-d redis redis-server /etc/redis/redis.conf

5、验证主从节点配置正确性

jsx 复制代码
docker exec -it redisMaster bash

redis-cli -p 6380

info Replication

如果上述配置没问题的话,正常可以看到主从节点的所有信息。

三、Docker安装Sentinel监听Redis

1、131服务器安装Sentinel1、Sentinel2、Sentinel3

创建配置文件存放目录、创建配置文件、编辑sentinel配置文件

jsx 复制代码
mkdir -p /data/sentinel1
mkdir -p /data/sentinel2
mkdir -p /data/sentinel3

touch /data/sentinel1/sentinel1.conf
touch /data/sentinel2/sentinel2.conf
touch /data/sentinel3/sentinel3.conf

sentinel1.conf配置文件内容:

jsx 复制代码
# 关闭保护模式
protected-mode no
# 配置端口号,各个节点不能相同
port 26379
daemonize no
pidfile /var/run/redis-sentinel.pid
loglevel notice
# 日志存放地址
logfile "/var/tmp/sentinel.log"
dir /tmp
# 外部IP访问地址,解决Docker-Net容器IP跨服务器无法访问问题
sentinel announce-ip 192.168.32.131
# 设置Redis主节点信息
sentinel monitor mymaster 192.168.32.131 6380 2
sentinel down-after-milliseconds mymaster 30000
acllog-max-len 128
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
SENTINEL resolve-hostnames no
SENTINEL announce-hostnames no
SENTINEL master-reboot-down-after-period mymaster 0

sentinel2.conf配置文件内容:

jsx 复制代码
# 关闭保护模式
protected-mode no
# 配置端口号,各个节点不能相同
port 26380
daemonize no
pidfile /var/run/redis-sentinel.pid
loglevel notice
# 日志存放地址
logfile "/var/tmp/sentinel.log"
dir /tmp
# 外部IP访问地址,解决Docker-Net容器IP跨服务器无法访问问题
sentinel announce-ip 192.168.32.131
# 设置Redis主节点信息
sentinel monitor mymaster 192.168.32.131 6380 2
sentinel down-after-milliseconds mymaster 30000
acllog-max-len 128
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
SENTINEL resolve-hostnames no
SENTINEL announce-hostnames no
SENTINEL master-reboot-down-after-period mymaster 0

sentinel3.conf配置文件内容:

jsx 复制代码
# 关闭保护模式
protected-mode no
# 配置端口号,各个节点不能相同
port 26381
daemonize no
pidfile /var/run/redis-sentinel.pid
loglevel notice
# 日志存放地址
logfile "/var/tmp/sentinel.log"
dir /tmp
# 外部IP访问地址,解决Docker-Net容器IP跨服务器无法访问问题
sentinel announce-ip 192.168.32.131
# 设置Redis主节点信息
sentinel monitor mymaster 192.168.32.131 6380 2
sentinel down-after-milliseconds mymaster 30000
acllog-max-len 128
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
SENTINEL resolve-hostnames no
SENTINEL announce-hostnames no
SENTINEL master-reboot-down-after-period mymaster 0

2、129服务器安装Sentinel4

jsx 复制代码
mkdir -p /data/sentinel4

cd /data/sentinel4

touch sentinel4.conf

vim sentinel4.conf

sentinel4.conf配置文件内容:

jsx 复制代码
# 关闭保护模式
protected-mode no
# 配置端口号,各个节点不能相同
port 26382
daemonize no
pidfile /var/run/redis-sentinel.pid
loglevel notice
# 日志存放地址
logfile "/var/tmp/sentinel.log"
dir /tmp
# 外部IP访问地址,解决Docker-Net容器IP跨服务器无法访问问题
sentinel announce-ip 192.168.32.129
# 设置Redis主节点信息
sentinel monitor mymaster 192.168.32.131 6380 2
sentinel down-after-milliseconds mymaster 30000
acllog-max-len 128
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
SENTINEL resolve-hostnames no
SENTINEL announce-hostnames no
SENTINEL master-reboot-down-after-period mymaster 0

3、启动四个Sentinel节点

131主服务器启动命令

jsx 复制代码
docker run --name sentinel1 \
-v /data/sentinel1/sentinel1.conf:/usr/local/etc/redis/sentinel.conf \
-d --net=host redis redis-sentinel /usr/local/etc/redis/sentinel.conf

docker run --name sentinel2 \
-v /data/sentinel2/sentinel2.conf:/usr/local/etc/redis/sentinel.conf \
-d --net=host redis redis-sentinel /usr/local/etc/redis/sentinel.conf

docker run --name sentinel3 \
-v /data/sentinel3/sentinel3.conf:/usr/local/etc/redis/sentinel.conf \
-d --net=host redis redis-sentinel /usr/local/etc/redis/sentinel.conf

129从服务器启动命令

jsx 复制代码
docker run --name sentinel4 \
-v /data/sentinel4/sentinel4.conf:/usr/local/etc/redis/sentinel.conf \
-d --net=host redis redis-sentinel /usr/local/etc/redis/sentinel.conf

4、验证Sentinel配置正确性

jsx 复制代码
docker exec -it sentinel1 bash

redis-cli -p 26379

info Sentinel

四、SpringBoot配置文件连接Redis集群

jsx 复制代码
spring:
    redis:
        sentinel:
            nodes: 
                - 192.168.31.131:26379
                - 192.168.31.131:26380
                - 192.168.31.131:26381
                - 192.168.31.129:26382
        #连接超时时间(毫秒)
        timeout: 30000
        jedis:
            pool:
                #连接池最大阻塞等待时间(使用负值表示没有限制)
                max-wait: -1
                #连接池中的最小空闲连接
                min-idle: 0
                #连接池中的最大空闲连接
                max-idle: 8
                #连接池最大连接数(使用负值表示没有限制)
                max-active: 8
相关推荐
无限大.2 分钟前
c语言200例 067
java·c语言·开发语言
余炜yw4 分钟前
【Java序列化器】Java 中常用序列化器的探索与实践
java·开发语言
攸攸太上4 分钟前
JMeter学习
java·后端·学习·jmeter·微服务
Kenny.志7 分钟前
2、Spring Boot 3.x 集成 Feign
java·spring boot·后端
不修×蝙蝠9 分钟前
八大排序--01冒泡排序
java
sky丶Mamba24 分钟前
Spring Boot中获取application.yml中属性的几种方式
java·spring boot·后端
数据龙傲天1 小时前
1688商品API接口:电商数据自动化的新引擎
java·大数据·sql·mysql
带带老表学爬虫1 小时前
java数据类型转换和注释
java·开发语言
千里码aicood1 小时前
【2025】springboot教学评价管理系统(源码+文档+调试+答疑)
java·spring boot·后端·教学管理系统
彭于晏6892 小时前
Android广播
android·java·开发语言