目录html
1、solr集群架构java
2、安装步骤linux
0、环境准备web
一、Zookeeper集群搭建redis
二、Solr集群的搭建spring
3、使用solrJ管理集群apache
CentOS-6.5-i386-bin-DVD1.isojson
jdk-7u72-linux-i586.tar.gzvim
apache-tomcat-7.0.47.tar.gztomcat
zookeeper-3.4.6.tar.gz
solr-4.10.3.tgz
第一步:须要安装jdk环境。
https://blog.csdn.net/qq_36326332/article/details/88345181
第二步:把zookeeper的压缩包上传到服务器。
[root@shuchang131 ~]# ll
总用量 309428
-rw-r--r--. 1 root root 8234674 9月 11 14:59 apache-tomcat-7.0.47.tar.gz
drwxr-xr-x. 3 root root 4096 9月 11 17:06 IK Analyzer 2012FF_hf1
-rw-r--r--. 1 root root 139463702 9月 11 14:59 jdk-7u55-linux-i586.tar.gz
-rw-r--r--. 1 root root 150010621 9月 11 14:59 solr-4.10.3.tgz.tgz
drwxr-xr-x. 10 1000 1000 4096 2月 20 2014 zookeeper-3.4.6
-rw-r--r--. 1 root root 17699306 9月 12 02:10 zookeeper-3.4.6.tar.gz
第三步:解压缩。
[root@shuchang131 ~]# tar -zxvf zookeeper-3.4.6.tar.gz
第四步:把zookeeper复制三份。
[root@shuchang131 ~]# mkdir /usr/local/solr-cloud
[root@shuchang131 ~]# cp -r zookeeper-3.4.6 /usr/local/solr-cloud/zookeeper01
[root@shuchang131 ~]# cp -r zookeeper-3.4.6 /usr/local/solr-cloud/zookeeper02
[root@shuchang131 ~]# cp -r zookeeper-3.4.6 /usr/local/solr-cloud/zookeeper03
第五步:在每一个zookeeper目录下建立一个data目录。
[root@shuchang131 ~]# mkdir /usr/local/solr-cloud/zookeeper01/data
[root@shuchang131 ~]# mkdir /usr/local/solr-cloud/zookeeper02/data
[root@shuchang131 ~]# mkdir /usr/local/solr-cloud/zookeeper03/data
第六步:在data目录下建立一个myid文件,文件名就叫作“myid”。内容就是每一个实例的id。例如一、二、3
[root@shuchang131 ~]# cd /usr/local/solr-cloud/zookeeper01/data/
[root@shuchang131 data]# echo 1 >> myid
[root@localhost data]# ll
total 4
-rw-r--r--. 1 root root 2 Apr 7 18:23 myid
[root@shuchang131 data]# cat myid
1
[root@shuchang131 ~]# cd /usr/local/solr-cloud/zookeeper02/data/
[root@shuchang131 data]# echo 2 >> myid
[root@shuchang131 data]# ll
total 4
-rw-r--r--. 1 root root 2 Apr 7 18:23 myid
[root@shuchang131 data]# cat myid
2
[root@shuchang131 ~]# cd /usr/local/solr-cloud/zookeeper03/data/
[root@shuchang131 data]# echo 3 >> myid
[root@localhost data]# ll
total 4
-rw-r--r--. 1 root root 2 Apr 7 18:23 myid
[root@localhost data]# cat myid
3
第七步:修改配置文件。把conf目录下的zoo_sample.cfg文件更名为zoo.cfg
[root@shuchang131 data]# cd /usr/local/solr-cloud/zookeeper01/conf/
[root@shuchang131 conf]# mv zoo_sample.cfg zoo.cfg
[root@shuchang131 conf]# vim zoo.cfg
dataDir=/usr/local/solr-cloud/zookeeper01/data/
# the port at which the clients will connect
clientPort=2181
#autopurge.purgeInterval=1
server.1=192.168.25.130:2881:3881
server.2=192.168,25.130:2882:3882
server.3=192.168.25.130:2883:3883
[root@shuchang131 data]# cd /usr/local/solr-cloud/zookeeper02/conf/
[root@shuchang131 conf]# mv zoo_sample.cfg zoo.cfg
[root@shuchang131 conf]# vim zoo.cfg
dataDir=/usr/local/solr-cloud/zookeeper02/data/
# the port at which the clients will connect
clientPort=2182
#autopurge.purgeInterval=1
server.1=192.168.25.130:2881:3881
server.2=192.168,25.130:2882:3882
server.3=192.168.25.130:2883:3883
[root@shuchang131 data]# cd /usr/local/solr-cloud/zookeeper03/conf/
[root@shuchang131 conf]# mv zoo_sample.cfg zoo.cfg
[root@shuchang131 conf]# vim zoo.cfg
dataDir=/usr/local/solr-cloud/zookeeper03/data/
# the port at which the clients will connect
clientPort=2183
#autopurge.purgeInterval=1
server.1=192.168.25.130:2881:3881
server.2=192.168,25.130:2882:3882
server.3=192.168.25.130:2883:3883
第八步:启动每一个zookeeper实例。
单个启动示例:
[root@shuchang131 solr-cloud]# /usr/local/solr-cloud/zookeeper01/bin/zkServer.sh start
[root@shuchang131 solr-cloud]# /usr/local/solr-cloud/zookeeper02/bin/zkServer.sh start
[root@shuchang131 solr-cloud]# /usr/local/solr-cloud/zookeeper03/bin/zkServer.sh start
批处理脚本启动
[root@shuchang131 solr-cloud]# vim start-all-zookeeper.sh
cd zookeeper01/bin
./zkServer.sh start
cd ../../
cd zookeeper02/bin
./zkServer.sh start
cd ../../
cd zookeeper03/bin
./zkServer.sh start
cd ../../
[root@shuchang131 solr-cloud]# chmod u+x start-all-zookeeper.sh
[root@shuchang131 solr-cloud]# ./start-all-zookeeper.sh
查看zookeeper的状态:
[root@shuchang131 solr-cloud]# /usr/local/solr-cloud/zookeeper01/bin/zkServer.sh status
[root@shuchang131 solr-cloud]# /usr/local/solr-cloud/zookeeper02/bin/zkServer.sh status
[root@shuchang131 solr-cloud]# /usr/local/solr-cloud/zookeeper03/bin/zkServer.sh status
第一步:建立四个tomcat实例。每一个tomcat运行在不一样的端口。8180、8280、8380、8480
[root@shuchang131 local]# ll
总用量 24
drwxr-xr-x. 9 root root 4096 9月 11 15:02 apache-tomcat-7.0.47
drwxr-xr-x. 8 uucp 143 4096 3月 18 2014 jdk1.7.0_55
drwxr-xr-x. 4 root root 4096 9月 11 08:35 redis
drwxr-xr-x. 4 root root 4096 9月 11 15:48 solr
drwxr-xr-x. 8 root root 4096 9月 11 15:09 solr-4.10.3
drwxr-xr-x. 13 root root 4096 9月 12 03:41 solr-cloud
[root@shuchang131 local]# cp apache-tomcat-7.0.47 /usr/local/solr-cloud/tomcat01
[root@shuchang131 local]# vim /usr/local/solr-cloud/tomcat01/conf/server.xml
<Server port="8105" shutdown="SHUTDOWN">
<Connector port="8109" protocol="AJP/1.3" redirectPort="8443" />
<Connector port="8180" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
[root@shuchang131 local]# cp apache-tomcat-7.0.47 /usr/local/solr-cloud/tomcat02
[root@shuchang131 local]# vim /usr/local/solr-cloud/tomcat02/conf/server.xml
<Server port="8205" shutdown="SHUTDOWN">
<Connector port="8209" protocol="AJP/1.3" redirectPort="8443" />
<Connector port="8280" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
[root@shuchang131 local]# cp apache-tomcat-7.0.47 /usr/local/solr-cloud/tomcat03
[root@shuchang131 local]# vim /usr/local/solr-cloud/tomcat03/conf/server.xml
<Server port="8305" shutdown="SHUTDOWN">
<Connector port="8309" protocol="AJP/1.3" redirectPort="8443" />
<Connector port="8380" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
[root@shuchang131 local]# cp apache-tomcat-7.0.47 /usr/local/solr-cloud/tomcat04
[root@shuchang131 local]# vim /usr/local/solr-cloud/tomcat04/conf/server.xml
<Server port="8405" shutdown="SHUTDOWN">
<Connector port="8409" protocol="AJP/1.3" redirectPort="8443" />
<Connector port="8480" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
第二步:部署solr的war包。把单机版的solr工程复制到集群中的tomcat中。
第三步:为每一个solr实例建立一个对应的solrhome。使用单机版的solrhome复制四份。
【第2、三步骤参考】:https://blog.csdn.net/qq_36326332/article/details/101079249
第四步:须要修改solr的web.xml文件。把solrhome关联起来。
[root@shuchang131 local]# vim /usr/local/solr-cloud/tomcat01/webapps/solr/WEB-INF/web.xml
<env-entry>
<env-entry-name>solr/home</env-entry-name>
<env-entry-value>/usr/local/solr-cloud/solrhome01</env-entry-value>
<env-entry-type>java.lang.String</env-entry-type>
</env-entry>
[root@shuchang131 local]# vim /usr/local/solr-cloud/tomcat02/webapps/solr/WEB-INF/web.xml
<env-entry>
<env-entry-name>solr/home</env-entry-name>
<env-entry-value>/usr/local/solr-cloud/solrhome02</env-entry-value>
<env-entry-type>java.lang.String</env-entry-type>
</env-entry>
[root@shuchang131 local]# vim /usr/local/solr-cloud/tomcat03/webapps/solr/WEB-INF/web.xml
<env-entry>
<env-entry-name>solr/home</env-entry-name>
<env-entry-value>/usr/local/solr-cloud/solrhome03</env-entry-value>
<env-entry-type>java.lang.String</env-entry-type>
</env-entry>
[root@shuchang131 local]# vim /usr/local/solr-cloud/tomcat04/webapps/solr/WEB-INF/web.xml
<env-entry>
<env-entry-name>solr/home</env-entry-name>
<env-entry-value>/usr/local/solr-cloud/solrhome04</env-entry-value>
<env-entry-type>java.lang.String</env-entry-type>
</env-entry>
第五步:配置solrCloud相关的配置。每一个solrhome下都有一个solr.xml,把其中的ip及端口号配置好。
[root@shuchang131 local]# cp solr /usr/local/solr-cloud/solrhome01
[root@shuchang131 local]# vim /usr/local/solr-cloud/solrhome01/solr.xml
<solr>
<solrcloud>
<str name="host">192.168.25.130</str>
<int name="hostPort">8180</int>
</solrcloud>
</solr>
[root@shuchang131 local]#cp solr /usr/local/solr-cloud/solrhome02
[root@shuchang131 local]# vim /usr/local/solr-cloud/solrhome02/solr.xml
<solr>
<solrcloud>
<str name="host">192.168.25.130</str>
<int name="hostPort">8280</int>
</solrcloud>
</solr>
[root@shuchang131 local]#cp solr /usr/local/solr-cloud/solrhome03
[root@shuchang131 local]# vim /usr/local/solr-cloud/solrhome03/solr.xml
<solr>
<solrcloud>
<str name="host">192.168.25.130</str>
<int name="hostPort">8380</int>
</solrcloud>
</solr>
[root@shuchang131 local]#cp solr /usr/local/solr-cloud/solrhome04
[root@shuchang131 local]# vim /usr/local/solr-cloud/solrhome04/solr.xml
<solr>
<solrcloud>
<str name="host">192.168.25.130</str>
<int name="hostPort">8480</int>
</solrcloud>
</solr>
第六步:让zookeeper统一管理配置文件。须要把solrhome/collection1/conf目录上传到zookeeper。上传任意solrhome中的配置文件便可。
使用工具上传配置文件:[root@shuchang131 local]# /root/solr-4.10.3/example/scripts/cloud-scripts/zkcli.sh
[root@shuchang131 local]# ./zkcli.sh -zkhost 192.168.25.130:2181,192.168.25.130:2182,192.168.25.130:2183 -cmd upconfig -confdir /usr/local/solr-cloud/solrhome01/collection1/conf -confname myconf
查看zookeeper上的配置文件:
使用zookeeper目录下的bin/zkCli.sh命令查看zookeeper上的配置文件:
[root@shuchang131 bin]# ./zkCli.sh
[zk: localhost:2181(CONNECTED) 0] ls /
[configs, zookeeper]
[zk: localhost:2181(CONNECTED) 1] ls /configs
[myconf]
[zk: localhost:2181(CONNECTED) 2] ls /configs/myconf
[admin-extra.menu-top.html, currency.xml, protwords.txt, mapping-FoldToASCII.txt, _schema_analysis_synonyms_english.json, _rest_managed.json, solrconfig.xml, _schema_analysis_stopwords_english.json, stopwords.txt, lang, spellings.txt, mapping-ISOLatin1Accent.txt, admin-extra.html, xslt, synonyms.txt, scripts.conf, update-script.js, velocity, elevate.xml, admin-extra.menu-bottom.html, clustering, schema.xml]
[zk: localhost:2181(CONNECTED) 3]
退出:
[zk: localhost:2181(CONNECTED) 3] quit
使用如下命令链接指定的zookeeper服务: ./zkCli.sh -server 192.168.25.130:2183 |
第七步:修改tomcat/bin目录下的catalina.sh 文件,关联solr和zookeeper。
把此配置添加到配置文件中:
JAVA_OPTS="-DzkHost=192.168.25.130:192.168.25.130:192.168.25.130:2183"
第八步:启动每一个tomcat实例。要包装zookeeper集群是启动状态。
第九步:访问集群
http://192.168.25.130:8180/solr/#/~cloud
第十步:建立新的Collection进行分片处理。
http://192.168.25.130:8180/solr/admin/collections?action=CREATE&name=collection2&numShards=2&replicationFactor=2
第十一步:删除不用的Collection。
http://192.168.25.130:8180/solr/admin/collections?action=DELETE&name=collection1
使用步骤:查询文档:单机版
第一步:把solrJ相关的jar包添加到工程中。
第二步:建立一个SolrServer对象,须要使用CloudSolrServer子类。构造方法的参数是zookeeper的地址列表。
第三步:须要设置DefaultCollection属性。
第四步:建立一SolrInputDocument对象。
第五步:向文档对象中添加域
第六步:把文档对象写入索引库。
第七步:提交。
package com.e3mall.solrj; import java.util.List; import java.util.Map; import org.apache.solr.client.solrj.SolrQuery; import org.apache.solr.client.solrj.SolrServer; import org.apache.solr.client.solrj.impl.HttpSolrServer; import org.apache.solr.client.solrj.response.QueryResponse; import org.apache.solr.common.SolrDocument; import org.apache.solr.common.SolrDocumentList; import org.apache.solr.common.SolrInputDocument; import org.junit.Test; public class TestSolrJ { /** * * @Description: 增改Solr文档 * @author WSC * @date 2019年9月23日 * @throws Exception * void */ @Test public void addDocument() throws Exception {// 添加和更新文档 // 建立一个SolrServer对象,建立一个链接。参数solr服务的url SolrServer solrServer = new HttpSolrServer("http://192.168.25.130:8080/solr/collection1"); // 建立一个文档对象SolrInputDocument SolrInputDocument doc = new SolrInputDocument(); // 向文档对象中添加域。文档中必须包含一个id域,全部域的名称必须在schema.xml中定义 doc.addField("id", "doc1"); doc.addField("item_title", "测试商品02"); doc.addField("item_price", 1002); // 把文档写入索引库 solrServer.add(doc); // 提交 solrServer.commit(); } /** * * @Description: 删除Solr文档 * @author WSC * @date 2019年9月23日 * @throws Exception * void */ @Test public void deleteDocument() throws Exception {// 删除文档 SolrServer httpSolrServer = new HttpSolrServer("http://192.168.25.130:8080/solr/collection1"); // 删除文档 // httpSolrServer.deleteById("doc1"); httpSolrServer.deleteByQuery("id:doc1"); // 提交 httpSolrServer.commit(); } /** * * @Description: 简单查询Solr文档 * @author WSC * @date 2019年9月23日 * @throws Exception * void */ @Test public void queryIndex() throws Exception { // 建立索引库对象 SolrServer solrServer = new HttpSolrServer("http://192.168.25.130:8080/solr/collection1"); // 建立一个SolrQuery对象 SolrQuery solrQuery = new SolrQuery(); // 设置查询条件 // solrQuery.setQuery("*:*"); solrQuery.set("q", "*:*"); // 执行查询,SolrResponse对象 QueryResponse queryResponse = solrServer.query(solrQuery); // 取文档列表。去查询结果的总记录数 SolrDocumentList documentList = queryResponse.getResults(); System.out.println("获取查询结果的总记录数:" + documentList.getNumFound()); // 遍历文档列表,区域中的内容 for (SolrDocument solrDocument : documentList) { System.out.println(solrDocument.get("id")); System.out.println(solrDocument.get("item_title")); System.out.println(solrDocument.get("item_sell_point")); System.out.println(solrDocument.get("item_price")); System.out.println(solrDocument.get("item_image")); System.out.println(solrDocument.get("item_category_name")); System.out.println("---------------------------------------------"); } } /** * * @Description: 复杂查询Solr文档 * @author WSC * @date 2019年9月23日 * @throws Exception * void */ @Test public void queryIndexFuZa() throws Exception { // 建立索引库对象 SolrServer solrServer = new HttpSolrServer("http://192.168.25.130:8080/solr/collection1"); // 建立一个SolrQuery对象 SolrQuery solrQuery = new SolrQuery(); // 设置查询条件 solrQuery.setQuery("手机"); solrQuery.setStart(0); solrQuery.setRows(20); solrQuery.set("df", "item_title"); solrQuery.setHighlight(true); solrQuery.addHighlightField("item_title"); solrQuery.setHighlightSimplePre("<em>"); solrQuery.setHighlightSimplePost("</em>"); // 执行查询,SolrResponse对象 QueryResponse queryResponse = solrServer.query(solrQuery); // 取文档列表。去查询结果的总记录数 SolrDocumentList documentList = queryResponse.getResults(); System.out.println("获取查询结果的总记录数:" + documentList.getNumFound()); // 遍历文档列表,区域中的内容 for (SolrDocument solrDocument : documentList) { System.out.println(solrDocument.get("id")); // 取高亮显示 Map<String, Map<String, List<String>>> highlighting = queryResponse.getHighlighting(); List<String> list = highlighting.get(solrDocument.get("id")).get("item_title"); String title; if (list != null && list.size() > 0) { title = list.get(0); } else { title = solrDocument.get("item_title").toString(); } System.out.println(title); System.out.println(solrDocument.get("item_sell_point")); System.out.println(solrDocument.get("item_price")); System.out.println(solrDocument.get("item_image")); System.out.println(solrDocument.get("item_category_name")); System.out.println("---------------------------------------------"); } } }
使用步骤:集群版 建立一个CloudSolrServer对象,其余处理和单机版一致。
package com.e3mall.solrj; import java.util.List; import java.util.Map; import org.apache.solr.client.solrj.SolrQuery; import org.apache.solr.client.solrj.impl.CloudSolrServer; import org.apache.solr.client.solrj.response.QueryResponse; import org.apache.solr.common.SolrDocument; import org.apache.solr.common.SolrDocumentList; import org.apache.solr.common.SolrInputDocument; import org.junit.Test; public class TestSolrCloud { @Test public void testAddDocument() throws Exception { // 建立一个集群的链接,应该使用CloudSolrServer建立。参数zkHost:zookeeper的地址列表 CloudSolrServer cloudSolrServer = new CloudSolrServer("192.168.25.130:2181,192.168.25.130:2182,192.168.25.130:2183"); // 建立一个defaultCollection属性 cloudSolrServer.setDefaultCollection("collection2"); // 建立一个文档对象 SolrInputDocument document = new SolrInputDocument(); // 向文档中添加域 document.setField("id", "solrCloud01"); document.setField("item_title", "测试商品01"); document.setField("item_price", 123); // 把文档写入索引库 cloudSolrServer.add(document); // 提交 cloudSolrServer.commit(); } @Test public void testQueryDocument() throws Exception { // 建立一个CloudSolrServer CloudSolrServer cloudSolrServer = new CloudSolrServer("192.168.25.130:2181,192.168.25.130:2182,192.168.25.130:2183"); // 设置一个默认的Collection cloudSolrServer.setDefaultCollection("collection2"); // 建立查询对象SolrQuery SolrQuery solrQuery = new SolrQuery(); // 设置查询条件 solrQuery.setQuery("商品01"); solrQuery.set("df", "item_title"); solrQuery.setHighlight(true); solrQuery.addHighlightField("item_title"); solrQuery.setHighlightSimplePre("<em style=\"color:red\">"); solrQuery.setHighlightSimplePost("</em>"); // 执行查询 QueryResponse queryResponse = cloudSolrServer.query(solrQuery); // 获取查询结果 SolrDocumentList solrDocumentList = queryResponse.getResults(); Map<String, Map<String, List<String>>> highlighting = queryResponse.getHighlighting(); long numFound = solrDocumentList.getNumFound(); System.out.println("查询结果总记录数:" + numFound); for (SolrDocument solrDocument : solrDocumentList) { System.out.println(solrDocument.get("id")); System.out.println(solrDocument.get("item_title")); System.out.println(solrDocument.get("item_price")); System.out.println(highlighting.get(solrDocument.get("id")).get("item_title")); } } }
把搜索功能切换到集群版
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd"> <!-- 单机版SolrJ:HttpSolrServer --> <!-- <bean id="httpSolrServer" class="org.apache.solr.client.solrj.impl.HttpSolrServer"> <constructor-arg name="baseURL" value="${SOLR_URL}" /> <constructor-arg name="baseURL" value="http://192.168.25.130:8080/solr/collection1"/> </bean> --> <!-- 集群版SolrJ:CloudSolrServer --> <bean id="cloudSolrServer" class="org.apache.solr.client.solrj.impl.CloudSolrServer"> <!-- <constructor-arg name="zkHost" value="192.168.25.130:2181,192.168.25.130:2182,192.168.25.130:2183" /> --> <constructor-arg name="zkHost" value="${SOLR_CLOUD_URL}" /> <!-- <constructor-arg index="0" value="${SOLR_CLOUD_URL}" /> --> <property name="defaultCollection" value="collection2"></property> </bean> </beans>