当前位置: 代码迷 >> 综合 >> 2022-05-21 zookeeper使用
  详细解决方案

2022-05-21 zookeeper使用

热度:91   发布时间:2023-09-11 02:26:55.0

采用docker 安装的zookeeper,比较方便端口为默认端口2181;springboot集成zk
1 创建配置类, 具体配置从配置文件yml中获取

/*** @Descriptin: 配置* @ClassName: ZookeeperConfig*/import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.zookeeper.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import java.util.concurrent.CountDownLatch;@Data
@Configuration
@Slf4j
public class ZookeeperConfig {/*** zookeeper 地址*/@Value("${zookeeper.address}")private String connect;/*** connect 超时时间*/@Value("${zookeeper.timeout}")private int timeout;/*** zookeeper client** @return*/@Bean(name = "zkClient")public ZooKeeper zkClient() {ZooKeeper zooKeeper = null;try {final CountDownLatch countDownLatch = new CountDownLatch(1);//连接成功后,会回调watcher监听,此连接操作是异步的,执行完new语句后,直接调用后续代码// 可指定多台服务地址 127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183zooKeeper = new ZooKeeper(connect, timeout, new Watcher() {@Overridepublic void process(WatchedEvent event) {if (Event.KeeperState.SyncConnected == event.getState()) {//如果收到了服务端的响应事件,连接成功countDownLatch.countDown();}}});countDownLatch.await();log.info("【初始化ZooKeeper连接状态....】={}", zooKeeper.getState());} catch (Exception e) {log.error("初始化ZooKeeper连接异常....】={}", e);}return zooKeeper;}
}

2 使用

import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import java.util.List;@SpringBootTest
class ZkServiceTest {@AutowiredZooKeeper zkClient;@Testvoid exists() {//zkService.exists()}@Testvoid testExists() throws KeeperException, InterruptedException {System.out.println(zkClient.exists("/ht_test_one", true));System.out.println(zkClient.exists("/ht_test_one", new Watcher() {@Overridepublic void process(WatchedEvent event) {System.out.println(event.getPath());}}));}@Testvoid createNode() throws KeeperException, InterruptedException {zkClient.create("/ht_test_one/test_two", "0521test".getBytes(),ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);}@Testvoid updateNode() throws KeeperException, InterruptedException {zkClient.setData("/ht_test_one/test_two", "test111".getBytes(),-1);}@Testvoid deleteNode() throws KeeperException, InterruptedException {zkClient.delete("/ht_test_one",-1);}@Testvoid getChildren() throws KeeperException, InterruptedException {String path = "/ht_test_one";List<String> children = zkClient.getChildren(path,false);for (String child : children) {byte[] data = zkClient.getData(path + "/" + child, new Watcher() {@Overridepublic void process(WatchedEvent event) {System.out.println(event.getType());}},new Stat());System.out.println(new String(data));}}@Testvoid getData() throws KeeperException, InterruptedException {byte[] data = zkClient.getData("/ht_test_one/test_two", new Watcher() {@Overridepublic void process(WatchedEvent event) {}}, new Stat());System.out.println(new String(data));}
}

注意的是: 删除节点的时候,如果有子节点不能直接删除!
game over ;
具体代码在test中

  相关解决方案