我的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>
正在回答
同学你好,
1、127.0.0.1是本地的地址,不是服务器地址。同学看一下服务器地址是多少,例如:
2、重启一下Zookeeper,再查看一下启动状态,例如:
如下是Zookeeper没有启动的状态:
检查一下ip地址和端口号是否写错了,同学应该写自己服务器的ip地址和端口号。看一下zookeeper服务是否启动了。再检查一下是否关闭了防火墙。 同学可以使用循环获得连接试试。
那个只是进程PID不是端口号,
- 参与学习 人
- 提交作业 9393 份
- 解答问题 16556 个
综合就业常年第一,编程排行常年霸榜,无需脱产即可学习,北上广深月薪过万 无论你是未就业的学生还是想转行的在职人员,不需要基础,只要你有梦想,想高薪
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星