Elastic Job 入门教程(五)— 配置Zookeeper集群注册中心

ゝ一世哀愁。 2022-05-10 08:52 631阅读 0赞

在我们之前的示例中,对于注册中心配置ZookeeperConfiguration中的serverLists属性,我们都是使用的Zookeeper单服务器

  1. reg-center:
  2. server-list: "192.168.43.61:2181"
  3. 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启动服务器

  1. ZooKeeper JMX enabled by default
  2. Using config: /home/icbc/local/apps/zookeeper/zookeeper-3.4.12/bin/../conf/zoo.cfg
  3. 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: leader

  • Follower

    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如下:

  1. reg-center:
  2. server-list: "192.168.43.61:2181,192.168.43.108:2181,192.168.43.108:2182"
  3. namespace: elastic-job-lite-spring-boot

启动我们的应用

  1. 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
  2. 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)
  3. 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
  4. 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
  5. 2018-09-27 15:21:02.934 INFO 8208 --- [ain-EventThread] o.a.c.f.state.ConnectionStateManager : State change: CONNECTED
  6. 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

发表评论

表情:
评论列表 (有 0 条评论,631人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Elastic-job视频教程

    1 课程内容介绍 ​ 本课程不仅讲解了Elastic-job分布式任务调度的使用方法及原理,而且通过案例讲解了如何构建分布式系统任务调度具体方案,通过从理论到实战的学习可