Solr集羣—SolrCloud

 

1.   學習計劃

  一、solr集羣搭建html

  二、使用solrj管理solr集羣linux

  三、把搜索功能切換到集羣版web

 

2.   什麼是SolrCloud

   SolrCloud(solr 雲)是Solr提供的分佈式搜索方案,當你須要大規模,容錯,分佈式索引和檢索能力時使用 SolrCloud。當一個系統的索引數據量少的時候是不須要使用SolrCloud的,當索引量很大,搜索請求併發很高,這時須要使用SolrCloud來知足這些需求。spring

   SolrCloud是基於SolrZookeeper的分佈式搜索方案,它的主要思想是使用Zookeeper做爲集羣的配置信息中心apache

  它有幾個特點功能:編程

    1)集中式的配置信息json

    2)自動容錯設計模式

    3)近實時搜索tomcat

    4)查詢時自動負載均衡服務器

 

3.   Solr集羣的系統架構

   

 

3.1. 物理結構

  三個Solr實例( 每一個實例包括兩個Core,不必定必須是兩個實例Core),組成一個SolrCloud。

3.2. 邏輯結構

  索引集合 collection 包括兩個分片 Shard(shard1和shard2),shard1 和shard2 分別由三個 Core 組成,其中一個Leader兩個Replication,Leader是由zookeeper選舉產生,zookeeper控制每一個shard上三個Core的索引數據一致,解決高可用問題。

  用戶發起索引請求分別從shard1和shard2上獲取,解決高併發問題。

 

3.2.1.    collection

  Collection在SolrCloud集羣中是一個邏輯意義上的完整的索引結構。它經常被劃分爲一個或多個Shard(分片),它們使用相同的配置信息。

  好比:針對商品信息搜索能夠建立一個collection。

   collection=shard1+shard2+....+shardX

 

3.2.2.    Core

  每一個Core是Solr中一個獨立運行單位,提供 索引和搜索服務。一個shard須要由一個Core或多個Core組成。因爲collection由多個shard組成因此collection通常由多個core組成。

3.2.3.    Master或Slave

  Master是master-slave結構中的主結點(一般說主服務器),Slave是master-slave結構中的從結點(一般說從服務器或備服務器)。同一個Shard下master和slave存儲的數據是一致的,這是爲了達到高可用目的

3.2.4.    Shard

  Collection的邏輯分片。每一個Shard被化成一個或者多個replication,經過選舉肯定哪一個是Leader。

 

 

3.3. 須要實現的solr集羣架構

   

 

  Zookeeper做爲集羣的管理工具。

    一、集羣管理:容錯、負載均衡。

    二、配置文件的集中管理

    三、集羣的入口

 

  須要實現zookeeper 高可用。須要搭建集羣。建議是奇數節點。須要三個zookeeper服務器。

 

  搭建solr集羣須要7臺服務器。

 

  搭建僞分佈式:

  須要三個zookeeper節點

  須要四個tomcat節點。

 

  建議虛擬機的內容1G以上。

4.   環境準備

  CentOS-6.5-i386-bin-DVD1.iso

       jdk-7u72-linux-i586.tar.gz

  apache-tomcat-7.0.47.tar.gz

  zookeeper-3.4.6.tar.gz

  solr-4.10.3.tgz

5.   安裝步驟

5.1. Zookeeper集羣搭建

  第一步:須要安裝jdk環境。

  第二步:把zookeeper的壓縮包上傳到服務器。

  第三步:解壓縮。

  第四步:把zookeeper複製三份。

[root@localhost ~]# mkdir /usr/local/solr-cloud [root@localhost ~]# cp -r zookeeper-3.4.6 /usr/local/solr-cloud/zookeeper01 [root@localhost ~]# cp -r zookeeper-3.4.6 /usr/local/solr-cloud/zookeeper02 [root@localhost ~]# cp -r zookeeper-3.4.6 /usr/local/solr-cloud/zookeeper03

 5.1.1.  建立Zookeeper標識myid

  第五步:在每一個zookeeper目錄下建立一個data目錄。

  第六步:在data目錄下建立一個myid文件,文件名就叫作「myid」。內容就是每一個實例的id。例如一、二、3

[root@localhost data]# echo 1 >> myid [root@localhost data]# ll total 4

-rw-r--r--. 1 root root 2 Apr  7 18:23 myid [root@localhost data]# cat myid 1

 5.1.2.  修改zoo.cfg,Zookeeper互聯

  第七步:修改配置文件。把conf目錄下的zoo_sample.cfg文件更名爲zoo.cfg

  

  2881是Zookeeper集羣的默認的通訊端口,3881是Zookeeper默認的選舉端口

server.1=192.168.25.154:2881:3881 server.2=192.168.25.154:2882:3882 server.3=192.168.25.154:2883:3883

 

  第八步:啓動每一個zookeeper實例。 

    啓動:bin/zkServer.sh start

 

  查看zookeeper的狀態:

    查看:bin/zkServer.sh status

 

5.2. Solr集羣的搭建

  solr集羣配置的兩點關鍵:

    一、配置集羣中各個solr項目之間的互聯 — solrHome 下的 solr.xml

    二、配置solr(tomcat)和Zookeeper的關聯  — tomcat/bin/catalina.sh

                         — Zookeeper集中管理配置文件

5.2.1.  部署Solr

  第一步:建立四個tomcat實例。每一個tomcat運行在不一樣的端口,8180、8280、8380、8480,須要修改tomcat的配置文件。

  第二步:部署solr的war包。把單機版的solr工程複製到集羣中的tomcat中。

