zookeeper在Windows下的使用及端口占用处理

最近因项目需要,用了zookeeper,但是网上找的一些资料不甚友好。顾记录一番,供大家参考。

zookeeper3.5版,提供一下下载(百度网盘)。

链接:https://pan.baidu.com/s/15dZ8OWeujlhYIyZvP44U7Q

提取码:7jq4

其他目录结构不赘述,直接看bin目录下的。.sh结尾的是Linux环境下的。.cmd 是Windows环境下的。

如下图:

zkServer.cmd --启动服务端

zkCli.cmd --启动客户端

20190701

常见问题

  1. 客户端启动一闪而过,没有启动

原因探索:

  • 一、编辑zkServer.cmd文件,在末尾添加pause 。这样运行出错就不会退出,会提示错误信息,方便找到原因。

20190702

例如原因如下:

没有找到zoo.cfg,初步判定是配置的问题。

到zookeeper目录中找conf\zoo.cfg ,没有找到此文件,但找到zoo_sample.cfg ,这个是一些zookeeper服务器的参数配置样板,zookeeper的配置可以参考这个文件。

解决方法

需要重要配置zoo.cfg 的路径文件。 

编辑zkEnv.cmd 文件(该文件是环境配置,如文件夹目录等。一般启动出错是在由于环境变量配置导致相关文件查找不到,只需要在其中配置相关变量即可),改变ZOOCFG 的值为%ZOOCFGDIR%\zoo_sample.cfg 即可。

@echo off
REM Licensed to the Apache Software Foundation (ASF) under one or more
REM contributor license agreements.  See the NOTICE file distributed with
REM this work for additional information regarding copyright ownership.
REM The ASF licenses this file to You under the Apache License, Version 2.0
REM (the "License"); you may not use this file except in compliance with
REM the License.  You may obtain a copy of the License at
REM
REM     http://www.apache.org/licenses/LICENSE-2.0
REM
REM Unless required by applicable law or agreed to in writing, software
REM distributed under the License is distributed on an "AS IS" BASIS,
REM WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
REM See the License for the specific language governing permissions and
REM limitations under the License.
set ZOOCFGDIR=%~dp0%..\conf
set ZOO_LOG_DIR=%~dp0%..\logs
set ZOO_LOG4J_PROP=INFO,CONSOLE
REM for sanity sake assume Java 1.6
REM see: http://java.sun.com/javase/6/docs/technotes/tools/windows/java.html
REM add the zoocfg dir to classpath
set CLASSPATH=%ZOOCFGDIR%
REM make it work in the release
SET CLASSPATH=%~dp0..\*;%~dp0..\lib\*;%CLASSPATH%
REM make it work for developers
SET CLASSPATH=%~dp0..\build\classes;%~dp0..\build\lib\*;%CLASSPATH%
set ZOOCFG=%ZOOCFGDIR%\zoo_sample.cfg
@REM setup java environment variables
if not defined JAVA_HOME (
  echo Error: JAVA_HOME is not set.
  goto :eof
)
set JAVA_HOME=%JAVA_HOME:"=%
if not exist "%JAVA_HOME%"\bin\java.exe (
  echo Error: JAVA_HOME is incorrectly set.
  goto :eof
)
set JAVA="%JAVA_HOME%"\bin\java

2.如上处理后还是无法启动

控制台显示:

ZooKeeper JMX enabled by default
Using config: /opt/zookeeper-3.5.4-beta/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED

以为启动成功,然后连接时报错,查看后台进程,没有Zookeeper进程。

2019-07-22 15:50:23,724 [myid:] - ERROR [main:ZooKeeperServerMain@79] - Unable to start AdminServer, exiting abnormally
org.apache.zookeeper.server.admin.AdminServer$AdminServerException: Problem starting AdminServer on address 0.0.0.0, port 8080 and command URL /commands
	at org.apache.zookeeper.server.admin.JettyAdminServer.start(JettyAdminServer.java:107)
	at org.apache.zookeeper.server.ZooKeeperServerMain.runFromConfig(ZooKeeperServerMain.java:138)
	at org.apache.zookeeper.server.ZooKeeperServerMain.initializeAndRun(ZooKeeperServerMain.java:106)
	at org.apache.zookeeper.server.ZooKeeperServerMain.main(ZooKeeperServerMain.java:64)
	at org.apache.zookeeper.server.quorum.QuorumPeerMain.initializeAndRun(QuorumPeerMain.java:128)
	at org.apache.zookeeper.server.quorum.QuorumPeerMain.main(QuorumPeerMain.java:82)
Caused by: java.io.IOException: Failed to bind to /0.0.0.0:8080
	at org.eclipse.jetty.server.ServerConnector.openAcceptChannel(ServerConnector.java:346)
	at org.eclipse.jetty.server.ServerConnector.open(ServerConnector.java:308)
	at org.eclipse.jetty.server.AbstractNetworkConnector.doStart(AbstractNetworkConnector.java:80)
	at org.eclipse.jetty.server.ServerConnector.doStart(ServerConnector.java:236)
	at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
	at org.eclipse.jetty.server.Server.doStart(Server.java:396)
	at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
	at org.apache.zookeeper.server.admin.JettyAdminServer.start(JettyAdminServer.java:103)
	... 5 more
Caused by: java.net.BindException: 地址已在使用
	at sun.nio.ch.Net.bind0(Native Method)
	at sun.nio.ch.Net.bind(Net.java:433)
	at sun.nio.ch.Net.bind(Net.java:425)
	at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223)
	at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74)
	at org.eclipse.jetty.server.ServerConnector.openAcceptChannel(ServerConnector.java:342)
	... 12 more

通过日志可以看到,8080的端口已经被占用。一种方法是去查看占用该端口的服务,将其关闭。另一种就是将ZK的服务占用端口更改(推荐)。方法如下:

去zoo_sample.cfg文件中修改端口号,修改成你喜欢的端口号即可。

如:admin.serverPort=8090

20190703

最后再重新启动 zkServer.cmd 服务

20190704

至此,便成功启动ZK服务了!

猜你喜欢

转载自blog.csdn.net/Roker_966/article/details/97273874