搞定了一切的一切以後下一步就是正式使用了,可是以前介紹的都是在單臺服務器上進行的部署,若是在生產環境出現了單臺故障怎麼辦呢?提供穩定性和性能的最直觀的方式就是集羣,solr官方提供了cloud的集羣方式java
附上:mysql
喵了個咪的博客:http://w-blog.cnweb
Solr官網:http://lucene.apache.org/solr/sql
PS:8.0.0版本已經發布,本文使用此時較爲穩定的7.7.1版本apache
下載solrvim
> 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目錄新建一個目錄,取名solrtomcat
> cd /data/wwwroot/default > mkdir solr
> 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
> mkdir /home/solr-home > cp -rf /usr/local/solr-7.7.1/server/solr/* /home/solr-home/
> 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> -->
分別給予權限服務器
> chown -R www:www /home/solr-home/ > chown -R www:www /data/wwwroot/default/solr/
把配置好的solr 和 solrhome 複製到其他兩個節點app
> 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了
PS:集羣下的Collection等同於單機的core
使用SolrCloud來去進行配置過程和單機模式基本一致,可是有如下幾個多出來的事情:
> 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增長新的分詞
> 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
承載了接口請求的節點壓力會比較大,其他在集羣狀況下提供了支持