您的位置:首页 > 编程语言 > Java开发

Solr 基于java的复制(Master-Slave配置)

2012-02-08 10:46 477 查看
转载:http://blog.csdn.net/ahopedog/article/details/5993191

Solr支持两种复制,一是java实现的复制,通过RequestHandler实现,Slave定期调用Master的ReqeuestHandler,与Master上的索引同步。另外是利用Unix脚本实现的复制。本文以案例的形式详细说明第一种复制的配置过程,搭建一个Master-Slave环境的Solr服务。

Solr基于J***A的主从复制有下面几个优点:


无需依赖外部脚本


只需在solrconfig.xml中配置


可以赋值配置文件


可以用相同的配置实现扩平台复制


无需依赖操作系统


与solr紧密集成,可以通过管理页面查看每个复制的状态

在本文中使用了2台独立的服务器,一台作为Master服务器,另一台作为Slave服务器。Slave定时从Master上复制索引文件到本地,保证索引文件同步。多个Slave的配置与一个Slave的配置相同,可以参考本案例自行扩展。

测试环境

服务器1(Master)

操作系统: CentOS Linux

IP: 192.168.1.1

JDK: Sun JDK 1.6

Web服务器: Apache Tomcat6

服务器2(Slave)

操作系统: CentOS Linux

IP: 192.168.1.2

JDK: Sun JDK 1.6

Web服务器: Apache Tomcat6

为了确保后面步骤的顺利进行,请先检查一下自己的测试环境是否能够正常运行。比如,每台服务器上的tomcat是否可以正常启动,并能访问默认页面(如果没有默认页面,可以自己建立一个简单jsp/html页面进行一下简单的测试)。tomcat除了在本机可以访问外,还要保证不同的服务器之间可以互相访问运行在其它服务器上的tomcat上的页面。这是因为,Slave的复制是通过http协议访问Master完成的,就像用浏览器访问一个web页面一样。

主要的配置工作可以分成2大部分,一是tomcat上配置solr应用, 二是配置solr的配置文件。

本文中可能会忽略一些关于solr的基本概念或基本solr的配置方面的说明,如果需要帮助请参考关于solr相关方面的文档。

准备工作:

在2台服务器上建立如下目录

/usr/local/program/webapps

/usr/local/program/solr.home

webapps用于放置solr的web的应用,solr.home存放solr的配置文件和索引文件。



1:建立solr的web应用

在服务器1上,将solr提供的solr.war拷贝到/usr/local/program/webapps下,solr.war在solr的下载文件中的example/webapps中。

在%CATALINA_HOME%/conf/Catalina/localhost目录下建立一个solr.xml文件,内容如下:

[xhtml]
view plaincopy

<?xml version="1.0" encoding="utf-8"?>
<Context docBase="/usr/local/program/webapps/solr.war" debug="0" crossContext="true">
<Environment name="solr/home" type="java.lang.String" value="/usr/local/program/solr.home" override="true"/>
</Context>




这是一种在tomcat上部署web应用的一种方式,特点是可以将web应用放在tomcat的默认应用目录webapps以外。

solr运行时需要一个变量solr.home,在上面的配置文件中Environment标签在jndi环境中定义了这个变量,使solr的主目录指向/usr/local/program/solr.home。solr主目录中存放着solr的配置文件和索引文件,后面会再次提到。



2:准备solr.home中内容

在下载到的solr压缩文件的example中有一个solr目录,此目录下是solr的一些配置文件,将此目录下的所有文件和子目录一起复制到/usr/local/program/solr.home(solr的主目录)下。

此时solr主目录中应该有2个子目录和一个文件,子目录bin、conf和文件readme.txt,bin和readme.txt不会有什么影响。conf下面是solr的主要配置文件,实现主从服务器复制主要的也就是配置这几个文件。



3:按照步骤1、2在服务器2(Slave)上部署Solr应用并准备好Solr主目录的内容。

4:在Master的配置文件中添加一个RequestHandler

RequestHandler是复制在Master端的主要手段,Slave会定期向这个RequestHandler发送请求,并完成索引文件的同步。

