4. ZooKeeper constructor

Zookeeper(Arguments) method, (there are a total of 4 construction methods depending on the parameters)

Parameter Description:

connectString: list of connected servers, separated by ","

sessionTimeout: Heartbeat detection time period (timeout, milliseconds)

watcher: event handler notifier

canBeReadOnly: identifies whether the current session supports read-only. By default, in a Zookeeper cluster, if a machine loses network connection with more than half of the machines in the cluster, the machine will no longer process client requests, but in some scenarios Next, when the ZooKeeper server has such a failure, we still hope that the ZooKeeper server can provide read services, which is the read-only mode of ZooKeeper.

sessionId and sessionPasswd: respectively represent the session ID and session key, which are used to uniquely determine a session, which can realize client session multiplexing, so as to achieve the effect of restoring the session. The specific usage method is that when connecting to the ZooKeeper server for the first time, the ID and secret key of the current session can be obtained by calling the following two interfaces of the ZooKeeper object instance:

long getSessionId();

byte[] getSessionPasswd();

After obtaining these two parameter values, you can pass in the constructor the next time you create a ZooKeeper object instance.


Note: The establishment of the zookeeper client and server session is an asynchronous process, that is to say, in the program, our program method returns immediately after processing the client initialization (that is, the program executes the code, so in most cases Next, we haven't really built a usable session, and it's really established when the session life cycle is in "CONNECTING", so we need to use CountDownLatch to coordinate)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325506867&siteId=291194637