centos6安装zookeeper集群+javademo(curator)

来源:互联网 发布:pump it up 编辑:程序博客网 时间:2024/06/07 00:58

引用:http://blog.csdn.net/dc_726/article/details/46475633
引用:http://m.blog.csdn.net/blog/no99es/50131123
curator版本(2.91)–下maven地址(注意版本问题、如果curator用3.0会报错KeeperErrorCode = Unimplemented):http://search.maven.org/#search%7Cga%7C1%7Corg.apache.curator
这里写图片描述
zookeeper版本(http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.4.7/zookeeper-3.4.7.tar.gz)
一、zookeeper安装步骤
wget http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.4.7/zookeeper-3.4.7.tar.gz

假设有三台机器,hostname和ip对应关系是:
192.168.3.16 zk01 test16
192.168.3.39 zk02 test39
192.168.3.96 zk03 test96
ZooKeeper不存在明显的master/slave关系,各个节点都是服务器,leader挂了,会立马从follower中选举一个出来作为leader.
由于没有主从关系,也不用配置SSH无密码登录了,各个zk服务器是自己启动的,互相之间通过TCP端口来交换数据。
修改配置文件conf/zoo.cfg

tickTime=2000
initLimit=10
syncLimit=5
dataDir=/home/zookeeper/local/var/zookeeper
clientPort=2181
server.1=test16:2888:3888
server.2=test39:2888:3888
server.3=test96:2888:3888
myid文件

要在每台机器的dataDir下,新建一个myid文件,里面存放一个数字,用来标识当前主机。
zookeeper@test16:echo1>>/usr/local/data/myidzookeeper@test39: echo “2” >> /usr/local/data/myid
zookeeper@test96:$ echo “3” >> /usr/local/data/myid
启动
./zkServer.sh start ./zkServer.sh status tail -f zookeeper.out

windows客户端连接:cmd >zkCli.cmd -server 192.168.3.96:2181
这里写图片描述

二、java代码

package test;import org.apache.curator.framework.CuratorFramework;import org.apache.curator.framework.CuratorFrameworkFactory;import org.apache.curator.retry.RetryNTimes;/** * Curator framework's client test. * Output: *  $ create /zktest hello  *  $ ls /  *  [zktest, zookeeper] *  $ get /zktest  *  hello *  $ set /zktest world  *  $ get /zktest  *  world *  $ delete /zktest  *  $ ls /  *  [zookeeper]  */public class CuratorClientTest {    /** Zookeeper info */    private static final String ZK_ADDRESS = "192.168.3.16:2181,192.168.3.39:2181,192.168.3.96:2181";    private static final String ZK_PATH = "/test2";    public static void main(String[] args) throws Exception{        CuratorFramework  client= CuratorFrameworkFactory.newClient(                ZK_ADDRESS,                new RetryNTimes(10, 5000)        );        client.start();//        System.out.println("zk client start successfully!");//        if(client.isStarted()){//是否创建成功//          System.out.println("创建客户端成功<><><><><><><><><><>");//        }else{//          System.out.println("创建客户端失败《》《》《》《》《》《》");//        }////        Thread.sleep(2000);        String data1 = "hello";//        client.create().forPath(ZK_PATH, data1.getBytes());//        try {//          print(client.getChildren().forPath("/"));//      } catch (Exception e) {//          // TODO Auto-generated catch block//          e.printStackTrace();//      }//     // 2.Client API test//        // 2.1 Create node//        String data1 = "hello";//        print("create", ZK_PATH, data1);        client.create().                creatingParentsIfNeeded().                forPath(ZK_PATH, data1.getBytes());////        // 2.2 Get node and data//        print("ls", "/");//        print(client.getChildren().forPath("/"));//        try {//          client.setData().forPath(ZK_PATH, "I love footballqq".getBytes());//      } catch (Exception e1) {//          // TODO Auto-generated catch block//          e1.printStackTrace();//      } //        print("get", ZK_PATH);//        try {//          print(client.getData().forPath(ZK_PATH));//      } catch (Exception e) {//          // TODO Auto-generated catch block//          e.printStackTrace();//      }     }     private static void print(String... cmds) {            StringBuilder text = new StringBuilder("$ ");            for (String cmd : cmds) {                text.append(cmd).append(" ");            }            System.err.println(text.toString());     }    private static void print(Object result) {        System.err.println(result instanceof byte[] ? new String(                (byte[]) result) : result);    }}

zookeeper学习的第一步骤,继续…

0 0
原创粉丝点击