fix(mining-service): Redis DB 11 冲突修复,迁移至 DB 16

问题:
  mining-service (2.0) 与 blockchain-service (1.0) 共用 Redis DB 11,
  存在数据污染和资源抢占风险。虽然当前 Key 前缀不同
  (mining:* vs blockchain:*),但缺乏结构性隔离保障。

方案:
  - 将 mining-service Redis DB 从 11 迁移到 16 (超出默认 0-15 范围)
  - Redis 基础设施配置增加 --databases 20,支持 DB 16+
  - 同步修正 .env.example 与代码默认值不一致的问题 (原 .env=1, 代码=11)

修改清单:
  - mining-service/infrastructure.module.ts: 代码默认值 11 → 16
  - mining-service/redis.service.ts: fallback 默认值 1 → 16
  - mining-service/.env.example: REDIS_DB=1 → REDIS_DB=16
  - docker-compose.2.0.yml: mining-service REDIS_DB 11 → 16 + 注释
  - docker-compose.yml: Redis 添加 --databases 20
  - docker-compose.infra.yml: Redis 添加 --databases 20
  - docker-compose.windows.yml: Redis 添加 --databases 20

部署注意:
  1. 需重启 Redis 容器使 --databases 20 生效
  2. 需重启 mining-service 使新 DB 16 生效
  3. 旧 DB 11 中 mining-service 的残留数据可手动清理:
     redis-cli -n 11 KEYS "mining:*" | xargs redis-cli -n 11 DEL

Redis DB 分配表 (修改后):
  1.0: DB 0-11 (identity=0, wallet=1, ..., blockchain=11)
  2.0: DB 8,10,12-16 (blockchain=8, contribution=10, trading=12,
       admin=13, auth=14, wallet=15, mining=16)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
hailin 2026-01-30 02:22:20 -08:00
parent 454b379f6c
commit 83384acdac
7 changed files with 9 additions and 7 deletions

View File

@ -46,6 +46,7 @@ services:
redis:
image: redis:7-alpine
container_name: rwa-redis
command: redis-server --databases 20
ports:
- "6379:6379"
healthcheck:

View File

@ -4,7 +4,7 @@
#
# 2.0 系统共享 1.0 的基础设施 (PostgreSQL, Redis, Kafka),但完全隔离:
# - 数据库:使用独立的数据库名称 (rwa_contribution, rwa_mining, etc.)
# - Redis使用独立的 DB 分区 (DB 10-15)
# - Redis使用独立的 DB 分区 (DB 8,10,12-16),避免与 1.0 服务 (DB 0-11) 冲突
# - Kafka仅通过 CDC 消费 1.0 的数据变更事件,单向同步
# - HTTP2.0 服务之间互相调用,不调用 1.0 服务
#
@ -69,11 +69,11 @@ services:
PORT: 3021
# PostgreSQL - 使用独立的数据库
DATABASE_URL: postgresql://${POSTGRES_USER:-postgres}:${POSTGRES_PASSWORD:-postgres}@postgres:5432/rwa_mining?schema=public
# Redis - 使用 DB 11 隔离
# Redis - 使用 DB 16 隔离 (避免与 1.0 blockchain-service 的 DB 11 冲突)
REDIS_HOST: redis
REDIS_PORT: 6379
REDIS_PASSWORD: ${REDIS_PASSWORD:-}
REDIS_DB: 11
REDIS_DB: 16
# Kafka
KAFKA_BROKERS: kafka:29092
# JWT 配置 (与 auth-service 共享密钥以验证 token)

View File

@ -36,6 +36,7 @@ services:
redis:
image: redis:7-alpine
container_name: rwa-redis
command: redis-server --appendonly yes --databases 20
ports:
- "6379:6379"
volumes:

View File

@ -52,7 +52,7 @@ services:
container_name: rwa-redis
environment:
TZ: Asia/Shanghai
command: redis-server --appendonly yes ${REDIS_PASSWORD:+--requirepass $REDIS_PASSWORD}
command: redis-server --appendonly yes --databases 20 ${REDIS_PASSWORD:+--requirepass $REDIS_PASSWORD}
ports:
- "6379:6379"
volumes:

View File

@ -10,7 +10,7 @@ DATABASE_URL=postgresql://postgres:password@localhost:5432/mining_db?schema=publ
REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_PASSWORD=
REDIS_DB=1
REDIS_DB=16
# Kafka
KAFKA_BROKERS=localhost:9092

View File

@ -49,7 +49,7 @@ import { KafkaProducerService } from './kafka/kafka-producer.service';
host: configService.get<string>('REDIS_HOST', 'localhost'),
port: configService.get<number>('REDIS_PORT', 6379),
password: configService.get<string>('REDIS_PASSWORD'),
db: configService.get<number>('REDIS_DB', 11),
db: configService.get<number>('REDIS_DB', 16),
}),
inject: [ConfigService],
},

View File

@ -20,7 +20,7 @@ export class RedisService implements OnModuleInit, OnModuleDestroy {
host: this.options.host,
port: this.options.port,
password: this.options.password,
db: this.options.db ?? 1,
db: this.options.db ?? 16,
retryStrategy: (times) => Math.min(times * 50, 2000),
});