温习zookeeper3.6.2的curator-client5.1.0

该博客介绍了如何使用Curator库在Java中实现对Zookeeper节点的创建、删除、更新和查询操作,以及设置节点监听器的方法。通过配置pom.xml文件引入相关依赖,并展示了创建客户端的两种方式,以及监听节点变动的代码示例。
摘要由CSDN通过智能技术生成

实现了zookeeper上节点的增,删,改,查和监听功能。

1.pom.xml配置:

<!-- 关于Zookeeper客户端的封装,这里只引用的recipes和client -->
		<dependency>
			<groupId>org.apache.curator</groupId>
			<artifactId>curator-recipes</artifactId>
			<version>5.1.0</version>
			<exclusions>
				<exclusion>
					<groupId>org.apache.zookeeper</groupId>
					<artifactId>zookeeper</artifactId>
				</exclusion>
			</exclusions>
		</dependency>

		<!-- 关于Zookeeper客户端的封装 -->
		<dependency>
			<groupId>org.apache.curator</groupId>
			<artifactId>curator-client</artifactId>
			<version>5.1.0</version>
		</dependency>

		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-simple</artifactId>
			<version>1.7.25</version>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>
		</plugins>
	</build>

2.监听节点变动

public static void watchNode() throws Exception {
		CuratorFramework client = ZkClient.getClientOne();
		client.start();
		CountDownLatch countDownLatch = new CountDownLatch(1);
		
		Watcher w = new Watcher() {
		    @Override
		    public void process(WatchedEvent watchedEvent) {
		    	EventType eventType = watchedEvent.getType();
		    	
		    	String path = watchedEvent.getPath();
		    	System.out.println(eventType+"--- --- ---"+path);
		    	
		        System.out.println("监听到的变化 watchedEvent = " + watchedEvent);
		        countDownLatch.countDown();
		    }
		};
		
		client.getData().usingWatcher(w).forPath("/demo/curd/node-1");
		countDownLatch.await();
		client.close();
	}

3.两种方式创建客户端

private static final String BROKERS = "10.190.1.38:2181";

	public static void main(String[] args) throws Exception {
		// ZkClient.createNode();
		//ZkClient.asyncCreateNode();
		//ZkClient.updateNode();
		// ZkClient.readNode();
		// ZkClient.deleteNode();
		ZkClient.watchNode();
	}

	private static CuratorFramework getClientOne() {
		ExponentialBackoffRetry retry = new ExponentialBackoffRetry(3000, 3);
		CuratorFramework client = CuratorFrameworkFactory.newClient(ZkClient.BROKERS, retry);
		return client;
	}

	private static CuratorFramework getClientTwo() {
		ExponentialBackoffRetry retry = new ExponentialBackoffRetry(1000, 3);
		CuratorFramework client = CuratorFrameworkFactory.builder().connectString(ZkClient.BROKERS)
				.connectionTimeoutMs(20000).sessionTimeoutMs(20000).retryPolicy(retry).build();
		return client;
	}

其他内容请参考《https://blog.csdn.net/weixin_43072970/article/details/106848786》。

官方例子下载地址: https://www.apache.org/dyn/closer.lua/curator/5.1.0/apache-curator-5.1.0-source-release.zip 。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值