All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alibaba.tmq.client.zookeeper.Zookeeper Maven / Gradle / Ivy

package com.alibaba.tmq.client.zookeeper;

import java.util.Collections;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.alibaba.tmq.client.context.ClientContext;
import com.alibaba.tmq.client.util.StringUtil;
import com.alibaba.tmq.common.constants.Constants;
import com.alibaba.tmq.common.exception.InitException;
import com.alibaba.tmq.common.util.ListUtil;

/**
 * Zookeeper
 * @author tianyao.myc
 *
 */
public class Zookeeper extends ClientContext implements Constants {

	private static final Log logger = LogFactory.getLog(Zookeeper.class);
	
//	private final ZkManager zkManager = new ZkManager();
	
	/**
	 * 初始化
	 *  InitException
	 */
	public void init() throws InitException {
		
		/** 初始化ZkManager */
		initZkManager();
		
	}
	
	/**
	 * 初始化ZkManager
	 *  InitException
	 */
	public void initZkManager() throws InitException {
//		ZkConfig zkConfig = new ZkConfig();
//		zkConfig.setClusterId(clientConfig.getClusterId());
//		zkConfig.setZkHostsAutoChange(clientConfig.isZkHostsAutoChange());
//		zkConfig.setZkHosts(clientConfig.getZkHosts());
//		zkConfig.setZkConnectionTimeout(clientConfig.getZkConnectionTimeout());
//		zkConfig.setZkSessionTimeout(clientConfig.getZkSessionTimeout());
//		zkManager.setZkConfig(zkConfig);
//		try {
//			zkManager.init();
//		} catch (Throwable e) {
//			throw new InitException("[Zookeeper]: initZkManager error", e);
//		}
//		
//		//设置zk地址列表
//		clientConfig.setZkHosts(zkConfig.getZkHosts());
	}

	
	public List getServerListForProducer() {
//		String serverClusterPath = PathUtil.getServerClusterPath(clientConfig.getClusterId());
//
//		List serverList = this.zkManager.getChildren(serverClusterPath);
		
		String ip = clientConfig.getLocalAddress();
		List serverList = httpService.acquireServers(clientConfig.getDomainName(), clientConfig.getClusterId(), Constants.PRODUCER_SOURCE, ip);
		
		if(ListUtil.isEmpty(serverList)) {
			return null;
		}
		
		checkIsolate(serverList);
		
		/** serverList排序 */
		Collections.sort(serverList);
		
		return serverList;
	}
	
	public List getbackupServerListForProducer() {
		
		if(StringUtil.isBlank(ClientContext.clientConfig.getBackupDomainName())) {
			return null;
		}
		
		String ip = clientConfig.getLocalAddress();
		List serverList = httpService.acquireServers(clientConfig.getBackupDomainName(), clientConfig.getBackupClusterId(), Constants.PRODUCER_SOURCE, ip);
		
		if(ListUtil.isEmpty(serverList)) {
			return null;
		}
		
		checkIsolate(serverList);
		
		/** serverList排序 */
		Collections.sort(serverList);
		
		return serverList;
	}
	
	public List getServerListForConsumer() {
//		String serverClusterPath = PathUtil.getServerClusterPath(clientConfig.getClusterId());
//
//		List serverList = this.zkManager.getChildren(serverClusterPath);
		
		String ip = clientConfig.getLocalAddress();
		List serverList = httpService.acquireServers(clientConfig.getDomainName(), clientConfig.getClusterId(), Constants.CONSUMER_SOURCE, ip);
		
		if(ListUtil.isEmpty(serverList)) {
			return null;
		}
		
		checkIsolate(serverList);
		
		/** serverList排序 */
		Collections.sort(serverList);
		
		return serverList;
	}
	
	public List getbackupServerListForConsumer() {
		
		if(StringUtil.isBlank(ClientContext.clientConfig.getBackupDomainName())) {
			return null;
		}
		
		String ip = clientConfig.getLocalAddress();
		List serverList = httpService.acquireServers(clientConfig.getBackupDomainName(), clientConfig.getBackupClusterId(), Constants.CONSUMER_SOURCE, ip);
		
		if(ListUtil.isEmpty(serverList)) {
			return null;
		}
		
		checkIsolate(serverList);
		
		/** serverList排序 */
		Collections.sort(serverList);
		
		return serverList;
	}
	
	private void checkIsolate(List serverList) {
		if (serverList.size() == 1 && serverList.get(0).equals(FAKE_SERVER)) {
			if (!clientRemoting.isIsolated()) {
				logger.warn("[Zookeeper]: receive fake server, isolated!!!");
				clientRemoting.setIsolated(true);
			}
			serverList.clear();
		} else {
			if (clientRemoting.isIsolated()) {
				logger.warn("[Zookeeper]: receive normal server and we are isolated before, remove isolation!!!");
				clientRemoting.setIsolated(false);
			}
		}
	}

//	public ZkManager getZkManager() {
//		return zkManager;
//	}
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy