fix(config): Correct Redis high availability deployment

This commit is contained in:
2026-03-17 11:19:59 +08:00
parent e57881f489
commit 14202e1bea
10 changed files with 846 additions and 28 deletions
+51 -4
View File
@@ -255,8 +255,6 @@ docker compose -f docker-compose-node-03.yml up -d
docker compose -f docker-compose-node-03.yml logs -f mysql
# 看到 "ready for connections" 后 Ctrl+C 退出
# 验证数据库已自动创建
docker exec mysql mysql -uroot -proot123456 -e "SHOW DATABASES;"
```
### 2.7 配置 MySQL 主从复制
@@ -283,7 +281,7 @@ CHANGE MASTER TO
MASTER_PASSWORD='repl_password_2024',
MASTER_AUTO_POSITION=1;
START SLAVE;
SHOW SLAVE STATUS\G
SHOW SLAVE STATUS\G;
EOF
# 确认 Slave_IO_Running 和 Slave_SQL_Running 都为 Yes
@@ -301,7 +299,7 @@ CHANGE MASTER TO
MASTER_PASSWORD='repl_password_2024',
MASTER_AUTO_POSITION=1;
START SLAVE;
SHOW SLAVE STATUS\G
SHOW SLAVE STATUS\G;
EOF
docker exec mysql mysql -uroot -proot123456 -e "SET GLOBAL read_only = ON; SET GLOBAL super_read_only = ON;"
@@ -734,6 +732,55 @@ environment:
**验证**:重启微服务后,Sentinel 控制台应显示真实宿主机 IP,且所有实例状态为"健康"
### Q14: 微服务报 Redis READONLY 错误
**问题描述**:微服务启动时报错:
```
io.lettuce.core.RedisReadOnlyException: READONLY You can't write against a read only replica.
```
**原因**:微服务使用单机模式连接 Redis(`REDIS_HOST`/`REDIS_PORT`),直接连接到了 Redis 从节点,而从节点是只读的。
**解决方案**:
1. 确保所有微服务使用 **Redis Sentinel 模式** 连接。在 `docker-compose-node-xx.yml` 中配置环境变量:
```yaml
environment:
# 删除 REDIS_HOST 和 REDIS_PORT,改用 Sentinel
- REDIS_PASSWORD=${REDIS_PASSWORD:-redis_HA_Cluster_2024}
- REDIS_SENTINEL_NODES=${REDIS_SENTINEL_NODES:-192.168.1.101:26379,192.168.1.102:26379,192.168.1.103:26379}
```
2. 确保 `application-ha.yml` 配置正确:
```yaml
spring:
data:
redis:
password: ${REDIS_PASSWORD:redis_HA_Cluster_2024}
database: 0
timeout: 10000
sentinel:
master: mymaster # 与 sentinel.conf 中的 master 名称一致
nodes: ${REDIS_SENTINEL_NODES:192.168.1.101:26379,192.168.1.102:26379,192.168.1.103:26379}
password: ${REDIS_PASSWORD:redis_HA_Cluster_2024}
```
**验证**:
```bash
# 查看当前 Redis 主节点
docker exec redis-sentinel redis-cli -p 26379 SENTINEL get-master-addr-by-name mymaster
# 输出应为主节点的 IP 和端口
# 1) "192.168.1.101"
# 2) "6379"
# 测试 Sentinel 连接
docker exec redis-sentinel redis-cli -p 26379 SENTINEL master mymaster
```
---
## 七、日常运维