搞定了一切的一切之後下一步就是正式使用了,但是之前介紹的都是在單台伺服器上進行的部署,如果在生産環境出現了單台故障怎麼辦呢?提供穩定性和性能的最直覺的方式就是叢集,solr官方提供了cloud的叢集方式
附上:
喵了個咪的部落格:
http://w-blog.cn Solr官網: http://lucene.apache.org/solr/PS:8.0.0版本已經釋出,本文使用此時較為穩定的7.7.1版本
一、基于tomcat安裝solr
下載下傳solr
> cd /app/install/
> wget http://mirror.bit.edu.cn/apache/lucene/solr/7.7.1/solr-7.7.1.tgz
> tar -zxvf solr-7.7.1.tgz
> mv solr-7.7.1 /usr/local/
> vim /etc/profile
export PATH=/usr/local/solr-7.7.1/bin:$PATH
> source /etc/profile
在tomcat包下的webapps目錄建立一個目錄,取名solr
> cd /data/wwwroot/default
> mkdir solr
- 将solr解壓包/solr-7.7.1/server/solr-webapp/webapp/下的内容拷貝到剛才tomcat下建立的檔案夾solr中
- 将/solr-7.7.1/server/lib/ext/下的jar包拷貝到/tomcat/webapps/solr/WEB-INF/lib/下
- 将/solr-7.7.1/server/lib/下metrics開頭的jar包也拷貝到剛才的目錄下
- 将/solr-7.7.1/dist/下 solr-dataimporthandler-extras-7.7.1.jar、solr-dataimporthandler-7.7.1.jar也拷貝到剛才的目錄下
> cp -rf /usr/local/solr-7.7.1/server/solr-webapp/webapp/* /data/wwwroot/default/solr
> cp -rf /usr/local/solr-7.7.1/server/lib/ext/* /data/wwwroot/default/solr/WEB-INF/lib
> cp -rf /usr/local/solr-7.7.1/server/lib/metrics-* /data/wwwroot/default/solr/WEB-INF/lib
> cp -rf /usr/local/solr-7.7.1/dist/solr-dataimporthandler-extras-7.7.1.jar /data/wwwroot/default/solr/WEB-INF/lib
> cp -rf /usr/local/solr-7.7.1/dist/solr-dataimporthandler-7.7.1.jar /data/wwwroot/default/solr/WEB-INF/lib
- 在你本地的/home下建立一個檔案夾,取名solr-home,将/solr-7.7.1/server/solr下的内容拷貝到剛才建立的solr-home下
> mkdir /home/solr-home
> cp -rf /usr/local/solr-7.7.1/server/solr/* /home/solr-home/
- 打開并編輯/tomcat/webapps/solr/WEB-INF/web.xml并且注釋檔案末尾的所有 元素節點
> vim /data/wwwroot/default/solr/WEB-INF/web.xml
<env-entry>
<env-entry-name>solr/home</env-entry-name>
<env-entry-value>/home/solr-home</env-entry-value>
<env-entry-type>java.lang.String</env-entry-type>
</env-entry>
<!--
<security-constraint>
<web-resource-collection>
<web-resource-name>Disable TRACE</web-resource-name>
<url-pattern>/</url-pattern>
<http-method>TRACE</http-method>
</web-resource-collection>
<auth-constraint/>
</security-constraint>
<security-constraint>
<web-resource-collection>
<web-resource-name>Enable everything but TRACE</web-resource-name>
<url-pattern>/</url-pattern>
<http-method-omission>TRACE</http-method-omission>
</web-resource-collection>
</security-constraint>
-->
-
配置solr日志(可選)
将/solr-7.7.1/server/resources/下的log4j2.xml檔案拷貝到solr/WEB-INF/classes/下,如果沒有則自己建立一個。
分别給予權限
> chown -R www:www /home/solr-home/
> chown -R www:www /data/wwwroot/default/solr/
-
-
- -
-
二,SolrCloud配置子節點
把配置好的solr 和 solrhome 複制到其餘兩個節點
> scp -r /home/solr-home root@solr-2:/home/
> scp -r /home/solr-home root@solr-3:/home/
> scp -r /data/wwwroot/default/solr root@solr-2:/data/wwwroot/default/
> scp -r /data/wwwroot/default/solr root@solr-3:/data/wwwroot/default/
配置solrCloud相關的配置。每個solrhome下都有一個solr.xml,把其中的ip及端口号配置好
> vim /home/solr-home/solr.xml
讓zookeeper統一管理配置檔案。需要把/conf目錄上傳到zookeeper
> cd /app/install/solr-7.7.1/server/scripts/cloud-scripts
> ./zkcli.sh -zkhost solr-1:2181,solr-2:2181,solr-3:2181 -cmd upconfig -confdir /home/solr-home/new_core/conf -confname myconf
使用zkCli.sh來确認一下檔案已經上傳了
修改tomcat/bin目錄下的catalina.sh檔案,關聯solr和zookeeper 三台都需要修改
> vim /usr/local/tomcat/bin/catalina.sh
JAVA_OPTS="-DzkHost=solr-1:2181,solr-2:2181,solr-3:2181"
此時重新開機所有的tomcat就可以正常通路到solr-ui了
三、添加Collection使用索引功能
PS:叢集下的Collection等同于單機的core
四、配置mysql索引
使用SolrCloud來去進行配置過程和單機模式基本一緻,但是有以下幾個多出來的事情:
- 修改完new_core的配置需要通過zookeeper進行配置檔案的上傳才能生效;
- 依賴的各種jar包mysql,分詞和拼音等每個叢集節點都需要添加才能生效;
> vim server/solr/new_core/conf/solrconfig.xml
在solrconfig.xml 下添加以下配置,添加位置大約在 680行,SearchHandler 配置上面:
<!-- Request Handlers
http://wiki.apache.org/solr/SolrRequestHandler
Incoming queries will be dispatched to a specific handler by name
based on the path specified in the request.
If a Request Handler is declared with startup="lazy", then it will
not be initialized until the first request that uses it.
-->
<!-- add property -->
<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">
<lst name="defaults">
<str name="config">data-config.xml</str>
</lst>
</requestHandler>
<!-- SearchHandler
http://wiki.apache.org/solr/SearchHandler
For processing Search Queries, the primary Request Handler
provided with Solr is "SearchHandler" It delegates to a sequent
of SearchComponents (see below) and supports distributed
queries across multiple shards
-->
> vim server/solr/new_core/conf/data-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<dataConfig>
<dataSource name="source"
type="JdbcDataSource"
driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/appstore"
user="root"
password="sunmi388"
/>
<document>
<entity name="app"
pk="id"
dataSource="source"
query="select * from app"
deltaImportQuery="select * from app where id = '${dih.delta.id}'"
deltaQuery="select id from app where update_date > '${dataimporter.last_index_time}' and type = 1">
<field column="id" name="id"/>
<field column="app_name" name="app_name"/>
<field column="update_date" name="update_date"/>
</entity>
</document>
</dataConfig>
> vim server/solr/new_core/conf/managed-schema
<!-- add propertity -->
<field name="appName" type="string" indexed="true" stored="true" />
<field name="score" type="string" indexed="true" stored="true" />
<field name="downLoadNum" type="string" indexed="true" stored="true" />
<field name="top" type="string" indexed="true" stored="true" />
<field name="type" type="string" indexed="true" stored="true" />
<field name="update_date" type="string" indexed="true" stored="true" />
添加jar包時每個節點都需要添加
> cd /data/wwwroot/default/solr/WEB-INF/lib/
> wget http://pic.w-blog.cn/mysql-connector-java.jar
上傳配置:
> ./zkcli.sh -zkhost solr-1:2181,solr-2:2181,solr-3:2181 -cmd upconfig -confdir /home/solr-home/new_core/conf -confname myconf
PS:分詞配置和依賴jar包同理
五、叢集機制測試
測試容錯
- 關閉主節點solr-1
- 在資料查詢上 另外兩個節點依然可以提供資料查詢服務
- 重新開機 solr-1,再次檢視界面可以發現主節點已經路由給了solr-2
測試分詞在叢集下的生效範圍,在solr-1增加新的分詞
> vim /data/wwwroot/default/solr/WEB-INF/classes/ext.dic
美團
- 儲存後重新開機所有節點重新初始化資料
增加了分詞檔案的節點可以識别
未增加分詞檔案的節點無法識别
影響範圍是局部的,并且一定要重新開機才能生效(當然這個是IK分詞庫決定的)
六、叢集壓力分散情況
驗證 查詢時自動負載均衡
通過一台伺服器對一台進行壓力檢視整個叢集的情況
> ab -c 1000 -n 100000 -k "http://solr-1:8080/solr/mycollection/select?q=appName%3Agongju"
solr-1
solr-2 -3
承載了接口請求的節點壓力會比較大,其餘在叢集情況下提供了支援