非常多使用Zookeeper的情景是须要我们嵌入Zookeeper作为自己的分布式应用系统的一部分来提供分布式服务。此时我们须要通过程序的方式来启动Zookeeper。此时能够通过Zookeeper API的ZooKeeperServerMain类来启动Zookeeper服务。
 
以下是一个集群模式下启动Zookeeper服务的样例
 
这里假定我们执行Zookeeper集群的三台机器名分别为fanbinx1,fanbinx2,fanbinx3
 

首先是zoo.cfg配置文件

tickTime=2000
dataDir=/tmp/zookeeper/data
clientPort=2181
initLimit=10
syncLimit=5
server.1=fanbinx1:2888:3888
server.2=fanbinx2:2888:3888
server.3=fanbinx3:2888:3888

启动Zookeeper集群服务的类。例如以下 
* 这个类同一时候使用同一个zoo.cfg配置文件来启动Zookeeper服务。

* 在每台机器上启动Zookeeper服务的时候推断当前机器是不是定义在zoo.cfg文件中,假设是获取当中的ID号,然后生成myid文件并将ID写入当中。
* 最后启动Zookeeper服务。

package my.zookeeperstudy.server;

import org.apache.commons.io.FileUtils;
import org.apache.zookeeper.server.ServerConfig;
import org.apache.zookeeper.server.ZooKeeperServerMain;
import org.apache.zookeeper.server.quorum.QuorumPeerConfig; import java.io.File;
import java.io.InputStream;
import java.net.InetAddress;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern; public class ClusteredZKServer { public static void main(String[] args) throws Exception {
InputStream is = ClusteredZKServer.class.getResourceAsStream("/my/zookeeperstudy/server/zoo.cfg");
Properties props = new Properties();
try {
props.load(is);
} finally {
is.close();
} for (String key : props.stringPropertyNames()) {
Pattern pKey = Pattern.compile("^server\\.(\\d)");
Pattern pValue = Pattern.compile("([\\w|.]*):\\d*:\\d*");
Matcher mKey = pKey.matcher(key);
Matcher mValue = pValue.matcher(props.getProperty(key));
if (mKey.find() && mValue.find()) {
String id = mKey.group(1);
String host = mValue.group(1);
String thisHostName = InetAddress.getLocalHost().getHostName();
String thisHostAddress = InetAddress.getLocalHost().getHostAddress();
if (host.equals(thisHostName) || host.equals(thisHostAddress)) {
//System.out.println(new File(props.getProperty("dataDir"), "myid").getAbsolutePath());
FileUtils.write(new File(props.getProperty("dataDir"), "myid"), id);
QuorumPeerConfig quorumConfig = new QuorumPeerConfig();
quorumConfig.parseProperties(props); final ZooKeeperServerMain zkServer = new ZooKeeperServerMain();
final ServerConfig config = new ServerConfig();
config.readFrom(quorumConfig);
zkServer.runFromConfig(config);
}
}
}
}
}

客户端測试代码例如以下,这里能够改动hostname为集群中的随意一台机器

package my.zookeeperstudy.server;

import org.apache.zookeeper.*;

import java.util.List;

