策展人领导者选举错误,而连接字符串不是新的TestingServer()。getConnectString

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了策展人领导者选举错误,而连接字符串不是新的TestingServer()。getConnectString相关的知识,希望对你有一定的参考价值。

将示例代码复制到本地,将zk连接字符串从新的TestingServer()。getConnectString更改为“host:port”,获取错误:

错误org.apache.curator.framework.recipes.leader.LeaderLatch getChildren()失败。 rc = -6 [main-EventThread]

码:

import org.apache.curator.framework.CuratorFramework
import org.apache.curator.framework.CuratorFrameworkFactory
import org.apache.curator.framework.recipes.leader.{LeaderLatch,LeaderLatchListener}
import org.apache.curator.retry.ExponentialBackoffRetry
import org.apache.curator.utils.CloseableUtils
import java.io.BufferedReader
import java.io.InputStreamReader
import java.util

object Main {
  private val PATH = "/lead"

  def main(args: Array[String]): Unit = {
    val clients = new util.ArrayList[CuratorFramework]
    val lists = new util.ArrayList[LeaderLatch]()

    try {
      for (i <- 0 to 2) {
        val client = CuratorFrameworkFactory.newClient("xxxxx.org:2181", new ExponentialBackoffRetry(10000, 1))
        //val client =  CuratorFrameworkFactory.newClient(new TestingServer().getConnectString, new ExponentialBackoffRetry(10000, 1))

        clients.add(client)
        client.start()
        client.blockUntilConnected()

        val example = new LeaderLatch(client, PATH, i.toString)
        lists.add(example)

        example.start()

        example.addListener(new LeaderLatchListener {
            override def isLeader(): Unit = {
            println(s"I am the lead $i")
          }

          override def notLeader(): Unit = {
            println(s"i am not the leader any more $i")
          }
        })
      }

      Thread.sleep(5000)
      println("Press enter/return to quit
")
      new BufferedReader(new InputStreamReader(System.in)).readLine
    }
    finally {
      System.out.println("Shutting down...")
      import scala.collection.JavaConversions._
      for(example <- lists)
        CloseableUtils.closeQuietly(example)
      for (client <- clients) {
        CloseableUtils.closeQuietly(client)
      }
    }
  }
}

库:

libraryDependencies += "org.apache.curator" % "apache-curator" % "4.0.0"
libraryDependencies += "org.apache.curator" % "curator-framework" % "4.0.0"
libraryDependencies += "org.apache.curator" % "curator-recipes" % "4.0.0"
libraryDependencies += "org.apache.curator" % "curator-test" % "4.0.0"
libraryDependencies += "org.apache.curator" % "curator-x-discovery" % "4.0.0"
libraryDependencies += "org.apache.curator" % "curator-x-async" % "4.0.0"
答案

想出来,理论上没什么,但有效。

  • 将策展人lib更改为3.3.0
  • zookeeper中的手动创建/引导节点

Zookeeper版本:3.4.10

所以它看起来像版本兼容性问题。

另一答案

您需要从策展人依赖项exclude("org.apache.zookeeper","zookeeper"),中排除zookeeper

然后添加正确的zookeeper依赖"org.apache.zookeeper" % "zookeeper" % "3.4.9"

我的zookeeper服务器是3.4.9

另一答案

zooker和curator版本兼容性。编辑maven依赖关系,例如:

<dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-x-discovery</artifactId>
            <version>4.0.1</version>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.zookeeper</groupId>
                    <artifactId>zookeeper</artifactId>
                </exclusion>
            </exclusions>
</dependency>

以上是关于策展人领导者选举错误,而连接字符串不是新的TestingServer()。getConnectString的主要内容,如果未能解决你的问题,请参考以下文章

sh 策展人install.sh

java 策展人(动物园管理员)

网易云音乐第三季星辰集创作营开营

Apache Curator ZooKeeper单元测试产生错误

区块链+国潮艺术展,科技与艺术的梦幻联动也很美!

图解Raft之领导者选举