1 什麼是SolrCloud
SolrCloud(solr 雲)是Solr提供的分布式搜尋方案,當你需要大規模,容錯,分布式索引和檢索能力時使用 SolrCloud。當一個系統的索引資料量少的時候是不需要使用SolrCloud的,當索引量很大,搜尋請求并發很高,這時需要使 用SolrCloud來滿足這些需求。
SolrCloud是基于Solr和Zookeeper的分布式搜尋方案,它的主要思想是使用Zookeeper作為叢集的配置資訊中心。
它有幾個特色功能:
1)集中式的配置資訊
2)自動容錯
3)近實時搜尋
4)查詢時自動負載均衡
1.1 Zookeeper是個什麼玩意?
顧名思義zookeeper就是動物園管理者,他是用來管hadoop(大象)、Hive(蜜蜂)、pig(小豬)的管理者, Apache Hbase和 Apache Solr 的分布式叢集都用到了zookeeper;Zookeeper:是一個分布式的、開源的程式協調服務,是hadoop項目下的一個子項目。
1.2 Zookeeper可以幹哪些事情
1、配置管理
在我們的應用中除了代碼外,還有一些就是各種配置。比如資料庫連接配接等。一般我們都是使用配置檔案的方式,在代碼中引入這些配置檔案。但是當我們隻有一種配置,隻有一台伺服器,并且不經常修改的時候,使用配置檔案是一個很好的做法,但是如果我們配置非常多,有很多伺服器都需要這個配置,而且還可能是動态的話使用配置檔案就不是個好主意了。這個時候往往需要尋找一種集中管理配置的方法,我們在這個集中的地方修改了配置,所有對這個配置感興趣的都可以獲得變更。比如我們可以把配置放在資料庫裡,然後所有需要配置的服務都去這個資料庫讀取配置。但是,因為很多服務的正常運作都非常依賴這個配置,是以需要這個集中提供配置服務的服務具備很高的可靠性。一般我們可以用一個叢集來提供這個配置服務,但是用叢集提升可靠性,那如何保證配置在叢集中的一緻性呢? 這個時候就需要使用一種實作了一緻性協定的服務了。Zookeeper就是這種服務,它使用Zab這種一緻性協定來提供一緻性。現在有很多開源項目使用Zookeeper來維護配置,比如在HBase中,用戶端就是連接配接一個Zookeeper,獲得必要的HBase叢集的配置資訊,然後才可以進一步操作。還有在開源的消息隊列Kafka中,也使用Zookeeper來維護broker的資訊。在Alibaba開源的SOA架構Dubbo中也廣泛的使用Zookeeper管理一些配置來實作服務治理。
2、名字服務
名字服務這個就很好了解了。比如為了通過網絡通路一個系統,我們得知道對方的IP位址,但是IP位址對人非常不友好,這個時候我們就需要使用域名來通路。但是計算機是不能是别域名的。怎麼辦呢?如果我們每台機器裡都備有一份域名到IP位址的映射,這個倒是能解決一部分問題,但是如果域名對應的IP發生變化了又該怎麼辦呢?于是我們有了DNS這個東西。我們隻需要通路一個大家熟知的(known)的點,它就會告訴你這個域名對應的IP是什麼。在我們的應用中也會存在很多這類問題,特别是在我們的服務特别多的時候,如果我們在本地儲存服務的位址的時候将非常不友善,但是如果我們隻需要通路一個大家都熟知的通路點,這裡提供統一的入口,那麼維護起來将友善得多了。
3、分布式鎖
其實在第一篇文章中已經介紹了Zookeeper是一個分布式協調服務。這樣我們就可以利用Zookeeper來協調多個分布式程序之間的活動。比如在一個分布式環境中,為了提高可靠性,我們的叢集的每台伺服器上都部署着同樣的服務。但是,一件事情如果叢集中的每個伺服器都進行的話,那互相之間就要協調,程式設計起來将非常複雜。而如果我們隻讓一個服務進行操作,那又存在單點。通常還有一種做法就是使用分布式鎖,在某個時刻隻讓一個服務去幹活,當這台服務出問題的時候鎖釋放,立即fail over到另外的服務。這在很多分布式系統中都是這麼做,這種設計有一個更好聽的名字叫LeaderElection(leader選舉)。比如HBase的Master就是采用這種機制。但要注意的是分布式鎖跟同一個程序的鎖還是有差別的,是以使用的時候要比同一個程序裡的鎖更謹慎的使用。
4、叢集管理
在分布式的叢集中,經常會由于各種原因,比如硬體故障,軟體故障,網絡問題,有些節點會進進出出。有新的節點加入進來,也有老的節點退出叢集。這個時候,叢集中其他機器需要感覺到這種變化,然後根據這種變化做出對應的決策。比如我們是一個分布式存儲系統,有一個中央控制節點負責存儲的配置設定,當有新的存儲進來的時候我們要根據現在叢集目前的狀态來配置設定存儲節點。這個時候我們就需要動态感覺到叢集目前的狀态。還有,比如一個分布式的SOA架構中,服務是一個叢集提供的,當消費者通路某個服務時,就需要采用某種機制發現現在有哪些節點可以提供該服務(這也稱之為服務發現,比如Alibaba開源的SOA架構Dubbo就采用了Zookeeper作為服務發現的底層機制)。還有開源的Kafka隊列就采用了Zookeeper作為Cosnumer的上下線管理。
2 Solr叢集的結構
3 Solr叢集的搭建
本教程的這套安裝是單機版的安裝,是以采用僞叢集的方式進行安裝,如果是真正的生産環境,将僞叢集的ip改下就可以了,步驟是一樣的。
SolrCloud結構圖如下:
需要三個zookeeper節點
四個solr節點
4 Zookeeper叢集的搭建
4.1 軟體環境
三個zookeeper執行個體。Zookeeper也是java開發的是以需要安裝jdk。
1、Linux系統
2、Jdk環境。
3、Zookeeper。
伺服器及軟體資訊資訊
1 | 192.168.106.80 | 伺服器上的位置:/usr/local/apache-tomcat-7.0.73 |
伺服器ip和端口号:192.168.106.80:8080 | ||
JDK-Version: 1.7.0_79 | ||
Zookeeper: /home/tuzq/software/zookeeper | ||
2 | 192.168.106.81 | 伺服器上的位置:/usr/local/apache-tomcat-7.0.73 |
伺服器ip和端口号:192.168.106.81:8080 | ||
JDK-Version: 1.7.0_79 | ||
Zookeeper: /home/tuzq/software/zookeeper | ||
3 | 192.168.106.82 | 伺服器上的位置:/usr/local/apache-tomcat-7.0.73 |
伺服器ip和端口号:192.168.106.82:8080 | ||
JDK-Version: 1.7.0_79 | ||
Zookeeper: /home/tuzq/software/zookeeper | ||
4 | 192.168.106.83 | 伺服器上的位置:/usr/local/apache-tomcat-8.5.12 |
伺服器ip和端口号:192.168.106.83:8080 | ||
JDK-Version: 1.7.0_79 |
使用僞分布式實作solr叢集。需要三個zookeeper執行個體,4個tomcat執行個體,可以在一台虛拟機上模拟。建議虛拟機1G以上記憶體。
其中,192.168.106.83伺服器上的tomcat是8.5的,當然也可以從其它伺服器上拷貝一個完整的過來,使用指令如下:
[[email protected] local]# ls apache-tomcat-7.0.73 bin etc games include java lib lib64 libexec redis sbin share solrcloud src [[email protected] local]# pwd /usr/local ##下面的意思是從目前的/usr/local中将apache-tomcat-7.0.73拷貝到192.168.106.83的root使用者下的/usr/local目錄下。 [[email protected] local]# scp -r apache-tomcat-7.0.73 [email protected]:/usr/local/ The authenticity of host '192.168.106.83 (192.168.106.83)' can't be established. RSA key fingerprint is dc:55:2c:80:6a:9e:3d:a5:56:d7:0d:41:ba:d2:56:3c. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.106.83' (RSA) to the list of known hosts. [email protected]'s password: 拷貝完後,進入83的/usr/local下檢視内容 [[email protected] local]# pwd /usr/local [[email protected] local]# ls apache-tomcat-7.0.73 apache-tomcat-8.5.12 bin etc games include jdk1.8.0_73 lib lib64 libexec sbin share solrcloud src [[email protected] local]# |
4.2 Zookeeper的安裝步驟
關于Zookeeper的叢集安裝配置參考:http://blog.csdn.net/tototuzuoquan/article/details/54003140
5 Solr執行個體的搭建
第一步:在4台伺服器上分别安裝tomcat,具體的軟體安資訊參考上面的4.1章節。
第二步:将solr-4.10.3.tgz.tgz分别上傳到/home/tuzq/software下。
效果圖如下:
解壓solr-4.10.3.tar.gz壓縮包。
cd /home/tuzq/software tar –zxvf solr-4.10.3.tgz.tgz 解壓指令執行完成之後執行ls指令,目前所在目錄下多了一個solr-4.10.3 [[email protected] software]# ls apache-tomcat-8.5.12.tar.gz jdk-8u73-linux-x64.tar.gz solr-4.10.3 solr-4.10.3.tgz.tgz |
從壓縮包中複制solr.war到tomcat。
其中192.168.106.80、192.168.106.81、192.168.106.82,192.168.106.83這三台伺服器執行的指令如下: [[email protected] software]# cd /home/tuzq/software/solr-4.10.3/example/webapps [[email protected] webapps]# ls solr.war [[email protected] webapps]#cp -R solr.war /usr/local/apache-tomcat-7.0.73/webapps [[email protected] webapps]# cd /usr/local/apache-tomcat-7.0.73/webapps [[email protected] webapps]# ls docs examples host-manager manager ROOT solr.war |
建立/usr/local/solrcloud這個檔案夾,并将/home/tuzq/software/solr-4.10.3/example的solr檔案拷貝到/usr/local/solrcloud,并将solr名稱改成solrhome
[[email protected] local] cd /usr/local [[email protected] local] mkdir solrcloud 拷貝solr,并改變solr的名稱 [[email protected] example]# cd /home/tuzq/software/solr-4.10.3/example [[email protected] example]# ls contexts example-DIH example-schemaless logs README.txt scripts solr-webapp webapps etc exampledocs lib multicore resources solr start.jar [[email protected] example]# cp -R solr /usr/local/solrcloud [[email protected] example]# cd /usr/local/solrcloud [[email protected] solrcloud]# ls solr [[email protected] solrcloud]#mv solr solrhome [[email protected] solrcloud]# ls solrhome [[email protected] solrcloud]# |
修改solrhome下的solr.xml
[[email protected] solrcloud]# cd solrhome [[email protected] solrhome]# ls bin collection1 README.txt solr.xml zoo.cfg [[email protected] solrhome]# pwd /usr/local/solrcloud/solrhome |
192.168.106.80伺服器上的配置修改如下(其中下面紅色部分表示的tomcat所在的伺服器的ip和端口号):
<solr> <solrcloud> <str name="host">${host:192.168.106.80}</str> <int name="hostPort">${jetty.port:8080}</int> <str name="hostContext">${hostContext:solr}</str> <int name="zkClientTimeout">${zkClientTimeout:30000}</int> <bool name="genericCoreNodeNames">${genericCoreNodeNames:true}</bool> </solrcloud> <!—下面的配置暫時不動--> <shardHandlerFactory name="shardHandlerFactory" class="HttpShardHandlerFactory"> <int name="socketTimeout">${socketTimeout:0}</int> <int name="connTimeout">${connTimeout:0}</int> </shardHandlerFactory> </solr> |
192.168.106.81伺服器上的配置修改如下(其中下面紅色部分表示的tomcat所在的伺服器的ip和端口号):
<solr> <solrcloud> <str name="host">${host:192.168.106.81}</str> <int name="hostPort">${jetty.port:8080}</int> <str name="hostContext">${hostContext:solr}</str> <int name="zkClientTimeout">${zkClientTimeout:30000}</int> <bool name="genericCoreNodeNames">${genericCoreNodeNames:true}</bool> </solrcloud> <!—下面的配置暫時不動--> <shardHandlerFactory name="shardHandlerFactory" class="HttpShardHandlerFactory"> <int name="socketTimeout">${socketTimeout:0}</int> <int name="connTimeout">${connTimeout:0}</int> </shardHandlerFactory> </solr> |
192.168.106.82伺服器上的配置修改如下(其中下面紅色部分表示的tomcat所在的伺服器的ip和端口号):
<solr> <solrcloud> <str name="host">${host:192.168.106.82}</str> <int name="hostPort">${jetty.port:8080}</int> <str name="hostContext">${hostContext:solr}</str> <int name="zkClientTimeout">${zkClientTimeout:30000}</int> <bool name="genericCoreNodeNames">${genericCoreNodeNames:true}</bool> </solrcloud> <!—下面的配置暫時不動--> <shardHandlerFactory name="shardHandlerFactory" class="HttpShardHandlerFactory"> <int name="socketTimeout">${socketTimeout:0}</int> <int name="connTimeout">${connTimeout:0}</int> </shardHandlerFactory> </solr> |
192.168.106.83伺服器上的配置修改如下(其中下面紅色部分表示的tomcat所在的伺服器的ip和端口号):
<solr> <solrcloud> <str name="host">${host:192.168.106.83}</str> <int name="hostPort">${jetty.port:8080}</int> <str name="hostContext">${hostContext:solr}</str> <int name="zkClientTimeout">${zkClientTimeout:30000}</int> <bool name="genericCoreNodeNames">${genericCoreNodeNames:true}</bool> </solrcloud> <!—下面的配置暫時不動--> <shardHandlerFactory name="shardHandlerFactory" class="HttpShardHandlerFactory"> <int name="socketTimeout">${socketTimeout:0}</int> <int name="connTimeout">${connTimeout:0}</int> </shardHandlerFactory> </solr> |
第三步:重新啟動tomcat(192.168.106.81~83四台都重新開機,以重新開機81為例),解壓war包。
[[email protected] bin] cd /usr/local/apache-tomcat-7.0.73/bin [[email protected] bin]# ./shutdown.sh [[email protected] bin]# ./startup.sh #通過下面的指令檢視tomcat啟動情況 [[email protected] bin]# ps -ef | grep tomcat | grep -v grep #進入webapp目錄下,檢視solr.war的解壓情況 [[email protected] bin] cd /usr/local/apache-tomcat-7.0.73/webapps [[email protected] webapps]# ls docs examples host-manager manager ROOT solr solr.war 注意:要先停掉tomcat,然後再删除這個solr.war [[email protected] bin] cd /usr/local/apache-tomcat-7.0.73/bin [[email protected] bin]# ./shutdown.sh [[email protected] bin] cd /usr/local/apache-tomcat-7.0.73/webapps [[email protected] webapps]# rm -rf solr.war [[email protected] webapps]# ls docs examples host-manager manager ROOT solr #重新開機一下tomcat. [[email protected] bin] cd /usr/local/apache-tomcat-7.0.73/bin [[email protected] bin]# ./startup.sh |
把solr-4.10.3目錄下example目錄下的關于日志相關的jar包添加到solr工程中。
#添加擴充依賴包(日志包) [[email protected] ext]# cd /home/tuzq/software/solr-4.10.3/example/lib/ext [[email protected] ext]# ls jcl-over-slf4j-1.7.6.jar jul-to-slf4j-1.7.6.jar log4j-1.2.17.jar slf4j-api-1.7.6.jar slf4j-log4j12-1.7.6.jar [[email protected] ext] cp -R * /usr/local/apache-tomcat-7.0.73/webapps/solr/WEB-INF/lib/ cd solr-4.10.3 #在WEB-INFO下建立一個classes目錄 cd /usr/local/apache-tomcat-7.0.73/webapps/solr/WEB-INF mkdir classes #将日志檔案拷貝到classes目錄下 cp -r /home/tuzq/software/solr-4.10.3/example/resources/log4j.properties /usr/local/apache-tomcat-7.0.73/webapps/solr/WEB-INF/classes |
第四步:建立solrhome。修改web.xml指定solrhome的位置。
cd /usr/local/apache-tomcat-7.0.73/webapps/solr/WEB-INF vim web.xml <env-entry> <env-entry-name>solr/home</env-entry-name> <env-entry-value>/usr/local/solrcloud/solrhome</env-entry-value> <env-entry-type>java.lang.String</env-entry-type> </env-entry> |
其它solr相關的配置參考:http://blog.csdn.net/tototuzuoquan/article/details/61446788
6 solr叢集的搭建
6.1 第一步
把solrhome中的配置檔案上傳到zookeeper叢集。使用zookeeper的用戶端上傳。
用戶端指令位置:/home/tuzq/software/solr-4.10.3/example/scripts/cloud-scripts
使用zookeeper統一管理配置檔案。需要把solrhome下的collection/的conf檔案上傳至zookeeper
執行下邊的指令将/usr/local/solrcloud/solrhome/collection1/conf下的配置檔案上傳到zookeeper(此指令為單條指令,雖然很長。此指令在solr-4.10.3/example/scripts/cloud-scripts/目錄下:
./zkcli.sh -zkhost 192.168.106.80:2181,192.168.106.81:2181,192.168.106.82:2181 -cmd upconfig -confdir /usr/local/solrcloud/solrhome/collection1/conf -confname myconf |
紅色字型部分的ip表示zookeeper叢集的ip位址以及對應的端口。
檢視配置檔案是否上傳成功:
[[email protected] bin]# cd /home/tuzq/software/zookeeper/bin
[[email protected] bin]# ./zkCli.sh
Connecting to localhost:2181
[zk: localhost:2181(CONNECTED) 8] ls /
[configs, zookeeper, myboys, mygirls]
[zk: localhost:2181(CONNECTED) 1] ls /configs
[myconf]
[zk: localhost:2181(CONNECTED) 13] ls /configs/myconf
[currency.xml, mapping-FoldToASCII.txt,protwords.txt, scripts.conf, synonyms.txt, stopwords.txt, velocity,_schema_analysis_synonyms_english.json, admin-extra.html, update-script.js,_schema_analysis_stopwords_english.json, solrconfig.xml,admin-extra.menu-top.html, elevate.xml, schema.xml, clustering,mapping-ISOLatin1Accent.txt, spellings.txt, xslt, _rest_managed.json, lang,admin-extra.menu-bottom.html]
[zk: localhost:2181(CONNECTED) 14]
6.2 第三步
修改每一台solr的tomcat 的 bin目錄下catalina.sh檔案中加入DzkHost指定zookeeper伺服器位址:
[[email protected] bin]#cd /usr/local/apache-tomcat-7.0.73/bin
[[email protected] bin]#vim catalina.sh
JAVA_OPTS="-DzkHost=192.168.106.80:2181,192.168.106.81:2181,192.168.106.82:2181"
紅色字型的ip表示zookeeper叢集的ip以及端口号(注意:每個之間的逗号之間不允許有空格)
(可以使用vim的查找功能查找到JAVA_OPTS的定義的位置,然後添加,可以在catalina.sh的第二行添加)
6.4 第四步
重新啟動tomcat
浏覽器上通路:http://192.168.106.81:8080/solr/#/~cloud
一個主節點多個備份節點,叢集隻有一片。
6.5 第五步
建立一個兩片的collection,每片是一主一備。
使用以下指令建立:
http://192.168.106.81:8080/solr/admin/collections?action=CREATE&name=collection2&numShards=2&replicationFactor=2
6.6 第六步
删除collection1.
http://192.168.106.81:8080/solr/admin/collections?action=DELETE&name=collection1
7、配置dataimport插件
1、 将/home/tuzq/software/solr-4.10.3下的contrib和dist拷貝到/usr/local/solrcloud(四台伺服器都需要這樣做)
[[email protected] solr-4.10.3]# cd /home/tuzq/software/solr-4.10.3 [[email protected] solr-4.10.3]# cp -R contrib /usr/local/solrcloud [[email protected] solr-4.10.3]# cp -R dist /usr/local/solrcloud |
使用下面的方式将資料導入所需的jar放到相應的位置:
[[email protected] contrib]# cd /usr/local/solrcloud/contrib [[email protected] contrib]# mkdir db [[email protected] contrib]# cd db [[email protected] db]# mkdir lib 進入:/home/tuzq/software/solr-4.10.3/dist,将: solr-dataimporthandler-4.10.3.jar、solr-dataimporthandler-extras-4.10.3.jar放到 将 solr-dataimporthandler-4.10.3.jar solr-dataimporthandler-extras-4.10.3.jar最終上傳到lib檔案夾下 最後的效果如下: [[email protected] lib]# pwd /usr/local/solrcloud/contrib/dataimporthandler/lib [[email protected] lib]# ls solr-dataimporthandler-4.10.3.jar solr-dataimporthandler-extras-4.10.3.jar [[email protected] lib]# 最終将mysql-connector-java-5.1.38.jar放入到:/usr/local/solrcloud/contrib/db/lib中 cd /usr/local/solrcloud/contrib/db/lib 最終效果如下: [[email protected] lib]# pwd /usr/local/solrcloud/contrib/db/lib [[email protected] lib]# ls mysql-connector-java-5.1.38.jar [[email protected] lib]# |
2、 建立分詞,建立方式參考:http://blog.csdn.net/tototuzuoquan/article/details/61446788中關于分詞部分的内容:
[[email protected] lib] cd /usr/local/apache-tomcat-7.0.73/webapps/solr/WEB-INF/lib 将IKAnalyzer2012_FF.jar和IKAnalyzer2012FF_u1.jar放入到該檔案夾下: 将IK-Analyzer-2012FF\src下的ext.dic、IKAnalyzer.cfg.xml、stopword.dic上傳到:/usr/local/apache-tomcat-7.0.73/webapps/solr/WEB-INF/classes |
3、 以192.168.106.80為例,發現在之前放置的如下檔案/usr/local/solrcloud/solrhome/collection1/conf,已經在建立叢集的時候給删除了。這時候進入:/home/tuzq/software/solr-4.10.3/example/solr/collection1/conf,在下面建立一個data-config.xml檔案,檔案内容是:
<?xml version="1.0" encoding="UTF-8" ?> <dataConfig> <dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://192.168.106.80:3306/solr" user="root" password="123456"/> <document> <entity name="product" query="SELECT pid,name,catalog,catalog_name,price,description,picture FROM products"> <field column="pid" name="id"/> <field column="name" name="product_name"/> <field column="catalog" name="product_catalog"/> <field column="catalog_name" name="product_catalog_name"/> <field column="price" name="product_price"/> <field column="description" name="product_description"/> <field column="picture" name="product_picture"/> </entity> </document> </dataConfig> |
修改schema.xml:
内容如下:
<fieldType name="text_ik" class="solr.TextField"> <analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/> </fieldType> <!--定義field,指定field的type屬性為text_ik--> <field name="title_ik" type="text_ik" indexed="true" stored="true" /> <field name="content_ik" type="text_ik" indexed="true" stored="false" multiValued="true"/> <!-- 商品名稱 --> <field name="product_name" type="text_ik" indexed="true" stored="true" /> <!-- 商品分類id --> <field name="product_catalog" type="string" indexed="true" stored="true" /> <!-- 商品分類名稱 --> <field name="product_catalog_name" type="string" indexed="true" stored="true" /> <!-- 商品價格 --> <field name="product_price" type="float" indexed="true" stored="true" /> <!-- 商品描述 --> <field name="product_description" type="text_ik" indexed="true" stored="false" /> <!-- 商品圖檔 --> <field name="product_pic" type="string" indexed="false" stored="true" /> <field name="product_keywords" type="text_ik" indexed="true" stored="false" multiValued="true"/> <!-- 使用複制域、将product_name和product_description 都複制到product_keywords,當搜尋product_keywords的時候 --> <copyField source="product_name" dest="product_keywords"/> <copyField source="product_description" dest="product_keywords"/> |
修改solrconfig.xml的配置,可以參考http://blog.csdn.net/tototuzuoquan/article/details/61446788中關于資料導入相關的配置:
<lib dir="${solr.install.dir:../..}/contrib/extraction/lib" regex=".*\.jar" /> <lib dir="${solr.install.dir:../..}/dist/" regex="solr-cell-\d.*\.jar" /> <lib dir="${solr.install.dir:../..}/contrib/clustering/lib/" regex=".*\.jar" /> <lib dir="${solr.install.dir:../..}/dist/" regex="solr-clustering-\d.*\.jar" /> <lib dir="${solr.install.dir:../..}/contrib/langid/lib/" regex=".*\.jar" /> <lib dir="${solr.install.dir:../..}/dist/" regex="solr-langid-\d.*\.jar" /> <lib dir="${solr.install.dir:../..}/contrib/velocity/lib" regex=".*\.jar" /> <lib dir="${solr.install.dir:../..}/dist/" regex="solr-velocity-\d.*\.jar" /> <lib dir="${solr.install.dir:../..}/contrib/dataimporthandler/lib" regex=".*\.jar"/> <lib dir="${solr.install.dir:../..}/contrib/db/lib" regex=".*\.jar"/> |
接着将/home/tuzq/software/solr-4.10.3/example/solr/collection1/conf中的配置檔案上傳到zookeeper。
進入/home/tuzq/software/solr-4.10.3/example/scripts/cloud-scripts:
執行如下指令:
cd /home/tuzq/software/solr-4.10.3/example/scripts/cloud-scripts
./zkcli.sh -zkhost 192.168.106.80:2181,192.168.106.81:2181,192.168.106.82:2181 -cmd upconfig -confdir /home/tuzq/software/solr-4.10.3/example/solr/collection1/conf -confname myconf |
接着通路:http://192.168.106.80:8080/solr
點選data-config.xml檔案
結果如下(發現資料庫連接配接已經修改過來了):
重新開機tomcat叢集。
[[email protected] bin] cd /usr/local/apache-tomcat-7.0.73/bin [[email protected] bin]# ./shutdown.sh [[email protected] bin]# ./startup.sh |
接着通路:http://192.168.106.80:8080/solr/
直接執行Execute,發現可以進行資料同步了。
最後:
進入
執行查詢之後,發現右側已經有資料了。
============================================
最後,solr叢集搭建好之後,就可以用于調用了,案例如下(關于jar的擷取參考:http://blog.csdn.net/tototuzuoquan/article/details/61709204):
代碼如下:
package cn.com.toto.cloud; 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.junit.Test; public class TestSolrCloud { @Test public void testCloud() throws Exception { //建立一個連接配接 String zkHost = "192.168.106.80:2181,192.168.106.81:2181,192.168.106.82:2181"; //參數:zookeeper伺服器的位址清單 CloudSolrServer server = new CloudSolrServer(zkHost); //指定預設連接配接的collection server.setDefaultCollection("collection2"); //建立一查詢對象 SolrQuery solrQuery = new SolrQuery(); solrQuery.setQuery("*:*"); //執行查詢 QueryResponse queryResponse = server.query(solrQuery); SolrDocumentList solrDocumentList = queryResponse.getResults(); System.out.println("查詢結果總數量:" + solrDocumentList.getNumFound()); for (SolrDocument solrDocument : solrDocumentList) { System.out.println(solrDocument.get("id")); System.out.println(solrDocument.get("product_name")); System.out.println(solrDocument.get("product_price")); System.out.println(solrDocument.get("product_picture")); } } } |
如果是整合在spring,springmvc的項目裡面,整合方式如下:
1.1.1 實作步驟
第一步:建立一個web工程
第二步:導入jar包。Springmvc+spring+solrJ的jar包
第三步:開發dao
第四步:開發service
第五步:開發controller。
1.1.2 Dao
@Repository public class ProductDaoImpl implements ProductDao { //如果是單集的是HtttpSolrServer,如果是叢集的環境下的,是SolrServer @Autowired private SolrServer solrServer; @Override public ResultModel queryProduct(SolrQuery solrQuery) throws Exception { //根據查詢條件查詢索引庫 QueryResponse response = solrServer.query(solrQuery); //取查詢結果 SolrDocumentList solrDocumentList = response.getResults(); //商品清單 List<ProductModel> productList = new ArrayList<>(); //周遊清單 for (SolrDocument solrDocument : solrDocumentList) { //取商品資訊 ProductModel model = new ProductModel(); model.setPid((String) solrDocument.get("id")); //取高亮顯示 Map<String, Map<String, List<String>>> highlighting = response.getHighlighting(); List<String> list = highlighting.get(solrDocument.get("id")).get("product_name"); String productName = ""; if (null != list && list.size() > 0) { productName = list.get(0); } else { productName = (String) solrDocument.get("product_name"); } model.setName(productName); model.setPrice((float) solrDocument.get("product_price")); model.setPicture((String) solrDocument.get("product_picture")); model.setCatalog_name((String) solrDocument.get("product_catalog_name")); //添加到商品清單 productList.add(model); } //添加到傳回值對象中 ResultModel resultModel = new ResultModel(); resultModel.setProductList(productList); //查詢結果的總數量 resultModel.setRecordCount(solrDocumentList.getNumFound()); return resultModel; } } |
1.1.3 Service
@Service public class ProductServiceImpl implements ProductService { @Autowired private ProductDao productDao; @Override public ResultModel queryProduct(String queryString, String catalog_name, String price, String sort, Integer page) throws Exception { SolrQuery solrQuery = new SolrQuery(); //拼裝查詢條件 //主查詢條件 if (null != queryString && !"".equals(queryString)) { solrQuery.setQuery(queryString); } else { solrQuery.setQuery("*:*"); } //根據商品分類過濾 if (null != catalog_name && !"".equals(catalog_name)) { solrQuery.addFilterQuery("product_catalog_name:" + catalog_name); } //價格區間過濾 if (null != price && !"".equals(price)) { String[] strings = price.split("-"); solrQuery.addFilterQuery("product_price:["+strings[0]+" TO "+strings[1]+"]"); } //排序條件 if ("1".equals(sort)) { solrQuery.setSort("product_price", ORDER.desc); } else { solrQuery.setSort("product_price", ORDER.asc); } //分頁條件 if (null == page) page = 1; //計算分頁 int start = (page - 1) * Global.PAGE_SIZE; solrQuery.setStart(start); solrQuery.setRows(Global.PAGE_SIZE); //設定預設搜尋域 solrQuery.set("df", "product_keywords"); //設定高亮 solrQuery.setHighlight(true); //高亮顯示的域 solrQuery.addHighlightField("product_name"); //高亮顯示的字首 solrQuery.setHighlightSimplePre("<span style=\"color:red\">"); //高亮字尾 solrQuery.setHighlightSimplePost("</span>"); //執行查詢 ResultModel resultModel = productDao.queryProduct(solrQuery); //計算總頁數 Long recordCount = resultModel.getRecordCount(); int pageCount = (int) (recordCount / Global.PAGE_SIZE); if (recordCount % Global.PAGE_SIZE > 0) { pageCount++; } resultModel.setPageCount(pageCount); resultModel.setCurPage(page); return resultModel; } } |
1.1.4 Controller
@Controller public class ProductController { @Autowired private ProductService productService; @RequestMapping("/list") public String queryProduct(String queryString, String catalog_name, String price, String sort, Integer page, Model model) throws Exception { //接收參數,調用service查詢商品清單 ResultModel resultModel = productService.queryProduct(queryString, catalog_name, price, sort, page); //把resultModel傳遞給頁面 model.addAttribute("result", resultModel); //參數回顯 model.addAttribute("queryString", queryString); model.addAttribute("catalog_name", catalog_name); model.addAttribute("price", price); model.addAttribute("sort", sort); model.addAttribute("page", page); //傳回jsp頁面 return"product_list"; } } |
1.1.5 Web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>SolrCloudJD</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <!-- 前端控制器 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping> <!-- post亂碼過濾器 --> <filter> <filter-name>Character Encoding</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>Character Encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app> |
1.1.6 Springmvc.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd "> <!-- 配置掃描包 --> <context:component-scan base-package="xxxxx"/> <!-- 配置注解驅動 --> <mvc:annotation-driven/> <!-- jsp視圖解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" > <!-- 字首 --> <property name="prefix" value="/WEB-INF/jsp/"></property> <!-- 字尾 --> <property name="suffix" value=".jsp"></property> </bean> <!-- 單機版的SolrServer --> <!-- <bean class="org.apache.solr.client.solrj.impl.HttpSolrServer"> <constructor-arg value="http://localhost:8080/solr/"></constructor-arg> </bean> --> <!-- 叢集環境SolrServer --> <bean class="org.apache.solr.client.solrj.impl.CloudSolrServer"> <constructor-arg index="0" value="192.168.106.80:2181,192.168.106.81:2181,192.168.106.82:2181"/> <property name="defaultCollection" value="collection2"/> </bean> </beans> |