Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder()
- .connectString(connectString)
- .retryPolicy(new ExponentialBackoffRetry(retryInitialWaitMs, maxRetryCount))
- .connectionTimeoutMs(connectionTimeoutMs)
- .sessionTimeoutMs(sessionTimeoutMs);
- /*
- * If authorization information is available, those will be added to the client. NOTE: These auth info are
- * for access control, therefore no authentication will happen when the client is being started. These
- * info will only be required whenever a client is accessing an already create ZNode. For another client of
- * another node to make use of a ZNode created by this node, it should also provide the same auth info.
- */
- if (zkUsername != null && zkPassword != null) {
- String authenticationString = zkUsername + ":" + zkPassword;
- builder.authorization("digest", authenticationString.getBytes())
- .aclProvider(new ACLProvider() {
- @Override
- public List<ACL> getDefaultAcl() {
- return ZooDefs.Ids.CREATOR_ALL_ACL;
- }
- @Override
- public List<ACL> getAclForPath(String path) {
- return ZooDefs.Ids.CREATOR_ALL_ACL;
- }
- });
- }
- CuratorFramework client = builder.build();
- client.start();
- client.create().withMode(CreateMode.PERSISTENT).forPath("/your/ZNode/path");
- client.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath("/your/ZNode/path");
- client.create().withMode(CreateMode.PERSISTENT).forPath("/your/ZNode/path","your data as String".getBytes());
- client.setData().forPath("/your/ZNode/path",data);
- client.getData().forPath("/path/to/ZNode");
- new ACLProvider() {
- @Override
- public List<ACL> getDefaultAcl () {
- return ZooDefs.Ids.CREATOR_ALL_ACL;
- }
- @Override
- public List<ACL> getAclForPath (String path){
- return ZooDefs.Ids.CREATOR_ALL_ACL;
- }
- }
- authorization("digest", authorizationString.getBytes())
- import java.security.NoSuchAlgorithmException;
- import java.util.ArrayList;
- import java.util.List;
- import org.apache.curator.RetryPolicy;
- import org.apache.curator.framework.CuratorFramework;
- import org.apache.curator.framework.CuratorFrameworkFactory;
- import org.apache.curator.framework.api.ACLProvider;
- import org.apache.curator.retry.ExponentialBackoffRetry;
- import org.apache.zookeeper.ZooDefs;
- import org.apache.zookeeper.data.ACL;
- import org.apache.zookeeper.data.Id;
- import org.apache.zookeeper.server.auth.DigestAuthenticationProvider;
- public class AdminClient {
- protected static CuratorFramework client = null;
- public void initializeClient() throws NoSuchAlgorithmException {
- String zkConnectString = "127.0.0.1:2181";
- RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
- final List<ACL> acls = new ArrayList<>();
- //full-control ACL
- String zkUsername = "adminuser";
- String zkPassword = "adminpass";
- String fullControlAuth = zkUsername + ":" + zkPassword;
- String fullControlDigest = DigestAuthenticationProvider.generateDigest(fullControlAuth);
- ACL fullControlAcl = new ACL(ZooDefs.Perms.ALL, new Id("digest", fullControlDigest));
- acls.add(fullControlAcl);
- //read-only ACL
- String zkReadOnlyUsername = "readuser";
- String zkReadOnlyPassword = "readpass";
- String readOnlyAuth = zkReadOnlyUsername + ":" + zkReadOnlyPassword;
- String readOnlyDigest = DigestAuthenticationProvider.generateDigest(readOnlyAuth);
- ACL readOnlyAcl = new ACL(ZooDefs.Perms.READ, new Id("digest", readOnlyDigest));
- acls.add(readOnlyAcl);
- //create the client with full-control access
- client = CuratorFrameworkFactory.builder()
- .connectString(zkConnectString)
- .retryPolicy(retryPolicy)
- .authorization("digest", fullControlAuth.getBytes())
- .aclProvider(new ACLProvider() {
- @Override
- public List<ACL> getDefaultAcl() {
- return acls;
- }
- @Override
- public List<ACL> getAclForPath(String string) {
- return acls;
- }
- })
- .build();
- client.start();
- //Now create, read, delete ZK nodes
- }
- }
- import java.security.NoSuchAlgorithmException;
- import org.apache.curator.RetryPolicy;
- import org.apache.curator.framework.CuratorFramework;
- import org.apache.curator.framework.CuratorFrameworkFactory;
- import org.apache.curator.retry.ExponentialBackoffRetry;
- public class ReadOnlyClient {
- protected static CuratorFramework client = null;
- public void initializeClient() throws NoSuchAlgorithmException {
- String zkConnectString = "127.0.0.1:2181";
- RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
- String zkReadOnlyUsername = "readuser";
- String zkReadOnlyPassword = "readpass";
- String readOnlyAuth = zkReadOnlyUsername + ":" + zkReadOnlyPassword;
- client = CuratorFrameworkFactory.builder()
- .connectString(zkConnectString)
- .retryPolicy(retryPolicy)
- .authorization("digest", readOnlyAuth.getBytes())
- .build();
- client.start();
- //Now read ZK nodes
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement