一.Solr集群概念
solr單機版搭建參考: https://www.cnblogs.com/frankdeng/p/9615253.html
1.概念
SolrCloud(solr 雲)是Solr提供的分布式搜索方案,當你需要大規模,容錯,分布式索引和檢索能力時使用 SolrCloud。當一個系統的索引數據量少的時候是不需要使用SolrCloud的,當索引量很大,搜索請求並發很高,這時需要使用SolrCloud來滿足這些需求。
2.結構
SolrCloud為了降低單機的處理壓力,需要由多台服務器共同來完成索引和搜索任務。實現的思路是將索引數據進行Shard(分片)拆分,每個分片由多台的服務器共同完成,當一個索引或搜索請求過來時會分別從不同的Shard的服務器中操作索引。SolrCloud需要Solr基於Zookeeper部署,Zookeeper是一個集群管理軟件,由於SolrCloud需要由多台服務器組成,由zookeeper來進行協調管理。
二.Solr集群安裝
這里把solr當成獨立服務安裝,官方參考文檔:http://lucene.apache.org/solr/guide/7_4/taking-solr-to-production.html#taking-solr-to-production
1. 安裝環境,集群部署
系統 | 節點名稱 | IP | Java |
zookeeper |
Solr |
---|---|---|---|---|---|
CentOS7.5 | node21 | 192.168.100.21 | √ | √ | 8983/8984 |
CentOS7.5 | node22 | 192.168.100.22 | √ | √ | 8983/8984 |
CentOS7.5 | node23 | 192.168.100.23 | √ | √ |
Zookeeper集群安裝參考:https://www.cnblogs.com/frankdeng/p/9018177.html ,啟動zookeeper集群 zkServer.sh start
2. 解壓安裝包,運行安裝腳本
[admin@node21 software]$ tar zxvf solr-7.4.0.tgz
[admin@node21 software]$ tar xzf solr-7.4.0.tgz solr-7.4.0/bin/install_solr_service.sh --strip-components=2
上一個命令將install_solr_service.sh
腳本從存檔中提取到當前目錄中,如果在Red Hat上安裝,請確保在運行Solr安裝腳本()之前安裝了lsof,sudo yum install lsof
。安裝腳本必須以root身份運行:下一個命令時運行服務安裝腳本。
[admin@node21 software]$ sudo bash ./install_solr_service.sh solr-7.4.0.tgz -i /opt/module/solr -d /opt/module/solr/solrhome -u solr -s solr -p 8983
報錯找不到JAVA_HOME,原因:如果使用bash,建議將其放入/etc/bashrc
(基於RH)或/etc/bash.bashrc
(基於Debian)
[root@node21 software]# bash ./install_solr_service.sh solr-7.4.0.tgz -i /opt/module/solr -d /opt/module/solr/solrhome -u solr -s solr -p 8983
切換root用戶再次執行安裝成功,默認情況下,腳本將分發存檔解壓縮/opt
,配置Solr以將文件寫入/var/solr
,並以solr
用戶身份運行Solr ,也可以指定路徑,安裝信息如下
[root@node21 software]# bash ./install_solr_service.sh solr-7.4.0.tgz -i /opt/module/solr -d /opt/module/solr/solrhome -u solr -s solr -p 8983 We recommend installing the 'lsof' command for more stable start/stop of Solr id: solr: no such user Creating new user: solr Extracting solr-7.4.0.tgz to /opt/module/solr Installing symlink /opt/module/solr/solr -> /opt/module/solr/solr-7.4.0 ... Installing /etc/init.d/solr script ... Installing /etc/default/solr.in.sh ... Service solr installed. Customize Solr startup configuration in /etc/default/solr.in.sh *** [WARN] *** Your Max Processes Limit is currently 4096. It should be set to 65000 to avoid operational disruption. If you no longer wish to see this warning, set SOLR_ULIMIT_CHECKS to false in your profile or solr.in.sh NOTE: Please install lsof as this script needs it to determine if Solr is listening on port 8983. Started Solr server on port 8983 (pid=2147). Happy searching! Found 1 Solr nodes: Solr process 2147 running on port 8983 INFO - 2018-09-07 11:39:26.210; org.apache.solr.util.configuration.SSLCredentialProviderFactory; Processing SSL Credential Provider chain: env;sysprop { "solr_home":"/opt/module/solr/data/data", "version":"7.4.0 9060ac689c270b02143f375de0348b7f626adebc - jpountz - 2018-06-18 16:55:13", "startTime":"2018-09-07T15:39:11.678Z", "uptime":"0 days, 0 hours, 0 minutes, 19 seconds", "memory":"25.3 MB (%5.2) of 490.7 MB"} [root@node21 software]#
3.安裝多個solr服務
node21上再實例化一個solr2,端口8984
[root@node21 software]# mkdir /opt/module/solr2 [root@node21 software]# bash ./install_solr_service.sh solr-7.4.0.tgz -i /opt/module/solr2 -d /opt/module/solr2/solrhome -u solr -s solr2 -p 8984
安裝腳本,安裝包拷貝一份到node22上,實例化solr 8983,solr2 8984兩個服務。
[root@node21 software]# scp -r install_solr_service.sh solr-7.4.0.tgz root@node22:`pwd` [root@node22 software]# mkdir /opt/module/solr [root@node22 software]# bash ./install_solr_service.sh solr-7.4.0.tgz -i /opt/module/solr -d /opt/module/solr/solrhome -u solr -s solr -p 8983 [root@node22 software]# mkdir /opt/module/solr2 [root@node22 software]# bash ./install_solr_service.sh solr-7.4.0.tgz -i /opt/module/solr2 -d /opt/module/solr2/solrhome -u solr -s solr2 -p 8984
啟動停止驗證服務命令
#在root用戶下操作
service solr start|stop|status
service solr2 start|stop|ststus
修改對應jetty服務的端口8983/8984
[root@node21 solr]# vi /opt/module/solr/solrhome/data/solr.xml [root@node21 solr]# vi /opt/module/solr2/solrhome/data/solr.xml [root@node22 solr]# vi /opt/module/solr/solrhome/data/solr.xml [root@node22 solr]# vi /opt/module/solr2/solrhome/data/solr.xml
4. 配置zk啟動優先級
solr集群需要zk來管理節點,目前solr是開機自啟動,然后自己手工再啟動zookeeper,solr是不能訪問的,要求zookeeper集群先於solr集群啟動,因此在設置zookeeper集群開機啟動前要先查看solr開機啟動的優先級 ls /etc/rc3.d/*solr*
"S50solr"分析:
S:代表啟動
50:代表啟動的順序,值越小越先啟動
solr:服務名字,就是/etc/init.d中的文件名
因zookeeper要先於solr啟動,所以它的啟動順序對應的值應該小於50
編寫腳本設置zookeeper開機啟動
[root@node21 software]# vi /etc/init.d/zookeeper #!/bin/bash #chkconfig:2345 20 90 #description:zookeeper #processname:zookeeper export JAVA_HOME=/opt/module/jdk1.8 export ZOO_LOG_DIR=/opt/module/zookeeper-3.4.12/logs case $1 in start) su root /opt/module/zookeeper-3.4.12/bin/zkServer.sh start;; stop) su root /opt/module/zookeeper-3.4.12/bin/zkServer.sh stop;; status) su root /opt/module/zookeeper-3.4.12/bin/zkServer.sh status;; restart) su root /opt/module/zookeeper-3.4.12/bin/zkServer.sh restart;; *) echo "require start|stop|status|restart" ;; esac
其中chkconfig:2345 20 90非常重要
2345:為主機運行的級別,表示主機運行在2、3、4、5個級別時都會啟動zookeeper,而0 、1、6級別時停止zookeeper
20:開機啟動的優先級,要比solr的50大
90:關機停止的順序
一般設置時,先啟動,則后停止,注意不要把啟動值設置得太小,否則可能一些系統核心服務還沒有啟動起來,導致你的應用無法啟動
JAVA_HOME是必須的
ZOO_LOG_DIR是可選的,用於保存zookeeper啟動時的日志文件,我把它指定到了自定義目錄,否則你得使用root用戶啟動zookeeper或者給root用戶根目錄的寫權限授予給zookeeper的啟動用戶
賦權限給腳本,啟動zookeeper,設置為開機啟動
[root@node21 software]# chmod +x /etc/init.d/zookeeper
[root@node21 software]# service zookeeper start
[root@node21 software]# chkconfig --add zookeeper
然后去/etc/rc3.d中查看zookeeper的啟動順序,查看zookeeper的停止順序
[root@node21 software]# ls /etc/rc3.d/*zoo* /etc/rc3.d/S20zookeeper [root@node21 software]# ls /etc/rc0.d/*zoo* /etc/rc0.d/K90zookeeper
5. 關聯solr集群與zk集群
1)設置zookeeper集群關聯solr集群,更新Solr的包含文件(solr.in.sh
或solr.in.cmd),這樣就不必在啟動Solr時輸入zk連接地址。
[root@node21 solr]# vi /etc/default/solr.in.sh vi /etc/default/solr2.in.sh [root@node22 solr]# vi /etc/default/solr.in.sh vi /etc/default/solr2.in.sh 修改如下信息(對應主機host注意更改): ZK_HOST="node21:2181,node22:2181,node23:2181/solr" SOLR_HOST="192.168.100.21"
首次連接需要創建節點管理目錄
[root@node21 solr]# ./solr/bin/solr zk mkroot /solr -z node21:2181,node22:2181,node23:2181
2)使用Solr的ZooKeeper CLI上傳solr配置信息到zk節點
[root@node21 module]# sh solr/solr-7.4.0/server/scripts/cloud-scripts/zkcli.sh -zkhost node21:2181,node22:2181,node23:2181 -cmd upconfig -confdir /opt/module/solr/solr- 7.4.0/server/solr/configsets/_default/conf -confname myconf
查看配置文件是否上傳成功,zk客戶端查看zkCli.sh
[zk: localhost:2181(CONNECTED) 13] ls /configs/myconf [protwords.txt, managed-schema, solrconfig.xml, synonyms.txt, stopwords.txt, lang, params.json] [zk: localhost:2181(CONNECTED) 14] ls /solr/configs/_default [managed-schema, protwords.txt, solrconfig.xml, synonyms.txt, stopwords.txt, lang, params.json]
6.配置solr集群的分片規則
創建core與collection,分片規則可以自定義,也可以自動分配,我這里采取自動分配
[root@node21 solr]# ./solr/bin/solr create -c collection1 -n collection1 -shards 2 -replicationFactor 2 -p 8983 -force [root@node22 solr2]# ./solr2/bin/solr create -c collection2 -n collection2 -shards 2 -replicationFactor 2 -p 8984 -force
參數說明:
-c <name>
要創建的核心或集合的名稱(必需)。
-n <configName>
配置名稱,默認與核心或集合的名稱相同。
-p <port>
發送create命令的本地Solr實例的端口; 默認情況下,腳本會嘗試通過查找正在運行的Solr實例來檢測端口。
-s <shards>
要么 -shards
將集合拆分為的分片數,默認為1; 僅適用於Solr在SolrCloud模式下運行的情況。
-rf <replicas>
要么 -replicationFactor
集合中每個文檔的副本數。默認值為1(無復制)。
-force
如果嘗試以“root”用戶身份運行create,則腳本將退出並顯示運行Solr或針對Solr的操作作為“root”的警告可能會導致問題。可以使用-force參數覆蓋此警告。
-d <confdir>
配置目錄。默認為_default
。
7. Web界面查看solr集群狀態
solr默認端口,查看地址:http://192.168.100.21:8983/solr/#/
zk客戶端查看solr集群信息
jps查看服務進程
刪除core與collection
[root@node21 solr]# ./solr/bin/solr delete -c collection1 INFO - 2018-09-09 07:59:04.479; org.apache.solr.util.configuration.SSLCredentialProviderFactory; Processing SSL Credential Provider chain: env;sysprop { "responseHeader":{ "status":0, "QTime":1560}, "success":{ "192.168.100.21:8983_solr":{"responseHeader":{ "status":0, "QTime":305}}, "192.168.100.21:8984_solr":{"responseHeader":{ "status":0, "QTime":305}}, "192.168.100.22:8983_solr":{"responseHeader":{ "status":0, "QTime":353}}, "192.168.100.22:8984_solr":{"responseHeader":{ "status":0, "QTime":421}}}} Deleted collection 'collection1' using command: http://192.168.100.21:8984/solr/admin/collections?action=DELETE&name=collection1
8. 安裝IK中文分詞器
解壓ik中文分詞安裝包
[root@node21 software]# ls ikanalyzer-solr5/ ext.dic IKAnalyzer.cfg.xml ik-analyzer-solr5-5.x.jar solr-analyzer-ik-5.1.0.jar stopword.dic
1)將IK分詞器 JAR 包拷貝到各solr安裝節點solr-7.4.0/server/solr-webapp/webapp/WEB-INF/lib/下
[root@node21 ikanalyzer-solr5]# cp ik-analyzer-solr5-5.x.jar solr-analyzer-ik-5.1.0.jar /opt/module/solr/solr-7.4.0/server/solr-webapp/webapp/WEB-INF/lib/
2)將詞典 配置文件拷貝到各solr節點solr-7.4.0/server/solr-webapp/webapp/WEB-INF/classes下
[root@node21 ikanalyzer-solr5]# mkdir -p /opt/module/solr/solr-7.4.0/server/solr-webapp/webapp/WEB-INF/classes [root@node21 ikanalyzer-solr5]# cp ext.dic IKAnalyzer.cfg.xml stopword.dic /opt/module/solr/solr-7.4.0/server/solr-webapp/webapp/WEB-INF/classes
3)更改在
solr-7.4.0/server/solr/configsets/_default/conf/managed-schema配置文件,末尾添加以下保存
[root@node21 software]# vi /opt/module/solr/solr-7.4.0/server/solr/configsets/_default/conf/managed-schema <!--IK中文分詞器--> <fieldType name="text_ik" class="solr.TextField"> <analyzer type="index" useSmart="false" class="org.wltea.analyzer.lucene.IKAnalyzer" /> <analyzer type="query" useSmart="true" class="org.wltea.analyzer.lucene.IKAnalyzer" /> </fieldType>
4)更新配置文件到zk節點
[root@node21 module]# sh /opt/module/solr/solr-7.4.0/server/scripts/cloud-scripts/zkcli.sh -zkhost node21:2181,node22:2181,node23:2181 -cmd upconfig -confdir /opt/modu le/solr/solr-7.4.0/server/solr/configsets/_default/conf -confname myconf
5)重啟服務測試中文分詞
root@node21 solr]# ./solr/bin/solr create -c collection3 -n collection3 -shards 2 -replicationFactor 2 -p 8983 -force
三.設置禁止開機啟動項
測試完畢之后,我這里設置solr與zookeeper服務禁止開機啟動,
顯示開機可以自動啟動的服務 [root@node21 ~]# chkconfig --list 添加開機自動啟動***服務 [root@node21 ~]# chkconfig --add *** 刪除開機自動啟動***服務 [root@node21 ~]# chkconfig --del ***
查看
[root@node21 ~]# chkconfig --list Note: This output shows SysV services only and does not include native systemd services. SysV configuration data might be overridden by native systemd configuration. If you want to list systemd services use 'systemctl list-unit-files'. To see services enabled on particular target use 'systemctl list-dependencies [target]'. netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off network 0:off 1:off 2:on 3:on 4:on 5:on 6:off redis_6379 0:off 1:off 2:on 3:on 4:on 5:on 6:off solr 0:off 1:off 2:on 3:on 4:on 5:on 6:off solr2 0:off 1:off 2:on 3:on 4:on 5:on 6:off zookeeper 0:off 1:off 2:on 3:on 4:on 5:on 6:off xinetd based services: chargen-dgram: off chargen-stream: off daytime-dgram: off daytime-stream: off discard-dgram: off discard-stream: off echo-dgram: off echo-stream: off tcpmux-server: off time-dgram: off time-stream: off
之后手動啟動
[root@node23 ~]# service zookeeper start [root@node23 ~]# service solr start [root@node23 ~]# service solr2 start