Skip to content

1. Create a ZkClient

Pierre-Antoine Ganaye edited this page Jul 27, 2014 · 1 revision

Simple client

val client = Zookeeper.newRichClient("192.168.0.1:2181,192.168.0.10:2181,192.168.0.5:2181")

newRichClient takes in parameter a String composed by the list of servers separated by commas

In this configuration AutoReconnect is disabled and ZkConfiguration set to :

ZkConfiguration(
        autoWatchReset = true,
        canReadOnly = true,
        chroot = "",
        sessionTimeout = 3000.milliseconds
      )

Client with ZkConfiguration

val client = Zookeeper
    .withZkConfiguration(
      autoWatchReset = true,
      canReadOnly = true,
      chroot = "",
      sessionTimeout = 3000.milliseconds
    )
    .newRichClient("192.168.0.1:2181,192.168.0.10:2181,192.168.0.5:2181")

withZkConfiguration takes in parameter :

  • autoWatchReset : if you want to reset watches after reconnection

  • canReadOnly : if read-only mode is allowed on client side

  • chroot : the chrooted path

  • sessionTimeout : the ZooKeeper session timeout

Could also be used this way :

val client = Zookeeper
    .withZkConfiguration()
    .newRichClient("192.168.0.1:2181,192.168.0.10:2181,192.168.0.5:2181")

Client with AutoReconnect

val client = Zookeeper
    .withAutoReconnect(
      autoRwServerSearch = Some(1.minute),
      preventiveSearch = Some(10.minutes),
      timeBetweenAttempts = 30.seconds,
      timeBetweenLinkCheck = Some(30.seconds),
      maxConsecutiveRetries = 10,
      maxReconnectAttempts = 5
    )
    .newRichClient("192.168.0.1:2181,192.168.0.10:2181,192.168.0.5:2181")

withAutoReconnect takes in parameter :

  • autoRwServerSearch : None if you want to disable automatic Read and Write server search, otherwise Some(Duration) where Duration is the time interval between each scan.

  • preventiveSearch : None if you want to disable preventive server search, otherwise Some(Duration) where Duration is the time interval between each scan.

  • timeBetweenLinkChecks : None if you want to disable automatic link check, otherwise Some(Duration) where Duration is the time interval between each verification.

  • timeBetweenAttempts : the duration between each reconnection attempts. If timeBetweenLinkChecks is disable this parameter is ignored.

  • maxConsecutiveRetries : the maximum number of consecutive retries during reconnection.

  • maxReconnectAttempts : the total maximum number of reconnection attempts.

Could also be used this way :

val client = Zookeeper
    .withAutoReconnect()
    .newRichClient("192.168.0.1:2181,192.168.0.10:2181,192.168.0.5:2181")

Client with ZkConfiguration and AutoReconnect

val client = Zookeeper
    .withAutoReconnect(
      autoRwServerSearch = Some(1.minute),
      preventiveSearch = Some(10.minutes),
      timeBetweenAttempts = 30.seconds,
      timeBetweenLinkCheck = Some(30.seconds),
      maxConsecutiveRetries = 10,
      maxReconnectAttempts = 5
    )
    .withZkConfiguration(
      autoWatchReset = true,
      canReadOnly = true,
      chroot = "",
      sessionTimeout = 3000.milliseconds
    )
    .newRichClient("192.168.0.1:2181,192.168.0.10:2181,192.168.0.5:2181")

Could also be used this way :

val client = Zookeeper
    .withAutoReconnect()
    .withZkConfiguration()
    .newRichClient("192.168.0.1:2181,192.168.0.10:2181,192.168.0.5:2181")