Elastic Job 入门教程(五)— 配置Zookeeper集群注册中心
在我们之前的示例中,对于注册中心配置ZookeeperConfiguration中的serverLists属性,我们都是使用的Zookeeper单服务器
reg-center:
server-list: "192.168.43.61:2181"
namespace: elastic-job-lite-spring-boot
serverLists属性本身是支持多个地址,如: host1:2181,host2:2181,下面我们简单搭建一个Zookeeper集群验证一下。
选取两台服务器:
- 192.168.43.61
- 192.168.43.108
61服务器搭建一台Zookeeper,其中clientPort为2181,108搭建两台Zookeeper,其中ClientPort分别为2181,2182
依次修改conf文件夹下的zoo.cfg配置
注意:在dataDir所配置的目录下,新建myid文件,文件内容为zoo.cfg配置文件中server.X对应的X,比如对于61服务器,server.X对应的X为0,那么我们需要指定myid文件的内容为0
zkServer.sh start启动服务器
ZooKeeper JMX enabled by default
Using config: /home/icbc/local/apps/zookeeper/zookeeper-3.4.12/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
zkServer.sh status检测服务器状态,是否正常启动
Leader
ZooKeeper JMX enabled by default
Using config: /home/icbc/local/apps/zookeeper1/zookeeper-3.4.12/bin/../conf/zoo.cfg
Mode: leaderFollower
ZooKeeper JMX enabled by default
Using config: /home/icbc/local/apps/zookeeper/zookeeper-3.4.12/bin/../conf/zoo.cfg
Mode: follower
搭建好Zookeeper服务器集群之后,我们重新配置ZookeeperConfiguration.serverLists如下:
reg-center:
server-list: "192.168.43.61:2181,192.168.43.108:2181,192.168.43.108:2182"
namespace: elastic-job-lite-spring-boot
启动我们的应用
2018-09-27 15:21:02.873 INFO 8208 --- [ main] org.apache.zookeeper.ZooKeeper : Initiating client connection, connectString=192.168.43.61:2181,192.168.43.108:2181,192.168.43.108:2182 sessionTimeout=60000 watcher=org.apache.curator.ConnectionState@88d6f9b
2018-09-27 15:21:02.918 INFO 8208 --- [68.43.108:2182)] org.apache.zookeeper.ClientCnxn : Opening socket connection to server 192.168.43.108/192.168.43.108:2182. Will not attempt to authenticate using SASL (unknown error)
2018-09-27 15:21:02.920 INFO 8208 --- [68.43.108:2182)] org.apache.zookeeper.ClientCnxn : Socket connection established to 192.168.43.108/192.168.43.108:2182, initiating session
2018-09-27 15:21:02.929 INFO 8208 --- [68.43.108:2182)] org.apache.zookeeper.ClientCnxn : Session establishment complete on server 192.168.43.108/192.168.43.108:2182, sessionid = 0x2000061f1490001, negotiated timeout = 60000
2018-09-27 15:21:02.934 INFO 8208 --- [ain-EventThread] o.a.c.f.state.ConnectionStateManager : State change: CONNECTED
2018-09-27 15:21:03.019 INFO 8208 --- [ main] com.alibaba.druid.pool.DruidDataSource : {dataSource-1} inited
使用Zookeeper可视化管理工具连接任意一台Zookeeper服务器,我们可以看到里面的节点状况
任意关闭一台Zookeeper服务器(如果关闭了Leader,Zookeeper会从剩余的服务器中选取中新的Leader),Elastic Job作业依然可以正常注册。
更多源码请参考:
https://github.com/ypmc/spring-cloud/tree/master/spring-elastic-job
还没有评论,来说两句吧...