本文整理汇总了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());
}