我部署了redis集群之后, 用redis-cli连接使用正常, 连接命令如下:
redis-cli -h 192.168.6.251 -c -p 19002
redis-cli -h 192.168.6.249 -c -p 19004
连接不同地址, 分别设置key可以获取到, 但是我用libhiredis库的API调用时就会报错, 只有一个节点可以调用.
int ret = 0;
char *hostname = "192.168.6.251";
int port = 19003;
struct timeval timeout = { 1, 500000 }; // 1.5 seconds
//c = redisConnect(hostname, port);
redisContext *con = redisConnectWithTimeout(hostname, port, timeout);
if (con == NULL || con->err) {
if (con) {
printf("Connection error: %s\n", con->errstr);
redisFree(con);
} else {
printf("Connection error: can't allocate redis context\n");
}
return -1;
}
redisReply *reply = (redisReply *)redisCommand(con,"set key hello");
printf("Set return: %s\n", reply->str);
freeReplyObject(reply);
freeRedis(con);
运行结果: Set return: MOVED 12539 192.168.6.251:19002
有人知道怎么解决么?
邮箱:gfzhang12@163.com