[ZooKeeper]ZooKeeper Java客户端ACL API

The following constants are provided by the ZooKeeper Java library:

The following are the standard ACL IDs:

ZooKeeper client comes with three standard ACLs:

  • ZooDefs.Ids.OPEN_ACL_UNSAFE; //(ZooDefs.Perms.ALL, ZooDefs.Ids.ANYONE_ID_UNSAFE)
  • ZooDefs.Ids.READ_ACL_UNSAFE;// (ZooDefs.Perms.READ, ZooDefs.Ids.ANYONE_ID_UNSAFE)
  • ZooDefs.Ids.CREATOR_ALL_ACL; //(ZooDefs.Perms.ALL, ZooDefs.Ids.AUTH_IDS)
    The ZooDefs.Ids.OPEN_ACL_UNSAFE is completely open free for all ACL: any application can execute any operation on the node and can create, list and delete its children. The ZooDefs.Ids.READ_ACL_UNSAFE is read-only access for any application. ZooDefs.Ids.CREATOR_ALL_ACL grants all permissions to the creator of the node. The creator must have been authenticated by the server (for example, using “digest” scheme) before it can create nodes with this ACL.

The following ZooKeeper operations deal with ACLs:

Here is a sample code that makes use of the above APIs to authenticate itself using the “foo” scheme and create an ephemeral node “/xyz” with create-only permissions.

Note
This is a very simple example which is intended to show how to interact with ZooKeeper ACLs specifically.

package felix.zookeeper.examples.acl;

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

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.ACL;
import org.apache.zookeeper.data.Stat;

public class ACLExample {

    /**
     * In this example this method gets the cert for your
     *   environment -- you must provide
     */
    public static String fooGetCertOnce(String id) {
        return null;
    }

    /** 
     * Watcher function -- empty for this example, not something you should
     * do in real code 
     * */
    private static Watcher watcher = new Watcher() {

        @Override
        public void process(WatchedEvent event) {
        }
    };

    public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
        String appId = "example.foo_test";
        String cert = fooGetCertOnce(appId);
        if (cert != null) {
            System.out.printf("Certificate for appid [%s] is [%s]\n", appId, cert);
        } else {
            System.err.printf( "Certificate for appid [%s] not found\n", appId);
            cert = "dummy";
        }

        ZooKeeper client = new ZooKeeper("localhost:3181", 10000, watcher, false);



        try {
            client.addAuthInfo("foo", cert.getBytes());

            List<ACL> CREATE_ONLY = Arrays.asList(new ACL[] { new ACL(ZooDefs.Perms.CREATE, ZooDefs.Ids.AUTH_IDS) });
            client.create("/xyz", "value".getBytes(), CREATE_ONLY, CreateMode.EPHEMERAL);
            Stat stat = new Stat();

            // this operation will fail with a NoAuthException
            client.getData("/xyz", watcher, stat);
        } catch (KeeperException.NoAuthException e) {
            System.err.printf("Error %d for %s\n", e.getMessage());
        } finally {
            client.close();
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您介绍一下 JavaZooKeeper 的简单使用。 首先,您需要通过 Maven 或 Gradle 将 ZooKeeper 客户端库添加到您的项目中。然后,您可以使用以下代码连接到 ZooKeeper 服务器: ```java String connectionString = "localhost:2181"; int sessionTimeout = 5000; Watcher watcher = event -> System.out.println("Event received: " + event); ZooKeeper zk = new ZooKeeper(connectionString, sessionTimeout, watcher); ``` 在这个例子中,我们使用本地的 ZooKeeper 服务器,并设置了一个会话超时时间为 5000 毫秒。我们还提供了一个 Watcher 对象来处理来自 ZooKeeper 的事件。 接下来,您可以使用 ZooKeeper API 来执行各种操作,例如创建节点、读取节点数据、设置节点数据等。例如,要创建一个节点,您可以使用以下代码: ```java String path = "/myNode"; byte[] data = "myData".getBytes(); CreateMode createMode = CreateMode.PERSISTENT; zk.create(path, data, ZooDefs.Ids.OPEN_ACL_UNSAFE, createMode); ``` 这将在 ZooKeeper 中创建一个名为 "/myNode" 的持久节点,并将其数据设置为 "myData"。 同样,您可以使用以下代码读取节点数据: ```java String path = "/myNode"; byte[] data = zk.getData(path, false, null); String dataStr = new String(data); System.out.println("Data of node " + path + ": " + dataStr); ``` 这将从 ZooKeeper 中读取 "/myNode" 节点的数据,并将其打印到控制台上。 这只是 ZooKeeper API 的一小部分,您可以根据需要进行更多操作。希望这可以帮助您开始使用 Java 中的 ZooKeeper

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值