Upgrade curator-recipes to 5.1.0 to fix API change for ZooKeeper data-source (#2963)

* Fix issue to upgrade curator-recipes and related API.
* Resolve test failure: ClassNotFoundException of ZooKeeperAdmin.
* Remove unused property in pom.
This commit is contained in:
pandaapo 2023-01-11 09:51:28 +08:00 committed by LearningGp
parent 44080bc886
commit 20a6a63e0c
2 changed files with 19 additions and 42 deletions

View File

@ -13,9 +13,7 @@
<packaging>jar</packaging> <packaging>jar</packaging>
<properties> <properties>
<zookeeper.version>3.4.14</zookeeper.version> <curator.version>5.1.0</curator.version>
<curator.version>4.0.1</curator.version>
<curator.test.version>2.12.0</curator.test.version>
</properties> </properties>
<dependencies> <dependencies>
@ -24,21 +22,10 @@
<artifactId>sentinel-datasource-extension</artifactId> <artifactId>sentinel-datasource-extension</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>${zookeeper.version}</version>
</dependency>
<dependency> <dependency>
<groupId>org.apache.curator</groupId> <groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId> <artifactId>curator-recipes</artifactId>
<version>${curator.version}</version> <version>${curator.version}</version>
<exclusions>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency> </dependency>
<dependency> <dependency>
@ -54,14 +41,8 @@
<dependency> <dependency>
<groupId>org.apache.curator</groupId> <groupId>org.apache.curator</groupId>
<artifactId>curator-test</artifactId> <artifactId>curator-test</artifactId>
<version>${curator.test.version}</version> <version>${curator.version}</version>
<scope>test</scope> <scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.alibaba</groupId> <groupId>com.alibaba</groupId>

View File

@ -9,8 +9,8 @@ import org.apache.curator.framework.AuthInfo;
import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory; import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.cache.ChildData; import org.apache.curator.framework.recipes.cache.ChildData;
import org.apache.curator.framework.recipes.cache.NodeCache; import org.apache.curator.framework.recipes.cache.CuratorCache;
import org.apache.curator.framework.recipes.cache.NodeCacheListener; import org.apache.curator.framework.recipes.cache.CuratorCacheListener;
import org.apache.curator.retry.ExponentialBackoffRetry; import org.apache.curator.retry.ExponentialBackoffRetry;
import java.util.Arrays; import java.util.Arrays;
@ -40,11 +40,11 @@ public class ZookeeperDataSource<T> extends AbstractDataSource<String, T> {
new ArrayBlockingQueue<Runnable>(1), new NamedThreadFactory("sentinel-zookeeper-ds-update", true), new ArrayBlockingQueue<Runnable>(1), new NamedThreadFactory("sentinel-zookeeper-ds-update", true),
new ThreadPoolExecutor.DiscardOldestPolicy()); new ThreadPoolExecutor.DiscardOldestPolicy());
private NodeCacheListener listener; private CuratorCacheListener listener;
private final String path; private final String path;
private CuratorFramework zkClient = null; private CuratorFramework zkClient = null;
private NodeCache nodeCache = null; private CuratorCache nodeCache = null;
public ZookeeperDataSource(final String serverAddr, final String path, Converter<String, T> parser) { public ZookeeperDataSource(final String serverAddr, final String path, Converter<String, T> parser) {
super(parser); super(parser);
@ -104,21 +104,17 @@ public class ZookeeperDataSource<T> extends AbstractDataSource<String, T> {
private void initZookeeperListener(final String serverAddr, final List<AuthInfo> authInfos) { private void initZookeeperListener(final String serverAddr, final List<AuthInfo> authInfos) {
try { try {
this.listener = new NodeCacheListener() { this.listener = CuratorCacheListener.builder().forNodeCache(() -> {
@Override try {
public void nodeChanged() { T newValue = loadConfig();
RecordLog.info("[ZookeeperDataSource] New property value received for ({}, {}): {}",
try {
T newValue = loadConfig();
RecordLog.info("[ZookeeperDataSource] New property value received for ({}, {}): {}",
serverAddr, path, newValue); serverAddr, path, newValue);
// Update the new value to the property. // Update the new value to the property.
getProperty().updateValue(newValue); getProperty().updateValue(newValue);
} catch (Exception ex) { } catch (Exception ex) {
RecordLog.warn("[ZookeeperDataSource] loadConfig exception", ex); RecordLog.warn("[ZookeeperDataSource] loadConfig exception", ex);
}
} }
}; }).build();
String zkKey = getZkKey(serverAddr, authInfos); String zkKey = getZkKey(serverAddr, authInfos);
if (zkClientMap.containsKey(zkKey)) { if (zkClientMap.containsKey(zkKey)) {
@ -148,8 +144,8 @@ public class ZookeeperDataSource<T> extends AbstractDataSource<String, T> {
} }
} }
this.nodeCache = new NodeCache(this.zkClient, this.path); this.nodeCache = CuratorCache.build(this.zkClient, this.path);
this.nodeCache.getListenable().addListener(this.listener, this.pool); this.nodeCache.listenable().addListener(this.listener, this.pool);
this.nodeCache.start(); this.nodeCache.start();
} catch (Exception e) { } catch (Exception e) {
RecordLog.warn("[ZookeeperDataSource] Error occurred when initializing Zookeeper data source", e); RecordLog.warn("[ZookeeperDataSource] Error occurred when initializing Zookeeper data source", e);
@ -163,7 +159,7 @@ public class ZookeeperDataSource<T> extends AbstractDataSource<String, T> {
throw new IllegalStateException("Zookeeper has not been initialized or error occurred"); throw new IllegalStateException("Zookeeper has not been initialized or error occurred");
} }
String configInfo = null; String configInfo = null;
ChildData childData = nodeCache.getCurrentData(); ChildData childData = nodeCache.get(path).orElse(null);
if (null != childData && childData.getData() != null) { if (null != childData && childData.getData() != null) {
configInfo = new String(childData.getData()); configInfo = new String(childData.getData());
@ -174,7 +170,7 @@ public class ZookeeperDataSource<T> extends AbstractDataSource<String, T> {
@Override @Override
public void close() throws Exception { public void close() throws Exception {
if (this.nodeCache != null) { if (this.nodeCache != null) {
this.nodeCache.getListenable().removeListener(listener); this.nodeCache.listenable().removeListener(listener);
this.nodeCache.close(); this.nodeCache.close();
} }
if (this.zkClient != null) { if (this.zkClient != null) {