Maven

xiaoxiao2021-02-27  195

Linux_Zookeeper 安装笔记

CuratorUtil.java

package com.demo.util; import java.util.List; import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFrameworkFactory; import org.apache.curator.retry.RetryNTimes; import org.apache.zookeeper.data.Stat; public class CuratorUtil { private static final String ZK_ADDRESS = "192.168.216.138:2181,192.168.216.139:2181,192.168.216.140:2181"; private static CuratorFramework client; static { client = CuratorFrameworkFactory.newClient(ZK_ADDRESS, new RetryNTimes(10, 5000)); client.start(); } public static String create(String zkPath, String data) throws Exception { print("create", zkPath, data); return client.create().creatingParentsIfNeeded().forPath(zkPath, data.getBytes()); } public static List<String> getChildren(String zkPath) throws Exception { print("ls", zkPath); List<String> children = client.getChildren().forPath(zkPath); print(children); return children; } public static String getData(String zkPath) throws Exception { print("get", zkPath); return new String(client.getData().forPath(zkPath)); } public static Stat setData(String zkPath, String data) throws Exception { print("set", zkPath, data); return client.setData().forPath(zkPath, data.getBytes()); } public static Void delNode(String zkPath) throws Exception { print("delete", zkPath); return client.delete().forPath(zkPath); } private static void print(String... cmds) { StringBuilder text = new StringBuilder("$ "); for (String cmd : cmds) { text.append(cmd).append(" "); } System.out.println(text.toString()); } private static void print(Object result) { System.out.println(result instanceof byte[] ? new String((byte[]) result) : result); } }

CuratorTest.java

package com.zookeeper.test; import org.junit.Test; import com.demo.util.CuratorUtil; public class CuratorTest { private static final String ZK_ROOT = "/"; private static final String ZK_PATH = "/demo"; @Test public void create() throws Exception { // 清掉ZK_PATH节点 CuratorUtil.delNode(ZK_PATH); // 查询ZK_ROOT子节点 CuratorUtil.getChildren(ZK_ROOT); // 创建ZK_PATH节点 CuratorUtil.create(ZK_PATH, "zhangsan"); // 查询ZK_ROOT子节点 CuratorUtil.getChildren(ZK_ROOT); // 查询ZK_PATH节点值 CuratorUtil.getData(ZK_PATH); // 修改ZK_PATH节点值 CuratorUtil.setData(ZK_PATH, "lisi"); // 查询ZK_PATH节点值 CuratorUtil.getData(ZK_PATH); // 删除ZK_PATH节点 CuratorUtil.delNode(ZK_PATH); // 查询ZK_ROOT子节点 CuratorUtil.getChildren(ZK_ROOT); } }

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.demo</groupId> <artifactId>maven-zookeeper</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-recipes</artifactId> <version>2.12.0</version> </dependency> </dependencies> </project>

maven-zookeeper 实例源码

转载请注明原文地址: https://www.6miu.com/read-11379.html

最新回复(0)