gpt4 book ai didi

ruby-on-rails - 如何连接到redis-rb中的集群Azure Redis缓存?

转载 作者:行者123 更新时间:2023-12-03 02:46:17 24 4
gpt4 key购买 nike

我正在尝试使用 Ruby Redis 客户端 redis-rb连接到为群集配置的 Azure Redis 缓存。

我尝试过的:

我用过这个related question成功连接到非集群 Azure Redis 缓存。我还可以使用它连接到集群 Azure Redis 缓存,当我尝试获取或设置 key 时,它会正确报告 MOVED:

Redis::CommandError (MOVED 1234 address_here:port_here)

我看过这个documentation用于创建与集群的连接:

Nodes can be passed to the client as an array of connection URLs.

nodes = (7000..7005).map { |port| "redis://127.0.0.1:#{port}" }
redis = Redis.new(cluster: nodes)

You can also specify the options as a Hash. The options are the same as for a single server connection.

(7000..7005).map { |port| { host: '127.0.0.1', port: port } }

我已使用这些示例针对单个可用 DNS 端点构建了一个示例,该示例因以下错误而失败:

irb(main):024:0> client = Redis.new(cluster: ["redis://my-redis-cluster.redis.cache.windows.net:6379"])
...
Redis::CannotConnectError (Redis client could not connect to any cluster nodes)

我已经尝试了文档中列出的每个变体,得到了相同的结果。

问题:

Azure Redis 缓存在单个 DNS 终结点上公开群集节点,而此 redis-rb 群集参数似乎需要已知节点终结点的集合。

是否可以使用此库连接到集群 Azure Redis 缓存?如果是这样,一个可重现的例子会是什么样子?如果使用 redis-rb 无法实现,但可以使用另一个 Ruby Redis 客户端实现,那么我也会对该解决方案感兴趣。

最佳答案

根据Azure官方文档How to configure Redis clustering for a Premium Azure Cache for Redis ,有两个常见问题解答已经解答了您的问题。

<强>1。 Do all Redis clients support clustering?

Not all clients support Redis clustering! Please check the documentation for the library you are using, to verify you are using a library and version which support clustering. StackExchange.Redis is one library that does support clustering, in its newer versions. For more information on other clients, see the Playing with the cluster section of the Redis cluster tutorial.

The Redis clustering protocol requires each client to connect to each shard directly in clustering mode, and also defines new error responses such as 'MOVED' na 'CROSSSLOTS'. Attempting to use a client that doesn't support clustering with a cluster mode cache can result in a lot of MOVED redirection exceptions, or just break your application, if you are doing cross-slot multi-key requests.

Note

If you are using StackExchange.Redis as your client, ensure you are using the latest version of StackExchange.Redis 1.0.481 or later for clustering to work correctly. If you have any issues with move exceptions, see move exceptions for more information.

<强>2。 How do I connect to my cache when clustering is enabled?

You can connect to your cache using the same endpoints, ports, and keys that you use when connecting to a cache that does not have clustering enabled. Redis manages the clustering on the backend so you don't have to manage it from your client.

所以请尝试按照下图使用redis-rb-cluster而不是 redis-rb 来连接 Azure Redis 缓存,以实现与非集群相同的集群功能。

enter image description here

关于ruby-on-rails - 如何连接到redis-rb中的集群Azure Redis缓存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58653151/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com