设为首页 收藏本站
查看: 776|回复: 0

[经验分享] java api读取solrcloud在 zookeeper 中的配置信息

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-11-21 14:52:21 | 显示全部楼层 |阅读模式
  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+&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 {     
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(&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&#26684;式如下:
  

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、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-141892-1-1.html 上篇帖子: zookeeper 安装 windows环境 下篇帖子: zookeeper单节点安装
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表