Chinaunix首页 | 论坛 | 博客
  • 博客访问: 562314
  • 博文数量: 104
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1559
  • 用 户 组: 普通用户
  • 注册时间: 2014-08-21 00:58
个人简介

锻炼精神,首先要锻炼肉体

文章分类

全部博文(104)

文章存档

2018年(1)

2016年(1)

2015年(101)

2014年(1)

我的朋友

分类: Java

2015-06-28 11:50:49

这篇文章来简单介绍一下, ZooKeeper的伪集群安装,以及基于本机的 ZooKeeper 如何搭建开发环境。
这里介绍的"集群安装"是伪集群安装,考虑到自己的计算机在同时开3,4 个虚拟机的情况下已经很难保证运行速度,
所以采用的是伪集群安装, 这里的为集群安装是通过把多个不同的端口号分配给不同的 zookeeper 进程来运行,即 127.0.0.1:{2181, 2182,2183}
而真正的集群安装是,把每个主机上面的 不同 IP 地址和相同端口号 {10.2.0.27,10.2.0.28,20.2.0.29}:2181分配给不同的 zookeeper 服务进程来运行。

如果不考虑性能,其实现的效果都是一样的,伪分布式在编程的时候更加容易调试,等到基于伪分布式编写的程序能够正常运行,再将其移植到分布式环境中
也是一个好方法。


ZooKeeper 的安装

1. 下载
wget />
2. 解压
tar -xzf zookeeper-3.4.6.tar.gz

3. 修改系统配置文件
vi /etc/profile
add
export PATH_TO_ZK=/home/zookeeper-3.4.6
export ZK_HOME=/home/zookeeper-3.4.6/bin
export PATH=$PATH:$ZK_HOME

source /etc/profile

4. 修改 ZooKeeper 配置文件

复制一份 ../config/ 路径下面的 zoo_sample.cfg 
cp zoo_sample.cfg zoo.cfg 
并将 zoo.cfg 文件按照如下的格式进行修改

点击(此处)折叠或打开

  1. # The number of milliseconds of each tick
  2. tickTime=2000

  3. # The number of ticks that the initial
  4. # synchronization phase can take
  5. initLimit=10

  6. # The number of ticks that can pass between
  7. # sending a request and getting an acknowledgement
  8. syncLimit=5

  9. # the directory where the snapshot is stored.
  10. # do not use /tmp for storage, /tmp here is just
  11. # example sakes.

  12. dataDir=/home/zookeeper-3.4.6/data

  13. # the port at which the clients will connect

  14. clientPort=2181


  15. server.1=127.0.0.1:2222:2223
  16. server.2=127.0.0.1:3333:3334
  17. server.3=127.0.0.1:4444:4445

其中 server.*=host:port1:port2 ,中的 host 用于指定 zookeeper 服务器进程运行的主机 IP 地址,

port1 指的是 server 进程运行的端口号, 而 port2 指的是当需要选举 leader 的时候用于竞争的端口号码。
因为我们在配置文件中修改了 dataDir 的路径,所以在 /home/zookeeper-3.4.6/ 路径下面创建一个名为 data 的文件夹。

要注意的是,当前我们改写的这个配置文件仅仅也是一个模板,启动不同的 zookeeper 服务进程的时候,
需要指定不同的配置文件。
因为我们要运行 3 个 zookeeper 服务进程,所以我们要分别创建 3 个不同的配置文件,并且为其设定不同的端口号
4.1 创建文件夹
    mkdir -p /home/zookeeper-3.4.6/z1/data
    mkdir -p /home/zookeeper-3.4.6/z2/data
   mkdir -p /home/zookeeper-3.4.6/z3/data
   
4.2 分别在各自的文件夹下面创建 myid 文件,并在文件中写入 1-225 的数值,用来与配置文件中的 server.X 的 X 相对应
  echo 1 > /home/zookeeper-3.4.6/z1/data/myid
  echo 2 > /home/zookeeper-3.4.6/z2/data/myid
   echo 3 > /home/zookeeper-3.4.6/z3/data/myid

