【问题标题】:How to connect to a clustered Azure Redis cache in redis-rb?如何连接到 redis-rb 中的集群 Azure Redis 缓存?
【发布时间】:2019-11-01 01:41:00
【问题描述】:

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

我尝试过的:

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

Redis::CommandError (MOVED 1234 address_here:port_here)

我已经看到这个documentation 用于创建与cluster 的连接:

节点可以作为连接 URL 的数组传递给客户端。

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

您还可以将选项指定为哈希。这些选项与单个服务器连接的选项相同。

(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 Cache for Redis 在单个 DNS 终结点上公开群集节点,而此 redis-rb 群集参数似乎需要已知节点终结点的集合。

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

【问题讨论】:

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


    【解决方案1】:

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

    1. Do all Redis clients support clustering?

    并非所有客户端都支持 Redis 集群!请检查您正在使用的库的文档,以验证您正在使用支持集群的库和版本。 StackExchange.Redis 是一个在其较新版本中支持集群的库。有关其他客户端的更多信息,请参阅Redis cluster tutorialPlaying with the cluster 部分。

    Redis 集群协议要求每个客户端以集群模式直接连接到每个分片,并且还定义了新的错误响应,例如 'MOVED' na 'CROSSSLOTS'。尝试使用不支持集群模式缓存的客户端可能会导致大量MOVED redirection exceptions,或者如果您正在执行跨槽多键请求,则只会破坏您的应用程序。

    注意

    如果您使用 StackExchange.Redis 作为客户端,请确保您使用的是最新版本的 StackExchange.Redis 1.0.481 或更高版本,以使集群正常工作。如果您对移动异常有任何问题,请参阅移动异常了解更多信息。

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

    您可以使用连接到未启用集群的缓存时使用的相同端点、端口和键来连接到您的缓存。 Redis 在后端管理集群,因此您无需从客户端进行管理。

    所以请尝试按照下图使用redis-rb-cluster而不是redis-rb连接Azure Redis Cache,启用集群和非集群一样。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-25
      • 2023-03-12
      相关资源
      最近更新 更多