Skip to content
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

Closed
wants to merge 6 commits into from

Conversation

0xByteLeon
Copy link

No description provided.

@0xByteLeon 0xByteLeon closed this Aug 31, 2022
@0xByteLeon 0xByteLeon reopened this Aug 31, 2022
// RedisCluster cache storage.
type RedisCluster struct {
storage.TablePrefix
client *redis.ClusterClient
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不需要再写一份redis_cluster.go,可以把redis.goredis.Client替换成UniversalClient,共享一份实现


// RedisCluster use RedisCluster as data storage, but used for test only.
type RedisCluster struct {
client *redis.ClusterClient
Copy link
Collaborator

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 {
Copy link
Collaborator

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/,")
Copy link
Collaborator

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, ",") {
Copy link
Collaborator

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就可以支持集群了。

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我看了一下这个接口,应该是不能完全支撑Redis 的Cluster 模式,参考这个链接

https://aws.amazon.com/cn/blogs/china/all-roads-lead-to-rome-use-go-redis-to-connect-amazon-elasticache-for-redis-cluster/

云服务商的Redis 是直接提供了一个域名,无法直接区分是cluster 或者 single node

Copy link
Collaborator

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客户端实现

https://github.com/mongodb/mongo-go-driver/blob/92f12ba78fda3414b12da221f029d769d343a910/mongo/options/clientoptions.go#L244

最终效果是集群模型和单机模型创建连接的代码不同,其他数据操作需要共享实现

@0xByteLeon 0xByteLeon closed this Sep 16, 2022
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.

2 participants