Giter Site home page Giter Site logo

rockefellor / zkclient Goto Github PK

View Code? Open in Web Editor NEW

This project forked from xiajunsongfan/zkclient

0.0 0.0 0.0 50 KB

zookeeper java客户端的封装,使用者不用再关心断线重连、watch监听、session过期等问题,并使用zookeeper 实现了分布式锁

License: Apache License 2.0

Java 100.00%

zkclient's Introduction

zkclient 项目

项目介绍:

zkclient 是对zookeeper java客户端进行的封装,主要实现了断线重连,session过期重注册,watch事件改为listen监听事件,分布式锁等

注意: 使用时需要自行编译安装到maven或打成jar使用

使用方式:

	<dependency>
		<artifactId>zk-client</artifactId>
    	<groupId>com.xj.zk</groupId>
    	<version>1.0.0-SNAPSHOT</version>
	</dependency>
	//1. 创建zkclient实例
	//参数:zookeeper地址,session超时时间(毫秒),连接超时时间(毫秒)
	ZkClient zk = new ZkClient("127.0.0.1:2181", 5000, 3000);

	//2. 创建一个临时节点(第3个参数为true),当session过期所有临时节点被删除时,这种类型的节点会在session重连后自动重新创建
	zk.create("/zk/test/ex", "{name:12}".getBytes(), true);

	//3. 监听器的使用,注册监听后只要监听的对象发生变化都会接收到事件回调(断线重连和session过期都不再需要进行重新注册),
	//不会像原生的watch那样只有一次触发。
	//3.1. 监听本节点数据变化,参数:节点路径,监听器对象
	zk.listenData("/zk/test/1", new Listener() {
		//发生变化节点的绝对路径,事件类型,变化后的数据
    	public void listen(String path, Watcher.Event.EventType eventType, byte[] data) throws ZkException, SocketException {
    		String msg = new String(data);
		}
    });
    //3.2. 监听节点下的孩子子节点变化,注意这个时候listen方法中data是没有数据的
    zk.listenChild("/zk/test", new Listener() {
    	public void listen(String path, Watcher.Event.EventType eventType, byte[] data) throws ZkException, SocketException {
                System.out.println(path + " " + eventType.name());
		}
	});
	//3.3. 监听节点下子节点的数据变化, 参数:父节点路径,监听器对象
	//   listen方法中的eventType 有2种类型 NodeDeleted节点被删除 和 NodeDataChanged节点数据发生变化
	zk.listenChildData("/zk/test", new Listener() {
		public void listen(String path, Watcher.Event.EventType eventType, byte[] data) throws ZkException, SocketException {
		System.out.println(path + " " + eventType.name() + " " + new String(data));
		}
	});
	//4. 分布式锁的使用
	public void lock() {
		SimpleLock lock = zk.getLock("/zk/lock");//创建锁对象
		try {
			if (lock.lock()) {//获得锁
				//处理业务
			}
		}finally {
			lock.unlock();//释放锁
		}
		//不再使用时要销毁这个锁
        lock.destroy();
	}
	//5.  其它API的使用基本和zookeeper原生的一样

zkclient's People

Contributors

xiajunsongfan avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.