您的位置:首页 > 其它

Solr之——Solr4.4主从模式配置

2015-12-10 17:26 441 查看
转载请注明出处:/article/8380957.html

废话不多说,直接进入主题,我们一起来搭建solr主从配置框架。

一、主从模式介绍

Solr(这里指Solr4.4)的主从模式实现是这样一个思路。首先至少有两个solr服务器,一个称为master(主服务),其余的一个或多个solr服务器被称为slave(附属服务)。每个slave轮询检查master的索引库版本,如果发现索引库版本有更新就会拉取master上的索引库。下图为solr官方文档的结构图:



注意:目前的我看到的文档只能slave从master获取索引库,反过来master从slave获取索引库是不行的。如果非要实现这种效果过可以使用Repeater(中转器)概念解决

二、安装Tomcat+Solr

2.1使用环境

操作系统:Windows7

服务器:Tomcat6(绿色版)

JDK:JDK1.6

Solr:Solr4.4

2.2 安装步骤

1、修改tomcat的connector编码为utf-8。(打开tomcat6\conf\server.xml修改如下内容)

<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="UTF-8" />


2、 解压solr4.4 zip包将solr-4.4.0\example\下的solr目录复制到另一个文件夹,这里为c:\web。(这个目录用于设置为solrhome参数)

3、 复制solr4.4\example\webapps\solr.war到tomcat的webapps目录。然后将solr4.4\example\lib\ext\下的jar包放到tomcat的lib目录。

启动一下tomcat,解压开solr.war。打开tomcat6\webapps\solr\web-inf\web.xml.修改如下内容:

<env-entry>
<env-entry-name>solr/home</env-entry-name>
<env-entry-value>C:/web/solr</env-entry-value>
<env-entry-type>java.lang.String</env-entry-type>
</env-entry>

4、 重启tomcat,能正常的看到如下页面(图2-1),说明安装成功。

注意:因为我在安装的时候遇到过一些filer不能启动的问题。如果出现这个异常记得在catalina.bat中加入如下参数: set JAVA_OPTS="-Dsolr.solr.home=c:/web/solr-Dsolr.velocity.enabled=false"。



三、配置Solr主从模式

本博文的目的是为了解决索引库的备份问题,在官网还提供了另一种方式就是基于shell脚本的方式,感兴趣的同学可以去参考一下apache-solr-ref-guide-4.4文档。这里我们只介绍基于主从模式的索引复制。

The Java-based implementation of index replication offers these benefits:

以下是官方给出的使用Solr本身完成索引库备份的优点:

1、 Replication without requiring external scripts

不依赖于外部脚本。

2、 The configuration affecting replication is controlled by a single file, solrconfig.xml

配置方式控制在一个文件中solrconfig.xml

3、 Supports the replication of configuration files as well as index files

支持复制配置文件和索引文件

4、 Works across platforms with same configuration

配置是跨平台的

5、 No reliance on OS-dependent hard links

不依赖系统特定的系统

6、 Tightly integrated with Solr; an admin page offers fine-grained control of each aspect of replication

与Solr紧密集成

7、 The Java-based replication feature is implemented as a RequestHandler. Configuring replication is therefore similar to any normal RequestHandler.

基于java的备份的特点是使用RequestHandler实现。配置备份和普通的RequestHandle非常相似

3.1 配置Master端

打开SolrHome\collections1\conf\solrconfig.xml,找到name为/replication的requestHandler节点,修改为如下代码:

<requestHandler name="/replication" class="solr.ReplicationHandler" >
<int name="maxNumberOfBackups">2</int>
<lst name="master">
<str name="replicateAfter">commit</str>
<str name="replicateAfter">optimize</str>
<str name="backupAfter">optimize</str>
<str name="confFiles">solrconfig.xml:cp_solrconfig.xml,schema.xml</str>
</lst>
</requestHandler>
参数解释:

replicateAfter:指定一个字符串表示在什么动作发生之后触发动作。可选值:commit,、optimize、 startup,也可以配置多个值,如果你配置为startup必须至少配置commit或optimize其中一项。

confFiles:指定需要复制的配置文件。可以指定多个配置文件用“,”隔开,默认复制conf目录下的配置文件。FileName:newFileName ,filename指的是master服务器上的名字,newFileName指的是复制到slave服务器上的名字。

backupAfter:用于指定何时产生一个备份。可选值:commit,、optimize、 startup。

maxNumberOfBackups:指定可以有多少份备份。

commitReserveDuration:如果你的索引库更新非常频繁而且网速又很慢,你可以指定这个参数增加他的时间值。默认为10秒。图3-1为官方对于参数解释的文档。



3.2 配置Slave端

打开SolrHome\collections1\conf\solrconfig.xml,找到name为/replication的requestHandler节点,修改为如下代码:

<requestHandler name="/replication" class="solr.ReplicationHandler" >
<lst name="slave">
<str name="masterUrl">http://masterHost:port/solr/collection1/replication</str>
<str name="pollInterval">00:00:20</str>
</lst>
</requestHandler>
参数解释:

masterUrl:master端的url。

pollInterval:间隔多少时间去master端拉取索引库。

<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://remote_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 honor 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>

3.3 测试

1、 启动你的master和slave服务,打开master端更新索引库。

2、 等到你配置的间隔的时间值后slave端服务器就可以查询到你刚才添加的索引了。如下图。

Master端



Slave端



3.4 Slave工作模式

Master像是一个仓库,Salve持续不断的(根据你定义的间隔时间)去检查Master索引库的版本。如果它发现一个新的版本Slave就会启动一个索引库复制的处理。步骤如下(来自apache-solr-ref-guide-4.4.pdf):

1、 The slave issues a filelist command to get the list of the files. This command returns the names of the files as well as some metadata (for example, size, a lastmodified timestamp, an alias if any).

2、 The slave checks with its own index if it has any of those files in the local index. It then runs the filecontent command to download the missing files. This uses a custom format (akin to the HTTP chunked encoding) to download the full content or a part
of each file. If the connection breaks in between , the download resumes from the point it failed. At any point, the slave tries 5 times before giving up a replication altogether.

3、 The files are downloaded into a temp directory, so that if either the slave or the master crashes during the download process, no files will be corrupted. Instead, the current replication will simply abort.

4、 After the download completes, all the new files are moved to the live index directory and the file's timestamp is same as its counterpart on the master.

5、 A commit command is issued on the slave by the Slave's ReplicationHandler and the new index is loaded.

四、相关知识

4.1 Repeater(中转器)

当你的系统很庞大的时候可能会有很多个slave节点,这样如果每个slave节点都从远程下载索引会耗尽带宽。解决这个问题就需要用到Repeater这个概念,Repeater就是将master和slave配置到一个服务器上。这样一个slave负责从远程master上下载索引,局域网内其他的slave从这个Repeater上下载。

4.2 RepplicationHandler的HTTP接口

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: