Java Code Examples for org.I0Itec.zkclient.ZkClient

The following are top voted examples for showing how to use org.I0Itec.zkclient.ZkClient. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: kafka-metamorph   File: Kafka08TestContext.java   View source code 42 votes vote down vote up
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;
}
 
Example 2
Project: EatDubbo   File: ZkclientZookeeperClient.java   View source code 6 votes vote down vote up
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);
		}
	});
}
 
Example 3
Project: dubbo2   File: ZkclientZookeeperClient.java   View source code 6 votes vote down vote up
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);
		}
	});
}
 
Example 4
Project: pepper-box   File: PepperBoxSamplerTest.java   View source code 6 votes vote down vote up
@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());

}
 
Example 5
Project: pepper-box   File: PepperBoxLoadGenTest.java   View source code 6 votes vote down vote up
@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());

}
 
Example 6
Project: elephant   File: ZkClientRegisterCenter.java   View source code 6 votes vote down vote up
@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("会话建立成功!");
		}
	});
}
 
Example 7
Project: azeroth   File: KafkaMonitor.java   View source code 6 votes vote down vote up
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();
}
 
Example 8
Project: mango   File: ZookeeperRegistry.java   View source code 6 votes vote down vote up
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);
}
 
Example 9
Project: dubbox-hystrix   File: ZkclientZookeeperClient.java   View source code 6 votes vote down vote up
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);
		}
	});
}
 
