如何使用StackExchange.Redis库向Redis群集发送多个命令

时间:2016-12-02 18:31:42

标签: stackexchange.redis

我花了很多天尝试使用不同的方法向redis群集发送多个命令,但仍然没有解决方案。这种方法使用的是StachExchange。我有一个redis集群,其中包含3个主服务器,每个服务器有1个从服端口30001,30002和30003是主人

这是我的代码

using StackExchange.Redis;
using System;

namespace redis
{
    class Program
    {
        static void Main(string[] args)
        {
            ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("192.168.1.100:30001,192.168.1.100:30002,192.168.1.100:30003,connectTimeout=1000");
            IDatabase db = redis.GetDatabase();

            db.StringSet("mykey", "abcdefg");

            db.StringSet("mykey1", "11111");
            db.StringSet("mykey2", "2222");

            string value2 = db.StringGet("mykey2");
            Console.WriteLine(value2);
        }
    }
}

及以下是错误

Unhandled Exception: StackExchange.Redis.RedisServerException: Endpoint 127.0.0.1:30003 serving hashslot 14687 is not reachable at this point of time. Please check connectTimeout value. If it is low, try increasing it to give the ConnectionMultiplexer a chance to recover from the network disconnect.
   at StackExchange.Redis.ConnectionMultiplexer.ExecuteSyncImpl[T](Message message, ResultProcessor`1 processor, ServerEndPoint server)
   at StackExchange.Redis.RedisBase.ExecuteSync[T](Message message, ResultProcessor`1 processor, ServerEndPoint server)
   at StackExchange.Redis.RedisDatabase.StringSet(RedisKey key, RedisValue value, Nullable`1 expiry, When when, CommandFlags flags)
   at redis.Program.Main(String[] args) in c:\users\xxxx\documents\visual studio 2015\Projects\redis\redis\Program.cs:line 31
Press any key to continue . . .

我还通过直接向redis cli运行命令做了一些实验,我发送命令后发现重定向,我认为这会导致问题

在发送命令之前,我已经向我的主人30001,30002和30003

发送信息
xxxx@RedisServer:~/cluster-test/30003$ redis-cli -c -p 30001
127.0.0.1:30001> set mykey "abcdefg"
-> Redirected to slot [14687] located at 127.0.0.1:30003
OK
127.0.0.1:30003> set mykey1 "11111"
-> Redirected to slot [1860] located at 127.0.0.1:30001
OK
127.0.0.1:30001> set mykey2 "2222"
-> Redirected to slot [14119] located at 127.0.0.1:30003
OK
127.0.0.1:30003> get mykey
"abcdefg"
127.0.0.1:30003> get mykey1
-> Redirected to slot [1860] located at 127.0.0.1:30001
"11111"
127.0.0.1:30001> get mykey2
-> Redirected to slot [14119] located at 127.0.0.1:30003
"2222"
127.0.0.1:30003>

我需要帮助将这些命令发送到redis群集。代码在非集群btw上完全正常运行

12/4更新
Endpoint 127.0.0.1:30003 serving hashslot 14687 is not reachable at...

查看上面的错误代码,我将连接更改为仅连接到端口30003

 ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("192.168.1.100:30003,connectTimeout=1000");
            IDatabase db = redis.GetDatabase();
            db.StringSet("mykey", "abcdefg");

它能够连接并设置 mykey

127.0.0.1:30003> get mykey
"abcdefg"

另一项测试

这次我在 mykey1

的位置设置调试
 ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("192.168.1.100:30003,connectTimeout=1000");
            IDatabase db = redis.GetDatabase();
            db.StringSet("mykey", "abcdefg");
            db.StringSet("mykey1", "11111");

mykey 行成功,因为我们正在连接 mykey 的hashslot所属的节点 但是 mykey1 有一个例子,因为它的hashslot属于30001节点

occurred in StackExchange.Redis.dll

Additional information: Endpoint 127.0.0.1:30001 serving hashslot 1860 is not reachable at this point of time. Please check connectTimeout value. If it is low, try increasing it to give the ConnectionMultiplexer a chance to recover from the network disconnect.

我认为问题是StackExchange.Redis客户端无法知道密钥属于哪个哈希表因为redis群集中的每个密钥都有对应的属于特定主节点的哈希值

另一项测试

mykey 属于30003,所以我先把192.168.1.100:30003放在连接字符串中 然后我知道 mykey1 属于192.168.1.100:30001所以我把它放在30003之后

ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("192.168.1.100:30003,192.168.1.100:30001,connectTimeout=1000");
            IDatabase db = redis.GetDatabase();
            db.StringSet("mykey", "abcdefg");
            db.StringSet("mykey1", "11111");

调试后的结果,设置 mykey

时会出现以下异常
An unhandled exception of type 'StackExchange.Redis.RedisServerException' occurred in StackExchange.Redis.dll

Additional information: Endpoint 127.0.0.1:30003 serving hashslot 14687 is not reachable at this point of time. Please check connectTimeout value. If it is low, try increasing it to give the ConnectionMultiplexer a chance to recover 

这个结果告诉我客户端正在使用192.168.1.100:30001,它使用了连接中的最后一个主机

2 个答案:

答案 0 :(得分:0)

理论上,是的。所有的位都在那里。但是,库作者当前不使用集群(因为它缺少4.2的路线图上的以DC为中心的功能)。为了帮助进行测试,我们最近改进了对群集配置的访问;我会试着通过它来运行你的样本。但是,如果您可以在库和命令行之间运行完全相同的测试,那么实际上我会感兴趣 - 在您的示例中,键实际上是不同的。我很乐意看到完全相同的密钥。

答案 1 :(得分:0)

由于交叉插槽错误,可能会发生这种情况。

看看:Redis Cross Slot error