zookeeper集群的的搭建点击打开链接

创建Maven项目


pom.xml   添加依赖:

<dependency>
    <groupId>org.apache.zookeeper</groupId>
    <artifactId>zookeeper</artifactId>
    <version>3.4.9</version>
</dependency>


连接zk创建节点

	@Test
	public void createZNode() throws Exception{
	
		String conn = "master:2181,slave1:2181,slave2:2181";
		//warcher shi guan cha zhe dui xiang ,yong yu hui diao
		ZooKeeper zk = new ZooKeeper(conn , 50000, new Watcher() {
			public void process(WatchedEvent event) {
				System.out.println("over : " + event);
			}
		});
		
		//zhuang tai dui xiang,zai getData shi hou, jie shou jie dian de zhuang tai xin xi
		ACL acl = new ACL(Perms.ALL,ZooDefs.Ids.ANYONE_ID_UNSAFE);
		List<ACL> acls = new ArrayList<ACL>();
		acls.add(acl);
		zk.create("/root", "helloworld".getBytes(), acls, CreateMode.PERSISTENT);
		System.out.println("over");
	}



修改(更新)ZNode

@Test
	public void updateZNode() throws Exception{
	
		String conn = "master:2181,slave1:2181,slave2:2181";
		//warcher shi guan cha zhe dui xiang ,yong yu hui diao
		ZooKeeper zk = new ZooKeeper(conn , 50000, new Watcher() {
			public void process(WatchedEvent event) {
				System.out.println("over : " + event);
			}
		});
		
		zk.setData("/root", "how are you?".getBytes(), 0);
		System.out.println("over");
	}


读取数据

	@Test
	public void getZNode() throws Exception{
	
		String conn = "master:2181,slave1:2181,slave2:2181";
		
		ZooKeeper zk = new ZooKeeper(conn , 50000, new Watcher() {
			public void process(WatchedEvent event) {
				System.out.println("over : " + event);
			}
		});
		
		
		Stat state = new Stat();
		byte[] bytes = zk.getData("/root", null, state);
		System.out.println(new String(bytes));
	}



创建临时节点

	@Test
	public void createEmpheralZNode() throws Exception{
	
		String conn = "master:2181,slave1:2181,slave2:2181";
		
		ZooKeeper zk = new ZooKeeper(conn , 50000, new Watcher() {
			public void process(WatchedEvent event) {
				System.out.println("over : " + event);
			}
		});
		
		System.out.println("over");
		// chuang jian lu jing 
		ACL acl = new ACL(Perms.ALL,ZooDefs.Ids.ANYONE_ID_UNSAFE);
		List<ACL> acls = new ArrayList<ACL>();
		acls.add(acl);
		zk.create("/root/mynode", "helloworld".getBytes(), acls, CreateMode.EPHEMERAL);
		System.out.println("over");
		
	}






创建一个顺序性的永久性节点

@Test
	public void createSequenceZNode() throws Exception{
	
		String conn = "master:2181,slave1:2181,slave2:2181";
		
		ZooKeeper zk = new ZooKeeper(conn , 50000, new Watcher() {
			public void process(WatchedEvent event) {
				System.out.println("over : " + event);
			}
		});
		
		System.out.println("over");
		// chuang jian lu jing 
		ACL acl = new ACL(Perms.ALL,ZooDefs.Ids.ANYONE_ID_UNSAFE);
		List<ACL> acls = new ArrayList<ACL>();
		acls.add(acl);
		zk.create("/root/mynode", "helloworld".getBytes(), acls, CreateMode.PERSISTENT_SEQUENTIAL);
		System.out.println("over");
		
	}


得到根下(孩子)子节点

@Test
	public void getChildren() throws Exception{
	
		String conn = "master:2181,slave1:2181,slave2:2181";
		
		ZooKeeper zk = new ZooKeeper(conn , 50000, new Watcher() {
			public void process(WatchedEvent event) {
				System.out.println("over : " + event);
			}
		});
		
		
		List<String> list = zk.getChildren("/", null);
		for(String s : list){
			System.out.println(s);
		}
	}

删除节点

@Test
	public void deleteZNode() throws Exception{
	
		String conn = "master:2181,slave1:2181,slave2:2181";
		
		ZooKeeper zk = new ZooKeeper(conn , 50000, new Watcher() {
			public void process(WatchedEvent event) {
				System.out.println("over : " + event);
			}
		});
		
		zk.delete("/root/mynode0000000002", 0);
	}


全部代码:

