Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use zookeeper in java

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report