本文整理匯總了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);
}
}