azure - Redis 集群的 MOVED 异常 - StackExchange.Redis

标签 azure redis cluster-computing stackexchange.redis

使用 Redis 官方教程和非环回 IP 地址在 Ubuntu 服务器 14.0 LTS (AZURE VM) 中配置 Redis 集群服务器,如 @Marcgravell article 中所述。 ,但在使用 Stackexchange.Redis 客户端时,某些键出现 MOVED 异常。嗨@Marcgravell,你能解释一下吗?谢谢。

最佳答案

我的程序终于可以运行了。感谢@hrishi18pathak。 我已经从https://github.com/hrishi18pathak/StackExchange.Redis/tree/SEClusteringBugFix下载了代码2015 年 7 月 17 日起发生变化的分支。主要更改在 ConnectionMultiplexer 和 ConfigurartionOptions 类中。

<小时/>

ConnectionMultiplexer.cs(修改UpdateClusterRange()方法,添加IsNodeCompatibleWithConfiguration()方法):

internal void UpdateClusterRange(ClusterConfiguration configuration)
    {
        if (configuration == null) return;
        foreach (var node in configuration.Nodes)
        {
            // do not update cluster range if the node configuration is incompatible with the multiplexer 

            // endpoints configuration. If a user has specified endpoints in the multiplexer configuration using

            // Dns endpoints, Stackexchange.redis returns the ConnectionMultiplexer object successfully 
            // after connecting to all the nodes in the connection string (using their host names). 
            // Also, before returning the multiplexer object it updates the mapping of nodes to their clusterslots 

            // and this mapping is of the format “DNS hostname:port” : “clusterSlotNumber”.

            // However at the same time, the client also issues “CLUSTER NODES” command to each of the nodes in the above list 

            // (to determine master and slave configuration) and re-updates the cluster mapping with the output of the clusternodes command 

            // which now is going to contain IP addresses (redis-server itself does not understand host names). 

            // So the cluster mapping is now updated to the format: “IP Address” : “clusterSlotNumber”

            // If the StackExchange.Redis has not been able to connect to all the nodes using their IP addresses by the time the first command (GET,SET) 
            // is issued to the cluster, it results in failure to connect to that particular node resulting in the “MOVED ERROR” 

            // (since it tries to hit a random node in the list subsequently)

            if (node.IsSlave || node.Slots.Count == 0 || !IsNodeCompatibleWithConfiguration(node)) continue;
            foreach (var slot in node.Slots)
            {
                var server = GetServerEndPoint(node.EndPoint);
                if (server != null) serverSelectionStrategy.UpdateClusterRange(slot.From, slot.To, server);
            }
        }
    }

/// <summary>
    /// Checks if the specified node has the same format as that of
    /// the endpoints specified in the multiplexer configuration i.e.
    /// if all nodes are dns endpoints then the specified node has to be a dns endpoint
    /// to be compatible.
    /// </summary>
    /// <param name="node"></param>
    /// <returns>True if node is compatible with multiplexer configuration</returns>
    private bool IsNodeCompatibleWithConfiguration(ClusterNode node)
    {
        return (this.configuration.HasAllDnsEndPoints() && (node.EndPoint is DnsEndPoint)) ||
            (!this.configuration.HasDnsEndPoints() && !(node.EndPoint is DnsEndPoint));
    }
<小时/>

ConfigurationOptions.cs(添加方法 HasAllDnsEndPoints())

internal bool HasAllDnsEndPoints()
    {
        return !endpoints.Any(ep => !(ep is DnsEndPoint));
    }

我仍然需要验证此修复是否有任何影响。

关于azure - Redis 集群的 MOVED 异常 - StackExchange.Redis,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30223105/

相关文章:

sql-server - 由于表名 "User"是保留名称,因此无法提取表数据

按计数对字符串进行排序的 Redis 建模

node.js - 无法连接到 MongoClient.connect 内的 Redis

python - AttributeError:“图形”对象没有属性“节点”

r - 关于 R 中椭球体的澄清

linux - 我可以在使用 qsub 提交 shell 脚本后删除它而不影响作业吗?

azure - 304 : The condition specified using HTTP conditional header(s) is not met

azure - Azure Cloud Shell 无法识别我的 New-AzureRmWebApp 绑定(bind)代码

sql - Azure SQL 创建数据库范围的凭据

amazon-web-services - Elasticache Redis 端点会随着时间的推移而改变吗?