基礎掌握:單機版solr請看http://blog.csdn.net/a623982022/article/details/78910059
什麼是SolrCloud:
SolrCloud(solr 雲)是Solr提供的分佈式搜索方案,當你需要大規模,容錯,分佈式索引和檢索能力時使用 SolrCloud。當一個系統的索引數據量少的時候是不需要使用SolrCloud
的,當索引量很大,搜索請求併發很高,這時需要使用SolrCloud來滿足這些需求。
SolrCloud是基於Solr和Zookeeper的分佈式搜索方案,它的主要思想是使用Zookeeper作爲集羣的配置信息中心。
它有幾個特色功能:
1)集中式的配置信息
2)自動容錯
3)近實時搜索
4)查詢時自動負載均衡
Solr集羣的系統架構:
物理結構:
三個Solr實例( 每個實例包括兩個Core),組成一個SolrCloud。
邏輯結構:
索引集合包括兩個Shard(shard1和shard2),shard1和shard2分別由三個Core組成,其中一個Leader兩個Replication,Leader是由zookeeper選舉產生,zookeeper控制每個shard上三個Core的索引數據一致,解決高可用問題。
用戶發起索引請求分別從shard1和shard2上獲取,解決高併發問題。
collection:
Collection在SolrCloud集羣中是一個邏輯意義上的完整的索引結構。它常常被劃分爲一個或多個Shard(分片),它們使用相同的配置信息。
比如:針對商品信息搜索可以創建一個collection。
collection=shard1+shard2+....+shardX
Core:
每個Core是Solr中一個獨立運行單位,提供 索引和搜索服務。一個shard需要由一個Core或多個Core組成。由於collection由多個shard組成所以collection一般由多個core組成。
Master或Slave
Master是master-slave結構中的主結點(通常說主服務器),Slave是master-slave結構中的從結點(通常說從服務器或備服務器)。同一個Shard下master和slave存儲的數據是一致的,這是爲了達到高可用目的。
Shard:
Collection的邏輯分片。每個Shard被化成一個或者多個replication,通過選舉確定哪個是Leader。
需要實現的solr集羣架構:
Zookeeper作爲集羣的管理工具。
1、集羣管理:容錯、負載均衡。
2、配置文件的集中管理
3、集羣的入口
需要實現zookeeper 高可用。需要搭建集羣。建議是奇數節點。需要三個zookeeper服務器。
搭建solr集羣需要7臺服務器,3個zookeeper節點,4個tomcat節點。建議虛擬機的內容1G以上。
環境準備:
CentOS-6.5-i386-bin-DVD1.iso
jdk-7u72-linux-i586.tar.gz
apache-tomcat-7.0.47.tar.gz
zookeeper-3.4.6.tar.gz
solr-4.10.3.tgz
安裝步驟:
說明:
服務器:3個zookeeper分別爲192.168.0.1,192.68.0.2,192.16.0.3 tomcat分別爲:4,5,6,7。
1.Zookeeper集羣搭建:
第一步:需要安裝jdk環境。
第二步:把zookeeper的壓縮包上傳到服務器。
第三步:解壓縮。
第四步:把zookeeper複製到solr-cloud目錄下。
服務器1:192.168.0.1
[root@localhost ~]# mkdir /usr/local/solr-cloud
[root@localhost ~]# cp -r zookeeper-3.4.6 /usr/local/solr-cloud/zookeeper01
服務器2,3同樣:複製過去改名爲zookeeper02,zookeeper03.
第五步:在每個zookeeper目錄下創建一個data目錄。
第六步:在每個data目錄下創建一個myid文件,文件名就叫做“myid”。內容就是每個實例的id。1,2,3。
第一臺[root@localhost data]# echo 1 >> myid
第二臺[root@localhost data]# echo 2 >> myid
第三臺[root@localhost data]# echo 3 >> myid
第七步:修改配置文件。把conf目錄下的zoo_sample.cfg文件改名爲zoo.cfg,修改文件內容。
3臺服務器分別修改成:zookeeper01,02,03,的data目錄。clientport修改爲:2181,8182,2183。
第八步:啓動每個zookeeper實例。
啓動bin/zkServer.sh start
查看zookeeper的狀態:
bin/zkServer.sh status
Solr集羣的搭建:參考solr上一篇文章第六步:讓zookeeper統一管理配置文件。需要把solrhome/collection1/conf目錄上傳到zookeeper。上傳任意solrhome中的配置文件即可。
第一步:創建四個tomcat實例在服務器的4,5,6,7。每個tomcat運行在不同的端口(主機不同,同一端口也可以)。8180、8280、8380、8480
vim tomcat01/conf/server.xml
第二步:部署solr的war包。把單機版的solr工程複製到集羣中的tomcat/webapps/中。
第三步:爲每個solr實例創建一個對應的solrhome。使用單機版的solrhome複製四份。
第四步:需要修改solr的web.xml文件。把solrhome關聯起來。
vim tomcat01/webapps/solr/WEB-INF/web.xml
第五步:配置solrCloud相關的配置。每個solrhome下都有一個solr.xml,把其中的ip及端口號配置好。
第六步:修改tomcat/bin目錄下的catalina.sh 文件,關聯solr和zookeeper。
把此配置添加到配置文件中:每個tomcat的都需要改
JAVA_OPTS="-DzkHost=192.168.0.1:2181,192.168.0.2:2182,192.168.0.3:2183"
第七步:讓zookeeper統一管理配置文件。需要把solrhome/collection1/conf目錄上傳到zookeeper。上傳任意solrhome中的配置文件即可。
使用工具上傳配置文件:/root/solr-4.10.3/example/scripts/cloud-scripts/zkcli.sh
./zkcli.sh -zkhost 192.168.0.1:2181,192.168.0.2:2182,192.168.0.3:2183 -cmd upconfig -confdir /usr/local/solr-cloud/solrhome01/collection1/conf -confname myconf
查看zookeeper上的配置文件:
使用zookeeper目錄下的bin/zkCli.sh命令查看zookeeper上的配置文件:
[root@localhost 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.0.1:2182
第八步:啓動每個tomcat實例。要保證zookeeper集羣是啓動狀態。
第九步:訪問集羣(記得關閉防火牆service iptables stop
chkconfig iptables off)
第十步:創建新的Collection進行分片處理。修改下面的ip,訪問該地址就會自己創建新的collections2
http://192.168.0.4:8180/solr/admin/collections?action=CREATE&name=collection2&numShards=2&replicationFactor=2
第十一步:刪除不用的Collection。
http://192.168.0.4:8180/solr/admin/collections?action=DELETE&name=collection1
這裏集羣已經搭建完畢,接下來在項目中使用
項目中使用solrJ管理集羣:
使用步驟:
第一步:把solrJ相關的jar包添加到工程中。
第二步:創建一個SolrServer對象,需要使用CloudSolrServer子類。構造方法的參數是zookeeper的地址列表。
第三步:需要設置DefaultCollection屬性。
第四步:創建一SolrInputDocument對象。
第五步:向文檔對象中添加域
第六步:把文檔對象寫入索引庫。
第七步:提交。
@Test
public void testSolrCloudAddDocument() throws Exception {
// 第一步:把solrJ相關的jar包添加到工程中。
// 第二步:創建一個SolrServer對象,需要使用CloudSolrServer子類。構造方法的參數是zookeeper的地址列表。
//參數是zookeeper的地址列表,使用逗號分隔
CloudSolrServer solrServer = new CloudSolrServer("192.168.0.1:2181,192.168.0.2:2182,192.168.0.3:2183");
// 第三步:需要設置DefaultCollection屬性。
solrServer.setDefaultCollection("collection2");
// 第四步:創建一SolrInputDocument對象。
SolrInputDocument document = new SolrInputDocument();
// 第五步:向文檔對象中添加域
document.addField("demo_title", "測試");
document.addField("demo_price", "100");
document.addField("id", "test001");
// 第六步:把文檔對象寫入索引庫。
solrServer.add(document);
// 第七步:提交。
solrServer.commit();
}
查詢:創建一個CloudSolrServer對象,其他處理和單機版一致。不在測試。把搜索功能切換到集羣版:
<?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:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx4.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util4.2.xsd">
<!-- 單機版solr服務配置 -->
<!-- <bean id="httpSolrServer" class="org.apache.solr.client.solrj.impl.HttpSolrServer">
<constructor-arg name="baseURL" value="http://192.168.0.1:8080/solr"></constructor-arg>
</bean> -->
<!-- 集羣版solr服務 -->
<bean id="cloudSolrServer" class="org.apache.solr.client.solrj.impl.CloudSolrServer">
<constructor-arg name="zkHost" value="192.168.0.1:2181,192.168.0.2:2182,192.168.0.3:2183"></constructor-arg>
<property name="defaultCollection" value="collection2"></property>
</bean>
</beans>
其他代碼都不用動,跟單機版一樣的。