zookeeper集群和Solr集群的搭建

1 zookeeper集群搭建

1.1 环境准备

CentOS-6.4-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

需要三个zookeeper节点
需要四个tomcat节点。
建议虚拟机的内存1G以上。

1.2 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。例如1、2、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

第七步:修改配置文件。把每一个zookeeper下的conf目录下的zoo_sample.cfg文件改名为zoo.cfg
在zookeeper01目录下的配置如下:(注意:配置的端口都不能冲突) 同理在zookeeper02 03下配置,注意端口不能冲突。

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

第八步:启动每个zookeeper实例。
a) 创建启动实例的批处理文件:在solr-cloud下创建,命令如下

[root@localhost solr-cloud]# vim zookeeper_start_all.sh

b) 使用vim 编辑内容如下:

cd /usr/local/solr-cloud/zookeeper01/bin
./zkServer.sh start

cd /usr/local/solr-cloud/zookeeper02/bin
./zkServer.sh start

cd /usr/local/solr-cloud/zookeeper03/bin
./zkServer.sh start

按ESC
输入:wq保存
退出。

改成可执行权限:

[root@localhost solr-cloud]# chmod u+x zookeeper_start_all.sh

c) 启动所有zookeeper实例

[root@localhost solr-cloud]# ./zookeeper_start_all.sh

查看zookeeper的状态:

[root@localhost solr-cloud]# zookeeper01/bin/zkServer.sh status

效果如下:
在这里插入图片描述

2 SolrCloud搭建

2.1 什么是SolrCloud

SolrCloud(solr 云)是Solr提供的分布式搜索方案,当你需要大规模,容错,分布式索引和检索能力时使用 SolrCloud。当索引量很大,搜索请求并发很高,这时需要使用SolrCloud来满足这些需求。
SolrCloud是基于Solr和Zookeeper的分布式搜索方案,它的主要思想是使用Zookeeper作为集群的配置信息中心。
Zookeeper它有几个特色功能:
1)集中式的配置信息(数据库连接池的配置文件,修改文件不用重启就可以生效)
2)自动容错
3)近实时搜索
4)查询时自动负载均衡

2.2 环境准备

CentOS-6.4-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

需要三个zookeeper节点
需要四个tomcat节点。
建议虚拟机的内存1G以上。

2.3 安装步骤

第一步:创建四个tomcat实例。每个tomcat运行在不同的端口。8180、8280、8380、8480
第二步:部署solr的war包。把单机版的solr工程复制到集群中的tomcat中。
第三步:为每个solr实例创建一个对应的solrhome。使用单机版的solrhome复制四份。
第四步:需要修改solr的web.xml文件。把solrhome关联起来。
第五步:配置solrCloud相关的配置。每个solrhome下都有一个solr.xml,把其中的ip及端口号配置好。
在这里插入图片描述
第六步:让zookeeper统一管理配置文件。需要把solrhome/collection1/conf目录上传到zookeeper。上传任意solrhome中的配置文件即可。(需要保证zookeeper集群是启动的状态)
使用工具上传配置文件:/root/solr-4.10.3/example/scripts/cloud-scripts/zkcli.sh

./zkcli.sh -zkhost 192.168.25.128:2281,192.168.25.128:2282,192.168.25.128:2283 -cmd upconfig -confdir /usr/local/solr-cloud/tomcat01/solrhome/collection1/conf/ -confname myconf

查看zookeeper上的配置文件:
使用zookeeper目录下的bin/zkCli.sh命令查看zookeeper上的配置文件:
注意点(如果不是默认的端口2181,要连接使用命令:
./zkCli.sh -server 192.168.25.154:2181
解释:
-server :指定ip地址
:2181 :指定连接的端口。

[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

第七步:修改(4个)每一个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集群是启动状态。
第九步:访问集群
在这里插入图片描述
第十步:创建新的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
在这里插入图片描述

3 使用solrJ管理集群

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

	}

3.2 把搜索功能切换到集群版

<?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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值