tile 发表于 2015-11-21 14:52:21

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

  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 staticint 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+&quot;中没有节点&quot;);
}else{
logger.info(path+&quot;中存在节点&quot;);
for(String child:list){
System.out.println(&quot;节点为:&quot;+child);
}
}
}catch (KeeperException.NoNodeException e) {
// TODO: handle exception
logger.error(&quot;get child error&quot;+e.getMessage());
}
}
public byte[] getData(String path) throws KeeperException, InterruptedException {   
returnthis.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(&quot;The URL of zkHost is not correct!! Its form must as below:\n zkHost:port&quot;
+ 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(&quot;/clusterstate.json&quot;),&quot;utf-8&quot;));
JSONObject jsonIttsb2b_policy=JSON.parseObject(jsonclusterstate.get(SolrCloudCollection.trim()).toString());
JSONObject jsonIttsb2b_policyCont=JSON.parseObject(jsonIttsb2b_policy.toString());
// System.out.println(jsonIttsb2b_policyCont.get(&quot;maxShardsPerNode&quot;));
int maxShards=Integer.valueOf(jsonIttsb2b_policyCont.get(&quot;maxShardsPerNode&quot;).toString());      
int replicafactor=Integer.valueOf( jsonIttsb2b_policy.get(&quot;replicationFactor&quot;).toString());
JSONObject jsonShardsCont=JSON.parseObject(jsonIttsb2b_policyCont.get(&quot;shards&quot;).toString());      
for(int i=1;i<=maxShards;i++)
{
JSONObject jsonSharditem=JSON.parseObject(jsonShardsCont.get(&quot;shard&quot;+i).toString());
JSONObject jsonReplica=JSON.parseObject(jsonSharditem.get(&quot;replicas&quot;).toString());      
for(int j=1;j<=replicafactor;j++)
{
JSONObject jsonCoreItem=JSON.parseObject(jsonReplica.get(&quot;core_node&quot;+j).toString());
this.solrCoreList.add(jsonCoreItem.get(&quot;base_url&quot;)+&quot;/&quot;+jsonCoreItem.get(&quot;core&quot;));      
}      
}
return this.getSolrCoreList();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
logger.info(&quot;UnsupportedEncodingException&quot;+e.getMessage());
} catch (KeeperException e) {
// TODO Auto-generated catch block
logger.info(&quot;KeeperException&quot;+e.getMessage());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
logger.info(&quot;InterruptedException&quot;+e.getMessage());
}
logger.info(&quot;there no zkNode find&quot;+System.currentTimeMillis());
return null;
}
//public static void main(String[] args)throws Exception{
//ZookeeperUtil zkoperator= new ZookeeperUtil();   
//         zkoperator.connect(&quot;zk.dev.corp.qunar.com:2181/ittsb2b_oms_policy_solr&quot;);
//         JSONObject jsonclusterstate=JSON.parseObject(new String(zkoperator.getData(&quot;/clusterstate.json&quot;),&quot;utf-8&quot;));
//         JSONObject jsonIttsb2b_policy=JSON.parseObject(jsonclusterstate.get(&quot;ittsb2b_policy&quot;).toString());
//         JSONObject jsonIttsb2b_policyCont=JSON.parseObject(jsonIttsb2b_policy.toString());
//         System.out.println(jsonIttsb2b_policyCont.get(&quot;maxShardsPerNode&quot;));
//         int maxShards=Integer.valueOf(jsonIttsb2b_policyCont.get(&quot;maxShardsPerNode&quot;).toString());      
//         int replicafactor=Integer.valueOf( jsonIttsb2b_policy.get(&quot;replicationFactor&quot;).toString());
//         JSONObject jsonShardsCont=JSON.parseObject(jsonIttsb2b_policyCont.get(&quot;shards&quot;).toString());
//         System.out.println(jsonShardsCont.get(&quot;shard1&quot;));
//         for(int i=1;i<=maxShards;i++)
//         {
//         JSONObject jsonSharditem=JSON.parseObject(jsonShardsCont.get(&quot;shard&quot;+i).toString());
//         JSONObject jsonReplica=JSON.parseObject(jsonSharditem.get(&quot;replicas&quot;).toString());
//         System.out.println(jsonReplica);
//         for(int j=1;j<=replicafactor;j++)
//         {
//         JSONObject jsonCoreItem=JSON.parseObject(jsonReplica.get(&quot;core_node&quot;+j).toString());
//         zkoperator.solrCoreList.add(jsonCoreItem.get(&quot;base_url&quot;)+&quot;/&quot;+jsonCoreItem.get(&quot;core&quot;));
//      /* System.out.println(jsonCoreItem.get(&quot;base_url&quot;));
//         System.out.println(jsonCoreItem.get(&quot;leader&quot;));
//         System.out.println(jsonCoreItem.get(&quot;state&quot;));
//         System.out.println(jsonCoreItem.get(&quot;core&quot;));*/
//         }
//      
//      
//         }
//       //System.out.println(jsonShards.get(&quot;replicationFactor&quot;));
////         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
页: [1]
查看完整版本: java api读取solrcloud在 zookeeper 中的配置信息