public class Client {
public static void main(String[] args) throws Exception {
ZooKeeper zk = new ZooKeeper("fanbinx1:2181,fanbinx2:2181,fanbinx3:2181", 10000,
new Watcher() {
public void process(WatchedEvent event) {
System.out.println("event: " + event.getType());
}
}); System.out.println(zk.getState()); zk.create("/myApps", "myAppsData".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
zk.create("/myApps/App1", "App1Data".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
zk.create("/myApps/App2", "App2Data".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
zk.create("/myApps/App3", "".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
zk.setData("/myApps/App3","App3Data".getBytes(), -1); System.out.println(zk.exists("/myApps", true));
System.out.println(new String(zk.getData("/myApps", true, null))); List<String> children = zk.getChildren("/myApps", true);
for (String child : children) {
System.out.println(new String(zk.getData("/myApps/" + child, true, null)));
zk.delete("/myApps/" + child,-1);
} zk.delete("/myApps",-1); zk.close();
}
}

測试
* 在集群中的各个机器上分别执行ClusteredZKServer类来启动Zookeeper服务。
* 然后在随意一台机器上执行Client类来连接Zookeeper并操作数据。

原文链接:Zookeeper实战之嵌入式执行Zookeeper集群模式

Zookeeper实战之嵌入式执行Zookeeper集群模式的更多相关文章

  1. zookeeper在windows下的伪集群模式

    参考:zookeeper在windows下的伪集群模式 踩到的坑: 注意windows下路径需要使用\ dataDir=D:\Program Files\Java\zookeeper-3.4.10-c ...

  2. Zookeeper 2、Zookeeper的安装和配置(集群模式)

    1.下载与解压 Zookeeper下载地址:http://www.apache.org/dyn/closer.cgi/zookeeper/ 下载完成以后解压到一个特定目录 同步时间所有节点的时间,并关 ...

  3. ZooKeeper(3.4.5) - 配置伪集群模式

    1. 准备 Java 运行环境,需要安装 Java1.6 或更高版本的 JDK. 2. 下载 ZooKeeper 的稳定版本 zookeeper-x.x.x.tar.gz,将其解压,约定目录名称为 % ...

  4. Zookeeper实战之单机集群模式

    前一篇文章介绍了Zookeeper的单机模式的安装及应用,但是Zookeeper是为了解决分布式应用场景的,所以通常都会运行在集群模式下.今天由于手头机器不足,所以今天打算在一台机器上部署三个Zook ...

  5. Zookeeper笔记之使用zk实现集群选主

    一.需求 在主从结构的集群中,我们假设硬件机器是很脆弱的,随时可能会宕机,当master挂掉之后需要从slave中选出一个节点作为新的master,使用zookeeper可以很简单的实现集群选主功能. ...

  6. Zookeeper简介及单机、集群模式搭建

    1.zookeeper简介 一个开源的分布式的,为分布式应用提供协调服务的apache项目. 提供一个简单的原语集合,以便于分布式应用可以在它之上构建更高层次的同步服务. 设计非常易于编程,它使用的是 ...

  7. Zookeeper详解-伪分布式和集群搭建(八)

    说到分布式开发Zookeeper是必须了解和掌握的,分布式消息服务kafka .hbase 到hadoop等分布式大数据处理都会用到Zookeeper,所以在此将Zookeeper作为基础来讲解. Z ...

  8. Windows环境下Zookeeper的安装和部署(单机模式和伪集群模式)

    第一部分:单机模式 1)下载地址:http://www.pirbot.com/mirrors/apache/zookeeper/,建议下载stable版本 2)解压缩 将下载好的压缩包解压到指定目录, ...

  9. zookeeper集群&伪集群模式部署

    1.什么是单机部署 一台服务器上面部署一个单机版本的zookeeper服务,用于提供服务. 2.什么是集群部署? 集群部署就是多台服务器上面各部署单独的一个zookeeper服务,然后组建一个集群 3 ...

随机推荐

  1. 洛谷——P3819 松江1843路

    https://www.luogu.org/problem/show?pid=3819 题目描述 涞坊路是一条长L米的道路,道路上的坐标范围从0到L,路上有N座房子,第i座房子建在坐标为x[i]的地方 ...

  2. Mining Station on the Sea (hdu 2448 SPFA+KM)

    Mining Station on the Sea Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav ...

  3. 使用 Go 语言开发大型 MMORPG 游戏服务器怎么样?(非常稳定、捕获所有异常、非常适合从头开始,但大公司已经有现成的C++框架、所以不会使用)

    使用 Go 语言开发大型 MMORPG 游戏服务器怎么样?和C Socket服务器比起来有什么优劣?可行性怎么样? 从2013年起,经朋友推荐开始用Golang编写游戏登陆服务器, 配合C++做第三方 ...

  4. mmx-编译脚本

    脚本目录位置 /home/zhangshuli/git2/vanzo_team/xulei/Mmx.py 在-/bin目录下,链接Mmx.py ln -sf ~/git2/vanzo_team/xul ...

  5. touch、touchevent-事件总结

    1.废话不多说,直接上代码,如下 package com.example.mygestrue; import android.support.v7.app.ActionBarActivity; imp ...

  6. matlab 可视化 —— 高级 api(montage)、insertObjectAnnotation、insertMaker

    1. montage:同时显示多个图像 thumbnails = train_x(:, :, :, 1:100); thumbnails = imresize(thumbnails, [64, 64] ...

  7. 1.1 Introduction中 Kafka as a Storage System官网剖析(博主推荐)

    不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ Kafka as a Storage System kafka作为一个存储系统 An ...

  8. JS原生选项卡 – 幻灯片效果

    <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title> ...

  9. TC Hangs when using quick search extended on win10 (1703)

    https://ghisler.ch/board/viewtopic.php?t=47682 I recently updated windows 10 to the latest released ...

  10. 基于am3358的led跑马灯測试

    #include <sys/ioctl.h> #include<stdio.h> #include <fcntl.h> #include <sys/types ...