5.2.2.  關聯solrHome

  第三步:爲每一個solr實例建立一個對應的solrhome(不太嚴謹,嚴謹來說,爲每一個solr項目建立對應的solrHom,一個solrHome下能夠有多個solr實例—solrCore)。使用單機版的solrhome複製四份。

  第四步:須要修改solr的web.xml文件。把solrhome和solr項目關聯起來。

5.2.3.  配置solrCloud,集羣模式

  第五步:配置solrCloud相關的配置。每一個solrhome下都有一個solr.xml,把其中各個部署的solr項目的ip及端口號配置好。

   

5.2.4.  上傳配置文件到Zookeeper  

  第六步:讓zookeeper統一管理配置文件。須要把solrhome/collection1/conf目錄上傳到zookeeper。上傳任意solrhome中的配置文件便可。

  使用工具上傳配置文件:/root/solr-4.10.3/example/scripts/cloud-scripts/zkcli.sh

./zkcli.sh -zkhost 192.168.25.154:2181,192.168.25.154:2182,192.168.25.154:2183 -cmd upconfig -confdir /usr/local/solr-cloud/solrhome01/collection1/conf -confname myconf

./zkcli.sh -zkhost 192.168.25.154:2181,192.168.25.154:2182,192.168.25.154:2183 -cmd  upconfig  -confdir /usr/local/solr-cloud/solrhome01/collection1/conf -confname  myconf

查看zookeeper上的配置文件:

使用zookeeper目錄下的bin/zkCli.sh命令查看zookeeper上的配置文件:

  

[root@localhost bin]# ./zkCli.sh [zk: localhost:2181(CONNECTED) 0] ls / [configs, zookeeper] [zk: localhost:2181(CONNECTED) 1] ls /configs [myconf] [zk: localhost:2181(CONNECTED) 2] ls /configs/myconf [admin-extra.menu-top.html, currency.xml, protwords.txt, mapping-FoldToASCII.txt, _schema_analysis_synonyms_english.json, _rest_managed.json, solrconfig.xml, _schema_analysis_stopwords_english.json, stopwords.txt, lang, spellings.txt, mapping-ISOLatin1Accent.txt, admin-extra.html, xslt, synonyms.txt, scripts.conf, update-script.js, velocity, elevate.xml, admin-extra.menu-bottom.html, clustering, schema.xml] [zk: localhost:2181(CONNECTED) 3]

 

退出:

[zk: localhost:2181(CONNECTED) 3] quit

5.2.5.  關聯solr(tomcat)和Zookeeper

第七步:修改tomcat/bin目錄下的catalina.sh 文件,關聯solr和zookeeper。

把此配置添加到配置文件中:

JAVA_OPTS="-DzkHost=192.168.25.154:2181,192.168.25.154:2182,192.168.25.154:2183"

 

 

第八步:啓動每一個tomcat實例。要保證zookeeper集羣是啓動狀態。

第九步:訪問集羣

 

 

5.2.6.  分片操做

第十步:建立新的Collection進行分片處理

http://192.168.25.154:8180/solr/admin/collections?action=CREATE&name=collection2&numShards=2&replicationFactor=2

 

 

第十一步:刪除不用的Collection

http://192.168.25.154:8180/solr/admin/collections?action=DELETE&name=collection1

 

 

 

 

 

6.   使用solrJ管理集羣

6.1. 添加文檔

使用步驟:

第一步:把solrJ相關的jar包添加到工程中。

第二步:建立一個SolrServer對象,須要使用CloudSolrServer子類。構造方法的參數是zookeeper的地址列表

第三步:須要設置DefaultCollection屬性。

第四步:建立一SolrInputDocument對象。

第五步:向文檔對象中添加域

第六步:把文檔對象寫入索引庫。

第七步:提交。

 

@Test public void testSolrCloudAddDocument() throws Exception { // 第一步:把solrJ相關的jar包添加到工程中。 // 第二步:建立一個SolrServer對象,須要使用CloudSolrServer子類。構造方法的參數是zookeeper的地址列表。 //參數是zookeeper的地址列表,使用逗號分隔
        CloudSolrServer solrServer = new CloudSolrServer("192.168.25.154:2181,192.168.25.154:2182,192.168.25.154:2183"); // 第三步:須要設置DefaultCollection屬性
        solrServer.setDefaultCollection("collection2"); // 第四步:建立一SolrInputDocument對象。
        SolrInputDocument document = new SolrInputDocument(); // 第五步:向文檔對象中添加域
        document.addField("item_title", "測試商品"); document.addField("item_price", "100"); document.addField("id", "test001"); // 第六步:把文檔對象寫入索引庫。
 solrServer.add(document); // 第七步:提交。
 solrServer.commit(); }

 

6.2. 查詢文檔

建立一個CloudSolrServer對象,其餘處理和單機版一致。

 

7.   把搜索功能切換到集羣版

  利用策略設計模式,基於接口編程,dao層在操做和維護索引庫的時候使用父類SolrServer,根據配置文件,pring @autowired註解會按照類型自動注入對應的SolrServer對象,以實現單機版和集羣版的切換。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx4.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util4.2.xsd">

    <!-- 單機版solr服務配置 -->
    <!-- <bean id="httpSolrServer" class="org.apache.solr.client.solrj.impl.HttpSolrServer"> <constructor-arg name="baseURL" value="http://192.168.25.154:8080/solr"></constructor-arg> </bean> -->
    <!-- 集羣版solr服務 -->
    <bean id="cloudSolrServer" class="org.apache.solr.client.solrj.impl.CloudSolrServer">
        <constructor-arg name="zkHost" value="192.168.25.154:2181,192.168.25.154:2182,192.168.25.154:2183"></constructor-arg>    
        <property name="defaultCollection" value="collection2"></property>
    </bean>
</beans>
相關文章
相關標籤/搜索