如何从其他计算机连接到Zookeeper服务器?

时间:2018-07-16 17:42:02

标签: c apache-zookeeper

我的代码(用C编写的C Client binding for zookeeper)可以在本地计算机上使用相同的IP(而不是localhost:2181)完美运行。但是,在另一台计算机上编译并执行我的代码会产生连接丢失错误。我无法使用我的公共IP连接到我的zookeeper服务器(我通过在Google上查找whatsmyip获得了我的publicIP)。我在终端上执行了ifconfig以获得10.111.129.199。我假设这是一个以10开头的私有IP。我已切换到的计算机正在运行SolarisOS。这导致我将zookeeper源代码中的单个功能从synch_fetch_and_add(我认为)更改为atomic_add,因为SolarisOS不支持sync_fetch...。根据ZooKeeper文档,SolarisOS is not currently supported由Zookeeper提供。我能够很好地编译Zookeeper,并被告知公司中的其他人已经在我们的系统上预先实现了Zookeeper。

我的程序试图在Zookeeper服务器上创建一个节点。我的代码如下:

int main(int argc, char *argv[]){
    //zh is a global zookeeper_handle for now.
    zh = zookeeper_init(host_port, my_watcher_func, 20000, 0, NULL, 0);
    if(zh == NULL){
        fprintf(stderr, "Error connecting to ZooKeeper Server!! \n");
        exit(EXIT_FAILURE);
        return 0;
    }
    int retval = create("/TFS/pool" , "1");
    printf("return value of create = %d\n", retval);
}

 int create(char* path, char* data){
    int value_length= -1;
    if(data != NULL){
        value_length = (int) strlen(data);
    }
    printf("creating node at path: %s with data %s\n", path, data);
    int retval = zoo_create( zh, path, data, value_length,
                            &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0);
    return retval;
}
/*empty watcher function*/
//I have no idea why this is needed. 
void my_watcher_func(zhandle_t *zzh, int type, int state,
                 const char *path, void *watcherCtx) {}

两个系统都运行GCC编译器。我认为问题不在代码中,因为它可以在本地正常运行,但是我面临的连接问题。 我假设如果从zookeeper_init()函数到Zookeeper的连接失败,则zh将返回0。但是,这不会发生,并继续到create()。

creating node at path: /TFS/pool with data abc

2018-07-16 
10:30:44,232:16332(0x2):ZOO_ERROR@handle_socket_error_msg@1670: Socket 
[10.111.129.190:2181] zk retcode=-4, errno=0(Error 0): connect() call 
failed 

return value of create = -4

当我远程登录到ip:port时,它将连接。我也知道Zookeeper在telnet期间检测到我的连接,因为我是在前台运行它。以下是我通过telnet 10.111.129.190 2181连接时在前台运行的zkServer.sh的输出。

2018-07-16 11:04:03,807 [myid:] - INFO  
[NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:NIOServerCnxnFactory@215] - 
Accepted socket connection from /10.7.1.70:61479

预期输出应为:

creating node at path: /TFS/pool with data 1

2018-07-16 12:14:37,078:3180(0x70000c98c000):ZOO_INFO@check_events@1764: initiated connection to server [10.111.129.190:10101]
2018-07-16 12:14:37,107:3180(0x70000c98c000):ZOO_INFO@check_events@1811: session establishment complete on server [10.111.129.190:10101], sessionId=0x10000590d2b0000, negotiated timeout=20000

return value of create = 0

此输出始终使我感到困惑,因为在启动zookeeper_handle之后建立了zookeeper连接。它是根据zoo_create()而不是zookeeper_init建立的。不会产生任何影响,只是建立连接的有趣时间。

我知道retcode = -4表示CONNECTIONLOSS,但它甚至无法与服务器建立连接。如果有的话我可以解决这个问题,请告诉!。

0 个答案:

没有答案