当我们的项目在不知不觉中做大了之后,各种问题就出来了,真jb头疼,比如性能,业务系统的并行计算的一致性协调问题,比如分布式架构的事务问题,

我们需要多台机器共同commit事务,经典的案例当然是银行转账,支付宝转账这种,如果是一台机器的话,这个还是很方便的,windows中自带了一个事务协

调器mstsc,但是呢,你那种很大很牛逼的项目不可能全是windows服务器,对吧,有些人为了解决这个问题,会采用2pc,3pc这种算法,或者是paxos的思

想进行分布式下的一致性处理,当然在这个世界上,真的不需要你自己去开发这种协调性,因为现在已经有了专门解决这种问题的解决方案,比如zookeeper。

一:zookeeper集群搭建

  有些人应该明白,zookeeper正是google的chubby的开源实现,使用zookeeper之前,我们先来搭建一个集群。

1. 下载

从官网上,我们可以看到,zookeeper的最新版本是3.4.8,下载地址是:http://apache.fayea.com/zookeeper/zookeeper-3.4.8/,可以下载一下:

2. 文件夹配置

接下来我们解压一下,根目录为zkcluster,下面使用clientport(3000,3001,3002)这样的端口作为文件夹名称,里面就是zookeeper解压包,如下面这样:

3. 配置zoo.cfg

现在我们有三个文件夹,也就是3个zookeeper程序,在3001/conf/下面有一个zoo_sample.cfg文件,现在我们改成zoo.cfg,并且修改如下:

# The number of milliseconds of each tick
tickTime=
# The number of ticks that the initial
# synchronization phase can take
initLimit=
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=/root/zkcluster/3001/data
dataLogDir=/root/zkcluster/3001/logs
# the port at which the clients will connect
clientPort=3001
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=
#
# 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=
# Purge task interval in hours
# Set to "" to disable auto purge feature
#autopurge.purgeInterval= server.1=192.168.161.134:2888:3888
server.2=192.168.161.134:2889:3889
server.3=192.168.161.134:2890:3890

这里我们要注意的是,红色的部分分别就是:指定zookeeper的data和log文件夹,指定clientport访问的端口和servers的列表。

4. 生成pid文件

我们在servers列表中,可以看到有server.1 ,server.2, server.3 三个字符串,生成pid文件的内容就取决如此,比如server.1的地址,

我们的pid文件里面就是1,不过要知道的是,pid文件要在data目录下,比如下面这样:

ok,同样的道理,3002和3003的文件夹同3001就可以了,比如他们的zoo.cfg如下:

--------  3002 --------------

# The number of milliseconds of each tick
tickTime=
# The number of ticks that the initial
# synchronization phase can take
initLimit=
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=/root/zkcluster//data
dataLogDir=/root/zkcluster//logs
# the port at which the clients will connect
clientPort=
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=
#
# 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=
# Purge task interval in hours
# Set to "" to disable auto purge feature
#autopurge.purgeInterval= server.=192.168.161.134::
server.=192.168.161.134::
server.=192.168.161.134::

--------  3003 --------------

# The number of milliseconds of each tick
tickTime=
# The number of ticks that the initial
# synchronization phase can take
initLimit=
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=/root/zkcluster//data
dataLogDir=/root/zkcluster//logs
# the port at which the clients will connect
clientPort=
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=
#
# 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=
# Purge task interval in hours
# Set to "" to disable auto purge feature
#autopurge.purgeInterval= server.=192.168.161.134::
server.=192.168.161.134::
server.=192.168.161.134::

5. 启动各自服务器

到现在为止,我们各个zookeeper程序的配置都结束了,接下来我们到各自目录的bin目录下,通过zkServer.sh来进行启动,比如下面这样:

ok,接下来我们来开始启动,通过如下命令即可:

./zkServer.sh start-foreground

现在我们都启动了,接下来我们可以用命令看下哪个server是leader,哪些是follower。。。

[root@localhost bin]# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /root/zkcluster//bin/../conf/zoo.cfg
Mode: follower
[root@localhost bin]# [root@localhost bin]# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /root/zkcluster//bin/../conf/zoo.cfg
Mode: leader
[root@localhost bin]# [root@localhost bin]# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /root/zkcluster//bin/../conf/zoo.cfg
Mode: follower
[root@localhost bin]#

到目前为止,我们的服务端操作都ok啦,,,是不是好吊。。。

二:驱动下载

1.  java的驱动就方便了,直接在源代码中就提供了,直接copy一下lib文件夹中的jar包就ok了,真是tmd的方便。

2. 用C#驱动的也不要太烦,要使用也是不难的,我们可以通过nuget下载一下就可以了,转换过来的版本也是3.4.8的最新版本,比如下面这样:

好了,大概就说这么多,希望对你有帮助~~~

