Zookeeper client (2)-Native client

In the Zookeeper client (1)-the native client , we mainly introduced the operations of adding, deleting, modifying, and inspecting nodes using the Zookeeper client, but do n’t forget the ACL permission control . Here we will take a look at the Zookeeper client. In the terminal, how do we use ACL permissions to control .


First, we need to connect to our server first. Here is similar to the previous one. One difference is that instead of using the anonymous inner class when Zookeeper connects directly, the test class inherits the Watch interface as follows:

public class ZookeeperAclTest implements Watcher{

    public static final String CONNECT_ADDR = "192.168.80.130:2181";
    public static CountDownLatch countDownLatch = new CountDownLatch(1);

    public static void main(String[] args) throws Exception {
        ZooKeeper zooKeeper = new ZooKeeper(CONNECT_ADDR, 5000, new ZookeeperAclTest());
        countDownLatch.await();
    }

    @Override
    public void process(WatchedEvent event) {
        if(event.getState() == Event.KeeperState.SyncConnected){
            countDownLatch.countDown();
        }
    }
}

After the connection is completed, we can perform ACL permission control , as follows, we add two permissions, and then give permission control when creating the node
Insert picture description here


In addition, for existing nodes, we can also add related permissions. Here we add permissions to an existing / node2, and then print out its permissions, as follows:
Insert picture description here
Insert picture description here


When we visited, we found that we did not have permission. Yes, we added permission to the / node2 node. When we access, we must first register authorization information for the current session before accessing.

Note that we can setAclannotate the command here , because we have just granted permission to node / node2, and here we must be granted permission without permission; or we can add the session authorization information on the top.
Insert picture description here
Insert picture description here

286 original articles published · Liked12 · Visitors 10,000+

Guess you like

Origin blog.csdn.net/newbie0107/article/details/104890969