一、什么是zookeeper,有什么用

  ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,它是集群的管理者,监视着集群中各个节点的状态根据节点提交的反馈进行下一步合理操作。最终,将简单易用的接口和性能高效、功能稳定的系统提供给用户(来自百度百科)。

  其主要的功能有

    1.命名服务   2.配置管理   3.集群管理   4.分布式锁  5.队列管理

二、zookeeper的单机部署

  1.下载并解压 zookeeper-3.4.10.tar.gz

  2.将conf目录下zoo_sample.cfg配置文件修改为zoo.conf。

  3.修改zoo.conf。

# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
#数据存放的位置(自主配置)
dataDir=/tmp/zookeeper/data
#日志存放的位置(新加的配置,默认在zookeeper的bin目录下的zookeeper.out)
dataLogDir=/tmp/zookeeper/logs
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
~

三、zookeeper集群搭建(伪集群)

  1.在服务器上解压三份zookeeper,

  2.分别将conf目录下zoo_sample.cfg配置文件修改为zoo.conf,并修改zoo.conf配置文件

  由于是在一台服务器上做测试,所以为了避免端口冲突,修改了端口

# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=/tmp/zookeeper0/data
# the port at which the clients will connect
clientPort=2180
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1 server.0=localhost:2287:3387
server.1=localhost:2288:3388
server.2=localhost:2289:3389
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=/tmp/zookeeper1/data
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1 server.0=localhost:2287:3387
server.1=localhost:2288:3388
server.2=localhost:2289:3389
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=/tmp/zookeeper2/data
# the port at which the clients will connect
clientPort=2182
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1 server.0=localhost:2287:3387
server.1=localhost:2288:3388
server.2=localhost:2289:3389

  4.分别启动几个zookeeper,这样集群就搭建完成。

四、命令行操作zookeeper

  1.启动zookeeper  : ./zkServer.sh start

  2.关闭zookeeper: ./zkServer.sh stop

  3.客户端连接 ./zkCli.sh -server localhost:2181

  4.查询当前zookeeper的状态  ./zkServer.sh status

  5.客户端连接上zookeeper后,可以使用help命令来查询zookeeper的命令

  6.关闭与服务端的连接 : close

  7.连接服务端:connect 127.0.0.1:2181

  8.创建节点  create  /name  value

  9.获取节点的信息  get /name

  10.列出节点  ls /name

  11.删除节点信息  delete  /name

  12.列出节点  ls2  /name   是ls的加强版

  13.列出历史执行命令   history

  14.重新执行某个命令和history结合使用   redo  20

  15.sync  强制同步

  16.stat  查看节点信息

  17.显示配额 listquota /name

  18.设置配额  setquota  /name

  19.删除配额  delquota /name

  20.addauth命令用于节点认证,使用方式:如addauth digest username:password

  21.setAcl命令用于设置节点Acl

    Acl由三部分构成:1为scheme,2为user,3为permission,一般情况下表示为scheme:id:permissions

  22. 获取节点的Acl,如getAcl /node1

  12.退出客户端 quit

五、zookeeper的使用

  

package testzookeeper;

import java.lang.management.ManagementFactory;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper; /**
* 模拟集群服务器连接
*/
public class ClusterClient implements Watcher, Runnable {
private static String membershipRoot = "/Members";
final ZooKeeper zk; public ClusterClient(String hostPort, String processId) throws Exception { zk = new ZooKeeper(hostPort, 2000, this);
if (zk != null) {
zk.create(membershipRoot + '/' + processId, processId.getBytes(), Ids.OPEN_ACL_UNSAFE,CreateMode.EPHEMERAL);
}
} public synchronized void close() {
try {
zk.close();
} catch (InterruptedException e) {
e.printStackTrace();
}
} @Override
public void process(WatchedEvent event) { } public void run() {
try {
synchronized (this) {
while (true) {
wait();
}
}
} catch (InterruptedException e) {
e.printStackTrace();
Thread.currentThread().interrupt();
} finally {
this.close();
}
} public static void main(String[] args) throws Exception { String hostPort = "111.230.239.152:2180,111.230.239.152:2181,111.230.239.152:2182";
String name = ManagementFactory.getRuntimeMXBean().getName();
String processId = name.substring(0, name.indexOf('@'));
new ClusterClient(hostPort, processId).run();
}
}
package testzookeeper;

