Solr搜索引擎 — SolrCloud安裝和集羣配置

搞定了一切的一切之後下一步就是正式使用了,但是之前介紹的都是在單臺服務器上進行的部署,如果在生產環境出現了單臺故障怎麼辦呢?提供穩定性和性能的最直觀的方式就是集羣,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包同理


五、集羣機制測試

測試容錯

  1. 關閉主節點solr-1
  2. 在數據查詢上 另外兩個節點依然可以提供數據查詢服務
  3. 重啓 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

承載了接口請求的節點壓力會比較大,其餘在集羣情況下提供了支持

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章