编辑Solr主目录/conf(/usr/local/program/solr.home/conf)下的solrconfig.xml文件,在<config></config>标签内添加如下内容:



[xhtml]
view plaincopy

<requestHandler name="/replication" class="solr.ReplicationHandler" >
<lst name="master">
<!--Replicate on 'startup' and 'commit'. 'optimize' is also a valid value for replicateAfter. -->
<str name="replicateAfter">startup</str>
<str name="replicateAfter">commit</str>
<!--Create a backup after 'optimize'. Other values can be 'commit', 'startup'. It is possible to have multiple entries of this config string. Note that this is just for backup, replication does not require this. -->
<!-- <str name="backupAfter">optimize</str> -->
<!--If configuration files need to be replicated give the names here, separated by comma -->
<str name="confFiles">schema.xml,stopwords.txt,elevate.xml</str>
<!--The default value of reservation is 10 secs.See the documentation below . Normally , you should not need to specify this -->
<str name="commitReserveDuration">00:00:10</str>
</lst>
</requestHandler>




requestHandler的属性name是这个Master上复制处理器的名字,无需修改,在slave的配置文件中将会用到这个属性值。

<lst name="master">说明这个Master节点,如果name=”slave”这个服务器就是Slave节点。

replicateAfter说明什么时候进行复制,取值可以是startup, commit, optimize分别是创建索引、提交索引、优化索引之后。每当这些动作执行结束后,Slave就可以从Master上进行复制了。



5:配置Slave的solrconfig.xml

在Slave服务器上的solr主目录(/usr/local/program/sole.home/conf)中的soleconfig.xml中加入下面配置

[xhtml]
view plaincopy

<requestHandler name="/replication" class="solr.ReplicationHandler" >
<lst name="slave">
<!--fully qualified url for the replication handler of master . It is possible to pass on this as a request param for the fetchindex command-->
<str name="masterUrl">http://master_host:port/solr/corename/replication</str>

<!--Interval in which the slave should poll master .Format is HH:mm:ss . If this is absent slave does not poll automatically. But a fetchindex can be triggered from the admin or the http API -->
<str name="pollInterval">00:00:20</str>

<!-- THE FOLLOWING PARAMETERS ARE USUALLY NOT REQUIRED-->
<!--to use compression while transferring the index files. The possible values are internal|external if the value is 'external' make sure that your master Solr has the settings to honour the accept-encoding header. see here for details http://wiki.apache.org/solr/SolrHttpCompression If it is 'internal' everything will be taken care of automatically. USE THIS ONLY IF YOUR BANDWIDTH IS LOW . THIS CAN ACTUALLY SLOWDOWN REPLICATION IN A LAN-->
<str name="compression">internal</str>

<!--The following values are used when the slave connects to the master to download the index files. Default values implicitly set as 5000ms and 10000ms respectively. The user DOES NOT need to specify these unless the bandwidth is extremely low or if there is an extremely high latency-->
<str name="httpConnTimeout">5000</str>
<str name="httpReadTimeout">10000</str>
<!-- If HTTP Basic authentication is enabled on the master, then the slave can be configured with the following -->
<str name="httpBasicAuthUser">username</str>
<str name="httpBasicAuthPassword">password</str>
</lst>
</requestHandler>




与Master上的配置很相似,只是属性值有些却别。

最重要的一个属性就是:

<str name="masterUrl">http://master_host:port/solr/corename/replication</str>

属性值是一个url,指向了Master服务器上的提供复制功能的requestHandler,

master_host:master服务器的IP或主机名

port:master服务器上部署solr应用的tomcat端口

solr:solr的web应用名称

corename: solr中的core,关于core的概念请参考相关文档。由于本案例中不涉及多个core,所以此处的url中可以去掉这项,也不对core进行过多的说明。

replaction: Master上提供复制的requestHandler名称,也就是requestHandler的name属性的取值。

<str name="pollInterval">00:00:20</str>是Slave定期检测Master的时间,格式是HH:mm:ss。这里配置的时间是20秒,因此Slave每个20秒会主动请求一次Master服务器,获得Master服务器上索引文件的状态,比如最后修改时间,文件尺寸等,如果与本机的索引文件不同就进行复制,实现同步。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: