java api读取solrcloud在 zookeeper 中的配置信息

json格式如下:

{"ittsb2b_policy":{
    "shards":{"shard1":{
        "range":"80000000-7fffffff",
        "state":"active",
        "replicas":{
          "core_node1":{
            "state":"active",
            "core":"ittsb2b_policy_shard1_replica1",
            "node_name":"10.86.40.109:20010_",
            "base_url":"http://10.86.40.109:20010"},
          "core_node2":{
            "state":"active",
            "core":"ittsb2b_policy_shard1_replica4",
            "node_name":"10.86.40.112:20010_",
            "base_url":"http://10.86.40.112:20010",
            "leader":"true"},
          "core_node3":{
            "state":"active",
            "core":"ittsb2b_policy_shard1_replica3",
            "node_name":"10.86.40.110:20010_",
            "base_url":"http://10.86.40.110:20010"},
          "core_node4":{
            "state":"active",
            "core":"ittsb2b_policy_shard1_replica2",
            "node_name":"10.86.40.111:20010_",
            "base_url":"http://10.86.40.111:20010"}}}},
    "maxShardsPerNode":"1",
    "router":{"name":"compositeId"},
    "replicationFactor":"4",
    "autoAddReplicas":"false"}}
package com.qunar.flight.ib2b.policy.search.util;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooKeeper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

@Service
public class ZookeeperUtil implements Watcher {
	private static Logger logger = LoggerFactory
			.getLogger(ZookeeperUtil.class);
	//每个replica的列表信息格式如下:
	//http://10.86.40.109:20010/ittsb2b_policy_shard1_replica1
	//http://10.86.40.112:20010/ittsb2b_policy_shard1_replica4
	//	http://10.86.40.110:20010/ittsb2b_policy_shard1_replica3
	//	http://10.86.40.111:20010/ittsb2b_policy_shard1_replica2
	private List<String> solrCoreList=new ArrayList<String>();
	
	public List<String> getSolrCoreList() {
		return solrCoreList;
	}


	public void setSolrCoreList(List<String> solrCoreList) {
		this.solrCoreList = solrCoreList;
	}
	//缓存时间  
    private static  int zkSolrCloudConnectTimeout ;     
    private static String zkSolrCloudAdds;
    protected ZooKeeper zooKeeper;  
    private String SolrCloudCollection;
    private long currenTimestamp;
    public long getCurrenTimestamp() {
		return currenTimestamp;
	}


	public void setCurrenTimestamp(long currenTimestamp) {
		this.currenTimestamp = currenTimestamp;
	}


	public String getSolrCloudCollection() {
		return SolrCloudCollection;
	}


	public static int getZkSolrCloudConnectTimeout() {
		return zkSolrCloudConnectTimeout;
	}


	public static void setZkSolrCloudConnectTimeout(int zkSolrCloudConnectTimeout) {
		ZookeeperUtil.zkSolrCloudConnectTimeout = zkSolrCloudConnectTimeout;
	}


	public static String getZkSolrCloudAdds() {
		return zkSolrCloudAdds;
	}


	public static void setZkSolrCloudAdds(String zkSolrCloudAdds) {
		ZookeeperUtil.zkSolrCloudAdds = zkSolrCloudAdds;
	}


	public void setSolrCloudCollection(String solrCloudCollection) {
		SolrCloudCollection = solrCloudCollection;
	}


	public static int getSessionTimeout() {
		return zkSolrCloudConnectTimeout;
	}


	public static void setSessionTimeout(int sessionTimeout) {
		ZookeeperUtil.zkSolrCloudConnectTimeout = sessionTimeout;
	}


	public static String getHosts() {
		return zkSolrCloudAdds;
	}


	public static void setHosts(String hosts) {
		ZookeeperUtil.zkSolrCloudAdds = hosts;
	}


	public ZooKeeper getZooKeeper() {
		return zooKeeper;
	}


	public void setZooKeeper(ZooKeeper zooKeeper) {
		this.zooKeeper = zooKeeper;
	}
	//protected CountDownLatch countDownLatch=new CountDownLatch(1);  
 
    public void connect() throws IOException, InterruptedException{     
           zooKeeper = new ZooKeeper(zkSolrCloudAdds,zkSolrCloudConnectTimeout,this);     
           this.currenTimestamp=System.currentTimeMillis();
       //    countDownLatch.await();     
     }     
	
