本文整理匯總了Java中org.I0Itec.zkclient.ZkClient類的典型用法代碼示例。如果您正苦於以下問題:Java ZkClient類的具體用法?Java ZkClient怎麽用?Java ZkClient使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
ZkClient類屬於org.I0Itec.zkclient包,在下文中一共展示了ZkClient類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: initialize
import org.I0Itec.zkclient.ZkClient; //導入依賴的package包/類
public void initialize() {
if (initialized) {
throw new IllegalStateException("Context has been already initialized");
}
zkServer = new EmbeddedZookeeper(TestZKUtils.zookeeperConnect());
zkClient = new ZkClient(zkServer.connectString(), 10000, 10000, ZKStringSerializer$.MODULE$);
port = TestUtils.choosePort();
KafkaConfig config = new KafkaConfig(TestUtils.createBrokerConfig(brokerId, port, true));
Time mock = new MockTime();
kafkaServer = new KafkaServer(config, mock);
kafkaServer.startup();
initialized = true;
}
示例2: ZkclientZookeeperClient
import org.I0Itec.zkclient.ZkClient; //導入依賴的package包/類
public ZkclientZookeeperClient(URL url) {
super(url);
client = new ZkClient(url.getBackupAddress());
client.subscribeStateChanges(new IZkStateListener() {
public void handleStateChanged(KeeperState state) throws Exception {
ZkclientZookeeperClient.this.state = state;
if (state == KeeperState.Disconnected) {
stateChanged(StateListener.DISCONNECTED);
} else if (state == KeeperState.SyncConnected) {
stateChanged(StateListener.CONNECTED);
}
}
public void handleNewSession() throws Exception {
stateChanged(StateListener.RECONNECTED);
}
});
}
示例3: ZkclientZookeeperClient
import org.I0Itec.zkclient.ZkClient; //導入依賴的package包/類
public ZkclientZookeeperClient(URL url) {
super(url);
client = new ZkClient(
url.getBackupAddress(),
url.getParameter(Constants.SESSION_TIMEOUT_KEY, Constants.DEFAULT_SESSION_TIMEOUT),
url.getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_REGISTRY_CONNECT_TIMEOUT));
client.subscribeStateChanges(new IZkStateListener() {
public void handleStateChanged(KeeperState state) throws Exception {
ZkclientZookeeperClient.this.state = state;
if (state == KeeperState.Disconnected) {
stateChanged(StateListener.DISCONNECTED);
} else if (state == KeeperState.SyncConnected) {
stateChanged(StateListener.CONNECTED);
}
}
public void handleNewSession() throws Exception {
stateChanged(StateListener.RECONNECTED);
}
});
}
示例4: setup
import org.I0Itec.zkclient.ZkClient; //導入依賴的package包/類
@Before
public void setup() throws IOException {
zkServer = new EmbeddedZookeeper();
String zkConnect = ZKHOST + ":" + zkServer.port();
zkClient = new ZkClient(zkConnect, 30000, 30000, ZKStringSerializer$.MODULE$);
ZkUtils zkUtils = ZkUtils.apply(zkClient, false);
Properties brokerProps = new Properties();
brokerProps.setProperty("zookeeper.connect", zkConnect);
brokerProps.setProperty("broker.id", "0");
brokerProps.setProperty("log.dirs", Files.createTempDirectory("kafka-").toAbsolutePath().toString());
brokerProps.setProperty("listeners", "PLAINTEXT://" + BROKERHOST +":" + BROKERPORT);
KafkaConfig config = new KafkaConfig(brokerProps);
Time mock = new MockTime();
kafkaServer = TestUtils.createServer(config, mock);
//AdminUtils.createTopic(zkUtils, TOPIC, 1, 1, new Properties(), RackAwareMode.Disabled$.MODULE$);
JMeterContext jmcx = JMeterContextService.getContext();
jmcx.setVariables(new JMeterVariables());
}
示例5: init
import org.I0Itec.zkclient.ZkClient; //導入依賴的package包/類
@Override
public void init() {
this.zkClient = new ZkClient(this.zkAddress, this.zkSessionTimeOut, this.zkConnectionTimeOut, new SerializableSerializer());
initRootPath();
this.zkClient.subscribeStateChanges(new IZkStateListener() {
@Override
public void handleStateChanged(KeeperState state) throws Exception {
if(zkReconnectionListener != null && state.name().equals(KeeperState.SyncConnected.name())){
zkReconnectionListener.handleStateForSyncConnected();
}
}
@Override
public void handleSessionEstablishmentError(Throwable error)throws Exception {
log.error("處理會話建立錯誤:{}",error);
}
@Override
public void handleNewSession() throws Exception {
log.info("會話建立成功!");
}
});
}
示例6: KafkaMonitor
import org.I0Itec.zkclient.ZkClient; //導入依賴的package包/類
public KafkaMonitor(String zkServers, String kafkaServers, int latThreshold) {
Validate.notBlank(zkServers);
Validate.notBlank(kafkaServers);
this.latThreshold = latThreshold;
zkClient = new ZkClient(zkServers, 10000, 10000, ZKStringSerializer$.MODULE$);
try {
zkConsumerCommand = new ZkConsumerCommand(zkClient, zkServers, kafkaServers);
kafkaConsumerCommand = new KafkaConsumerCommand(kafkaServers);
} catch (Exception e) {
e.printStackTrace();
}
//
initCollectionTimer();
}
示例7: ZookeeperRegistry
import org.I0Itec.zkclient.ZkClient; //導入依賴的package包/類
public ZookeeperRegistry(URL url, ZkClient zkClient) {
super(url);
this.zkClient = zkClient;
IZkStateListener zkStateListener = new IZkStateListener() {
@Override
public void handleStateChanged(Watcher.Event.KeeperState state) throws Exception {
// do nothing
}
@Override
public void handleNewSession() throws Exception {
logger.info("zkRegistry get new session notify.");
}
@Override
public void handleSessionEstablishmentError(Throwable throwable) throws Exception {
}
};
this.zkClient.subscribeStateChanges(zkStateListener);
}
示例8: createTopic
import org.I0Itec.zkclient.ZkClient; //導入依賴的package包/類
/**
* Create a Kafka topic with the given parameters.
*
* @param topic The name of the topic.
* @param partitions The number of partitions for this topic.
* @param replication The replication factor for (partitions of) this topic.
* @param topicConfig Additional topic-level configuration settings.
*/
public void createTopic(final String topic,
final int partitions,
final int replication,
final Properties topicConfig) {
log.debug("Creating topic { name: {}, partitions: {}, replication: {}, config: {} }",
topic, partitions, replication, topicConfig);
// Note: You must initialize the ZkClient with ZKStringSerializer. If you don't, then
// createTopic() will only seem to work (it will return without error). The topic will exist in
// only ZooKeeper and will be returned when listing topics, but Kafka itself does not create the
// topic.
final ZkClient zkClient = new ZkClient(
zookeeperConnect(),
DEFAULT_ZK_SESSION_TIMEOUT_MS,
DEFAULT_ZK_CONNECTION_TIMEOUT_MS,
ZKStringSerializer$.MODULE$);
final boolean isSecure = false;
final ZkUtils zkUtils = new ZkUtils(zkClient, new ZkConnection(zookeeperConnect()), isSecure);
AdminUtils.createTopic(zkUtils, topic, partitions, replication, topicConfig, RackAwareMode.Enforced$.MODULE$);
zkClient.close();
}
示例9: createTopic
import org.I0Itec.zkclient.ZkClient; //導入依賴的package包/類
/**
* Create a Kafka topic with the given parameters.
*
* @param topic The name of the topic.
* @param partitions The number of partitions for this topic.
* @param replication The replication factor for (partitions of) this topic.
* @param topicConfig Additional topic-level configuration settings.
*/
public void createTopic(String topic,
int partitions,
int replication,
Properties topicConfig) {
log.debug("Creating topic { name: {}, partitions: {}, replication: {}, config: {} }",
topic, partitions, replication, topicConfig);
// Note: You must initialize the ZkClient with ZKStringSerializer. If you don't, then
// createTopic() will only seem to work (it will return without error). The topic will exist in
// only ZooKeeper and will be returned when listing topics, but Kafka itself does not create the
// topic.
ZkClient zkClient = new ZkClient(
zookeeperConnect(),
DEFAULT_ZK_SESSION_TIMEOUT_MS,
DEFAULT_ZK_CONNECTION_TIMEOUT_MS,
ZKStringSerializer$.MODULE$);
boolean isSecure = false;
ZkUtils zkUtils = new ZkUtils(zkClient, new ZkConnection(zookeeperConnect()), isSecure);
AdminUtils.createTopic(zkUtils, topic, partitions, replication, topicConfig, RackAwareMode.Enforced$.MODULE$);
zkClient.close();
}
示例10: DubboZkclientZookeeperClient
import org.I0Itec.zkclient.ZkClient; //導入依賴的package包/類
public DubboZkclientZookeeperClient(URL url) {
super(url);
client = new ZkClient(url.getBackupAddress());
client.subscribeStateChanges(new IZkStateListener() {
public void handleStateChanged(Watcher.Event.KeeperState keeperState) throws Exception {
DubboZkclientZookeeperClient.this.state = state;
if(state == Watcher.Event.KeeperState.Disconnected) {
stateChanged(StateListener.DISCONNECTED);
} else if(state == Watcher.Event.KeeperState.SyncConnected) {
stateChanged(StateListener.CONNECTED);
}
}
public void handleNewSession() throws Exception {
stateChanged(StateListener.RECONNECTED);
}
});
}
示例11: close
import org.I0Itec.zkclient.ZkClient; //導入依賴的package包/類
@Override
public void close() {
closed = true;
// 停止心跳
this.heartbeatStop();
// 關閉KafkaProuder
if (LazySingletonProducer.isInstanced()) {
// producer實際上已經初始化
LazySingletonProducer.getInstance(this.config).close();
}
// 關閉client,臨時節點消失,監控係統進行感知報警
ZkClient client = this.zkRegister == null ? null : this.zkRegister.getClient();
if (null != client) {
client.close();
}
}
示例12: stop
import org.I0Itec.zkclient.ZkClient; //導入依賴的package包/類
@Override
public void stop() {
super.stop();
// 停止心跳
this.heartbeatStop();
// 關閉KafkaProuder
if (LazySingletonProducer.isInstanced()) {
// producer實際上已經初始化
LazySingletonProducer.getInstance(this.config).close();
}
// 關閉client,臨時節點消失,監控係統進行感知報警
ZkClient client = this.zkRegister == null ? null : this.zkRegister.getClient();
if (null != client) {
client.close();
}
}
示例13: 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;
}
}
示例14: 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;
}
}
}
示例15: AutoTopicWhitelistingManager
import org.I0Itec.zkclient.ZkClient; //導入依賴的package包/類
public AutoTopicWhitelistingManager(KafkaBrokerTopicObserver srcKafkaTopicObserver,
KafkaBrokerTopicObserver destKafkaTopicObserver,
HelixMirrorMakerManager helixMirrorMakerManager,
String patternToExcludeTopics,
int refreshTimeInSec,
int initWaitTimeInSec) {
_srcKafkaTopicObserver = srcKafkaTopicObserver;
_destKafkaTopicObserver = destKafkaTopicObserver;
_helixMirrorMakerManager = helixMirrorMakerManager;
_patternToExcludeTopics = patternToExcludeTopics;
_refreshTimeInSec = refreshTimeInSec;
_initWaitTimeInSec = initWaitTimeInSec;
_zkClient = new ZkClient(_helixMirrorMakerManager.getHelixZkURL(), 30000, 30000, ZKStringSerializer$.MODULE$);
_zkUtils = ZkUtils.apply(_zkClient, false);
_blacklistedTopicsZPath = String.format("/%s/BLACKLISTED_TOPICS", _helixMirrorMakerManager.getHelixClusterName());
}