import java.io.IOException;
import java.util.List; import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper; /**
*模拟集群监控
*
*/
public class ClusterMonitor { private static String membershipRoot = "/Members";
private final Watcher connectionWatcher;
private final Watcher childrenWatcher;
private ZooKeeper zk;
boolean alive = true; public ClusterMonitor(String HostPort) throws Exception {
connectionWatcher = new Watcher() {
@Override
public void process(WatchedEvent event) {
if (event.getType() == Watcher.Event.EventType.None
&& event.getState() == Watcher.Event.KeeperState.SyncConnected) {
System.out.println("Client connect success !!!");
}
}
}; childrenWatcher = new Watcher() {
@Override
public void process(WatchedEvent event) { if (event.getType() == Event.EventType.NodeChildrenChanged) {
List<String> children;
try {
children = zk.getChildren(membershipRoot, this);
System.out.println("Cluster Membership Change!!!");
System.out.println("Members: " + children);
}catch (Exception e) {
e.printStackTrace();
}
}
}
}; zk = new ZooKeeper(HostPort, 2000, connectionWatcher); if (zk.exists(membershipRoot, false) == null) {
zk.create(membershipRoot, "ClusterMonitorRoot".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
} List<String> children = zk.getChildren(membershipRoot, childrenWatcher);
System.err.println("Members: " + children);
} public synchronized void close() {
try {
zk.close();
} catch (InterruptedException e) {
e.printStackTrace();
}
} public void run() {
try {
synchronized (this) {
while (alive) {
wait();
}
}
} catch (InterruptedException e) {
e.printStackTrace();
Thread.currentThread().interrupt();
} finally {
this.close();
}
} public static void main(String[] args) throws Exception { String hostPort = "111.230.239.152:2180,111.230.239.152:2181,111.230.239.152:2182";
new ClusterMonitor(hostPort).run();
}
}

zookeeper的搭建和简单的使用的更多相关文章

  1. Dubbo+zookeeper+SpringMVC搭建最简单的分布式项目

    Dubbo 是什么 一款分布式服务框架 高性能和透明化的RPC远程服务调用方案 SOA服务治理方案 Dubbo 架构流程图 Provider:服务提供方 Consumer:服务消费者 Registry ...

  2. 用dubbo+zookeeper+spring搭建一个简单的http接口程序

    dubbo是一个分布式服务框架,是阿里巴巴开发的一个解决RPC远程调用优化的核心框架,包含负载均衡算法,能提高分布式系统的性能. zookeeper是hadoop的一个子项目,主要用来解决分布式系统的 ...

  3. SpringBoot+Dubbo+Zookeeper整合搭建简单的分布式应用

    为什么要使用分布式系统? 容错 减少延迟/提高性能 可用性 负载均衡 总而言之,其实目的只有一个,”用户体验“. 什么是分布式系统? 分布式系统是由使用分发中间件连接的自治计算机组成的网络.它们有助于 ...

  4. Spring Cloud简介/版本选择/ZooKeeper例子搭建简单说明

    一.什么是Spring Cloud 官方的说法就是Spring Cloud 给开发者提供一套按照一定套路快速开发分布式系统的工具. 具体点就是Spring Boot实现的微服务架构开发工具.它为微服务 ...

  5. 超详细,新手都能看懂 !使用SpringBoot+Dubbo 搭建一个简单的分布式服务

    来自:JavaGuide Github 地址:https://github.com/Snailclimb/springboot-integration-examples 目录: 使用 SpringBo ...

  6. 使用 SpringBoot+Dubbo 搭建一个简单分布式服务

    实战之前,先来看几个重要的概念 开始实战之前,我们先来简单的了解一下这样几个概念:Dubbo.RPC.分布式.由于本文的目的是带大家使用SpringBoot+Dubbo 搭建一个简单的分布式服务,所以 ...

