This is the seventh day of my participation in the August More text Challenge. For details, see:August is more challenging

There is a small bug on the line, which is caused by the initialization of ZooKeeper. Please record the repair process. In fact, it has nothing to do with ZooKeeper, but is a concurrent matter

My program, connected to the ZK, to do some distributed notification and logic control things, using someone else’s encapsulated client, to register, listen, get notification, response to the corresponding logic.

However, when Spring started loading, it reported an error:

zookeeper is not initialized…. What the…

Zk registration failure means that the control logic that depends on ZK is time-limited, such as dynamic log level switching, and we need to find out why.

First, you find the ZK client in the Spring configuration file

As you can see, the zK server address and node path are initialized in the init method (the synchronization logic is set to false). False means to restart a thread during init to do the initialization

Next comes the operation classes that register the nodes and listeners.

Here, in the form of configuration, add the above zkClient to the class property, and then in the init method, register the listener that will be used

The problem is that zkClient is initialized when the registration method of super is called.

We set sync to false, so we’re just handing over the initialization, so if we use it to register, it’s going to throw an exception. Obviously, the release of the zkClient class at this time is not secure


To solve this problem, one way is to set sync to true, sync mode, and wait until you register.

The second option is to inject zkClient with deprecated configuration

Annotation form and set to thread visible.

Class security release, is the premise of a program normal operation, should pay attention to, take as a warning ~