chore(config): Extend Nacos high availability improvements

This commit is contained in:
2026-03-17 17:53:14 +08:00
parent 88bb0c0cd0
commit 1016814cf2
2 changed files with 121 additions and 38 deletions
+100 -18
View File
@@ -322,12 +322,56 @@ docker exec redis redis-cli -a redis_HA_Cluster_2024 INFO replication | grep -E
访问 http://192.168.1.101:8848/nacos (账号: nacos/nacos)
#### 方式一:SQL 脚本初始化(推荐)
```bash
# 在 Node-01 执行,初始化所有配置到 Nacos 数据库
docker exec -i mysql mysql -uroot -proot123456 nacos_config < /opt/HA-OVP/sql/nacos-config-init.sql
# 验证配置是否导入成功
docker exec mysql mysql -uroot -proot123456 -e "SELECT data_id FROM nacos_config.config_info;"
```
SQL 脚本会自动创建以下配置:
| Data ID | 说明 |
|---------|------|
| common.yaml | 公共配置(MySQL连接池、MyBatis、Feign等) |
| common-redis-dev.yaml | 开发环境 Redis 单机配置 |
| common-redis-ha.yaml | 生产环境 Redis Sentinel 配置 |
| video-gateway.yaml | Gateway 服务专属配置 |
| video-auth.yaml | Auth 服务专属配置 |
| video-user.yaml | User 服务专属配置 |
| video-content.yaml | Content 服务专属配置 |
| video-comment.yaml | Comment 服务专属配置 |
| video-recommend.yaml | Recommend 服务专属配置 |
| video-admin.yaml | Admin 服务专属配置 |
#### 方式二:控制台手动创建
1. **创建命名空间**: 命名空间 → 新建命名空间 → 命名空间ID: `public`, 命名空间名: `公共命名空间`
2. **添加配置文件**: 配置管理 → 配置列表 → 创建配置
- Data ID: `video-gateway.yaml`
- Data ID: `common.yaml`
- Group: `DEFAULT_GROUP`
- 配置内容: 参考 `config/nacos-config/` 目录下的配置文件
3. **重复以上步骤添加其他服务的配置**
- 配置内容: 参考 `sql/nacos-config-init.sql` 中的内容
3. **重复以上步骤添加其他配置文件**
#### 配置架构说明
配置采用分层加载模式:
```
application.yml (本地,极简)
├── common.yaml (公共配置)
├── common-redis-${profile}.yaml (按环境的Redis配置)
└── ${spring.application.name}.yaml (服务专属配置)
```
**环境切换**:通过 `SPRING_PROFILES_ACTIVE` 环境变量
- `dev`: 加载 `common-redis-dev.yaml`(单机Redis)
- `ha`: 加载 `common-redis-ha.yaml`(Sentinel哨兵)
**配置热更新**:修改 Nacos 中的配置后,服务会自动感知并更新,无需重启!
### 2.10 初始化 MinIO
@@ -743,44 +787,82 @@ io.lettuce.core.RedisReadOnlyException: READONLY You can't write against a read
**解决方案**:
1. 确保所有微服务使用 **Redis Sentinel 模式** 连接。在 `docker-compose-node-xx.yml` 中配置环境变量:
1. 使用 Nacos 配置中心管理 Redis 配置,通过 `SPRING_PROFILES_ACTIVE` 区分环境:
- `dev`: 加载 `common-redis-dev.yaml`(单机模式)
- `ha`: 加载 `common-redis-ha.yaml`(Sentinel模式)
2. 确保 docker-compose 中配置了正确的环境变量:
```yaml
environment:
# 删除 REDIS_HOST 和 REDIS_PORT,改用 Sentinel
- SPRING_PROFILES_ACTIVE=ha
- 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` 配置正确:
3. 确保 Nacos 中 `common-redis-ha.yaml` 配置正确:
```yaml
spring:
data:
redis:
password: ${REDIS_PASSWORD:redis_HA_Cluster_2024}
database: 0
timeout: 10000
sentinel:
master: mymaster # 与 sentinel.conf 中的 master 名称一致
master: mymaster
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
```
### Q15: Nacos 配置不生效或配置冲突
**问题描述**:修改 Nacos 配置后服务没有更新,或启动时配置异常。
**原因**:
1. 本地 `application.yml` 与 Nacos 配置冲突
2. 配置加载顺序问题
3. `spring.config.import` 配置错误
**解决方案**:
1. **确保本地 application.yml 极简**:只保留 Nacos 连接配置
```yaml
spring:
application:
name: video-auth
profiles:
active: ${SPRING_PROFILES_ACTIVE:dev}
config:
import:
- optional:nacos:common.yaml?refreshEnabled=true
- optional:nacos:common-redis-${spring.profiles.active}.yaml?refreshEnabled=true
- optional:nacos:${spring.application.name}.yaml?refreshEnabled=true
cloud:
nacos:
discovery:
server-addr: ${NACOS_SERVER_ADDR:127.0.0.1:8848}
namespace: ${NACOS_NAMESPACE:}
username: ${NACOS_USERNAME:nacos}
password: ${NACOS_PASSWORD:nacos}
```
2. **配置加载顺序**:
- common.yaml(公共配置)
- common-redis-${profile}.yaml(环境配置)
- ${spring.application.name}.yaml(服务配置)
3. **验证配置加载**:
```bash
# 查看服务启动日志
docker logs video-auth 2>&1 | grep -i nacos
# 应看到类似: "Loading nacos config, dataId: common.yaml"
```
4. **强制刷新配置**:修改 Nacos 配置后,服务会自动感知更新(`refreshEnabled=true`)
---
## 七、日常运维
+21 -20
View File
@@ -1,11 +1,12 @@
-- Nacos 配置初始化脚本
-- 在 Nacos 数据库中执行此脚本,创建所有配置
-- 注意:tenant_id 为空字符串 '' 表示 public 命名空间
-- Nacos 2.x 版本需要 encrypted_data_key 字段
-- ============================================
-- 1. 公共基础配置 (common.yaml)
-- ============================================
INSERT INTO config_info (data_id, group_id, content, tenant_id, app_name, src_user) VALUES
INSERT INTO config_info (data_id, group_id, content, tenant_id, app_name, src_user, encrypted_data_key) VALUES
('common.yaml', 'DEFAULT_GROUP', '# 公共配置 - 所有微服务共享
spring:
datasource:
@@ -62,26 +63,26 @@ feign:
logging:
level:
com.nuaa.video: info
org.springframework.security: info', '', '', 'nacos');
org.springframework.security: info', '', '', 'nacos', '');
-- ============================================
-- 2. 开发环境 Redis 配置 (common-redis-dev.yaml)
-- 单机模式
-- ============================================
INSERT INTO config_info (data_id, group_id, content, tenant_id, app_name, src_user) VALUES
INSERT INTO config_info (data_id, group_id, content, tenant_id, app_name, src_user, encrypted_data_key) VALUES
('common-redis-dev.yaml', 'DEFAULT_GROUP', '# Redis 单机配置 - 开发环境
spring:
data:
redis:
host: ${REDIS_HOST:5.180.253.115}
port: ${REDIS_PORT:6379}
password: ${REDIS_PASSWORD:redis_EsJ8FX}', '', '', 'nacos');
password: ${REDIS_PASSWORD:redis_EsJ8FX}', '', '', 'nacos', '');
-- ============================================
-- 3. 高可用环境 Redis 配置 (common-redis-ha.yaml)
-- Sentinel 哨兵模式
-- ============================================
INSERT INTO config_info (data_id, group_id, content, tenant_id, app_name, src_user) VALUES
INSERT INTO config_info (data_id, group_id, content, tenant_id, app_name, src_user, encrypted_data_key) VALUES
('common-redis-ha.yaml', 'DEFAULT_GROUP', '# Redis Sentinel 高可用配置 - 生产环境
spring:
data:
@@ -90,12 +91,12 @@ spring:
sentinel:
master: mymaster
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}', '', '', 'nacos');
password: ${REDIS_PASSWORD:redis_HA_Cluster_2024}', '', '', 'nacos', '');
-- ============================================
-- 4. Gateway 服务配置 (video-gateway.yaml)
-- ============================================
INSERT INTO config_info (data_id, group_id, content, tenant_id, app_name, src_user) VALUES
INSERT INTO config_info (data_id, group_id, content, tenant_id, app_name, src_user, encrypted_data_key) VALUES
('video-gateway.yaml', 'DEFAULT_GROUP', '# Gateway 服务配置
spring:
cloud:
@@ -152,12 +153,12 @@ gateway:
- /comment/reply/public/**
- /comment/public/**
- /admin/auth/captcha
- /admin/auth/login', '', '', 'nacos');
- /admin/auth/login', '', '', 'nacos', '');
-- ============================================
-- 5. Auth 服务配置 (video-auth.yaml)
-- ============================================
INSERT INTO config_info (data_id, group_id, content, tenant_id, app_name, src_user) VALUES
INSERT INTO config_info (data_id, group_id, content, tenant_id, app_name, src_user, encrypted_data_key) VALUES
('video-auth.yaml', 'DEFAULT_GROUP', '# Auth 服务配置
spring:
datasource:
@@ -182,12 +183,12 @@ jwt:
secret: video-platform-secret-key-2024-nuaa
expiration: 86400
token-header: Authorization
token-prefix: "Bearer "', '', '', 'nacos');
token-prefix: "Bearer "', '', '', 'nacos', '');
-- ============================================
-- 6. User 服务配置 (video-user.yaml)
-- ============================================
INSERT INTO config_info (data_id, group_id, content, tenant_id, app_name, src_user) VALUES
INSERT INTO config_info (data_id, group_id, content, tenant_id, app_name, src_user, encrypted_data_key) VALUES
('video-user.yaml', 'DEFAULT_GROUP', '# User 服务配置
spring:
datasource:
@@ -218,12 +219,12 @@ minio:
endpoint: http://${MINIO_HOST:minio}:${MINIO_PORT:9000}
accessKey: minioadmin
secretKey: minioadmin123
bucketName: video-platform', '', '', 'nacos');
bucketName: video-platform', '', '', 'nacos', '');
-- ============================================
-- 7. Content 服务配置 (video-content.yaml)
-- ============================================
INSERT INTO config_info (data_id, group_id, content, tenant_id, app_name, src_user) VALUES
INSERT INTO config_info (data_id, group_id, content, tenant_id, app_name, src_user, encrypted_data_key) VALUES
('video-content.yaml', 'DEFAULT_GROUP', '# Content 服务配置
spring:
datasource:
@@ -281,12 +282,12 @@ jwt:
secret: video-platform-secret-key-2024-nuaa
expiration: 86400
token-header: Authorization
token-prefix: "Bearer "', '', '', 'nacos');
token-prefix: "Bearer "', '', '', 'nacos', '');
-- ============================================
-- 8. Comment 服务配置 (video-comment.yaml)
-- ============================================
INSERT INTO config_info (data_id, group_id, content, tenant_id, app_name, src_user) VALUES
INSERT INTO config_info (data_id, group_id, content, tenant_id, app_name, src_user, encrypted_data_key) VALUES
('video-comment.yaml', 'DEFAULT_GROUP', '# Comment 服务配置
spring:
datasource:
@@ -327,12 +328,12 @@ jwt:
auth:
ignore-urls:
- /comment/public/**
- /actuator/**', '', '', 'nacos');
- /actuator/**', '', '', 'nacos', '');
-- ============================================
-- 9. Recommend 服务配置 (video-recommend.yaml)
-- ============================================
INSERT INTO config_info (data_id, group_id, content, tenant_id, app_name, src_user) VALUES
INSERT INTO config_info (data_id, group_id, content, tenant_id, app_name, src_user, encrypted_data_key) VALUES
('video-recommend.yaml', 'DEFAULT_GROUP', '# Recommend 服务配置
spring:
datasource:
@@ -389,12 +390,12 @@ auth:
ignore-urls:
- /recommend/hot/**
- /recommend/tag/**
- /actuator/**', '', '', 'nacos');
- /actuator/**', '', '', 'nacos', '');
-- ============================================
-- 10. Admin 服务配置 (video-admin.yaml)
-- ============================================
INSERT INTO config_info (data_id, group_id, content, tenant_id, app_name, src_user) VALUES
INSERT INTO config_info (data_id, group_id, content, tenant_id, app_name, src_user, encrypted_data_key) VALUES
('video-admin.yaml', 'DEFAULT_GROUP', '# Admin 服务配置
spring:
datasource:
@@ -436,4 +437,4 @@ operation-log:
- /admin/auth/login
- /admin/auth/logout
- /admin/auth/captcha
- /actuator/**', '', '', 'nacos');
- /actuator/**', '', '', 'nacos', '');