Subscribe to Zookeeper events only after application starts

Sometimes ZK events came before the app start, causing repartition change events, while event listeners are not yet initialized by Spring
This commit is contained in:
ViacheslavKlimov 2025-02-27 12:45:22 +02:00
parent 442603a502
commit aa0179303c

View File

@ -143,6 +143,7 @@ public class ZkDiscoveryService implements DiscoveryService, PathChildrenCacheLi
} else {
log.info("Received application ready event. Starting current ZK node.");
}
subscribeToEvents();
if (client.getState() != CuratorFrameworkState.STARTED) {
log.debug("Ignoring application ready event, ZK client is not started, ZK client state [{}]", client.getState());
return;
@ -212,6 +213,7 @@ public class ZkDiscoveryService implements DiscoveryService, PathChildrenCacheLi
try {
destroyZkClient();
initZkClient();
subscribeToEvents();
publishCurrentServer();
} catch (Exception e) {
log.error("Failed to reconnect to ZK: {}", e.getMessage(), e);
@ -227,7 +229,6 @@ public class ZkDiscoveryService implements DiscoveryService, PathChildrenCacheLi
client.start();
client.blockUntilConnected();
cache = new PathChildrenCache(client, zkNodesDir, true);
cache.getListenable().addListener(this);
cache.start();
stopped = false;
log.info("ZK client connected");
@ -239,6 +240,10 @@ public class ZkDiscoveryService implements DiscoveryService, PathChildrenCacheLi
}
}
private void subscribeToEvents() {
cache.getListenable().addListener(this);
}
private void unpublishCurrentServer() {
try {
if (nodePath != null) {