我的java客户端连接不上zookeeper

我的java客户端连接不上zookeeper

# 具体遇到的问题
我的linux下的zookeeper是开启的,代码为

ZooKeeper JMX enabled by default

Using config: /root/apache-zookeeper-3.6.0-bin/bin/../conf/zoo.cfg

Starting zookeeper ... already running as process 2921.

首先一个问题是,我为什么开启linux的进程为2921。

我的客户端显示

10:23:32 INFO zookeeper.ZooKeeper: Client environment:os.memory.free=114MB

10:23:32 INFO zookeeper.ZooKeeper: Client environment:os.memory.max=1797MB

10:23:32 INFO zookeeper.ZooKeeper: Client environment:os.memory.total=123MB

10:23:32 INFO zookeeper.ZooKeeper: Initiating client connection, connectString=127.0.0.1:2921 sessionTimeout=10000 watcher=com.imooc.zkjavaapi.ZkConnect@5ef04b5

10:23:32 INFO common.X509Util: Setting -D jdk.tls.rejectClientInitiatedRenegotiation=true to disable client-initiated TLS renegotiation

10:23:36 INFO zookeeper.ClientCnxnSocket: jute.maxbuffer value is 1048575 Bytes

10:23:36 INFO zookeeper.ClientCnxn: zookeeper.request.timeout value is 0. feature enabled=false

客户端开始连接ZK服务器了

CONNECTING

10:23:36 INFO zookeeper.ClientCnxn: Opening socket connection to server activate.navicat.com/127.0.0.1:2921.

10:23:36 INFO zookeeper.ClientCnxn: SASL config status: Will not attempt to authenticate using SASL (unknown error)

CONNECTING


Process finished with exit code 0

我用的是本机,IP地址为192.168.139.128,所以我尝试用他来取代127.0.0.1,还是没用

:30:02 INFO zookeeper.ZooKeeper: Client environment:os.memory.free=113MB

10:30:02 INFO zookeeper.ZooKeeper: Client environment:os.memory.max=1797MB

10:30:02 INFO zookeeper.ZooKeeper: Client environment:os.memory.total=123MB

10:30:02 INFO zookeeper.ZooKeeper: Initiating client connection, connectString=192.168.139.128:2921 sessionTimeout=10000 watcher=com.imooc.zkjavaapi.ZkConnect@5ef04b5

10:30:02 INFO common.X509Util: Setting -D jdk.tls.rejectClientInitiatedRenegotiation=true to disable client-initiated TLS renegotiation

10:30:05 INFO zookeeper.ClientCnxnSocket: jute.maxbuffer value is 1048575 Bytes

10:30:05 INFO zookeeper.ClientCnxn: zookeeper.request.timeout value is 0. feature enabled=false

客户端开始连接ZK服务器了

CONNECTING

CONNECTING


Process finished with exit code 0

我在linux系统下连接得好好的,但是在java客户端就不行了。都是3.6.0,版本也没错啊。

# 报错信息的截图

# 相关课程内容截图

# 尝试过的解决思路和结果

# 粘贴全部相关代码,切记添加代码注释(请勿截图)

在这里输入代码,可

package com.imooc.zkjavaapi;

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;

import java.io.IOException;

/**
* 连接到ZK服务端,打印连接状态
*/
public class ZkConnect implements Watcher {

public static final String SERVER_PATH= "192.168.139.128:2921";

public static final Integer TimeOUT = 10000;

public static void main(String[] args) throws IOException, InterruptedException {
/**
* 客户端和服务端是异步连接,连接成功后,客户端会收到Watcher通知。
* connectString:服务器的ip+端口号,比如127.0.0.1:2181
*/
ZooKeeper zooKeeper = new ZooKeeper(SERVER_PATH, TimeOUT, new ZkConnect());
System.out.println("客户端开始连接ZK服务器了");
System.out.println(zooKeeper.getState());
Thread.sleep(2000);
System.out.println(zooKeeper.getState());
}

public void process(WatchedEvent watchedEvent) {
System.out.println("收到了通知" + watchedEvent);
}
}
log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{HH:mm:ss} %p %c{2}: %m%n
​<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>zk-practice</artifactId>
<version>1.0-SNAPSHOT</version>

<repositories>
<repository>
<id>nexus-aliyun</id>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<layout>default</layout>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.6.0</version>
</dependency>
</dependencies>
</project>


正在回答

登陆购买课程后可参与讨论,去登陆

3回答

同学你好,

1、127.0.0.1是本地的地址,不是服务器地址。同学看一下服务器地址是多少,例如:

http://img1.sycdn.imooc.com//climg/5fa8a106099b478206000326.jpg

2、重启一下Zookeeper,再查看一下启动状态,例如:

http://img1.sycdn.imooc.com//climg/5fa8a1bd0931d68405250320.jpg

如下是Zookeeper没有启动的状态:

http://img1.sycdn.imooc.com//climg/5fa8a54e09716b1805860187.jpg

检查一下ip地址和端口号是否写错了,同学应该写自己服务器的ip地址和端口号。看一下zookeeper服务是否启动了。再检查一下是否关闭了防火墙。 同学可以使用循环获得连接试试。

qq_精慕门6417434 2021-07-14 20:14:38

那个只是进程PID不是端口号,

好帮手慕阿慧 2020-11-08 14:59:25

同学你好,

1、2191是zookeeper进程,启动zookeeper时,系统会给进程分配一个进程号。

同学看一下zookeeper端口号是多少。如下:

http://img1.sycdn.imooc.com//climg/5fa7965709d794c005110123.jpg

2、建议同学关闭防火墙,命令如下:

systemctl stop firewalld.service

3、同学可以使用循环,每隔3秒获取一下状态。

参考代码如下:

http://img1.sycdn.imooc.com//climg/5fa7992c0945b24d14821039.jpg

连接zookeeper可能需要一分钟多,建议同学等待一下。

  • 提问者 慕仰7036876 #1
    link/ether 00:0c:29:30:20:cc brd ff:ff:ff:ff:ff:ff inet 192.168.139.128/24 brd 192.168.139.255 scope global dynamic ens33 valid_lft 1401sec preferred_lft 1401sec inet6 fe80::20c:29ff:fe30:20cc/64 scope link valid_lft forever preferred_lft forever 我试了192.168.139.128,也试了192.168.139.255,也试了127.0.0.1,但是都不管用。 显示错误信息为 22:35:28 WARN zookeeper.ClientCnxn: Session 0x0 for sever activate.navicat.com/127.0.0.1:2181, Closing socket connection. Attempting reconnect except it is a SessionExpiredException. java.net.ConnectException: Connection refused: no further information at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method) at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717) at org.apache.zookeeper.ClientCnxnSocketNIO.doTransport(ClientCnxnSocketNIO.java:342) at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1262)
    2020-11-08 22:38:03
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师