4.3 将刚刚修改好的配置文件,分别拷贝到 /home/zookeeper-3.4.6/{z1,z2 ,z3} 路径下面,
并将其重新命名为 {z1.cfg, z2.cfg, z3.cfg},
不过,除了 z1.cfg , z2,z3.cfg 配置文件中的 clientPort 要分别重新命名为 {2182, 2183} 因为主要以端口号来区分
不同的服务进程,从而实现伪分布 , 如果端口号相同,主机是不会允许在同一 IP:port 上运行 2 个或以上的进程的,
所以若不修改,启动的时候一定会出现错误。

5. 启动多个 ZooKeeper 服务器进程
首先,将目录切换到 z1 路径下,也就是存放 z1.cfg 配置文件的路径下面,执行如下命令

[root@ayanami_rei z1]# /home/zookeeper-3.4.6/bin/zkServer.sh  start ./z1.cfg

如果,成功启动 zookeeper server 进程的话,将会显示如下的提示信息:
JMX enabled by default
Using config: ./z1.cfg
Starting zookeeper ... STARTED

接下来,分别切换到 z2,z3 的路径下面以同样的方式对应不同的配置文件{z2.cfg, z3.cfg }来启动 zookeeper server 进程


6. 启动单个 ZooKeeper 客户端进程

等到全部 server  进程启动之后, 一如下的命令启动客户端进程:

 ${PATH_TO_ZK}/bin/zkCli.sh -server 127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183

