Skip to content

fix: Redis 连接支持集群/哨兵模式自适配 (#89)#115

Draft
ljluestc wants to merge 5 commits intoxinliangnote:masterfrom
ljluestc:fix/redis-cluster-adaptive-89
Draft

fix: Redis 连接支持集群/哨兵模式自适配 (#89)#115
ljluestc wants to merge 5 commits intoxinliangnote:masterfrom
ljluestc:fix/redis-cluster-adaptive-89

Conversation

@ljluestc
Copy link
Copy Markdown

fix: Redis 连接支持集群/哨兵模式自适配 (Closes #89)

问题描述
当前项目 Redis 连接方式硬编码使用 redis.NewClient(单机模式),无法支持 Redis Cluster 或 Sentinel 部署。用户需要手动改造代码才能适配不同的 Redis 部署模式。

解决方案
使用 go-redis/v7 提供的 redis.UniversalClient 接口替代 *redis.Client,通过 redis.NewUniversalClient 根据配置自动选择连接模式:

• 单地址 (addr) → 单机模式(向后兼容,无需修改现有配置)
• 多地址 (addrs) → 集群模式(Redis Cluster)
• 多地址 + masterName → 哨兵模式(Redis Sentinel)

改动文件

  1. configs/configs.go — Redis 配置新增 Addrs []string 和 MasterName string 字段,保留 Addr 向后兼容

  2. configs/fat_configs.toml — 新增 addrs 和 masterName 配置项

  3. internal/repository/redis/options/options.go — 新增子包,包含 RedisConfig 结构体和 BuildUniversalOptions() 核心适配逻辑

  4. internal/repository/redis/redis.go — cacheRepo.client 从 *redis.Client 改为 redis.UniversalClient,redisConnect() 调用 options.BuildUniversalOptions() 后使用 redis.NewUniversalClient

  5. internal/render/install/execute.go — 安装向导支持逗号分隔的多地址输入,验证改用 redis.NewUniversalClient

  6. internal/repository/redis/options/options_test.go — 5 个测试用例覆盖单机/集群/哨兵/优先级/空配置

使用 redis.UniversalClient 替代 *redis.Client,根据配置自动适配单机/集群/哨兵模式。

- 配置层新增 addrs 和 masterName 字段,保留 addr 向后兼容
- 提取 options 子包封装 BuildUniversalOptions 适配逻辑
- 安装向导支持逗号分隔的多地址输入
- 新增 5 个单元测试覆盖各模式
@ljluestc ljluestc force-pushed the fix/redis-cluster-adaptive-89 branch from c7b4594 to d30ddaa Compare March 28, 2026 05:06
- Removed Oz co-author references
- Added comprehensive step-by-step implementation guide
- Included HTTP and WebSocket proxy code examples
- Added local testing procedures and performance benchmarks
Add comprehensive reverse proxy functionality:
- HTTPProxier using Go's httputil.ReverseProxy
- WebSocketProxier with bidirectional message relay
- Proxy handler supporting both protocols
- Router integration with proxy routes
- Test script for local validation

This resolves issue xinliangnote#91 by enabling native proxy support for
both HTTP and WebSocket connections.
@ljluestc ljluestc force-pushed the fix/redis-cluster-adaptive-89 branch from 62057d4 to 4f5a1e9 Compare March 28, 2026 05:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

请问redis 集群的方式链接,需要如何改造成自适配?

1 participant