Example 10
Project: kafka-0.11.0.0-src-with-comment   File: KafkaEmbedded.java   View source code 6 votes vote down vote up
/**
 * 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();
}
 
Example 11
Project: kafka-streams-machine-learning-examples   File: KafkaEmbedded.java   View source code 6 votes vote down vote up
/**
 * 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();
}
 
Example 12
Project: zookeeper-sandbox   File: DubboZkclientZookeeperClient.java   View source code 6 votes vote down vote up
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);
        }
    });
}
 
Example 13
Project: SkyEye   File: KafkaAppender.java   View source code 6 votes vote down vote up
@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();
    }
}
 
Example 14
Project: SkyEye   File: KafkaAppender.java   View source code 6 votes vote down vote up
@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();
    }
}
 
Example 15
Project: SkyEye   File: IncrementIdGen.java   View source code 6 votes vote down vote up
/**
 * 利用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;
    }
}
 
Example 16
Project: dubbocloud   File: ZkclientZookeeperClient.java   View source code 6 votes vote down vote up
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);
		}
	});
}
 
Example 17
Project: dubbos   File: ZkclientZookeeperClient.java   View source code 6 votes vote down vote up
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);
		}
	});
}
 
Example 18
Project: sumk   File: ZkClientHolder.java   View source code 6 votes vote down vote up
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;
		}
	}
}
 
Example 19
Project: dubbo-comments   File: ZkclientZookeeperClient.java   View source code 6 votes vote down vote up
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);
		}
	});
}
 
Example 20
Project: dubbox   File: ZkclientZookeeperClient.java   View source code 6 votes vote down vote up
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);
		}
	});
}
 
Example 21
Project: dubbo   File: ZkclientZookeeperClient.java   View source code 6 votes vote down vote up
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);
		}
	});
}
 
Example 22
Project: uReplicator   File: AutoTopicWhitelistingManager.java   View source code 6 votes vote down vote up
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());
}
 
Example 23
Project: uReplicator   File: KafkaBrokerTopicObserver.java   View source code 6 votes vote down vote up
public KafkaBrokerTopicObserver(String brokerClusterName, String zkString, long refreshTimeIntervalInMillis) {
  LOGGER.info("Trying to init KafkaBrokerTopicObserver {} with ZK: {}", brokerClusterName,
          zkString);
  _kakfaClusterName = brokerClusterName;
  _refreshTimeIntervalInMillis = refreshTimeIntervalInMillis;
  _zkClient = new ZkClient(zkString, 30000, 30000, ZKStringSerializer$.MODULE$);
  _zkClient.subscribeChildChanges(KAFKA_TOPICS_PATH, this);
  _zkUtils = ZkUtils.apply(_zkClient, false);
  registerMetric();
  executorService.scheduleAtFixedRate(new Runnable() {
    @Override
    public void run() {
      tryToRefreshCache();
    }
  }, 0, _refreshTimeIntervalInMillis, TimeUnit.SECONDS);
}
 
Example 24
Project: uReplicator   File: KafkaStarterUtils.java   View source code 6 votes vote down vote up
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;
}
 
Example 25
Project: javabase   File: ZclientChange.java   View source code 6 votes vote down vote up
private static void test() {
    final ZkClient zkClient = new ZkClient("localhost:2181");

    zkClient.subscribeStateChanges(new IZkStateListener() {
        @Override
        public void handleStateChanged(Watcher.Event.KeeperState state) throws Exception {
            //当zookeeper被关闭时候 会触发这个事件
            if (state == Watcher.Event.KeeperState.Disconnected) {
              log.info("Disconnected");
            } else if (state == Watcher.Event.KeeperState.SyncConnected) {
                //  当zookeeper被重新连接的时候 会触发这个事件
                log.info("SyncConnected");
            }
        }
        @Override
        public void handleNewSession() throws Exception {
            log.info("handleNewSession");
        }

        @Override
        public void handleSessionEstablishmentError(Throwable error) throws Exception {
            log.info("handleSessionEstablishmentError");
        }
    });
}
 
Example 26
Project: javabase   File: SafeZclient.java   View source code 6 votes vote down vote up
private static void client() {
	try {
		ZkClient zc = new ZkClient("localhost:2181");
		String userTwo = "gao:gao";
		zc.addAuthInfo("digest", userTwo.getBytes());

           //使用admin就可以创建
		// String userOne = "admin:admin";
		// zc.addAuthInfo("digest",userOne.getBytes());
		
		String data = zc.readData("/safe");
		log.info("只读用户 " + userTwo + "读取safe节点返回数据:" + data);
		
		zc.create("/safe2", "test", CreateMode.EPHEMERAL);
		log.info("只读用户创建节点safe2成功:" + zc.readData("/safe2"));
		
		zc.create("/safe/test", "zookeeper加密是针对节点加密的", CreateMode.EPHEMERAL);
		log.info("只读用户创建节点失败:" + zc.readData("/safe/test"));
		
	} catch (Exception e) {
		e.printStackTrace();
	}
	
}
 
Example 27
Project: javabase   File: ZclientServerPub.java   View source code 6 votes vote down vote up
private static void test() throws InterruptedException {
	final ZkClient zkClient4subChild = new ZkClient("localhost:2181");
	
	zkClient4subChild.create("/serverroot", "serverroot", Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
	
	Thread.sleep(4000);
	zkClient4subChild.create("/serverroot/server1", "server1", Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
	
	Thread.sleep(4000);
	zkClient4subChild.create("/serverroot/server2", "server2", Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
	
	Thread.sleep(20000);
	zkClient4subChild.delete("/serverroot/server1");
	zkClient4subChild.delete("/serverroot/server2");
	zkClient4subChild.delete("/serverroot");
}
 
Example 28
Project: javabase   File: ServerManagerMonitor.java   View source code 6 votes vote down vote up
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 + "]");
            });
        }
    });



}
 
Example 29
Project: Availability-Monitor-for-Kafka   File: KafkaUtils.java   View source code 6 votes vote down vote up
/**
 * Creates a Topic.
 *
 * @param topicName             Topic name.
 * @param partitions        Number of partitions for the topic.
 * @param replicationFactor Replication factor.
 * @param curatorFramework CuratorFramework.
 */
