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

标签 ruby-on-rails ruby azure redis azure-redis-cache

我正在尝试使用 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/

相关文章:

azure - 如何摆脱 Nuget

ruby-on-rails - Rails 3:路由错误 - 未初始化的常量 MyController::Google

mysql - 由于错误的迁移而尝试重置我的数据库,如果没有 SQLException 甚至无法重置

ruby-on-rails - 回形针错误 - NotIdentifiedByImageMagickError 使用亚马逊 S3

ruby-on-rails - bundle 更新安装时间更长

Ruby CSV,使用方括号作为行分隔符

ruby-on-rails - 通过 CarrierWave 的邮箱附件

ruby - 如何遍历可枚举的除第一个元素之外的所有元素

Azure 机器学习工作室表示交叉验证模块的值为 "Value Required"

Azure Synapse - 有没有办法从 ODBC 源批量导入数据?