import java.util.ArrayList;
import java.util.List;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooDefs.Perms;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.ACL;
import org.apache.zookeeper.data.Stat;
import org.junit.Test;

public class TestZookeeper {
	
//连接zk创建节点
	@Test
	public void createZNode() throws Exception{
	
		String conn = "master:2181,slave1:2181,slave2:2181";
		//warcher shi guan cha zhe dui xiang ,yong yu hui diao
		ZooKeeper zk = new ZooKeeper(conn , 50000, new Watcher() {
			public void process(WatchedEvent event) {
				System.out.println("over : " + event);
			}
		});
		
		//zhuang tai dui xiang,zai getData shi hou, jie shou jie dian de zhuang tai xin xi
		ACL acl = new ACL(Perms.ALL,ZooDefs.Ids.ANYONE_ID_UNSAFE);
		List<ACL> acls = new ArrayList<ACL>();
		acls.add(acl);
		zk.create("/root", "helloworld".getBytes(), acls, CreateMode.PERSISTENT);
		System.out.println("over");
	}
	
	
//修改(更新)ZNode
	
	@Test
	public void updateZNode() throws Exception{
	
		String conn = "master:2181,slave1:2181,slave2:2181";
		//warcher shi guan cha zhe dui xiang ,yong yu hui diao
		ZooKeeper zk = new ZooKeeper(conn , 50000, new Watcher() {
			public void process(WatchedEvent event) {
				System.out.println("over : " + event);
			}
		});
		
		zk.setData("/root", "how are you?".getBytes(), 0);
		System.out.println("over");
	}
	
	
//读取数据
	@Test
	public void getZNode() throws Exception{
	
		String conn = "master:2181,slave1:2181,slave2:2181";
		
		ZooKeeper zk = new ZooKeeper(conn , 50000, new Watcher() {
			public void process(WatchedEvent event) {
				System.out.println("over : " + event);
			}
		});
		
		
		Stat state = new Stat();
		byte[] bytes = zk.getData("/root", null, state);
		System.out.println(new String(bytes));
	}
	
	
//创建临时节点
	@Test
	public void createEmpheralZNode() throws Exception{
	
		String conn = "master:2181,slave1:2181,slave2:2181";
		
		ZooKeeper zk = new ZooKeeper(conn , 50000, new Watcher() {
			public void process(WatchedEvent event) {
				System.out.println("over : " + event);
			}
		});
		
		System.out.println("over");
		// chuang jian lu jing 
		ACL acl = new ACL(Perms.ALL,ZooDefs.Ids.ANYONE_ID_UNSAFE);
		List<ACL> acls = new ArrayList<ACL>();
		acls.add(acl);
		zk.create("/root/mynode", "helloworld".getBytes(), acls, CreateMode.EPHEMERAL);
		System.out.println("over");
		
	}
	
//创建一个顺序话的永久性节点	
	@Test
	public void createSequenceZNode() throws Exception{
	
		String conn = "master:2181,slave1:2181,slave2:2181";
		
		ZooKeeper zk = new ZooKeeper(conn , 50000, new Watcher() {
			public void process(WatchedEvent event) {
				System.out.println("over : " + event);
			}
		});
		
		System.out.println("over");
		// chuang jian lu jing 
		ACL acl = new ACL(Perms.ALL,ZooDefs.Ids.ANYONE_ID_UNSAFE);
		List<ACL> acls = new ArrayList<ACL>();
		acls.add(acl);
		zk.create("/root/mynode", "helloworld".getBytes(), acls, CreateMode.PERSISTENT_SEQUENTIAL);
		System.out.println("over");
		
	}	
	
	
//得到根下(孩子)子节点
	@Test
	public void getChildren() throws Exception{
	
		String conn = "master:2181,slave1:2181,slave2:2181";
		
		ZooKeeper zk = new ZooKeeper(conn , 50000, new Watcher() {
			public void process(WatchedEvent event) {
				System.out.println("over : " + event);
			}
		});
		
		
		List<String> list = zk.getChildren("/", null);
		for(String s : list){
			System.out.println(s);
		}
	}	
	
	
//删除节点
	
	@Test
	public void deleteZNode() throws Exception{
	
		String conn = "master:2181,slave1:2181,slave2:2181";
		
		ZooKeeper zk = new ZooKeeper(conn , 50000, new Watcher() {
			public void process(WatchedEvent event) {
				System.out.println("over : " + event);
			}
		});
		
		zk.delete("/root/mynode0000000002", 0);
	}
	
}




Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