    public void getChild(String path) throws KeeperException, InterruptedException{     
        try{  
            List<String> list=this.zooKeeper.getChildren(path, false);  
            if(list.isEmpty()){  
            	logger.info(path+"中没有节点");  
            }else{  
            	logger.info(path+"中存在节点");  
                for(String child:list){  
                	System.out.println("节点为:"+child);  
                }  
            }  
        }catch (KeeperException.NoNodeException e) {  
            // TODO: handle exception  
            logger.error("get child error"+e.getMessage());
  
        }  
    }
    public byte[] getData(String path) throws KeeperException, InterruptedException {     
        return  this.zooKeeper.getData(path, false,null);     
    }   
    public void close() throws InterruptedException{     
        zooKeeper.close();     
    }    
	@Override
	public void process(WatchedEvent event) {
		/*// TODO Auto-generated method stub
		 if(event.getState()==KeeperState.SyncConnected){  
	            countDownLatch.countDown();  
	        }  */
	} 
	private synchronized void init() {
		if (zooKeeper == null) {
			try {
				this.connect();				
			} catch (Exception e) {
				logger.error("The URL of zkHost is not correct!! Its form must as below:\n zkHost:port"
						+ e.getMessage());
			}
		}
	}	
	/**
	 * 获取replication列表信息
	 * @return
	 */
	public List<String> getCoreListFromZK()
	{
		if(zooKeeper==null)
		{
			this.init();
		}
		this.setCurrenTimestamp(System.currentTimeMillis());
		 try {
			 JSONObject jsonclusterstate=JSON.parseObject(new String(this.getData("/clusterstate.json"),"utf-8"));
	         JSONObject jsonIttsb2b_policy=JSON.parseObject(jsonclusterstate.get(SolrCloudCollection.trim()).toString()); 
	         JSONObject jsonIttsb2b_policyCont=JSON.parseObject(jsonIttsb2b_policy.toString());
	        // System.out.println(jsonIttsb2b_policyCont.get("maxShardsPerNode"));
	         int maxShards=Integer.valueOf(jsonIttsb2b_policyCont.get("maxShardsPerNode").toString());       
	         int replicafactor=Integer.valueOf( jsonIttsb2b_policy.get("replicationFactor").toString());
	         JSONObject jsonShardsCont=JSON.parseObject(jsonIttsb2b_policyCont.get("shards").toString());	       
	         for(int i=1;i<=maxShards;i++)
	         {
	        	 JSONObject jsonSharditem=JSON.parseObject(jsonShardsCont.get("shard"+i).toString());
	        	 JSONObject jsonReplica=JSON.parseObject(jsonSharditem.get("replicas").toString());	        	
	        	 for(int j=1;j<=replicafactor;j++)
	        	 {
	        		 JSONObject jsonCoreItem=JSON.parseObject(jsonReplica.get("core_node"+j).toString());
	        		 this.solrCoreList.add(jsonCoreItem.get("base_url")+"/"+jsonCoreItem.get("core"));	        		
	        	 }	        	
	         }
	         return this.getSolrCoreList();			
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			 logger.info("UnsupportedEncodingException"+e.getMessage());
		} catch (KeeperException e) {
			// TODO Auto-generated catch block
			logger.info("KeeperException"+e.getMessage());
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			logger.info("InterruptedException"+e.getMessage());
		}
		 logger.info("there no zkNode find"+System.currentTimeMillis());
		 return null;
	}
//	public static void main(String[] args)  throws Exception{
//		ZookeeperUtil zkoperator  = new ZookeeperUtil();     
//         zkoperator.connect("zk.dev.corp.qunar.com:2181/ittsb2b_oms_policy_solr");  
//         JSONObject jsonclusterstate=JSON.parseObject(new String(zkoperator.getData("/clusterstate.json"),"utf-8"));
//         JSONObject jsonIttsb2b_policy=JSON.parseObject(jsonclusterstate.get("ittsb2b_policy").toString()); 
//         JSONObject jsonIttsb2b_policyCont=JSON.parseObject(jsonIttsb2b_policy.toString());
//         System.out.println(jsonIttsb2b_policyCont.get("maxShardsPerNode"));
//         int maxShards=Integer.valueOf(jsonIttsb2b_policyCont.get("maxShardsPerNode").toString());       
//         int replicafactor=Integer.valueOf( jsonIttsb2b_policy.get("replicationFactor").toString());
//         JSONObject jsonShardsCont=JSON.parseObject(jsonIttsb2b_policyCont.get("shards").toString());
//         System.out.println(jsonShardsCont.get("shard1"));
//         for(int i=1;i<=maxShards;i++)
//         {
//        	 JSONObject jsonSharditem=JSON.parseObject(jsonShardsCont.get("shard"+i).toString());
//        	 JSONObject jsonReplica=JSON.parseObject(jsonSharditem.get("replicas").toString());
//        	 System.out.println(jsonReplica);
//        	 for(int j=1;j<=replicafactor;j++)
//        	 {
//        		 JSONObject jsonCoreItem=JSON.parseObject(jsonReplica.get("core_node"+j).toString());
//        		 zkoperator.solrCoreList.add(jsonCoreItem.get("base_url")+"/"+jsonCoreItem.get("core"));
//        		/* System.out.println(jsonCoreItem.get("base_url"));
//        		 System.out.println(jsonCoreItem.get("leader"));
//        		 System.out.println(jsonCoreItem.get("state"));
//        		 System.out.println(jsonCoreItem.get("core"));*/
//        	 }
//        	
//        	
//         }
//       //  System.out.println(jsonShards.get("replicationFactor"));
         int replicaFactor=jsonIttsb2b_policy.get
         System.out.println(replicaFactor);
//        // JSONObject json
//	}
}

如果想直接模拟使用那么取消main中的注释即可直接进行使用

返回的solrCoreList格式如下:


http://10.86.40.109:20010/ittsb2b_policy_shard1_replica1
http://10.86.40.112:20010/ittsb2b_policy_shard1_replica4
http://10.86.40.110:20010/ittsb2b_policy_shard1_replica3
http://10.86.40.111:20010/ittsb2b_policy_shard1_replica2

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