Error: Could not find or load main class org.apache.zookeeper.server.quorum.QuorumPeerMain
对于zookeeper的安装,
- 下载安装包http://zookeeper.apache.org/
- 解压到你的安装位置,这里apache-zookeeper-3.5.5.tar.gz是我下载的最新安装包,/opt/module/是我的安装目录
[root@host-10-75-169-14 zookeeper]# tar -zxvf apache-zookeeper-3.5.5.tar.gz -C /opt/module/ - 修改配置文件,根据自己需求,修改zookeeper的数据存储位置,也是官方建议的修改:
[root@host-10-75-169-14 apache-zookeeper-3.5.5]# vi conf/zoo_sample.cfg
将 dataDir=/tmp/zookeeper
修改为 dataDir=/opt/module/apache-zookeeper-3.5.5/zkData
保存之后,将文件重命名为zoo.cfg:
[root@host-10-75-169-14 apache-zookeeper-3.5.5]# mv conf/zoo_sample.cfg conf/zoo.cfg - 尝试启动zookeeper,如下:
使用目前的 apache-zookeeper-3.5.5.tar.gz,我在启动zookeeper时,是这样的。
[root@host-10-75-169-14 apache-zookeeper-3.5.5]# ./bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /opt/module/apache-zookeeper-3.5.5/bin/…/conf/zoo.cfg
Starting zookeeper … STARTED
[root@host-10-75-169-14 apache-zookeeper-3.5.5]# jps
15337 QuorumPeerMain
15356 Jps
[root@host-10-75-169-14 apache-zookeeper-3.5.5]# jps
15374 Jps
[root@host-10-75-169-14 apache-zookeeper-3.5.5]# ./bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /opt/module/apache-zookeeper-3.5.5/bin/…/conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Error contacting service. It is probably not running.
[root@host-10-75-169-14 apache-zookeeper-3.5.5]# ./bin/zkServer.sh stop
ZooKeeper JMX enabled by default
Using config: /opt/module/apache-zookeeper-3.5.5/bin/…/conf/zoo.cfg
Stopping zookeeper … ./bin/zkServer.sh: line 213: kill: (15337) - No such process
STOPPED
启动之后立即使用jps命令可以看到QuorumPeerMain启动了,但是在第二次查看的时候就没有了。这是查看zkServer的状态,发现Error contacting service. It is probably not running. 我是单机启动,没有集群。查看log文件:
[root@host-10-75-169-14 apache-zookeeper-3.5.5]# vi logs/zookeeper-root-server-host-10-75-169-14.out
Error: Could not find or load main class org.apache.zookeeper.server.quorum.QuorumPeerMain
发现问题是,Could not find or load main class org.apache.zookeeper.server.quorum.QuorumPeerMain。关于这个也没有发现解决办法。以至于我都重新安装了JDK,都没有解决问题。
最后我去试了试不同的版本,即zookeeper-3.4.14.tar.gz,发现竟然顺利启动了,状态中的模式也是我想要的standalone。
[root@host-10-75-169-14 zookeeper-3.4.14]# ./bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /opt/module/zookeeper-3.4.14/bin/…/conf/zoo.cfg
Starting zookeeper … STARTED
[root@host-10-75-169-14 zookeeper-3.4.14]# jps
15187 QuorumPeerMain
15203 Jps
[root@host-10-75-169-14 zookeeper-3.4.14]# jps
15187 QuorumPeerMain
15224 Jps
[root@host-10-75-169-14 zookeeper-3.4.14]# jps
15187 QuorumPeerMain
15239 Jps
[root@host-10-75-169-14 zookeeper-3.4.14]# ./bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /opt/module/zookeeper-3.4.14/bin/…/conf/zoo.cfg
Mode: standalone
[root@host-10-75-169-14 zookeeper-3.4.14]# ./bin/zkServer.sh stop
ZooKeeper JMX enabled by default
Using config: /opt/module/zookeeper-3.4.14/bin/…/conf/zoo.cfg
Stopping zookeeper … STOPPED
同样的,我又尝试了apache-zookeeper-3.5.5-bin.tar.gz,这个安装包也可以。
感慨万千,去吃冰棍儿了!
集群安装时,
- 需要在conf/zoo.cfg末尾中添加集群信息,我这里的主机名分别时walter101,walter102,walter103,当然也可以配置为IP地址;myid为1,2,3;2888是该服务器与集群中leader交互信息的端口;3888为服务器进行选举时相互通信的端口。这两个端口可根据自己情况进行配置。
[root@walter101 zookeeper-3.4.14]# vi conf/zoo.cfg
…
###########################cluster#################################
server.1=walter101:2888:3888
server.2=walter102:2888:3888
server.3=walter103:2888:3888 - 在data存储目录内配置myid。我这里walter101的myid为1,walter102的myid为2,walter103的myid为3.
[root@walter101 zookeeper-3.4.14]# vi zkData/myid
1 - 注意关闭防火墙
- 查看防火墙状态
[root@walter101 zookeeper-3.4.14]# firewall-cmd --state
not running - 如果防火墙状态为running,需要关闭防火墙
[root@walter101 zookeeper-3.4.14]# systemctl stop firewalld.service - 如果想要永久关闭防火墙,可用下面命令
[root@walter102 zookeeper-3.4.14]# systemctl disable firewalld.service
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
- 查看防火墙状态
我在没有关闭防火墙的时候就出现了下面错误:
2019-09-06 10:00:26,955 [myid:1] - INFO [WorkerSender[myid=1]:QuorumPeer$QuorumServer@185] - Resolved hostname: 192.168.206.102 to address: /192.168.206.102
2019-09-06 10:00:26,956 [myid:1] - WARN [WorkerSender[myid=1]:QuorumCnxManager@584] - Cannot open channel to 3 at election address /192.168.206.103:3888
java.net.NoRouteToHostException: No route to host (Host unreachable)
这个错误可在日志文件中查看,如果在安装目录中有logs文件夹,否则会在运行zkServer.sh start命令的目录下生成zookeeper.out日志文件,由此可查看运行日志。
我这里配置了3台服务器,因此依次启动1,2,3号服务器之后,三台服务器中zookeeper的状态为:
[root@walter101 zookeeper-3.4.14]# ./bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /opt/module/zookeeper-3.4.14/bin/…/conf/zoo.cfg
Mode: follower
[root@walter102 zookeeper-3.4.14]# ./bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /opt/module/zookeeper-3.4.14/bin/…/conf/zoo.cfg
Mode: leader
[root@walter103 zookeeper-3.4.14]# ./bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /opt/module/zookeeper-3.4.14/bin/…/conf/zoo.cfg
Mode: follower
可以看到walter102为leader,walter101和walter103为follower。由此可以说明其选举机制(半数机制):
- 该集群中共有三台服务器;
- 当walter101启动时,walter101投票给自己,只有一票,未超过半数,因此不能选出leader;
- 当walter102启动时,walter101投票给walter102,因为walter102的myid更大,walter102也投票给自己,所以walter102有两票,超过半数,即walter102成为leader,walter101为follower;
- walter103启动时,此时walter102已经为leader,所以walter103自动为follower。
- 如果服务器正常工作数量少于半数,则该集群无法正常工作。
来源:CSDN
作者:ItsWalter
链接:https://blog.csdn.net/ItsWalter/article/details/100534551