分布式架构中一致性解决方案——Zookeeper集群搭建的更多相关文章

  1. 分布式协调服务Zookeeper集群搭建

    分布式协调服务Zookeeper集群搭建 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.安装jdk环境 1>.操作环境 [root@node101.yinzhengjie ...

  2. Zookeeper 集群搭建--单机伪分布式集群

    一. zk集群,主从节点,心跳机制(选举模式) 二.Zookeeper集群搭建注意点 1.配置数据文件 myid 1/2/3 对应 server.1/2/3 2.通过./zkCli.sh -serve ...

  3. kafka学习(二)-zookeeper集群搭建

    zookeeper概念 ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,它包含一个简单的原语集,分布式应用程序可以基于它实现同步服务,配置维护和命名 服务等.Zookeeper是h ...

  4. Zookeeper集群搭建以及python操作zk

    一.Zookeeper原理简介 ZooKeeper是一个开放源码的分布式应用程序协调服务,它包含一个简单的原语集,分布式应用程序可以基于它实现同步服务,配置维护和命名服务等. Zookeeper设计目 ...

  5. zookeeper集群搭建及Leader选举算法源码解析

    第一章.zookeeper概述 一.zookeeper 简介 zookeeper 是一个开源的分布式应用程序协调服务器,是 Hadoop 的重要组件. zooKeeper 是一个分布式的,开放源码的分 ...

  6. Zookeeper集群搭建及原理

    1 概述 1.1 简介 ZooKeeper 是 Apache 的一个顶级项目,为分布式应用提供高效.高可用的分布式协调服务,提供了诸如数据发布/订阅.负载均衡.命名服务.分布式协调/通知和分布式锁等分 ...

  7. Kafka学习之(五)搭建kafka集群之Zookeeper集群搭建

    Zookeeper是一种在分布式系统中被广泛用来作为:分布式状态管理.分布式协调管理.分布式配置管理.和分布式锁服务的集群.kafka增加和减少服务器都会在Zookeeper节点上触发相应的事件kaf ...

  8. ZooKeeper集群搭建过程

    ZooKeeper集群搭建过程 提纲 1.ZooKeeper简介 2.ZooKeeper的下载和安装 3.部署3个节点的ZK伪分布式集群 3.1.解压ZooKeeper安装包 3.2.为每个节点建立d ...

  9. 【图文详解】Zookeeper集群搭建(CentOs6.3)

    Zookeeper简介: Zookeeper是一个分布式协调服务,就是为用户的分布式应用程序提供协调服务的. A.zookeeper是为别的分布式程序服务的 B.Zookeeper本身就是一个分布式程 ...

随机推荐

  1. vue+node开发手机端h5页面开发遇到的坑

    项目进行中...随时更新 这里记录了一些手机端调试的方式 一 css 1.文字超过span宽度显示...(单行文字) .topWrap .introduce span { padding: 0 17p ...

  2. OpenCV2邻域和模板操作

    在图像处理中,通过当前位置的邻域像素计算新的像素值是很常见的操作.当邻域包含图像的上几行和下几行时,就需要同时扫描图像的若干行,这就是图像的邻域操作了.至于模板操作是实现空间滤波的基础,通常是使用一个 ...

  3. 测试为什么Low

    你从来没有因为一个歌手不会写曲填词而说歌手很Low! 你从来没有因为一个演员不会摄影.唱歌而说演员很Low! 你从来没有因为一个记者不会摄影,拍照而说记者很Low! 你从来没有因为一个美食家不会烧菜, ...

  4. jQuery-1.9.1源码分析系列(十六)ajax——响应数据处理和api整理

    ajax在得到请求响应后主要会做两个处理:获取响应数据和使用类型转化器转化数据 a.获取响应数据 获取响应数据是调用ajaxHandleResponses函数来处理. ajaxHandleRespon ...

  5. 在 C# 中执行 msi 安装

    有时候我们需要在程序中执行另一个程序的安装,这就需要我们去自定义 msi 安装包的执行过程. 需求 比如我要做一个安装管理程序,可以根据用户的选择安装不同的子产品.当用户选择了三个产品时,如果分别显示 ...

  6. angular.js规范写法

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  7. C#循环测试题

    关于如下程序结构的描述中,哪一项是正确的?   for ( ; ; ) { 循环体; //何问起   }   a) 不执行循环体b) 一直执行循环体,即死循环c) 执行循环体一次d) 程序不符合语法要 ...

  8. 在DOS使用SVN之执行命令整理(TortoiseProc.exe)

    原文链接: http://www.cnblogs.com/andrew-blog/archive/2012/08/21/SVN_DOS_Commands.html TortoiseSVN因为所有的命令 ...

  9. WPF平台Grid控件性能比较

    WPF官方发布第一个版本至今已经有10年了, 我们几乎在同时也开始了XAML开发.即使经过多年打造,我们依旧尝试提高:我们真的成功打造了高效灵活的控件吗?我没有在其他地方找到任何关于优秀的WPF表格性 ...

  10. c#模拟js escape方法

    public static string Escape(string s) { StringBuilder sb = new StringBuilder(); byte[] ba = System.T ...