  7. hadoop2.6.2+hbase+zookeeper环境搭建

    1.hadoop环境搭建,版本:2.6.2,参考:http://www.cnblogs.com/bookwed/p/5251393.html 启动服务:在master机器上,进入hadoop安装目录, ...

  8. Android中NDK的搭建及简单使用 Android.mk相关介绍 JNI的使用

    Android中NDK的搭建及简单使用: 使用NDK,简述其重要步骤:.搭建NDK环境(作用:用于自动生成jni下的.c对应的so文件)---到Android NDK官网或Android官网下载ndk ...

  9. 用nodejs搭建一个简单的服务器

    使用nodejs搭建一个简单的服务器 nodejs优点:性能高(读写文件) 数据操作能力强 官网:www.nodejs.org 验证是否安装成功:cmd命令行中输入node -v 如果显示版本号表示安 ...

随机推荐

  1. P1319 压缩技术

    很多小伙伴卡在此题的原因可能是因为不知道怎么让它输入无限个数字吧?除了用string,在这里我是看到“压缩码保证 N * N=交替的各位数之和”这一句话,想到用while循环.只要输入的数的总和t小于 ...

  2. 【XSY2732】Decalcomania 可持久化线段树 分治

    题目描述 有一个陶瓷瓶周围有\(n\)个可以印花的位置.第\(i\)个与第\(i+1\)个位置之间的距离为\(d_i\),在第\(i\)个位置印图案要\(t_i\)秒. 机器刚开始在\(0\)号位置, ...

  3. 【BZOJ5285】[HNOI2018]寻宝游戏(神仙题)

    [BZOJ5285][HNOI2018]寻宝游戏(神仙题) 题面 BZOJ 洛谷 题解 既然是二进制按位的运算,显然按位考虑. 发现这样一个关系,如果是\(or\)的话,只要\(or\ 1\),那么无 ...

  4. 计算几何细节梳理&模板

    点击%XZY巨佬 向量的板子 #include<bits/stdc++.h> #define I inline using namespace std; typedef double DB ...

  5. 【BZOJ2034】最大收益(贪心)

    [BZOJ2034]最大收益(贪心) 题面 BZOJ 题解 首先显然让价值越大的占用一个时刻一定更优. 所以把所有东西按照价值排序之后来处理,那么显然就是把前面的全部放好之后,考虑来放当前这个东西,如 ...

  6. [SNOI2017]一个简单的询问【莫队+容斥原理】

    题目大意 给你一个数列,让你求两个区间内各个数出现次数的乘积的和. 分析 数据范围告诉我们可以用莫队过. 我并不知道什么曼哈顿什么乱七八糟的东西,但是我们可以用容斥原理将这个式子展开来. \[\sum ...

  7. cf406E Hamming Triples (推公式)

    考虑某两行a和b的dis 如果相同:$|a-b|$ 如果不同:$n-|a-b|$ 然后考虑三行的dis,不妨设a>=b>=c 那么有4种情况: 1.a,b,c 0/1的种类相同:$|a-b ...

  8. HDU6341 Let Sudoku Rotate (杭电多校4J)

    给一个由4*4个4*4的小格组成数独,这些数独是由一个块逆时针旋转得来的,所以要还原的话就模拟出顺时针的过程,先把里面的字母转化成数字,然后从第一个块开始枚举,每个dfs和之前枚举的已经满足条件的块, ...

  9. thinkphp5 上传服务器后 Access denied

    服务器报 Access denied,要么报 No input files,但是在网上查了查说是将 PHP 的cgi.fix_pathinfo 改成 1 即可,但是改成 1 显然是有解析漏洞的,尝试寻 ...

  10. 洛谷P3265 装备购买

    这个大毒瘤题....居然反向卡精度.... 别的题eps要开小,这个毒瘤要开大... 我一开始是1e-12,挂的奇惨无比,50分...... 然后改成1e-7,就70分了... 1e-5 90分 1e ...