-
Notifications
You must be signed in to change notification settings - Fork 786
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support redis cluster as cache or database #547
Conversation
// RedisCluster cache storage. | ||
type RedisCluster struct { | ||
storage.TablePrefix | ||
client *redis.ClusterClient |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不需要再写一份redis_cluster.go
,可以把redis.go
的redis.Client
替换成UniversalClient
,共享一份实现
|
||
// RedisCluster use RedisCluster as data storage, but used for test only. | ||
type RedisCluster struct { | ||
client *redis.ClusterClient |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
data storage的Redis接口只用来测试,支持集群模型意义不大。如果要用Redis存数据的话需要RedisJSON,目前还没实现,所以不需要为data storage支持Redis集群
server *miniredis.Miniredis | ||
} | ||
|
||
func newMockRedisCluster(t *testing.T) *mockRedisCluster { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
测试没过,既然data storage没有支持集群的意义,可以直接删了>_<
} | ||
return defaultValue | ||
} | ||
redisDSN = env("REDIS_URI", "redis://127.0.0.1:6379/,") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
如果将redis.go
中的客户端改为UniversalClient,那么只需要redis_test.go
测试通过就行,不需要针对集群的测试啦
opt, err := redis.ParseURL(path) | ||
if err != nil { | ||
return nil, err | ||
if strings.Contains(path, ",") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
现在的集群URL无法支持用户名和密码以及其他配置项。单机版客户端的配置项redis.Options
可以使用ParseURL
解析,但是通用客户端的配置项redis,UniversalOptions
缺少解析函数,实际上可以实现一个redis,UniversalOptions
解析函数,然后使用redis,UniversalOptions
创建redis,UniversalClient
就可以支持集群了。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我看了一下这个接口,应该是不能完全支撑Redis 的Cluster 模式,参考这个链接
云服务商的Redis 是直接提供了一个域名,无法直接区分是cluster 或者 single node
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
或者我们用redis+cluster://
前缀进行识别(类似 redis/redis-rb#887 ),但是需要自己解析一下URL,因为集群模式需要支持多IP地址。多地址的URL解析比较复杂,可以参考MongoDB客户端实现
最终效果是集群模型和单机模型创建连接的代码不同,其他数据操作需要共享实现
No description provided.