public static void createTopic(String topicName, int partitions, int replicationFactor, CuratorFramework curatorFramework) {
    if (partitions <= 0)
        throw new AdminOperationException("number of partitions must be larger than 0");

    if (replicationFactor <= 0)
        throw new AdminOperationException("replication factor must be larger than 0");

    if (!topicExists(topicName, curatorFramework)) {
        m_logger.info(String.format("Topic %s not found, creating...", topicName));
        ZkClient zkClient = fromCurator(curatorFramework);
        try {
            AdminUtils.createTopic(zkClient, topicName, partitions, replicationFactor, new Properties());
            m_logger.info("Topic created. name: {}, partitions: {}, replicationFactor: {}", topicName,
                    partitions, replicationFactor);
        } catch (TopicExistsException ignore) {
            m_logger.info("Topic exists. name: {}", topicName);
        } finally {
            if (zkClient != null) {
                zkClient.close();
            }
        }
    } else {
        m_logger.info(String.format("Topic %s found!", topicName));
    }
}
 
Example 30
Project: jeesuite-libs   File: KafkaMonitor.java   View source code 6 votes vote down vote up
public KafkaMonitor(String zkServers,String kafkaServers,int latThreshold) {
	Validate.notBlank(zkServers);
	Validate.notBlank(kafkaServers);
	this.latThreshold = latThreshold;
	
	zkClient = new ZkClient(zkServers, 10000, 10000, new ZKStringSerializer());
	
	try {			
		zkConsumerCommand = new ZkConsumerCommand(zkClient,zkServers, kafkaServers);
		kafkaConsumerCommand = new KafkaConsumerCommand(kafkaServers);
	} catch (Exception e) {
		e.printStackTrace();
	}
	// 
	initCollectionTimer();
}
 
Example 31
Project: jeesuite-libs   File: ZkClientTest.java   View source code 6 votes vote down vote up
public static void main(String[] args) throws Exception {
	ZkClient zkClient = new ZkClient("127.0.0.1:2181", 10000, 5000, new ZKStringSerializer());
	
	ExecutorService service = Executors.newFixedThreadPool(5);
	
	for (int i = 0; i < 100; i++) {
		service.execute(new Runnable() {
			@Override
			public void run() {
				String path = KafkaConst.ZK_PRODUCER_ACK_PATH + RandomStringUtils.random(5, true, true);
				zkClient.createEphemeral(path);
				System.out.println(path);
			}
		});
		if(i % 5 == 0){
			Thread.sleep(500);
			System.out.println("Sleep");
		}
	}
	
}
 
Example 32
Project: kafka-testkit   File: KafkaEmbedded.java   View source code 6 votes vote down vote up
/**
 * 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();
}
 
Example 33
Project: HeliosStreams   File: KafkaAdminClient.java   View source code 6 votes vote down vote up
/**
 * Creates and connects a new KafkaAdminClient
 * @param zkConnect The zk connect string
 * @param sessionTimeout The session timeout
 * @param connectionTimeout The connection timeout
 */
private KafkaAdminClient(final String zkConnect, final int sessionTimeout, final int connectionTimeout) {
	this.zkConnect = zkConnect;
	this.sessionTimeout = sessionTimeout;
	this.connectionTimeout = connectionTimeout;
	zkClient = new ZkClient(
			zkConnect,
			this.sessionTimeout,
			this.connectionTimeout,
		    ZKStringSerializer$.MODULE$);
	zkConnection = new ZkConnection(zkConnect, DEFAULT_SESSION_TIMEOUT);
	zkConnection.connect(this);
	zkUtils = new ZkUtils(zkClient, zkConnection, false);
	final String endPoint = findEndpoint(zkConnection);
	adminClient = endPoint==null ? null : AdminClient.createSimplePlaintext(endPoint);
	
}
 
