本文整理汇总了Java中org.I0Itec.zkclient.ZkClient.createPersistent方法的典型用法代码示例。如果您正苦于以下问题:Java ZkClient.createPersistent方法的具体用法?Java ZkClient.createPersistent怎么用?Java ZkClient.createPersistent使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.I0Itec.zkclient.ZkClient
的用法示例。
在下文中一共展示了ZkClient.createPersistent方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: nextId
import org.I0Itec.zkclient.ZkClient; //导入方法依赖的package包/类
/**
* 利用zookeeper
* @return
*/
@Override
public String nextId() {
String app = this.registerDto.getApp();
String host = this.registerDto.getHost();
ZkClient zkClient = this.registerDto.getZkClient();
String path = Constants.ZK_REGISTRY_ID_ROOT_PATH + Constants.SLASH + app + Constants.SLASH + host;
if (zkClient.exists(path)) {
// 如果已经有该节点,表示已经为当前的host上部署的该app分配的编号(应对某个服务重启之后编号不变的问题),直接获取该id,而无需生成
return zkClient.readData(Constants.ZK_REGISTRY_ID_ROOT_PATH + Constants.SLASH + app + Constants.SLASH + host);
} else {
// 节点不存在,那么需要生成id,利用zk节点的版本号每写一次就自增的机制来实现
Stat stat = zkClient.writeDataReturnStat(Constants.ZK_REGISTRY_SEQ, new byte[0], -1);
// 生成id
String id = String.valueOf(stat.getVersion());
// 将数据写入节点
zkClient.createPersistent(path, true);
zkClient.writeData(path, id);
return id;
}
}
示例2: makeSure
import org.I0Itec.zkclient.ZkClient; //导入方法依赖的package包/类
public static void makeSure(ZkClient client, final String dataPath) {
int start = 0, index;
while (true) {
index = dataPath.indexOf("/", start + 1);
if (index == start + 1) {
return;
}
String path = dataPath;
if (index > 0) {
path = dataPath.substring(0, index);
start = index;
}
if (!client.exists(path)) {
client.createPersistent(path);
}
if (index < 0 || index == dataPath.length() - 1) {
return;
}
}
}
示例3: startServer
import org.I0Itec.zkclient.ZkClient; //导入方法依赖的package包/类
public static KafkaServerStartable startServer(final int port, final int brokerId,
final String zkStr, final Properties configuration) {
// Create the ZK nodes for Kafka, if needed
int indexOfFirstSlash = zkStr.indexOf('/');
if (indexOfFirstSlash != -1) {
String bareZkUrl = zkStr.substring(0, indexOfFirstSlash);
String zkNodePath = zkStr.substring(indexOfFirstSlash);
ZkClient client = new ZkClient(bareZkUrl);
client.createPersistent(zkNodePath, true);
client.close();
}
File logDir = new File("/tmp/kafka-" + Double.toHexString(Math.random()));
logDir.mkdirs();
configureKafkaPort(configuration, port);
configureZkConnectionString(configuration, zkStr);
configureBrokerId(configuration, brokerId);
configureKafkaLogDirectory(configuration, logDir);
KafkaConfig config = new KafkaConfig(configuration);
KafkaServerStartable serverStartable = new KafkaServerStartable(config);
serverStartable.startup();
return serverStartable;
}
示例4: startServer
import org.I0Itec.zkclient.ZkClient; //导入方法依赖的package包/类
public static KafkaServerStartable startServer(final int port, final int brokerId, final String zkStr,
final Properties configuration) {
// Create the ZK nodes for Kafka, if needed
int indexOfFirstSlash = zkStr.indexOf('/');
if (indexOfFirstSlash != -1) {
String bareZkUrl = zkStr.substring(0, indexOfFirstSlash);
String zkNodePath = zkStr.substring(indexOfFirstSlash);
ZkClient client = new ZkClient(bareZkUrl);
client.createPersistent(zkNodePath, true);
client.close();
}
File logDir = new File("/tmp/kafka-" + Double.toHexString(Math.random()));
logDir.mkdirs();
configureKafkaPort(configuration, port);
configureZkConnectionString(configuration, zkStr);
configureBrokerId(configuration, brokerId);
configureKafkaLogDirectory(configuration, logDir);
KafkaConfig config = new KafkaConfig(configuration);
KafkaServerStartable serverStartable = new KafkaServerStartable(config);
serverStartable.startup();
return serverStartable;
}
示例5: init
import org.I0Itec.zkclient.ZkClient; //导入方法依赖的package包/类
private void init() {
zkClient = new ZkClient(url, sessionTimeout, connectionTimeout,
new BytesPushThroughSerializer());
try {
zkClient.createPersistent(HOME.concat(root).concat("/").
concat(parent), true);
} catch (ZkNodeExistsException e) {
e.printStackTrace();
}
cleanExecutors = Executors.newCachedThreadPool();
}
示例6: SendCounterHandler
import org.I0Itec.zkclient.ZkClient; //导入方法依赖的package包/类
public SendCounterHandler(String producerGroup, String zkServers) {
this.producerGroup = producerGroup;
int sessionTimeoutMs = 10000;
int connectionTimeoutMs = 10000;
zkClient = new ZkClient(zkServers, sessionTimeoutMs, connectionTimeoutMs,
ZKStringSerializer$.MODULE$);
//
groupPath = ROOT + "/" + producerGroup;
if (!zkClient.exists(groupPath)) {
zkClient.createPersistent(groupPath, true);
}
initCollectionTimer();
}
示例7: register
import org.I0Itec.zkclient.ZkClient; //导入方法依赖的package包/类
/**
* 向注册中心进行注册,生成该服务的编号并返回
* @param registerDto
* @return
*/
@Override
public String register(RegisterDto registerDto) {
String host = registerDto.getHost();
String app = registerDto.getApp();
// 向注册中心注册
ZkClient zkClient = registerDto.getZkClient();
zkClient.createPersistent(Constants.ZK_REGISTRY_SERVICE_ROOT_PATH + Constants.SLASH + app, true);
IdGen idGen = new IncrementIdGen(registerDto);
String id = idGen.nextId();
zkClient.createEphemeral(Constants.ZK_REGISTRY_SERVICE_ROOT_PATH + Constants.SLASH + app + Constants.SLASH + host, id);
return id;
}
示例8: ZkConfigDb
import org.I0Itec.zkclient.ZkClient; //导入方法依赖的package包/类
public ZkConfigDb ( String zkServers, String rootZkNode )
{
fZkConnection = new ZkConnection ( zkServers );
zkClient = new ZkClient ( fZkConnection );
zkRoot = rootZkNode == null ? "" : rootZkNode;
if ( rootZkNode.length() > 0 && !zkClient.exists(zkRoot) )
{
zkClient.createPersistent ( zkRoot, true );
}
}
示例9: test
import org.I0Itec.zkclient.ZkClient; //导入方法依赖的package包/类
/**
* @Description: zkClient主要做了两件事情。
* @see:一件是在session loss和session expire时自动创建新的ZooKeeper实例进行重连。
* @see:一件是将一次性watcher包装为持久watcher。
* @see:后者的具体做法是简单的在watcher回调中,重新读取数据的同时再注册相同的watcher实例。
*/
private static void test() {
final ZkClient zc = new ZkClient("123.56.118.135:2181");
// 创建根节点
/* Persistent与Ephemeral 就是持久化保存到本地和不持久化的区别 ,不能再临时节点下面创建子节点 */
zc.createPersistent("/testroot");
// zc.createEphemeral("/testroot");
// 创建子节点
zc.create("/testroot/node1", "node1", CreateMode.EPHEMERAL);
zc.createPersistent("/testroot/node2");
zc.create("/testroot/node2/test", "node1", CreateMode.EPHEMERAL);
List<String> children = zc.getChildren("/");
log.info("根节点下面的字节点个数" + children.size());
// 获得子节点个数
int chidrenNumbers = zc.countChildren("/testroot");
log.info("子节点个数" + chidrenNumbers);
zc.writeData("/testroot/node2/test", "给节点写数据");
// 删除节点
zc.delete("/testroot/node2/test");
zc.delete("/testroot/node2");
zc.delete("/testroot/node1");
zc.delete("/testroot");
}
示例10: SendCounterHandler
import org.I0Itec.zkclient.ZkClient; //导入方法依赖的package包/类
public SendCounterHandler(String producerGroup, ZkClient zkClient) {
this.zkClient = zkClient;
this.producerGroup =producerGroup;
//
groupPath = ROOT + "/" + producerGroup;
if(!zkClient.exists(groupPath)){
zkClient.createPersistent(groupPath, true);
}
initCollectionTimer();
}
示例11: save
import org.I0Itec.zkclient.ZkClient; //导入方法依赖的package包/类
@Override
public void save() {
ZkClient client = client();
try {
client.createPersistent(path, true);
client.writeData(path, Nodes.toJson().toString().getBytes(Charset.forName("utf-8")));
} finally { client.close(); }
}
示例12: init
import org.I0Itec.zkclient.ZkClient; //导入方法依赖的package包/类
private void init(){
zkClient = new ZkClient(zkConnects);
if(!zkClient.exists(Constants.ZK_ROOT)){
zkClient.createPersistent(Constants.ZK_ROOT);
}
}