solr集羣

1. 什麼是SolrCloud

 

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

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

它有幾個特色功能:

1)集中式的配置信息

2)自動容錯

3)近實時搜索

4)查詢時自動負載均衡

 

2. Solr集羣的系統架構

 

2.1. 物理結構

三個Solr實例( 每個實例包括兩個Core),組成一個SolrCloud

2.2. 邏輯結構

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

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

 

2.2.1. collection

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

比如:針對商品信息搜索可以創建一個collection

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

 

2.2.2. Core

每個CoreSolr中一個獨立運行單位,提供 索引和搜索服務。一個shard需要由一個Core或多個Core組成。由於collection由多個shard組成所以collection一般由多個core組成。

2.2.3. MasterSlave

Mastermaster-slave結構中的主結點(通常說主服務器),Slavemaster-slave結構中的從結點(通常說從服務器或備服務器)。同一個Shardmasterslave存儲的數據是一致的,這是爲了達到高可用目的。

2.2.4. Shard

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

 

 

2.3. 需要實現的solr集羣架構

Zookeeper作爲集羣的管理工具。

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

2、配置文件的集中管理

3、集羣的入口

 

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

 

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

 

搭建僞分佈式:

需要三個zookeeper節點

需要四個tomcat節點。

 

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

3. 環境準備

    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

4. 安裝步驟

4.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

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

第六步:在data目錄下創建一個myid文件,文件名就叫做“myid”。內容就是每個實例的id。例如123

[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

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

 

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

 

4.2. Solr集羣的搭建

第一步:創建四個tomcat實例。每個tomcat運行在不同的端口。8180828083808480

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

第三步:爲每個solr實例創建一個對應的solrhome。使用單機版的solrhome複製四份。

第四步:需要修改solrweb.xml文件。把solrhome關聯起來。

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

 

第六步:讓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

查看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

 

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

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

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

 

 

 

第八步:啓動每個tomcat實例。要包裝zookeeper集羣是啓動狀態。

第九步:訪問集羣

 

第十步:創建新的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

 

 

 

5. 使用solrJ管理集羣

5.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();

 

}

 

5.2. 查詢文檔

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

 

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

<?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>

 

發佈了18 篇原創文章 · 獲贊 11 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章