基本使用

Zookeeper说明

创建节点(znode) 方法: create:提供了两套创建节点的方法,同步和异步创建节点方式。

同步方式:

  • 参数1,节点路径《名称) : InodeName (不允许递归创建节点,也就是说在父节点不存在的情况下,不允许创建子节点)

  • 参数2,节点内容: 要求类型是字节数组(也就是说,不支持序列化方式,如果需要实现序列化,可使用java相关序列化框架,如Hessian、Kryo框架)

  • 参數3,节点权限: 使用Ids.OPEN_ACL_UNSAFE开放权限即可。(这个参数一般在权展没有太高要求的场景下,没必要关注)

  • 参数4,节点类型: 创建节点的类型: CreateMode,提供四种首点象型

    • PERSISTENT(持久节点)

    • PERSISTENT SEQUENTIAL(持久顺序节点)

    • EPHEMERAL(临时节点)

    • EPHEMERAL SEQUENTAL(临时顺序节点)

maven引入依赖

<dependency>
    <groupId>org.apache.zookeeper</groupId>
    <artifactId>zookeeper</artifactId>
    <version>3.4.6</version>
</dependency>

Zookeeper客户端连接

public class ZookeeperDemo {
    /**
     * 集群连接地址
     */
    private static final String CONNECT_ADDR = "192.168.110.138:2181,192.168.110.147:2181,192.168.110.148:2181";
    /**
     * session超时时间
     */
    private static final int SESSION_OUTTIME = 2000;
    /**
     * 信号量,阻塞程序执行,用户等待zookeeper连接成功,发送成功信号,
     */
    private static final CountDownLatch countDownLatch = new CountDownLatch(1);

    public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
        ZooKeeper zk = new ZooKeeper(CONNECT_ADDR, SESSION_OUTTIME, new Watcher() {

            public void process(WatchedEvent event) {
                // 获取时间的状态
                KeeperState keeperState = event.getState();
                EventType tventType = event.getType();
                // 如果是建立连接
                if (KeeperState.SyncConnected == keeperState) {
                    if (EventType.None == tventType) {
                        // 如果建立连接成功,则发送信号量,让后阻塞程序向下执行
                        countDownLatch.countDown();
                        System.out.println("zk 建立连接");
                    }
                }
            }

        });
        // 进行阻塞
        countDownLatch.await();
        //创建父节点
//        String result = zk.create("/testRott", "12245465".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
//        System.out.println("result:" + result);
        //创建子节点
        String result = zk.create("/testRott/children", "children 12245465".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        System.out.println("result:"+result);
        zk.close();
    }

}

解释:countDownLatch.await(); 进行阻塞,告诉主线程先等等不要执行了,等到建立zk建立连接ok了,通过countDownLatch.countDown();唤醒主线程可以执行往下执行了。

资料

9. 使用ZooKeeper Java API编程

如果报错:log4j:WARN No appenders could be found for logger 证明没有配置log4g

参考:log4j:WARN No appenders could be found for logger

pom 引入

<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.16</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.6.1</version>
</dependency>

配置log4j.properties

# Configure logging for testing: optionally with log file
log4j.rootLogger=WARN, stdout
# log4j.rootLogger=WARN, stdout, logfile

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n

log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=target/spring.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n

Last updated