第三章 Getting Started with the Zookeeper API(开始学习Zookeeper API)【上】
来源:互联网 发布:克里斯韦伯数据 编辑:程序博客网 时间:2024/06/05 15:58
概述:
1、设置Zookeeper ClassPath
为方便命令行编译,我们都知道编译过程需要依赖于第三方jar,这里就是依赖于Zookeeper的jar,一般我们都是依赖IDE进行编译了(常用例如eclipse),今天也尝试一下通过命令行编译java。问题来了,我们怎么知道Zookeeper依赖那些jar,你是否记得bin目录下有zkEnv.cmd文件(window系统下用),这个就是启动客户端和服务端的设置一些变量,当然包括必须要的jar包。可以打开看一下:
可以试一下:(window系统不区分大小写)
2、创建一个Zookeeper会话
构造函数
Zookeeper(String connectString, int sessionTimeout, Watcher watcher)
注释:
- connectString:所有主机名和端口组成字符串
- sessionTimeout: 会话超时时间(单位毫秒)在这段时间内没有与服务器交互,服务器会自动关闭会话。
- watcher: 就是相当于信使,将服务器端的消息状态传递给客户端,Watcher是接口需要具体实现
3、实现一个Watcher(监听器)
public interface Watcher { void process(WatchedEvent event);}只有一个未实现的方法
import java.io.IOException;import org.apache.zookeeper.WatchedEvent;import org.apache.zookeeper.Watcher;import org.apache.zookeeper.ZooKeeper;public class Master implements Watcher{ZooKeeper zk;String hostPort;Master(String hostPort){this.hostPort = hostPort;}void startZK() throws IOException{zk = new ZooKeeper(hostPort, 15000, this);}public void process(WatchedEvent e) {System.out.println(e);}public static void main(String[] args) throws Exception {Master m = new Master(args[0]);m.startZK();Thread.sleep(60000);}}总结:
- 1、先实例化一个Watcher,再实例化ZooKeeper,因为Zookeeper实例化之后需要回调Watcher。
- 2、线程休眠60秒,为可以看很多打印信息,注意只要完成连接,这个线程就是守护线程,也就程序退了,线程还存在。
方法一:
步骤1:首先启动Zookeeper服务器, zkServer.cmd (也就是运行Zookeeper的bin目录的zkServer.cmd文件)
步骤2:通过命令行来编译以上代码,为方便将代码放置在bin目录下
如果一下情况,说明你没有配置日志文件,将log4j.properties复制到配置文件中。
log4j:WARN No appenders could be found for logger(org.apache.zookeeper.ZooKeeper).log4j:WARN Please initialize the log4j system properly.方法二:采用IDE工具(Eclipse)
这里采用的是Maven工程,需要手动引用jar包,当然还有日志文件
<dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.10</version></dependency>
<dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version></dependency>代码还是不变,可能
然后选择Master右键Run as....配置一下参数就好
总结:如果连接失败之后,不要重新创建新会话,Zookeeper起来之后会自动检测断开连接会话,重试重连。
阅读全文
0 0
- 第三章 Getting Started with the Zookeeper API(开始学习Zookeeper API)【上】
- 《ZooKeeper》 Chapter 3 Getting Started with the ZooKeeper API
- 第二章 Getting to Grips with ZooKeeper(开始掌握Zookeeper)【上】
- JAVA 3D原创翻译 Getting Started with the Java 3D API。- 第一章 1.3 (上)
- getting started with zookeeper and first zookeeper session
- ZooKeeper Getting Started Guide
- ZooKeeper Getting Started Guide
- Getting Started with Windows Live Contacts API
- Getting Started with Lync 2010 Model API
- 翻译- O'Reilly ZooKeeper-第三章 ZooKeeper API入门
- 《Getting Started with D3》中文翻译(第三章)
- Getting Started With the Java Rule Engine API (JSR 94): Toward Rule-Based Applications
- JAVA 3D原创翻译 Getting Started with the Java 3D API。- 第一章 1.1 1.2
- ZooKeeper API
- ZooKeeper API
- zookeeper api
- zookeeper API
- ZooKeeper API
- 常见编程题——回文串
- HDU2044 一只小蜜蜂... 动态规划入门-递推
- ionic 无限轮播与点击跳转
- LeetCode 238. Product of Array Except Self
- BZOJ1036
- 第三章 Getting Started with the Zookeeper API(开始学习Zookeeper API)【上】
- 欢迎使用CSDN-markdown编辑器
- 题目125-盗梦空间
- Spring MVC分析,知识重构(1)
- 64位windows系统下对应python3.5安装matplotlib模块
- TypeScript和es5的JavaScript的语法比较
- 1015 最大公约数和最小公倍数
- 通过nginx配置文件抵御攻击,防御CC攻击的经典思路!
- 生产web应用java内存及回收配置