Advertisement
Guest User

Untitled

a guest
Mar 14th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.74 KB | None | 0 0
  1. CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder()
  2. .connectString(connectString)
  3. .retryPolicy(new ExponentialBackoffRetry(retryInitialWaitMs, maxRetryCount))
  4. .connectionTimeoutMs(connectionTimeoutMs)
  5. .sessionTimeoutMs(sessionTimeoutMs);
  6. /*
  7. * If authorization information is available, those will be added to the client. NOTE: These auth info are
  8. * for access control, therefore no authentication will happen when the client is being started. These
  9. * info will only be required whenever a client is accessing an already create ZNode. For another client of
  10. * another node to make use of a ZNode created by this node, it should also provide the same auth info.
  11. */
  12. if (zkUsername != null && zkPassword != null) {
  13. String authenticationString = zkUsername + ":" + zkPassword;
  14. builder.authorization("digest", authenticationString.getBytes())
  15. .aclProvider(new ACLProvider() {
  16. @Override
  17. public List<ACL> getDefaultAcl() {
  18. return ZooDefs.Ids.CREATOR_ALL_ACL;
  19. }
  20.  
  21. @Override
  22. public List<ACL> getAclForPath(String path) {
  23. return ZooDefs.Ids.CREATOR_ALL_ACL;
  24. }
  25. });
  26. }
  27.  
  28. CuratorFramework client = builder.build();
  29.  
  30. client.start();
  31.  
  32. client.create().withMode(CreateMode.PERSISTENT).forPath("/your/ZNode/path");
  33.  
  34. client.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath("/your/ZNode/path");
  35.  
  36. client.create().withMode(CreateMode.PERSISTENT).forPath("/your/ZNode/path","your data as String".getBytes());
  37.  
  38. client.setData().forPath("/your/ZNode/path",data);
  39.  
  40. client.getData().forPath("/path/to/ZNode");
  41.  
  42. new ACLProvider() {
  43. @Override
  44. public List<ACL> getDefaultAcl () {
  45. return ZooDefs.Ids.CREATOR_ALL_ACL;
  46. }
  47.  
  48. @Override
  49. public List<ACL> getAclForPath (String path){
  50. return ZooDefs.Ids.CREATOR_ALL_ACL;
  51. }
  52. }
  53.  
  54. authorization("digest", authorizationString.getBytes())
  55.  
  56. import java.security.NoSuchAlgorithmException;
  57. import java.util.ArrayList;
  58. import java.util.List;
  59. import org.apache.curator.RetryPolicy;
  60. import org.apache.curator.framework.CuratorFramework;
  61. import org.apache.curator.framework.CuratorFrameworkFactory;
  62. import org.apache.curator.framework.api.ACLProvider;
  63. import org.apache.curator.retry.ExponentialBackoffRetry;
  64. import org.apache.zookeeper.ZooDefs;
  65. import org.apache.zookeeper.data.ACL;
  66. import org.apache.zookeeper.data.Id;
  67. import org.apache.zookeeper.server.auth.DigestAuthenticationProvider;
  68.  
  69. public class AdminClient {
  70.  
  71. protected static CuratorFramework client = null;
  72.  
  73. public void initializeClient() throws NoSuchAlgorithmException {
  74. String zkConnectString = "127.0.0.1:2181";
  75. RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
  76. final List<ACL> acls = new ArrayList<>();
  77.  
  78. //full-control ACL
  79. String zkUsername = "adminuser";
  80. String zkPassword = "adminpass";
  81. String fullControlAuth = zkUsername + ":" + zkPassword;
  82. String fullControlDigest = DigestAuthenticationProvider.generateDigest(fullControlAuth);
  83. ACL fullControlAcl = new ACL(ZooDefs.Perms.ALL, new Id("digest", fullControlDigest));
  84. acls.add(fullControlAcl);
  85.  
  86. //read-only ACL
  87. String zkReadOnlyUsername = "readuser";
  88. String zkReadOnlyPassword = "readpass";
  89. String readOnlyAuth = zkReadOnlyUsername + ":" + zkReadOnlyPassword;
  90. String readOnlyDigest = DigestAuthenticationProvider.generateDigest(readOnlyAuth);
  91. ACL readOnlyAcl = new ACL(ZooDefs.Perms.READ, new Id("digest", readOnlyDigest));
  92. acls.add(readOnlyAcl);
  93.  
  94. //create the client with full-control access
  95. client = CuratorFrameworkFactory.builder()
  96. .connectString(zkConnectString)
  97. .retryPolicy(retryPolicy)
  98. .authorization("digest", fullControlAuth.getBytes())
  99. .aclProvider(new ACLProvider() {
  100. @Override
  101. public List<ACL> getDefaultAcl() {
  102. return acls;
  103. }
  104.  
  105. @Override
  106. public List<ACL> getAclForPath(String string) {
  107. return acls;
  108. }
  109. })
  110. .build();
  111. client.start();
  112. //Now create, read, delete ZK nodes
  113. }
  114. }
  115.  
  116. import java.security.NoSuchAlgorithmException;
  117. import org.apache.curator.RetryPolicy;
  118. import org.apache.curator.framework.CuratorFramework;
  119. import org.apache.curator.framework.CuratorFrameworkFactory;
  120. import org.apache.curator.retry.ExponentialBackoffRetry;
  121.  
  122. public class ReadOnlyClient {
  123.  
  124. protected static CuratorFramework client = null;
  125.  
  126. public void initializeClient() throws NoSuchAlgorithmException {
  127. String zkConnectString = "127.0.0.1:2181";
  128. RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
  129. String zkReadOnlyUsername = "readuser";
  130. String zkReadOnlyPassword = "readpass";
  131. String readOnlyAuth = zkReadOnlyUsername + ":" + zkReadOnlyPassword;
  132. client = CuratorFrameworkFactory.builder()
  133. .connectString(zkConnectString)
  134. .retryPolicy(retryPolicy)
  135. .authorization("digest", readOnlyAuth.getBytes())
  136. .build();
  137. client.start();
  138. //Now read ZK nodes
  139. }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement