zookeeper实战

关键字: zookeep部署, zookeep java测试程序


1.部署zookeeper

首先系统要安装配置好jdk环境,具体可以参考相关资料文档,然后

访问官网地址 http://zookeeper.apache.org/releases.html#download 下载最新版的二进制包

本文下载的二进制压缩包 zookeeper-3.4.6.tar.gz 到 /opt目录,然后解压


编辑zookeeper配置文件 /opt/zookeeper-3.4.6/conf/zoo.cfg 内容如下:
tickTime=2000

initLimit=10

syncLimit=5

dataDir=/var/lib/zookeeper

clientPort=2181

#保存退出


启动zookeeper服务进程

cd /opt/zookeeper-3.4.6/

bin/zkServer.sh start

然后jps看看进程是否成功运行,netstat看看是否监听2181端口,如果都成功了,接下来使用zookeeper命令行先创建一个测试节点


启动zookeeper命令行执行下面命令

bin/zkCli.sh -server 127.0.0.1:2181

当提示 [zk: 127.0.0.1:2181(CONNECTED) 0] 标示已经连接上了本地的zookeeper服务,可以开始操作了

#创建节点zk_test,绑定字符串"my_data"

create /zk_test my_data

#设置节点对应数据

set /zk_test test1

#查看节点信息

get /zk_test


接下来运行zookeeper的API测试程序


2.测试zookeeper java 测试程序


先创建好java测试程序的目录组织列表如下,使用的是tree命令

├── build
│   ├── classes
│   │   └── efly
│   │       ├── DataMonitor$1.class
│   │       ├── DataMonitor.class
│   │       ├── DataMonitor$DataMonitorListener.class
│   │       ├── Executor.class
│   │       └── Executor$StreamWriter.class
│   └── jar
│       └── Executor.jar
├── lib
│   ├── slf4j-api-1.6.1.jar
│   └── zookeeper-3.4.6.jar
├── myManifest
├── readme.txt
└── src
    └── efly
        ├── DataMonitor.java
        └── Executor.java


build/classes是java编译后的class存放路径

build/jar是编译成jar文件的存放路径

lib是依赖第三方库的存放路径,比如zookeeper测试程序需要用到zookeeper和slf4j的jar包,这两个包均可以在zookeeper的二进制压缩包里面可以找到

src是存放测试源代码目录


Executor.java

这个测试程序是用户监听一个znode节点,然后当节点发生变化的时候,把内容写入用户指定的文件,然后启动用户指定的程序,每当znode节点发生一次改变都会触发一次

/**
 * A simple example program to use DataMonitor to start and
 * stop executables based on a znode. The program watches the
 * specified znode and saves the data that corresponds to the
 * znode in the filesystem. It also starts the specified program
 * with the specified arguments when the znode exists and kills
 * the program if the znode goes away.
 */
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;

public class Executor
    implements Watcher, Runnable, DataMonitor.DataMonitorListener
{
    String znode;

    DataMonitor dm;

    ZooKeeper zk;

    String filename;

    String exec[];

    Process child;

    public Executor(String hostPort, String znode, String filename,
            String exec[]) throws KeeperException, IOException {
        this.filename = filename;
        this.exec = exec;
        zk = new ZooKeeper(hostPort, 3000, this);
        dm = new DataMonitor(zk, znode, null, this);
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        if (args.length < 4) {
            System.err
                    .println("USAGE: Executor hostPort znode filename program [args ...]");
            System.exit(2);
        }
        String hostPort = args[0];
        String znode = args[1];
        String filename = args[2];
        String exec[] = new String[args.length - 3];
        System.arraycopy(args, 3, exec, 0, exec.length);
        try {
            new Executor(hostPort, znode, filename, exec).run();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /***************************************************************************
     * We do process any events ourselves, we just need to forward them on.
     *
     * @see org.apache.zookeeper.Watcher#process(org.apache.zookeeper.proto.WatcherEvent)
     */
    public void process(WatchedEvent event) {
        dm.process(event);
    }

    public void run() {
        try {
            synchronized (this) {
                while (!dm.dead) {
                    wait();
                }
            }
        } catch (InterruptedException e) {
        }
    }

    public void closing(int rc) {
        synchronized (this) {
            notifyAll();
        }
    }

    static class StreamWriter extends Thread {
        OutputStream os;

        InputStream is;

        StreamWriter(InputStream is, OutputStream os) {
            this.is = is;
            this.os = os;
            start();
        }

        public void run() {
            byte b[] = new byte[80];
            int rc;
            try {
                while ((rc = is.read(b)) > 0) {
                    os.write(b, 0, rc);
                }
            } catch (IOException e) {
            }

        }
    }

    public void exists(byte[] data) {
        if (data == null) {
            if (child != null) {
                System.out.println("Killing process");
                child.destroy();
                try {
                    child.waitFor();
                } catch (InterruptedException e) {
                }
            }
            child = null;
        } else {
            if (child != null) {
                System.out.println("Stopping child");
                child.destroy();
                try {
                    child.waitFor();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            try {
                FileOutputStream fos = new FileOutputStream(filename);
                fos.write(data);
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                System.out.println("Starting child");
                child = Runtime.getRuntime().exec(exec);
                new StreamWriter(child.getInputStream(), System.out);
                new StreamWriter(child.getErrorStream(), System.err);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

DataMonitor.java

/**
 * A simple class that monitors the data and existence of a ZooKeeper
 * node. It uses asynchronous ZooKeeper APIs.
 */
import java.util.Arrays;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.AsyncCallback.StatCallback;
import org.apache.zookeeper.KeeperException.Code;
import org.apache.zookeeper.data.Stat;

public class DataMonitor implements Watcher, StatCallback {

    ZooKeeper zk;

    String znode;

    Watcher chainedWatcher;

    boolean dead;

    DataMonitorListener listener;

    byte prevData[];

    public DataMonitor(ZooKeeper zk, String znode, Watcher chainedWatcher,
            DataMonitorListener listener) {
        this.zk = zk;
        this.znode = znode;
        this.chainedWatcher = chainedWatcher;
        this.listener = listener;
        // Get things started by checking if the node exists. We are going
        // to be completely event driven
        zk.exists(znode, true, this, null);
    }

    /**
     * Other classes use the DataMonitor by implementing this method
     */
    public interface DataMonitorListener {
        /**
         * The existence status of the node has changed.
         */
        void exists(byte data[]);

        /**
         * The ZooKeeper session is no longer valid.
         *
         * @param rc
         *                the ZooKeeper reason code
         */
        void closing(int rc);
    }

    public void process(WatchedEvent event) {
        String path = event.getPath();
        if (event.getType() == Event.EventType.None) {
            // We are are being told that the state of the
            // connection has changed
            switch (event.getState()) {
            case SyncConnected:
                // In this particular example we don't need to do anything
                // here - watches are automatically re-registered with 
                // server and any watches triggered while the client was 
                // disconnected will be delivered (in order of course)
                break;
            case Expired:
                // It's all over
                dead = true;
                listener.closing(KeeperException.Code.SessionExpired);
                break;
            }
        } else {
            if (path != null && path.equals(znode)) {
                // Something has changed on the node, let's find out
                zk.exists(znode, true, this, null);
            }
        }
        if (chainedWatcher != null) {
            chainedWatcher.process(event);
        }
    }

    public void processResult(int rc, String path, Object ctx, Stat stat) {
        boolean exists;
        switch (rc) {
        case Code.Ok:
            exists = true;
            break;
        case Code.NoNode:
            exists = false;
            break;
        case Code.SessionExpired:
        case Code.NoAuth:
            dead = true;
            listener.closing(rc);
            return;
        default:
            // Retry errors
            zk.exists(znode, true, this, null);
            return;
        }

        byte b[] = null;
        if (exists) {
            try {
                b = zk.getData(znode, false, null);
            } catch (KeeperException e) {
                // We don't need to worry about recovering now. The watch
                // callbacks will kick off any exception handling
                e.printStackTrace();
            } catch (InterruptedException e) {
                return;
            }
        }
        if ((b == null && b != prevData)
                || (b != null && !Arrays.equals(prevData, b))) {
            listener.exists(b);
            prevData = b;
        }
    }
}


#编译程序

javac -d build/classes/ -cp .:lib/* src/efly/Executor.java src/efly/DataMonitor.java

#运行程序,这里输入zookeeper服务地址端口,然后是znode节点,保存内容的文件,运行指定程序这里用cat显示/tmp/test

java -cp build/classes/:lib/* efly.Executor 127.0.0.1:2181 /zk_test /tmp/test cat /tmp/test

#当运行程序之后,再到zookeeper命令行,去设置一下/zk_test节点,就会触发程序监听逻辑响应然后执行指定程序了

set /zk_test test123

#然后再看看/tmp/test文件目录,以及观察测试程序运行cat /tmp/test 文件内容


#编译成jar包,首先需要创建一个Manifest文件

比如我们创建 myManifest ,内容如下:

Main-Class: efly.Executor
Class-Path: /home/mj/java/lib/zookeeper-3.4.6.jar
 /home/mj/java/lib/slf4j-api-1.6.1.jar

#Main-Class是指定了入口main

#Class-Path是指定需要打入jar的第三方jar包全路径


#运行打包命令

jar -cfm ./build/jar/Executor.jar myManifest -C build/classes/ .

#运行jar包

java -jar build/jar/Executor.jar 127.0.0.1:2181 /zk_test /tmp/test cat /tmp/test





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值