下载好zookeeper后想启动zookeeper,如果是windows直接运行%zookeeer安装目录%/bin/zkServer.cmd即可,运行的时候可能会出现闪退的问题。

首先要定位到问题,闪退的原因可能有几种。找到闪退的原因然后对症下药。

实际上这个zkServer.cmd是一个脚本文件,脚本里面有个命令pause。可以让程序暂停到这里不往下运行。类似于前端的debgger。和后端的断点debug启动服务。

打开zkServer.cmd文件在内容最后加上pause

@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
echo on
call %JAVA% "-Dzookeeper.log.dir=%ZOO_LOG_DIR%" "-Dzookeeper.root.logger=%ZOO_LOG4J_PROP%" -cp "%CLASSPATH%" %ZOOMAIN% "%ZOOCFG%" %*
pause # 在该位置加上这个pause
endlocal

重新启动zkServer.cmd

定位问题。我遇到的问题有两种情况

  1. 找不到zoo.cfg文件

​ 在下载zookeeper后,在%zookeeper安装目录%/conf目录下有一个zoo.sample.cfg文件,这个文件就是给你一个模板,告诉你zookeeper应该怎么配置。你可以直接把zoo.sample.cfg文件改名为zoo.cfg再重启zkServer.cmd,问题解决

​ 在zoo.cfg中配置了高可用的一些属性

# Zookeeper 服务器之间或客户端与服务器之间维持心跳的时间间隔,也就是每个 tickTime 时# 间就会发送一个心跳。tickTime以毫秒为单位。
tickTime=2000
    
# Leader-Follower初始通信时限
# 集群中的follower服务器(F)与leader服务器(L)之间初始连接时能容忍的最多心跳数#(tickTime的数量)。
initLimit=10
    
#Leader-Follower同步通信时限
#集群中的follower服务器与leader服务器之间请求和应答之间能容忍的最多心跳数(tickTime的数量)。
syncLimit=5
    
#数据文件目录
#Zookeeper保存数据的目录,默认情况下,Zookeeper将写数据的日志文件也保存在这个目录里。
dataDir=E:/zookeeper/data
# 客户端连接端口
# 客户端连接 Zookeeper 服务器的端口,Zookeeper 会监听这个端口,接受客户端的访问请求。
clientPort=2181

一个数据节点中包含镜像的个数,实际上zookeeper他是为了实现服务的高可用性,默认会有3个节点,zookeeper集群有一个特性:集群中只要有过半机器正常,就可以对外开放,默认有三个机器,这样有一台机器挂掉时,这个集群还是能够使用的一般设置为奇数个原因是3个和4个节点的容忍度是一样的可以省一台机器。
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
  1. jdk没有配置,或者jdk路径出错。

    zookeeper是一个java进程依赖于jdk,可以查看一下%zk安装目录%/bin/zkEnv.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.
    
    set ZOOCFGDIR=%~dp0%..\conf
    set ZOO_LOG_DIR=%~dp0%..
    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%
    这个地方就是配置启动zk找到的配置文件,默认去conf下的zoo.cfg文件,也就是出现闪退第一种情况去查找这个文件改名为zoo.cfg的原因
    set ZOOCFG=%ZOOCFGDIR%\zoo.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
    找到环境变量,看看有没有配置这个"JAVA_HOME"环境变量,注意这个值中的目录文件,在系统的环境变量中值不要有\
        我的环境变量的值是C:\Program Files\Java\jdk1.8.0_151
        如果你在这个后面有一个\那么zk去找环境变量的时候就会报错不要有\
    
    
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