Mol*_*Ice 18 java apache-zookeeper apache-curator
使用CuratorFramework,有人可以解释我怎么做:
使用用户名foo和密码bar?那些不知道这个用户/通行证的人将无法做任何事情.
出于此问题的目的,我不关心通过明文发送的SSL或密码.
Ime*_*gha 15
Apache Curator中的ACL用于访问控制.因此,ZooKeeper不提供任何认证机制,如clients who don't have correct password cannot connect to ZooKeeper or cannot create ZNodes.它可以做的是,防止未经授权的客户端访问特定的Znode/ZNode.为此,您必须设置CuratorFramework实例,如下所述.请记住,这将保证具有给定ACL的ZNode创建可以由同一客户端或呈现相同身份验证信息的客户端再次访问.
首先,您应该CuratorFramework按如下方式构建instane.这里,connectString表示ip and port集合中zookeeper服务器组合的逗号分隔列表.
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();
Run Code Online (Sandbox Code Playgroud)
现在你必须开始它.
client.start();
Run Code Online (Sandbox Code Playgroud)
创建路径.
client.create().withMode(CreateMode.PERSISTENT).forPath("/your/ZNode/path");
Run Code Online (Sandbox Code Playgroud)
在这里,CreateMode指定要创建的节点类型.可用的类型是PERSISTENT,EPHEMERAL,EPHEMERAL_SEQUENTIAL,PERSISTENT_SEQUENTIAL,CONTAINER.Java Docs
如果您不确定路径是否/your/ZNode已存在,也可以创建它们.
client.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath("/your/ZNode/path");
Run Code Online (Sandbox Code Playgroud)
设置数据
您可以在创建ZNode或更高版本时设置数据.如果要在创建时设置数据,请将数据作为byte数组作为第二个参数传递给forPath()方法.
client.create().withMode(CreateMode.PERSISTENT).forPath("/your/ZNode/path","your data as String".getBytes());
Run Code Online (Sandbox Code Playgroud)
如果你以后再这样做,(数据应该作为字节数组给出)
client.setData().forPath("/your/ZNode/path",data);
Run Code Online (Sandbox Code Playgroud)
最后
我不明白你的意思get this path.Apache Curator是一个java客户端(比Curator Recipes更多),它Apache Zookeeper在后台使用并隐藏了Zookeeper的边缘情况和复杂性.在Zookeeper中,他们使用ZNodes存储数据的概念.您可以将其视为Linux目录结构.所有ZNodePaths应该以/(root)开头,您可以根据需要继续指定目录,如ZNodePaths.例如:/someName/another/test/sample.
如上图所示,ZNode以树形结构组织.每个ZNode可以存储多达1MB的数据.因此,如果要检索存储在ZNode中的数据,则需要知道该ZNode的路径.(就像你应该知道数据库的表和列以便检索数据).
如果要在给定路径中检索数据,
client.getData().forPath("/path/to/ZNode");
Run Code Online (Sandbox Code Playgroud)
当你想与策展人合作时,你必须知道这一切.
还有一件事
Apache Curator中的ACL用于访问控制.也就是说,如果你设置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;
}
}
Run Code Online (Sandbox Code Playgroud)
只有具有与创建者相同的凭据的客户端才能在以后获得对相应ZNode的访问权限.Autherization详细信息设置如下(请参阅客户端构建示例).还有其他可用的ACL模式,OPEN_ACL_UNSAFE如果将其设置为ACLProvider,则不执行任何访问控制.
authorization("digest", authorizationString.getBytes())
Run Code Online (Sandbox Code Playgroud)
稍后将使用它们来控制对给定ZNode的访问.
简而言之,如果您想阻止其他人干扰您的ZNode,您可以将ACLProvider设置为返回CREATOR_ALL_ACL并将授权设置为digest如上所示.只有使用相同授权字符串("username:password")的CuratorFramework实例才能访问这些ZNode.但它不会阻止其他人在不干扰你的路径中创建ZNode.
希望你找到你想要的东西:-)
| 归档时间: |
|
| 查看次数: |
1864 次 |
| 最近记录: |