本文整理匯總了Java中org.I0Itec.zkclient.ZkClient.exists方法的典型用法代碼示例。如果您正苦於以下問題:Java ZkClient.exists方法的具體用法?Java ZkClient.exists怎麽用?Java ZkClient.exists使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.I0Itec.zkclient.ZkClient
的用法示例。
在下文中一共展示了ZkClient.exists方法的13個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的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: monitor
import org.I0Itec.zkclient.ZkClient; //導入方法依賴的package包/類
private static void monitor() {
ZkClient zc=new ZkClient("localhost:2181",1000);
//創建監控節點
if(!zc.exists("/monitor"))
zc.create("/monitor",null, CreateMode.PERSISTENT);
if(!zc.exists("/monitor/client"))
zc.create("/monitor/client",null, CreateMode.PERSISTENT);
zc.subscribeChildChanges("/monitor/client",new IZkChildListener(){
@Override
public void handleChildChange(String parentPath, List<String> currentChilds) throws Exception {
System.out.println("------------客戶端發生變化---------childPath="+parentPath );
currentChilds.forEach((String childPath)->{
System.out.println("parentPath = [" + parentPath + "], currentChilds = [" + currentChilds + "]");
});
}
});
}
示例4: exists
import org.I0Itec.zkclient.ZkClient; //導入方法依賴的package包/類
public boolean exists(String cluster, String path) {
ZkClient zkClient = ZkPool.getZkClient(cluster);
try {
if (zkClient == null) {
return false;
}
return zkClient.exists(path);
} finally {
ZkPool.releaseZkClient(cluster, zkClient);
}
}
示例5: 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();
}
示例6: 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 );
}
}
示例7: readAndListen
import org.I0Itec.zkclient.ZkClient; //導入方法依賴的package包/類
public static NamePairs readAndListen(String zkUrl, String path, IZkDataListener listener) {
ZkClient client = ZkClientHolder.getZkClient(zkUrl);
if (!client.exists(path)) {
return null;
}
String data = ZkClientHolder.data2String(client.readData(path));
if (listener != null) {
client.subscribeDataChanges(path, listener);
}
return new NamePairs(data);
}
示例8: ls
import org.I0Itec.zkclient.ZkClient; //導入方法依賴的package包/類
private static void ls() {
for (int i = 0; i <20 ; i++) {
ZkClient zc = new ZkClient("127.0.0.1:2181");
if(!zc.exists("/tt"))
zc.create("/tt", "test", CreateMode.EPHEMERAL);
log.info(zc.readData("/tt"));
}
}
示例9: server
import org.I0Itec.zkclient.ZkClient; //導入方法依賴的package包/類
/**
* server端對safe接點加密
*/
private static void server() {
try {
ZkClient zc = new ZkClient("localhost:2181");
List<ACL> acls = getACL(zc);
if (zc.exists("/safe"))
zc.delete("/safe");
zc.create("/safe", "safe", acls, CreateMode.PERSISTENT);
} catch (Exception e) {
e.printStackTrace();
}
}
示例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: start
import org.I0Itec.zkclient.ZkClient; //導入方法依賴的package包/類
public synchronized void start(String name) {
List<String> list = addressMap.get(name);
if (null != list) {
return;
}
list = new ArrayList<>();
// 創建 ZooKeeper 客戶端
ZkClient zkClient = new ZkClient(zkAddress, Constant.ZK_SESSION_TIMEOUT, Constant.ZK_CONNECTION_TIMEOUT);
LOGGER.info("connect zookeeper={}", zkAddress);
// 獲取 service 節點
String servicePath = Constant.ZK_REGISTRY_PATH + "/" + name;
if (!zkClient.exists(servicePath)) {
throw new RpcFrameworkException(RpcErrorMsgConstant.FRAMEWORK_REGISTER_ERROR,
String.format("can not find any service node on path: %s", servicePath));
}
List<String> addressList = zkClient.getChildren(servicePath);
if (CollectionUtil.isEmpty(addressList)) {
throw new RpcFrameworkException(RpcErrorMsgConstant.FRAMEWORK_REGISTER_ERROR,
String.format("can not find any address node on path: %s", servicePath));
}
for (String path : addressList) {
String addressPath = servicePath + "/" + path;
String ipPort = zkClient.readData(addressPath);
list.add(ipPort);
}
addressMap.put(name, list);
}
示例12: checkPath
import org.I0Itec.zkclient.ZkClient; //導入方法依賴的package包/類
public boolean checkPath(ZkClient zkClient, String path){
boolean isExists = zkClient.exists(path);
if(!isExists){
logger.error("invalid zk path:{}", path);
return false;
}
return true;
}
示例13: 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);
}
}