标签:
http://www.mamicode.com/info-detail-892923.html
Solr是一个独立的企业级搜索应用服务器,它对外提供类似于Web-service的API接口。用户可以通过http请求,向搜索引擎服务器提交一定格式的XML文件,生成索引;也可以通过Http Get操作提出查找请求,并得到XML格式的返回结果。
SolrCloud是Solr4.0版本以后基于Solr和Zookeeper的分布式搜索方案,它的主要思想是使用Zookeeper作为集群的配置信息中心。它有以下几个特点:
更多关于SolrCloud的相关介绍可参考以下链接:
准备三台服务器:
scp /etc/hosts solr-cloud-slave1:/etc/
scp /ect/hosts solr-cloud-slave2:/etc/
service iptables stop
chkconfig iptables off
source /etc/profile.d/jdk_env.sh
scp -r /usr/local/lib/jdk solr-cloud-slave1:/usr/local/lib/
scp /etc/profile.d/jdk_env.sh solr-cloud-slave1:/ect/profile.d/
source /etc/profile.d/jdk_env.sh
scp -r /usr/local/lib/jdk solr-cloud-slave2:/usr/local/lib/
scp /ect/profile.d/jdk_env.sh solr-cloud-slave2:/etc/profile.d/
source /etc/profile.d/jdk_env.sh
cd /opt/SolrCloud/Zookeeper/zookeeper-3.4.6/conf
cp zoo_sample.cfg zoo.cfg
scp -r /opt/SolrCloud/Zookeeper/zookeeper-3.4.6/ solr-cloud-slave1:/opt/SolrCloud/Zookeeper/
scp -r /apps/data/zookeeper/ solr-cloud-slave1:/apps/data/
scp -r /opt/SolrCloud/Zookeeper/zookeeper-3.4.6/ solr-cloud-slave2:/opt/SolrCloud/Zookeeper/
scp -r /apps/data/zookeeper/ solr-cloud-slave2:/apps/data/
cd /opt/SolrCloud/Zookeeper/zookeeper-3.4.6/bin/
./zkServer.sh start
cp /home/software/solr-5.2.1/server/lib/ext/*.jar /opt/SolrCloud/Solr/solr/WEB-INF/lib/
scp /apps/data/solr-cores/solr.xml solr-cloud-slave1:/apps/data/solr-cores/
scp /apps/data/solr-cores/solr.xml solr-cloud-slave2:/apps/data/solr-cores/
mkdir -p /opt/SolrCloud/Tomcat/apache-tomcat-7.0.62/conf/Catalina/localhost
<?xml version="1.0" encoding="UTF-8"?>
<Context docBase="/opt/SolrCloud/Tomcat/apache-tomcat-7.0.62/webapps/solr" debug="0" crossContext="true">
<Environment name="solr/home" type="java.lang.String" value="/apps/data/solr-cores/" override="true"/>
</Context>
修改 /opt/SolrCloud/Tomcat/apache-tomcat-7.0.62/bin/catalina.sh 加入以下内容:
scp -r /opt/SolrCloud/Tomcat/apache-tomcat-7.0.62/ solr-cloud-slave1:/opt/SolrCloud/Tomcat/
scp -r /opt/SolrCloud/Solr/ solr-cloud-slave1:/opt/SolrCloud/
scp -r /apps/data/solr-cores/ solr-cloud-slave1:/apps/data/
scp -r /opt/SolrCloud/Tomcat/apache-tomcat-7.0.62/ solr-cloud-slave2:/opt/SolrCloud/Tomcat/
scp -r /opt/SolrCloud/Solr/ solr-cloud-slave2:/opt/SolrCloud/
scp -r /apps/data/solr-cores/ solr-cloud-slave2:/apps/data/
java -classpath .:/opt/SolrCloud/Tomcat/apache-tomcat-7.0.62/webapps/solr/WEB-INF/lib/* org.apache.solr.cloud.ZkCLI -cmd upconfig -zkhost solr-cloud-master:2181,solr-cloud-slave1:2181,solr-cloud-slave2:2181 -confdir /opt/SolrCloud/Solr/solr-config/ -confname solrconfig
cd /opt/SolrCloud/Zookeeper/zookeeper-3.4.6/bin/
./zkCli.sh -server solr-cloud-master:2181
cd /opt/SolrCloud/Tomcat/apache-tomcat-7.0.62/bin/
./startup.sh
启动solr-cloud-slave1和solr-cloud-slave2机器上的Tomcat。
curl ‘http://solr-cloud-master:8080/solr/admin/collections?action=CREATE&name=collection_1&numShards=3&replicationFactor=1‘
查看 /apps/data/solr-cores/ 目录:
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.CloudSolrClient;
import org.apache.solr.client.solrj.impl.LBHttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
public class SolrCloudTest {
public static final Log LOG = LogFactory.getLog(SolrCloudTest.class);
private static CloudSolrClient cloudSolrClient;
private static LBHttpSolrClient solrClient;
private static synchronized CloudSolrClient getCloudSolrServer(final String zkHost) {
LOG.info("connection to : " + zkHost + "\n");
if (cloudSolrClient == null) {
cloudSolrClient = new CloudSolrClient(zkHost);
}
return cloudSolrClient;
}
private void addIndex(SolrClient solrClient) {
try {
Collection<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();
for (int i = 0; i <= 200; i++) {
SolrInputDocument doc = new SolrInputDocument();
String key = "";
key = String.valueOf(i);
doc.addField("id", key);
doc.addField("test_s", key + "value");
docs.add(doc);
}
LOG.info("docs info:" + docs + "\n");
solrClient.add(docs);
solrClient.commit();
} catch (SolrServerException e) {
System.out.println("Add docs Exception !!!");
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
System.out.println("Unknowned Exception!!!!!");
e.printStackTrace();
}
}
public void search(CloudSolrClient cloudSolrClient, String Str) {
SolrQuery query = new SolrQuery();
query.setRows(20);
query.setQuery(Str);
try {
LOG.info("query string: "+ Str);
System.out.println("query string: "+ Str);
QueryResponse response = cloudSolrClient.query(query);
SolrDocumentList docs = response.getResults();
System.out.println(docs);
System.out.println(docs.size());
System.out.println("doc num:" + docs.getNumFound());
System.out.println("elapse time:" + response.getQTime());
for (SolrDocument doc : docs) {
String area = (String) doc.getFieldValue("test_s");
String id = (String) doc.getFieldValue("id");
System.out.println("id: " + id);
System.out.println("tt_s: " + area);
System.out.println();
}
} catch (SolrServerException e) {
e.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
}
}
public void deleteSolrData(String solrUrl) {
try {
solrClient = new LBHttpSolrClient(solrUrl);
UpdateResponse res = solrClient.deleteByQuery("*:*");
System.out.println(res.getStatus());
solrClient.commit();
LOG.info("Delete Success!");
} catch (SolrServerException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
final String solrUrl = "http://solr-cloud-master:8080/solr/collection_1";
final String zkHost = "192.168.1.131:2181";
final String defaultCollection = "collection_1";
final int zkClientTimeout = 10000;
final int zkConnectTimeout = 10000;
CloudSolrClient cloudSolrClient = getCloudSolrServer(zkHost);
cloudSolrClient.setDefaultCollection(defaultCollection);
cloudSolrClient.setZkClientTimeout(zkClientTimeout);
cloudSolrClient.setZkConnectTimeout(zkConnectTimeout);
try{
cloudSolrClient.connect();
System.out.println("connect solr cloud zk sucess");
} catch (Exception e){
LOG.error("connect to collection "+defaultCollection+" error\n");
System.out.println("error message is:"+e);
e.printStackTrace();
System.exit(1);
}
SolrCloudTest solrCloudTest = new SolrCloudTest();
try{
solrCloudTest.addIndex(cloudSolrClient);
} catch(Exception e){
e.printStackTrace();
}
solrCloudTest.search(cloudSolrClient, "id:*");
// solrCloudTest.deleteSolrData(solrUrl);
cloudSolrClient.close();
}
}
Solr5.x版本和Solr4.x的版本在安装上发生了比较大的变化,另外API操作上也发生了比较大的变化,安装相对来说比较简单,最大的难点在于对其进行性能方面的优化,在进行压力测试中发现,持续对solr集群进行操作会出现服务不可用的异常,原因目前还没有找到,有时还会出现Zookeeper集群服务异常导致数据插入失败。针对这些问题都需要后续不断地进行深入的研究后,才能得出结论或解决方案。
本文参考了:http://www.tuicool.com/articles/AJR3Mv
标签:
原文地址:http://www.cnblogs.com/hyl8218/p/5537486.html