Java Code Examples for com.jeesuite.common.util.NodeNameHolder#getNodeId()

The following examples show how to use com.jeesuite.common.util.NodeNameHolder#getNodeId() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SendCounterHandler.java    From jeesuite-libs with Apache License 2.0 6 votes vote down vote up
private void updateProducerStat(String topic, boolean error) {
	if (!producerStats.containsKey(topic)) {
		synchronized (producerStats) {
			String path = groupPath + "/" + topic;
			if(!zkClient.exists(path)){
				zkClient.createPersistent(path, true);
			}
			//节点临时目录
			path = path + "/" + NodeNameHolder.getNodeId();
			zkClient.createEphemeral(path);
			statPaths.put(topic, path);
			producerStats.put(topic, new AtomicLong[] { new AtomicLong(0), new AtomicLong(0), new AtomicLong(0), new AtomicLong(0) });
		}
	}
	if (!error) {
		producerStats.get(topic)[0].incrementAndGet();
		producerStats.get(topic)[2].incrementAndGet();
	} else {
		producerStats.get(topic)[1].incrementAndGet();
		producerStats.get(topic)[3].incrementAndGet();
	}
	
	commited.set(false);
}
 
Example 2
Source File: SnowflakeGenerator.java    From jeesuite-libs with Apache License 2.0 5 votes vote down vote up
/**
 * 需要zookeeper保存节点信息
 */
public SnowflakeGenerator() {
	try {
		String appName = ResourceUtils.getProperty("spring.application.name", ResourceUtils.getProperty("jeesuite.configcenter.appName"));
		Validate.notBlank(appName, "config[spring.application.name] not found");
		String zkServer = ResourceUtils.getAndValidateProperty("zookeeper.servers");
		
		zk = new ZooKeeper(zkServer, 10000, this);
		String path = String.format(ROOT_PATH, appName);
		
		String[] parts = StringUtils.split(path, "/");
		String tmpParent = "";
		Stat stat;
		for (int i = 0; i < parts.length; i++) {
			tmpParent = tmpParent + "/" + parts[i];
			stat = zk.exists(tmpParent, false);
			if (stat == null) {
				zk.create(tmpParent, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
			}
		}
		String nodePath = path + "/" + NodeNameHolder.getNodeId();
		zk.create(nodePath, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
		int workerId = zk.getChildren(path, false).size();
		if (workerId > maxWorkerId || workerId < 0) {
			throw new IllegalArgumentException(
					String.format("worker Id can't be greater than %d or less than 0", maxWorkerId));
		}
		this.workerId = workerId;
	} catch (Exception e) {
		this.workerId = RandomUtils.nextInt(1, 31);
	}
	this.datacenterId = 1;
}
 
Example 3
Source File: TopicConsumerSpringProvider.java    From jeesuite-libs with Apache License 2.0 4 votes vote down vote up
@Override
  public void afterPropertiesSet() throws Exception {

if(StringUtils.isNotBlank(scanPackages)){
	String[] packages = org.springframework.util.StringUtils.tokenizeToStringArray(this.scanPackages, ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
	scanAndRegisterAnnotationTopics(packages);
}

Validate.isTrue(topicHandlers != null && topicHandlers.size() > 0, "at latest one topic");
//当前状态
if(status.get() > 0)return;

routeEnv = StringUtils.trimToNull(ResourceUtils.getProperty(KafkaConst.PROP_ENV_ROUTE));

if(routeEnv != null){
	logger.info("current route Env value is:",routeEnv);
	Map<String, MessageHandler> newTopicHandlers = new HashMap<>();
	for (String origTopicName : topicHandlers.keySet()) {
		newTopicHandlers.put(routeEnv + "." + origTopicName, topicHandlers.get(origTopicName));
	}
	topicHandlers = newTopicHandlers;
}

//make sure that rebalance.max.retries * rebalance.backoff.ms > zookeeper.session.timeout.ms.
configs.put("rebalance.max.retries", "5");  
configs.put("rebalance.backoff.ms", "1205"); 
configs.put("zookeeper.session.timeout.ms", "6000"); 

configs.put("key.deserializer",StringDeserializer.class.getName());  

if(!configs.containsKey("value.deserializer")){
      	configs.put("value.deserializer", KyroMessageDeserializer.class.getName());
      }

if(useNewAPI){
	if("smallest".equals(configs.getProperty("auto.offset.reset"))){
		configs.put("auto.offset.reset", "earliest");
	}else if("largest".equals(configs.getProperty("auto.offset.reset"))){
		configs.put("auto.offset.reset", "latest");
	}
}else{			
	//强制自动提交
	configs.put("enable.auto.commit", "true");
}

//同步节点信息
groupId = configs.get(org.apache.kafka.clients.consumer.ConsumerConfig.GROUP_ID_CONFIG).toString();

logger.info("\n===============KAFKA Consumer group[{}] begin start=================\n",groupId);

consumerId = NodeNameHolder.getNodeId();
//
configs.put("consumer.id", consumerId);

//kafka 内部处理 consumerId = groupId + "_" + consumerId
consumerId = groupId + "_" + consumerId;
//
if(!configs.containsKey("client.id")){
	configs.put("client.id", consumerId);
}
//
  	start();
  	
  	logger.info("\n===============KAFKA Consumer group[{}],consumerId[{}] start finished!!=================\n",groupId,consumerId);
  }
 
Example 4
Source File: JobContext.java    From jeesuite-libs with Apache License 2.0 4 votes vote down vote up
public String getNodeId() {
	return NodeNameHolder.getNodeId();
}