如果正常运行的话,将会显示如下提示信息
Connecting to 127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183
2015-06-28 11:18:39,386 [myid:] - INFO  [main:Environment@100] - Client environment:zookeeper.version=3.4.6-1569965, built on 02/20/2014 09:09 GMT
2015-06-28 11:18:39,407 [myid:] - INFO  [main:Environment@100] - Client environment:host.name=<NA>
2015-06-28 11:18:39,407 [myid:] - INFO  [main:Environment@100] - Client environment:java.version=1.8.0_45
2015-06-28 11:18:39,443 [myid:] - INFO  [main:Environment@100] - Client environment:java.vendor=Oracle Corporation
2015-06-28 11:18:39,443 [myid:] - INFO  [main:Environment@100] - Client environment:java.home=/usr/local/jdk-1.8/jre
2015-06-28 11:18:39,444 [myid:] - INFO  [main:Environment@100] - Client environment:java.class.path=/home/zookeeper-3.4.6/bin/../build/classes:/home/zookeeper-3.4.6/bin/../build/lib/*.jar:/home/zookeeper-3.4.6/bin/../lib/slf4j-log4j12-1.6.1.jar:/home/zookeeper-3.4.6/bin/../lib/slf4j-api-1.6.1.jar:/home/zookeeper-3.4.6/bin/../lib/netty-3.7.0.Final.jar:/home/zookeeper-3.4.6/bin/../lib/log4j-1.2.16.jar:/home/zookeeper-3.4.6/bin/../lib/jline-0.9.94.jar:/home/zookeeper-3.4.6/bin/../zookeeper-3.4.6.jar:/home/zookeeper-3.4.6/bin/../src/java/lib/*.jar:/home/zookeeper-3.4.6/bin/../conf:.:/usr/local/jdk-1.8/jre/lib/rt.jar:/usr/local/jdk-1.8/lib/dt.jar:/usr/local/jdk-1.8=/lib/tools.jar:/java_conf/maven/demos


基于 ZooKeeper API 的编程环境搭建

1. 首先,使用 idea 搭建一个 Maven 项目
2. 修改 Maven 项目中的 pom.xml 文件

添加如下常用的依赖关系

点击(此处)折叠或打开

  1. <dependencies>


  2.         <dependency>
  3.             <groupId>org.slf4j</groupId>
  4.             <artifactId>slf4j-api</artifactId>
  5.             <version>1.7.1</version>
  6.         </dependency>

  7.         <dependency>
  8.             <groupId>junit</groupId>
  9.             <artifactId>junit</artifactId>
  10.             <version>4.11</version>
  11.         </dependency>

  12.         <dependency>
  13.             <groupId>log4j</groupId>
  14.             <artifactId>log4j</artifactId>
  15.             <version>1.2.16</version>
  16.         </dependency>

  17.         <dependency>
  18.             <groupId>org.apache.zookeeper</groupId>
  19.             <artifactId>zookeeper</artifactId>
  20.             <version>3.4.6</version>
  21.             <exclusions>
  22.                 <exclusion>
  23.                     <groupId>com.sun.jmx</groupId>
  24.                     <artifactId>jmxri</artifactId>
  25.                 </exclusion>
  26.                 <exclusion>
  27.                     <groupId>com.sun.jdmk</groupId>
  28.                     <artifactId>jmxtools</artifactId>
  29.                 </exclusion>
  30.                 <exclusion>
  31.                     <groupId>javax.jms</groupId>
  32.                     <artifactId>jms</artifactId>
  33.                 </exclusion>
  34.             </exclusions>
  35.         </dependency>

  36.         <dependency>
  37.             <groupId>org.apache.curator</groupId>
  38.             <artifactId>curator-recipes</artifactId>
  39.             <version>2.1.0-incubating</version>
  40.         </dependency>
  41.     </dependencies>

3. 编写简单的测试程序

点击(此处)折叠或打开

  1. package org.apache.zookeeper.demo;


  2. import org.apache.zookeeper.*;
  3. import org.apache.zookeeper.data.Stat ;
  4. import org.apache.zookeeper.Watcher.Event ;


  5. /**
  6.  * Created by root on 6/28/15.
  7.  */

  8. public class App implements Watcher
  9. {


  10.     public void process( WatchedEvent event )
  11.     {

  12.         if ( event.getType() == Event.EventType.NodeChildrenChanged)
  13.         {
  14.             System.out.println ("child changed ") ;
  15.         }
  16.         if ( event.getType() == Event.EventType.NodeDeleted )
  17.         {
  18.             System.out.println ("node deleted ") ;
  19.         }
  20.         System.out.println(" operation event invoke :") ;
  21.         System.out.println (event) ;
  22.     }

  23.     public static void main ( String [] args ) throws Exception
  24.     {
  25.         final String CONNECT_STRING = "127.0.0.1:2181" ;
  26.         final int SESSION_LIMIT = 20000 ;


  27.         App a = new App ( ) ;



  28.         final ZooKeeper zk = new ZooKeeper( CONNECT_STRING , SESSION_LIMIT , a ) ;

  29.         Stat e = zk.exists("/kylin" , a ) ;

  30.         System.out.println("exists "+ e) ;

  31.         zk.setData("/kylin" , "server.1=127.0.0.1:2181".getBytes() , -1 ) ;

  32.         // here we gonna to create a path , and see what will happen

  33.         // here we set the wabher for this path
  34.         zk.exists("/new_create_path_for_test" , a) ;

  35.         // here we try to re-create , and see if this operation will invoke the Watcher execution
  36.         zk.create("/new_create_path_for_test/children11" ,"test_path".getBytes() ,
  37.                 ZooDefs.Ids.OPEN_ACL_UNSAFE ,
  38.                 CreateMode.PERSISTENT) ;


  39.     }
  40. }

运行结果


operation event invoke :
WatchedEvent state:SyncConnected type:None path:null
exists 8589934607,12884901909,1435410248916,1435462858903,6,0,0,0,23,0,8589934607

operation event invoke :
WatchedEvent state:SyncConnected type:NodeDataChanged path:/kylin

Process finished with exit code 0

end
阅读(2515) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

夏目玲子2015-07-25 21:35:00

自从知道了curator 这个东西,就觉得这篇文没啥参考价值了