Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use zookeeper in java

Shulou Source: shulou.com Published: 2022-06-02 10:20:52 09月27日 Update

This article introduces how to use zookeeper in java, the content is very detailed, interested friends can refer to, hope to be helpful to you.

1. Introduce maven dependency org.apache.zookeeper zookeeper 3.4.62.ZooKeeper node operation ZooKeeper zooKeeper = new ZooKeeper ("127.0.0.1 ZooKeeper zooKeeper 2181", 4000, null); / / connect zookeeper service zooKeeper.create ("/ test", "20190725", ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); / / create a node zooKeeper.setData with the name test ("/ test", "20190726",-1) / / update the data of the node zooKeeper.delete ("/ test",-1); / delete the node Stat stat = zooKeeper.exists (path, false); / / check whether the node has zooKeeper.getData ("/ test", false, stat); / / get the node data 3. Add authorized access to ZooKeeper nodes

World: the default permission to create a node. When the only id is anyone, the mode is world:anyone:rwadc, which means that everyone has rwadc permission for this node. Id is used here instead of expression

Id is not required for auth. But it should be expressed as expression here. Namely (scheme:expression:perm)

Digest uses the user name: password encoded as md5 as the id of the access control list. But here id is not part of the authorization statement, and this is also in the way of expression. User name: the password is encoded with sha1 and then base64.

Host uses the user hostname as the id of the access control list. But it's important to note that the expression uses the suffix of the hostname.

Ip is similar to the hostname, where the client's ip address is used as the id of the access control list. Expressions can be used to set the ip whitelist in this way addr/bits.

3.1 digest mode Id id = new Id ("digest", DigestAuthenticationProvider.generateDigest ("zookeeper:123456")); ACL acl = new ACL (); acl.setPerms (Perms.ALL); acl.setId (id); zooKeeper.setACL ("/ test", Lists.newArrayList (acl),-1); 3.2 ip mode Id id = new Id ("ip", "198.16.1.25"); / / ip segments are supported, such as 198.16.1.25/16ACL acl = new ACL () Acl.setPerms (Perms.ALL); acl.setId (id); zooKeeper.setACL (path, Lists.newArrayList (acl),-1); 3.3host mode Id id = new Id ("host", "baidu.com"); ACL acl = new ACL (); acl.setPerms (Perms.ALL); acl.setId (id); zooKeeper.setACL (path, Lists.newArrayList (acl),-1); 4.watcher

Execute listening events when the zookeeper node changes

Public class ZkWatcher implements Watcher {private static Logger logger = LoggerFactory.getLogger (ZkWatcher.class); @ Override public void process (WatchedEvent watchedEvent) {if (watchedEvent.getType () = = EventType.NodeDeleted) {logger.info ("Delete node" + watchedEvent.getPath ());} if (watchedEvent.getType () = = EventType.NodeCreated) {logger.info ("create node" + watchedEvent.getPath ()) } if (watchedEvent.getType () = = EventType.NodeDataChanged) {logger.info ("Update Node" + watchedEvent.getPath ());} if (watchedEvent.getType () = = EventType.NodeChildrenChanged) {logger.info ("Child Node changes" + watchedEvent.getPath ());} first way: zookeeper.register (new ZkWatcher ()) The second way: ZooKeeper zk = new ZooKeeper ("127.0.0.1 zookeeper 2181", 4000, new ZkWatcher ()); so much for sharing about how to use zookeeper in java. I hope the above can be helpful and learn more. If you think the article is good, you can share it for more people to see.

Tags: Node mode mode host user code control content password data more permissions user name expression change help update good inaction event Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn NVidia Microsoft Apple Linux