目录
现象
启动命令
d:\zk>bin\zkServer.cmd start
错误
d:\zk>call "D:\DEV\JAVA\JDK"\bin\java "-Dzookeeper.log.dir=d:\zk\bin\..\logs" "-Dzookeeper.root.logger=INFO,CONSOLE" "-Dzookeeper.log.file=zookeeper-YQY-server-DUCKLING-S.log" "-XX:+HeapDumpOnOutOfMemoryError" "-XX:OnOutOfMemoryError=cmd /c taskkill /pid %%p /t /f" -cp "d:\zk\bin\..\build\classes;d:\zk\bin\..\build\lib\*;d:\zk\bin\..\*;d:\zk\bin\..\lib\*;d:\zk\bin\..\conf" org.apache.zookeeper.server.quorum.QuorumPeerMain "d:\zk\bin\..\conf\zoo.cfg" start
2021-06-02 18:41:21,899 [myid:] - INFO [main:DatadirCleanupManager@78] - autopurge.snapRetainCount set to 3
2021-06-02 18:41:21,901 [myid:] - INFO [main:DatadirCleanupManager@79] - autopurge.purgeInterval set to 0
2021-06-02 18:41:21,902 [myid:] - INFO [main:DatadirCleanupManager@101] - Purge task is not scheduled.
2021-06-02 18:41:21,902 [myid:] - WARN [main:QuorumPeerMain@125] - Either no config or no quorum defined in config, running in standalone mode
2021-06-02 18:41:21,903 [myid:] - INFO [main:ManagedUtil@46] - Log4j found with jmx enabled.
2021-06-02 18:41:21,952 [myid:] - ERROR [main:ZooKeeperServerMain@66] - Invalid arguments, exiting abnormally
java.lang.NumberFormatException: For input string: "d:\zk\bin\..\conf\zoo.cfg"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at org.apache.zookeeper.server.ServerConfig.parse(ServerConfig.java:63)
at org.apache.zookeeper.server.ZooKeeperServerMain.initializeAndRun(ZooKeeperServerMain.java:103)
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)
2021-06-02 18:41:21,954 [myid:] - INFO [main:ZooKeeperServerMain@67] - Usage: ZooKeeperServerMain configfile | port datadir [ticktime] [maxcnxns]
Usage: ZooKeeperServerMain configfile | port datadir [ticktime] [maxcnxns]
原因
为何Linux可以,而Windows下不行呢?这里分别打开了两个版本的启动文件。
zkServer.sh 代码片段
可以看到,该脚本接收了输入参数,并且在start分之后到调用了java程序。
相当于start是针对zkServer.sh的,而不是针对zk主类 org.apache.zookeeper.server.quorum.QuorumPeerMain的。
case $1 in
start)
echo -n "Starting zookeeper ... "
if [ -f "$ZOOPIDFILE" ]; then
if kill -0 `cat "$ZOOPIDFILE"` > /dev/null 2>&1; then
echo $command already running as process `cat "$ZOOPIDFILE"`.
exit 1
fi
fi
nohup "$JAVA" $ZOO_DATADIR_AUTOCREATE "-Dzookeeper.log.dir=${ZOO_LOG_DIR}" \
"-Dzookeeper.log.file=${ZOO_LOG_FILE}" "-Dzookeeper.root.logger=${ZOO_LOG4J_PROP}" \
-XX:+HeapDumpOnOutOfMemoryError -XX:OnOutOfMemoryError='kill -9 %p' \
-cp "$CLASSPATH" $JVMFLAGS $ZOOMAIN "$ZOOCFG" > "$_ZOO_DAEMON_OUT" 2>&1 < /dev/null &
if [ $? -eq 0 ]
then
case "$OSTYPE" in
*solaris*)
/bin/echo "${!}\\c" > "$ZOOPIDFILE"
;;
*)
/bin/echo -n $! > "$ZOOPIDFILE"
;;
esac
if [ $? -eq 0 ];
then
sleep 1
pid=$(cat "${ZOOPIDFILE}")
if ps -p "${pid}" > /dev/null 2>&1; then
echo STARTED
else
echo FAILED TO START
exit 1
fi
else
echo FAILED TO WRITE PID
exit 1
fi
else
echo SERVER DID NOT START
exit 1
fi
;;
除此之外,还可以看到支持start-foreground、stop、restart、status等操作。
zkServer.cmd代码片段
@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.
setlocal
call "%~dp0zkEnv.cmd"
set ZOOMAIN=org.apache.zookeeper.server.quorum.QuorumPeerMain
set ZOO_LOG_FILE=zookeeper-%USERNAME%-server-%COMPUTERNAME%.log
echo on
call %JAVA% "-Dzookeeper.log.dir=%ZOO_LOG_DIR%" "-Dzookeeper.root.logger=%ZOO_LOG4J_PROP%" "-Dzookeeper.log.file=%ZOO_LOG_FILE%" "-XX:+HeapDumpOnOutOfMemoryError" "-XX:OnOutOfMemoryError=cmd /c taskkill /pid %%%%p /t /f" -cp "%CLASSPATH%" %ZOOMAIN% "%ZOOCFG%" %*
endlocal
cmd中,并没有对输入的参数进行任何解析,而是直接将其交给了org.apache.zookeeper.server.quorum.QuorumPeerMain类。
源码分析
根据错误提示,直接找到源码。
org.apache.zookeeper.server.ServerConfig.parse(ServerConfig.java:63)
因版本不通过,代码行数可能有所差异,可以根据方法名称找到关键信息。
/**
* Parse arguments for server configuration
* @param args clientPort dataDir and optional tickTime and maxClientCnxns
* @throws IllegalArgumentException on invalid usage
*/
public void parse(String[] args) {
if (args.length < 2 || args.length > 4) {
throw new IllegalArgumentException("Invalid number of arguments:" + Arrays.toString(args));
}
clientPortAddress = new InetSocketAddress(Integer.parseInt(args[0]));
dataDir = new File(args[1]);
dataLogDir = dataDir;
if (args.length >= 3) {
tickTime = Integer.parseInt(args[2]);
}
if (args.length == 4) {
maxClientCnxns = Integer.parseInt(args[3]);
}
}
根据异常综合判断,可能与提交的参数的数量、参数的名称有关。
todo 这里没能从代码中分析出具体的失败原因,需要把zk代码下到本地IDE上运行才能准确判断。
当前时间有限,先把坑挖出来,日后再填。
解决方法
windows下,直接使用zkServer.cmd,不需要加参数即可启动。
d:\zk>bin\zkServer.cmd
d:\zk>bin\zkServer.cmd
d:\zk>call "D:\DEV\JAVA\JDK"\bin\java "-Dzookeeper.log.dir=d:\zk\bin\..\logs" "-Dzookeeper.root.logger=INFO,CONSOLE" "-Dzookeeper.log.file=zookeeper-YQY-server-DUCKLING-S.log" "-XX:+HeapDumpOnOutOfMemoryError" "-XX:OnOutOfMemoryError=cmd /c taskkill /pid %%p /t /f" -cp "d:\zk\bin\..\build\classes;d:\zk\bin\..\build\lib\*;d:\zk\bin\..\*;d:\zk\bin\..\lib\*;d:\zk\bin\..\conf" org.apache.zookeeper.server.quorum.QuorumPeerMain "d:\zk\bin\..\conf\zoo.cfg"
2021-06-02 18:44:13,309 [myid:] - INFO [main:QuorumPeerConfig@133] - Reading configuration from: d:\zk\bin\..\conf\zoo.cfg
2021-06-02 18:44:13,322 [myid:] - INFO [main:QuorumPeerConfig@385] - clientPortAddress is 0.0.0.0/0.0.0.0:2181
2021-06-02 18:44:13,322 [myid:] - INFO [main:QuorumPeerConfig@389] - secureClientPort is not set
2021-06-02 18:44:13,325 [myid:] - INFO [main:DatadirCleanupManager@78] - autopurge.snapRetainCount set to 3
2021-06-02 18:44:13,326 [myid:] - INFO [main:DatadirCleanupManager@79] - autopurge.purgeInterval set to 0
2021-06-02 18:44:13,327 [myid:] - INFO [main:DatadirCleanupManager@101] - Purge task is not scheduled.
2021-06-02 18:44:13,327 [myid:] - WARN [main:QuorumPeerMain@125] - Either no config or no quorum defined in config, running in standalone mode
2021-06-02 18:44:13,328 [myid:] - INFO [main:ManagedUtil@46] - Log4j found with jmx enabled.
2021-06-02 18:44:13,376 [myid:] - INFO [main:QuorumPeerConfig@133] - Reading configuration from: d:\zk\bin\..\conf\zoo.cfg
2021-06-02 18:44:13,377 [myid:] - INFO [main:QuorumPeerConfig@385] - clientPortAddress is 0.0.0.0/0.0.0.0:2181
2021-06-02 18:44:13,377 [myid:] - INFO [main:QuorumPeerConfig@389] - secureClientPort is not set
2021-06-02 18:44:13,378 [myid:] - INFO [main:ZooKeeperServerMain@117] - Starting server
2021-06-02 18:44:13,398 [myid:] - INFO [main:Environment@109] - Server environment:zookeeper.version=3.5.5-390fe37ea45dee01bf87dc1c042b5e3dcce88653, built on 05/03/2019 12:07 GMT
2021-06-02 18:44:13,398 [myid:] - INFO [main:Environment@109] - Server environment:host.name=Duckling-S
2021-06-02 18:44:13,398 [myid:] - INFO [main:Environment@109] - Server environment:java.version=1.8.0_271
2021-06-02 18:44:13,399 [myid:] - INFO [main:Environment@109] - Server environment:java.vendor=Oracle Corporation
2021-06-02 18:44:13,401 [myid:] - INFO [main:Environment@109] - Server environment:java.home=D:\DEV\JAVA\JDK\jre
2021-06-02 18:44:13,401 [myid:] - INFO [main:Environment@109] - Server environment:java.class.path=d:\zk\bin\..\build\classes;d:\zk\bin\..\build\lib\*;d:\zk\bin\..\*;d:\zk\bin\..\lib\audience-annotations-0.5.0.jar;d:\zk\bin\..\lib\commons-cli-1.2.jar;d:\zk\bin\..\lib\jackson-annotations-2.9.0.jar;d:\zk\bin\..\lib\jackson-core-2.9.8.jar;d:\zk\bin\..\lib\jackson-databind-2.9.8.jar;d:\zk\bin\..\lib\javax.servlet-api-3.1.0.jar;d:\zk\bin\..\lib\jetty-http-9.4.17.v20190418.jar;d:\zk\bin\..\lib\jetty-io-9.4.17.v20190418.jar;d:\zk\bin\..\lib\jetty-security-9.4.17.v20190418.jar;d:\zk\bin\..\lib\jetty-server-9.4.17.v20190418.jar;d:\zk\bin\..\lib\jetty-servlet-9.4.17.v20190418.jar;d:\zk\bin\..\lib\jetty-util-9.4.17.v20190418.jar;d:\zk\bin\..\lib\jline-2.11.jar;d:\zk\bin\..\lib\json-simple-1.1.1.jar;d:\zk\bin\..\lib\log4j-1.2.17.jar;d:\zk\bin\..\lib\netty-all-4.1.29.Final.jar;d:\zk\bin\..\lib\slf4j-api-1.7.25.jar;d:\zk\bin\..\lib\slf4j-log4j12-1.7.25.jar;d:\zk\bin\..\lib\zookeeper-3.5.5.jar;d:\zk\bin\..\lib\zookeeper-jute-3.5.5.jar;d:\zk\bin\..\conf
2021-06-02 18:44:13,402 [myid:] - INFO [main:Environment@109] - Server environment:java.library.path=D:\DEV\JAVA\JDK\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Program Files\VanDyke Software\Clients\;D:\DEV\Oracle\product\12.2.0\client_1\bin;D:\Program Files (x86)\VMware\VMware Workstation\bin\;D:\DEV\CUDA\Development\bin;D:\DEV\CUDA\Development\libnvvp;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files\NVIDIA Corporation\Nsight Compute 2020.2.1\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;C:\Program Files\TortoiseSVN\bin;D:\DEV\JAVA\JDK\bin;C:\Users\YQY\.cargo\bin;C:\Users\YQY\AppData\Local\Microsoft\WindowsApps;;D:\DEV\Python\PyCharmCommunityEdition2020.2.3\bin;;D:\DEV\JAVA\idea\IntelliJIDEACommunityEdition2020.2.3\bin;;D:\DEV\VSCode\MicrosoftVSCode\bin;.
2021-06-02 18:44:13,402 [myid:] - INFO [main:Environment@109] - Server environment:java.io.tmpdir=C:\Users\YQY\AppData\Local\Temp\
2021-06-02 18:44:13,402 [myid:] - INFO [main:Environment@109] - Server environment:java.compiler=<NA>
2021-06-02 18:44:13,403 [myid:] - INFO [main:Environment@109] - Server environment:os.name=Windows 10
2021-06-02 18:44:13,403 [myid:] - INFO [main:Environment@109] - Server environment:os.arch=amd64
2021-06-02 18:44:13,403 [myid:] - INFO [main:Environment@109] - Server environment:os.version=10.0
2021-06-02 18:44:13,404 [myid:] - INFO [main:Environment@109] - Server environment:user.name=YQY
2021-06-02 18:44:13,404 [myid:] - INFO [main:Environment@109] - Server environment:user.home=C:\Users\YQY
2021-06-02 18:44:13,404 [myid:] - INFO [main:Environment@109] - Server environment:user.dir=d:\zk
2021-06-02 18:44:13,404 [myid:] - INFO [main:Environment@109] - Server environment:os.memory.free=1456MB
2021-06-02 18:44:13,405 [myid:] - INFO [main:Environment@109] - Server environment:os.memory.max=21824MB
2021-06-02 18:44:13,405 [myid:] - INFO [main:Environment@109] - Server environment:os.memory.total=1472MB
2021-06-02 18:44:13,406 [myid:] - INFO [main:ZooKeeperServer@938] - minSessionTimeout set to 4000
2021-06-02 18:44:13,406 [myid:] - INFO [main:ZooKeeperServer@947] - maxSessionTimeout set to 40000
2021-06-02 18:44:13,407 [myid:] - INFO [main:ZooKeeperServer@166] - Created server with tickTime 2000 minSessionTimeout 4000 maxSessionTimeout 40000 datadir D:\zk\data\version-2 snapdir D:\zk\data\version-2
2021-06-02 18:44:13,427 [myid:] - INFO [main:Log@193] - Logging initialized @267ms to org.eclipse.jetty.util.log.Slf4jLog
2021-06-02 18:44:13,520 [myid:] - WARN [main:ContextHandler@1588] - o.e.j.s.ServletContextHandler@61832929{/,null,UNAVAILABLE} contextPath ends with /*
2021-06-02 18:44:13,520 [myid:] - WARN [main:ContextHandler@1599] - Empty contextPath
2021-06-02 18:44:13,531 [myid:] - INFO [main:Server@370] - jetty-9.4.17.v20190418; built: 2019-04-18T19:45:35.259Z; git: aa1c656c315c011c01e7b21aabb04066635b9f67; jvm 1.8.0_271-b09
2021-06-02 18:44:13,563 [myid:] - INFO [main:DefaultSessionIdManager@365] - DefaultSessionIdManager workerName=node0
2021-06-02 18:44:13,563 [myid:] - INFO [main:DefaultSessionIdManager@370] - No SessionScavenger set, using defaults
2021-06-02 18:44:13,566 [myid:] - INFO [main:HouseKeeper@149] - node0 Scavenging every 600000ms
2021-06-02 18:44:13,573 [myid:] - INFO [main:ContextHandler@855] - Started o.e.j.s.ServletContextHandler@61832929{/,null,AVAILABLE}
2021-06-02 18:44:13,759 [myid:] - INFO [main:AbstractConnector@292] - Started ServerConnector@15975490{HTTP/1.1,[http/1.1]}{0.0.0.0:8080}
2021-06-02 18:44:13,759 [myid:] - INFO [main:Server@410] - Started @606ms
2021-06-02 18:44:13,760 [myid:] - INFO [main:JettyAdminServer@112] - Started AdminServer on address 0.0.0.0, port 8080 and command URL /commands
2021-06-02 18:44:13,764 [myid:] - INFO [main:ServerCnxnFactory@135] - Using org.apache.zookeeper.server.NIOServerCnxnFactory as server connection factory
2021-06-02 18:44:13,766 [myid:] - INFO [main:NIOServerCnxnFactory@673] - Configuring NIO connection handler with 10s sessionless connection timeout, 2 selector thread(s), 24 worker threads, and 64 kB direct buffers.
2021-06-02 18:44:13,768 [myid:] - INFO [main:NIOServerCnxnFactory@686] - binding to port 0.0.0.0/0.0.0.0:2181
2021-06-02 18:44:13,779 [myid:] - INFO [main:ZKDatabase@117] - zookeeper.snapshotSizeFactor = 0.33
2021-06-02 18:44:13,785 [myid:] - INFO [main:FileSnap@83] - Reading snapshot D:\zk\data\version-2\snapshot.0
2021-06-02 18:44:13,811 [myid:] - INFO [main:FileTxnSnapLog@372] - Snapshotting: 0x12c to D:\zk\data\version-2\snapshot.12c
2021-06-02 18:44:13,825 [myid:] - INFO [main:ContainerManager@64] - Using checkIntervalMs=60000 maxPerMinute=10000