`
lobin
  • 浏览: 427187 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

zookeeper

 
阅读更多

启动zk客户端

>.\zkCli.cmd

指定host和port:

>zkCli.cmd -server localhost:2191

 

connect

格式:

connect host:port

例子:

connect localhost 2181

 

acl

ACLs是由scheme:expression和perms组成的(scheme:expression, perms),其中scheme:expression是Ids。

Ids格式:

scheme:id

 

acl格式:scheme:id:perm

其中

scheme:id表示Ids

perm用于指定权限,支持CREATE、READ、WRITE、DELETE以及ADMIN:

CREATE: you can create a child node

READ: you can get data from a node and list its children.

WRITE: you can set data for a node

DELETE: you can delete a child node

 

ADMIN: you can set permissions

 

acl支持4种scheme:world、auth、digest以及ip:

world has a single id, anyone, that represents anyone.

auth doesn't use any id, represents any authenticated user.

digest uses a username:password string to generate MD5 hash which is then used as an ACL ID identity. Authentication is done by sending the username:password in clear text. When used in the ACL the expression will be the username:base64 encoded SHA1 password digest.

ip uses the client host IP as an ACL ID identity. The ACL expression is of the form addr/bits where the most significant bits of addr are matched against the most significant bits of the client host IP.

 

设置ACL

格式:

setAcl path acl

例子:

setAcl /test1 world:anyone:cdrw

 

获取ACL

格式:

getAcl path

例子:

getAcl /test1

'world,'anyone

: cdrwa

 

如果scheme指定world,id只能是anyone,表示任何人都有对应的权限。

例如:

create /test1 'test data'

 

getAcl /test1

'world,'anyone

: cdrwa

 

setAcl /test1 world:anyone:cdrw

 

getAcl /test1

'world,'anyone

: cdrw

 

由于上面已经收回了ADMIN: you can set permissions的权限,所以要是在设置acl话报Authentication is not valid : /test1的错误:

setAcl /test1 world:anyone:cdrw

Authentication is not valid : /test1

 

如果scheme指定auth,则不需要指定id,表示任何认证的用户都有对应的权限。

例如:

create /test2 'test data'

 

getAcl /test2

'world,'anyone

: cdrwa

 

在设置acl时,如果scheme指定auth,需要先通过addauth命令添加认证用户:

addauth digest user1:pwd1

setAcl /test2 auth::cdrwa

 

addauth digest user2:pwd2

setAcl /test2 auth::cdrwa

 

这样写没有报错,但不会生效:

setAcl /test2 auth:user3:pwd3:cdrwa

 

addauth digest user4:pwd4

setAcl /test2 auth::cdrwa

 

getAcl /test2

'digest,'user1:a9l5yfb9zl8WCXjVmi5/XOC0Ep4=

: cdrwa

这里的user1:a9l5yfb9zl8WCXjVmi5/XOC0Ep4=就是上面添加的认证用户user1:pwd1。

 

public interface Perms {
    int READ = 1 << 0;

    int WRITE = 1 << 1;

    int CREATE = 1 << 2;

    int DELETE = 1 << 3;

    int ADMIN = 1 << 4;

    int ALL = READ | WRITE | CREATE | DELETE | ADMIN;
}

 

public interface Ids {
    /**
     * This Id represents anyone.
     */
    public final Id ANYONE_ID_UNSAFE = new Id("world", "anyone");

    /**
     * This Id is only usable to set ACLs. It will get substituted with the
     * Id's the client authenticated with.
     */
    public final Id AUTH_IDS = new Id("auth", "");

    /**
     * This is a completely open ACL .
     */
    public final ArrayList<ACL> OPEN_ACL_UNSAFE = new ArrayList<ACL>(
	Collections.singletonList(new ACL(Perms.ALL, ANYONE_ID_UNSAFE)));

    /**
     * This ACL gives the creators authentication id's all permissions.
     */
    public final ArrayList<ACL> CREATOR_ALL_ACL = new ArrayList<ACL>(
	Collections.singletonList(new ACL(Perms.ALL, AUTH_IDS)));

    /**
     * This ACL gives the world the ability to read.
     */
    public final ArrayList<ACL> READ_ACL_UNSAFE = new ArrayList<ACL>(
	Collections
		.singletonList(new ACL(Perms.READ, ANYONE_ID_UNSAFE)));
}

 

 

import org.apache.zookeeper.*;
import org.apache.zookeeper.data.ACL;
import org.apache.zookeeper.data.Stat;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

public class ZookeeperSession implements Watcher {

    private ZooKeeper zk;

    public boolean closed;

    public ZookeeperSession(String connectionString) throws ZookeeperException {
        try {
            zk = new ZooKeeper(connectionString, 3000, this);
        } catch (IOException e) {
            throw new ZookeeperException(e);
        }
    }

    public boolean exists(String path) throws ZookeeperException {
        return exists(path, false);
    }

    public boolean exists(String path, boolean watch) throws ZookeeperException {
        Stat stat = null;
        try {
            stat = zk.exists(path, watch);
        } catch (KeeperException e) {
            throw new ZookeeperException(e);
        } catch (InterruptedException e) {
            throw new ZookeeperException(e);
        }
        return stat != null;
    }

    public boolean exists(final String path, Watcher watcher) throws ZookeeperException {
        Stat stat = null;
        try {
            stat = zk.exists(path, watcher);
        } catch (KeeperException e) {
            throw new ZookeeperException(e);
        } catch (InterruptedException e) {
            throw new ZookeeperException(e);
        }
        return stat != null;
    }

    public void exists(String path, boolean watch, AsyncCallback.StatCallback cb, Object ctx) {
        zk.exists(path, watch, cb, ctx);
    }

    public void exists(final String path, Watcher watcher, AsyncCallback.StatCallback cb, Object ctx) {
        zk.exists(path, watcher, cb, ctx);
    }

    /**
     * The maximum allowable size of the data array is 1 MB (1,048,576 bytes).
     * Arrays larger than this will cause a KeeperExecption to be thrown.
     *
     * @param path
     * @param data
     * @return
     * @throws ZookeeperException
     */
    public String create(String path, byte data[]) throws ZookeeperException {
        return create(path, data, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    }

    public String create(final String path, byte data[], List<ACL> acl, CreateMode createMode) throws ZookeeperException {
        try {
            return zk.create(path, data, acl, createMode);
        } catch (KeeperException e) {
            throw new ZookeeperException(e);
        } catch (InterruptedException e) {
            throw new ZookeeperException(e);
        }
    }

    /**
     * whether the session to zookeeper is opened or closed. if the
     * session is current closed, then the current session is close,
     * this is no question. but if the session current is not close,
     * this not indicates the session current is normal open state.
     * @return
     */
    public boolean isClose() {
        return false;
    }

    @Override
    public void process(WatchedEvent e) {
        Event.EventType type = e.getType();
        System.out.println("event Event(" + type + ") received.");

        Event.KeeperState state = e.getState();
        System.out.println("zk current state " + state + ", on Event(" + type + ")");
        if (type == Event.EventType.None) {
            switch (state) {
                case SyncConnected:
                    closed = false;
                    break;
                case Expired:
                    closed = true;
                    break;
            }
        }
    }
}

 

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.data.ACL;

import java.util.List;

public class ZookeeperClient {

    private String connectionString;

    public ZookeeperClient(String connectionString) {
        this.connectionString = connectionString;
    }

    public ZookeeperSession getSession() throws ZookeeperException {
        return new ZookeeperSession(connectionString);
    }

    public boolean exists(String path) throws ZookeeperException {
        ZookeeperSession session = getSession();
        return session.exists(path);
    }

    public boolean exists(String path, boolean watch) throws ZookeeperException {
        ZookeeperSession session = getSession();
        return session.exists(path, watch);
    }

    public boolean exists(final String path, Watcher watcher) throws ZookeeperException {
        ZookeeperSession session = getSession();
        return session.exists(path, watcher);
    }

    public String create(String path, byte data[]) throws ZookeeperException {
        ZookeeperSession session = getSession();
        return session.create(path, data);
    }

    public String create(final String path, byte data[], List<ACL> acl, CreateMode createMode) throws ZookeeperException {
        ZookeeperSession session = getSession();
        return session.create(path, data, acl, createMode);
    }
}

 

1、zookeeper中的消息结构: https://lobin.iteye.com/blog/2384664

2、zookeeper快照(Snapshot)文件结构:https://lobin.iteye.com/blog/2384782

3、zookeeper事务日志文件结构:https://lobin.iteye.com/blog/2386346

分享到:
评论

相关推荐

    zookeeper-3.4.6_zookeeper_

    《Zookeeper:分布式服务治理的核心组件》 Zookeeper,作为Apache的一个开源项目,是分布式应用程序协调服务的基石,它是一个高可用、高性能的分布式一致性服务。在标题“zookeeper-3.4.6_zookeeper_”中,我们可以...

    apache-zookeeper(apache-zookeeper-3.7.1-bin.tar.gz)

    apache-zookeeper分布式框架,压缩包内容:(apache-zookeeper-3.7.1-bin.tar.gz、apache-zookeeper-3.7.1.tar.gz、apache-zookeeper-3.6.4-bin.tar.gz、apache-zookeeper-3.6.4.tar.gz、apache-zookeeper-3.5.10-...

    ZooKeeper-分布式过程协同技术详解 和从Paxos到Zookeeper

    《ZooKeeper:分布式过程协同技术详解》与《从Paxos到Zookeeper:分布式一致性原理与实践》这两本书深入探讨了分布式系统中的关键组件ZooKeeper及其背后的一致性算法Paxos。ZooKeeper是由Apache软件基金会开发的一个...

    zookeeper 3.6.3 源码下载

    ZooKeeper 3.6.3 是一个广泛用于分布式系统的协调服务,它为分布式应用程序提供了高效且可靠的命名服务、配置管理、集群同步、分布式锁等核心功能。在深入理解源码之前,我们需要先了解ZooKeeper的基本概念和工作...

    zookeeper-3.9.1.zip

    Apache ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,它是集群的管理者,监视着分布式应用程序的运行状态,提供诸如命名服务、配置管理、分布式同步、组服务等分布式基础服务。Zookeeper的设计目标...

    Zookeeper_安装和配置

    Zookeeper 是一个分布式的,开放源码的分布式应用程序协调服务,它是集群的管理者,监视着集群中各个节点的状态根据节点提交的反馈进行下一步合理操作。最终将简单易用的接口和性能高效、功能稳定的系统提供给用户。...

    centos8安装zookeeper3.8.0详细步骤

    CentOS 8 安装 ZooKeeper 3.8.0 详细步骤 ZooKeeper 是一个分布式应用程序协调服务,提供了配置管理、名称服务、分布式同步和提供组服务等功能。下面是 CentOS 8 安装 ZooKeeper 3.8.0 的详细步骤。 1. 下载安装包...

    linux中zookeeper安装包zookeeper-3.4.8.tar

    在IT领域,Zookeeper是一个非常重要的分布式协调服务,由Apache Hadoop项目开发并维护。它在大规模分布式系统中被广泛用于数据管理、配置共享、命名服务、群组服务以及分布式同步。Zookeeper-3.4.8是其一个稳定版本...

    zookeeper可视化工具

    **Zookeeper可视化工具详解** Apache ZooKeeper 是一个分布式协调服务,它为分布式应用程序提供高度可靠的命名服务、配置管理、集群同步、领导选举等核心功能。在运维和开发过程中,为了更方便地管理和监控...

    zookeeper-3.4.12版本

    Zookeeper是Apache软件基金会的一个开源项目,主要用于分布式协调服务,它是集群管理的基石,被广泛应用于大数据、云计算等领域。Zookeeper 3.4.12是该系统的一个稳定版本,提供了解压即用的便利性。 一、Zookeeper...

    zookeeper版本为zookeeper-3.4.10.tar.gz

    ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,它是集群的管理者,监视着集群中各个节点的状态根据节点提交的反馈进行下一步合理操作。最终将简单易用的接口和性能高效、功能稳定的系统提供给用户。...

    apache-zookeeper-3.5.6-bin.tar

    Apache ZooKeeper 是一个分布式协调服务,它为分布式应用程序提供了一个高度可用、高性能的框架,用于管理数据和配置信息,处理命名服务、分布式同步以及组服务等问题。ZooKeeper 的设计目标是简化分布式环境中的...

    apache-zookeeper-3.8.4-bin.tar

    Apache ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,它是集群的管理者,监视着分布式应用程序,提供了诸如配置维护、命名服务、分布式同步、组服务等这些分布式基础服务。Zookeeper是Apache Hadoop...

    zookeeper集群升级方案

    ### Zookeeper 集群升级方案详解 #### 一、需求背景 随着业务的发展和技术的进步,现有的Zookeeper集群系统版本过低(当前版本为3.3.4),导致某些功能特性无法得到支持或表现不佳,这直接影响到了业务的正常运行...

    zookeeper-3.8.0安装包下载

    Apache ZooKeeper 是一个高度可靠的分布式协调系统,广泛应用于云原生环境中的服务发现、配置管理、命名服务等场景。Zookeeper-3.8.0 是该系统的最新版本,提供了更稳定和高效的服务。 Zookeeper 的核心概念包括...

    zookeeper连接工具zktools

    《Zookeeper连接工具ZkTools详解》 Zookeeper作为一个分布式协调服务,在云原生环境中扮演着至关重要的角色。它提供了一种可靠的方式来管理和维护配置信息、命名服务、集群同步、分布式锁等。为了方便开发者与...

    zookeeper限制ip版

    《Zookeeper 3.4.14 IP限制功能详解及源码改造》 Apache ZooKeeper 是一个分布式的,开放源码的分布式应用程序协调服务,它是集群的管理者,监视着集群中各个节点的状态根据节点提交的反馈进行下一步合理操作。...

    ZooKeeper3.4.9 windos和linux

    《ZooKeeper 3.4.9:在Windows与Linux上的部署与应用》 ZooKeeper,一个由Apache基金会开发的分布式协调服务,是许多大型分布式系统中的关键组件。3.4.9版本是ZooKeeper的一个稳定版本,提供了一系列增强功能和性能...

    zookeeper-3.4.6.zip

    《Apache ZooKeeper 3.4.6:分布式协调服务详解》 Apache ZooKeeper 是一个开源的分布式协调服务,它为分布式应用提供了一个高效且可靠的命名服务、配置管理、集群同步和分布式锁等基础功能。在Zookeeper 3.4.6版本...

    zookeeper增加权限登录验证

    ZooKeeper 增加权限登录验证 ZooKeeper 是一个广泛使用的分布式协调服务,它提供了许多有用的功能,如配置管理、名字服务、分布式锁等。然而,在 ZooKeeper 中存在一些安全漏洞,例如未经授权的访问、数据泄露等。...

Global site tag (gtag.js) - Google Analytics