Example 34
Project: light-task-scheduler   File: ZkClientZkClient.java   View source code 6 votes vote down vote up
public ZkClientZkClient(Config config) {
    String registryAddress = NodeRegistryUtils.getRealRegistryAddress(config.getRegistryAddress());
    zkClient = new ZkClient(registryAddress, connectionTimeout);

    zkClient.subscribeStateChanges(new IZkStateListener() {

        public void handleStateChanged(Watcher.Event.KeeperState state) throws Exception {
            ZkClientZkClient.this.state = state;
            if (state == KeeperState.Disconnected) {
                stateChanged(StateListener.DISCONNECTED);
            } else if (state == KeeperState.SyncConnected) {
                stateChanged(StateListener.CONNECTED);
            } else if (state == KeeperState.Expired) {
                stateChanged(StateListener.DISCONNECTED);
            }
        }

        public void handleNewSession() throws Exception {
            stateChanged(StateListener.RECONNECTED);
        }
    });
}
 
Example 35
Project: dubbo-learning   File: ZkclientZookeeperClient.java   View source code 6 votes vote down vote up
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);
		}
	});
}
 
Example 36
Project: DubboCode   File: ZkclientZookeeperClient.java   View source code 6 votes vote down vote up
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);
		}
	});
}
 
Example 37
Project: flink   File: KafkaTestEnvironmentImpl.java   View source code 6 votes vote down vote up
@Override
public void deleteTestTopic(String topic) {
	ZkUtils zkUtils = getZkUtils();
	try {
		LOG.info("Deleting topic {}", topic);

		ZkClient zk = new ZkClient(zookeeperConnectionString, Integer.valueOf(standardProps.getProperty("zookeeper.session.timeout.ms")),
			Integer.valueOf(standardProps.getProperty("zookeeper.connection.timeout.ms")), new ZooKeeperStringSerializer());

		AdminUtils.deleteTopic(zkUtils, topic);

		zk.close();
	} finally {
		zkUtils.close();
	}
}
 
Example 38
Project: flink   File: KafkaTestEnvironmentImpl.java   View source code 6 votes vote down vote up
@Override
public int getLeaderToShutDown(String topic) throws Exception {
	ZkClient zkClient = createZkClient();
	PartitionMetadata firstPart = null;
	do {
		if (firstPart != null) {
			LOG.info("Unable to find leader. error code {}", firstPart.errorCode());
			// not the first try. Sleep a bit
			Thread.sleep(150);
		}

		Seq<PartitionMetadata> partitionMetadata = AdminUtils.fetchTopicMetadataFromZk(topic, zkClient).partitionsMetadata();
		firstPart = partitionMetadata.head();
	}
	while (firstPart.errorCode() != 0);
	zkClient.close();

	return firstPart.leader().get().id();
}
 
Example 39
Project: flink   File: KafkaTestEnvironmentImpl.java   View source code 6 votes vote down vote up
@Override
public void deleteTestTopic(String topic) {
	ZkUtils zkUtils = getZkUtils();
	try {
		LOG.info("Deleting topic {}", topic);

		ZkClient zk = new ZkClient(zookeeperConnectionString, Integer.valueOf(standardProps.getProperty("zookeeper.session.timeout.ms")),
			Integer.valueOf(standardProps.getProperty("zookeeper.connection.timeout.ms")), new ZooKeeperStringSerializer());

		AdminUtils.deleteTopic(zkUtils, topic);

		zk.close();
	} finally {
		zkUtils.close();
	}
}
 
Example 40
Project: flink   File: KafkaTestEnvironmentImpl.java   View source code 6 votes vote down vote up
@Override
public void deleteTestTopic(String topic) {
	ZkUtils zkUtils = getZkUtils();
	try {
		LOG.info("Deleting topic {}", topic);

		ZkClient zk = new ZkClient(zookeeperConnectionString, Integer.valueOf(standardProps.getProperty("zookeeper.session.timeout.ms")),
			Integer.valueOf(standardProps.getProperty("zookeeper.connection.timeout.ms")), new ZooKeeperStringSerializer());

		AdminUtils.deleteTopic(zkUtils, topic);

		zk.close();
	} finally {
		zkUtils.close();
	}
}