Java Code Examples for org.I0Itec.zkclient.ZkClient#create()

The following examples show how to use org.I0Itec.zkclient.ZkClient#create() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SafeZclient.java    From javabase with Apache License 2.0 6 votes vote down vote up
private static void client() {
	try {
		ZkClient zc = new ZkClient("localhost:2181");
		String userTwo = "gao:gao";
		zc.addAuthInfo("digest", userTwo.getBytes());

           //使用admin就可以创建
		// String userOne = "admin:admin";
		// zc.addAuthInfo("digest",userOne.getBytes());
		
		String data = zc.readData("/safe");
		log.info("只读用户 " + userTwo + "读取safe节点返回数据:" + data);
		
		zc.create("/safe2", "test", CreateMode.EPHEMERAL);
		log.info("只读用户创建节点safe2成功:" + zc.readData("/safe2"));
		
		zc.create("/safe/test", "zookeeper加密是针对节点加密的", CreateMode.EPHEMERAL);
		log.info("只读用户创建节点失败:" + zc.readData("/safe/test"));
		
	} catch (Exception e) {
		e.printStackTrace();
	}
	
}
 
Example 2
Source File: ZclientServerPub.java    From javabase with Apache License 2.0 6 votes vote down vote up
private static void test() throws InterruptedException {
	final ZkClient zkClient4subChild = new ZkClient("localhost:2181");
	
	zkClient4subChild.create("/serverroot", "serverroot", Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
	
	Thread.sleep(4000);
	zkClient4subChild.create("/serverroot/server1", "server1", Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
	
	Thread.sleep(4000);
	zkClient4subChild.create("/serverroot/server2", "server2", Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
	
	Thread.sleep(20000);
	zkClient4subChild.delete("/serverroot/server1");
	zkClient4subChild.delete("/serverroot/server2");
	zkClient4subChild.delete("/serverroot");
}
 
Example 3
Source File: ServerManagerMonitor.java    From javabase with Apache License 2.0 6 votes vote down vote up
private static void monitor() {
    ZkClient zc=new ZkClient("localhost:2181",1000);
    //创建监控节点
    if(!zc.exists("/monitor"))
    zc.create("/monitor",null, CreateMode.PERSISTENT);

    if(!zc.exists("/monitor/client"))
    zc.create("/monitor/client",null, CreateMode.PERSISTENT);

    zc.subscribeChildChanges("/monitor/client",new IZkChildListener(){
        @Override
        public void handleChildChange(String parentPath, List<String> currentChilds) throws Exception {
            System.out.println("------------客户端发生变化---------childPath="+parentPath );
            currentChilds.forEach((String childPath)->{
                System.out.println("parentPath = [" + parentPath + "], currentChilds = [" + currentChilds + "]");
            });
        }
    });



}
 
Example 4
Source File: ZkRegisterCenter.java    From krpc with MIT License 5 votes vote down vote up
public static void register() {
    try {

        ZookeeperInfo zookeeperInfo = Global.getInstance().getZookeeperInfo();
        zc = new ZkClient(zookeeperInfo.getAddr(), zookeeperInfo.getSessionTimeOut(), zookeeperInfo.getConnectionTimeOut());

        StringBuffer stringBuffer = new StringBuffer("/krpc");

        if (!zc.exists(stringBuffer.toString())) {
            zc.create(stringBuffer.toString(), "", CreateMode.PERSISTENT);

            log.info("创建根节点krpc");
        }
        stringBuffer.append("/").append(Global.getInstance().getServiceName());

        if (!zc.exists(stringBuffer.toString())) {
            zc.create(stringBuffer.toString(), "", CreateMode.PERSISTENT);
            log.info("创建{}服务节点", Global.getInstance().getServiceName());
        }

        stringBuffer.append("/").append(Global.getInstance().getIp()).append(":").append(Global.getInstance().getPort());

        zc.create(stringBuffer.toString(), Global.getInstance().getServiceName(), CreateMode.EPHEMERAL);
    } catch (Exception e) {
        log.error("register error!", e);
    }
}
 
Example 5
Source File: EmbeddedZookeeperTest.java    From li-apache-kafka-clients with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Test
public void testSimpleScenario() throws Exception {
  String connectionString;
  String host;
  int port;
  try (EmbeddedZookeeper zk = new EmbeddedZookeeper()) {
    connectionString = zk.getConnectionString();
    host = zk.getHostAddress();
    port = zk.getPort();
    Assert.assertEquals(host + ":" + port, connectionString);
    ZkClient client = new ZkClient(connectionString);
    try {
      String path = "/" + UUID.randomUUID().toString();
      client.waitUntilConnected(5, TimeUnit.SECONDS);
      client.create(path, "payload", CreateMode.PERSISTENT);
      Assert.assertEquals("payload", client.readData(path));
    } finally {
      client.close();
    }
  }
  //now verify shut down
  try {
    new Socket(host, port);
    Assert.fail("expected to fail");
  } catch (ConnectException ignored) {

  }
}
 
Example 6
Source File: SafeZclient.java    From javabase with Apache License 2.0 5 votes vote down vote up
private static void ls() {
	for (int i = 0; i <20 ; i++) {
		ZkClient zc = new ZkClient("127.0.0.1:2181");
		if(!zc.exists("/tt"))
		zc.create("/tt", "test", CreateMode.EPHEMERAL);
		log.info(zc.readData("/tt"));
	}
}
 
Example 7
Source File: SafeZclient.java    From javabase with Apache License 2.0 5 votes vote down vote up
/**
 * server端对safe接点加密
 */
private static void server() {
	try {
		ZkClient zc = new ZkClient("localhost:2181");
		List<ACL> acls = getACL(zc);
		if (zc.exists("/safe"))
			zc.delete("/safe");
		zc.create("/safe", "safe", acls, CreateMode.PERSISTENT);
		
	} catch (Exception e) {
		e.printStackTrace();
	}
}
 
Example 8
Source File: ZclientNormal.java    From javabase with Apache License 2.0 5 votes vote down vote up
/**
 * @Description: zkClient主要做了两件事情。
 * @see:一件是在session loss和session expire时自动创建新的ZooKeeper实例进行重连。
 * @see:一件是将一次性watcher包装为持久watcher。
 * @see:后者的具体做法是简单的在watcher回调中,重新读取数据的同时再注册相同的watcher实例。
 */
private static void test() {
	final ZkClient zc = new ZkClient("123.56.118.135:2181");
	// 创建根节点
	/* Persistent与Ephemeral 就是持久化保存到本地和不持久化的区别 ,不能再临时节点下面创建子节点 */
	
	zc.createPersistent("/testroot");
	// zc.createEphemeral("/testroot");
	// 创建子节点
	zc.create("/testroot/node1", "node1", CreateMode.EPHEMERAL);
	zc.createPersistent("/testroot/node2");
	zc.create("/testroot/node2/test", "node1", CreateMode.EPHEMERAL);
	List<String> children = zc.getChildren("/");
	log.info("根节点下面的字节点个数" + children.size());
	
	// 获得子节点个数
	
	int chidrenNumbers = zc.countChildren("/testroot");
	log.info("子节点个数" + chidrenNumbers);
	
	zc.writeData("/testroot/node2/test", "给节点写数据");
	
	// 删除节点
	zc.delete("/testroot/node2/test");
	zc.delete("/testroot/node2");
	zc.delete("/testroot/node1");
	zc.delete("/testroot");
}
 
Example 9
Source File: ClientB.java    From javabase with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    ZkClient zc=new ZkClient("localhost:2181",1000);
    //创建监控节点
    zc.create("/monitor/client/clientB","clientB", CreateMode.EPHEMERAL);
    try {
        System.in.read();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 10
Source File: ClientA.java    From javabase with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {

        ZkClient zc=new ZkClient("localhost:2181",1000);
        //创建监控节点
        zc.create("/monitor/client/clientA","clientA", CreateMode.EPHEMERAL);
        try {
            System.in.read();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
Example 11
Source File: ZkUtil.java    From java-study with Apache License 2.0 2 votes vote down vote up
/**
 * 创建目录并添加参数
 * @param zk
 * @param path
 * @param data
 * @return
 */
public static void create(ZkClient zk,String path,String data) throws ZkInterruptedException, IllegalArgumentException, ZkException, RuntimeException{
	if(!zk.exists(path)){
		zk.create(path,data,CreateMode.PERSISTENT);
	}
}