关于solr的集群主要分为主从和SolrCloud两种。主从,比较适合以读为主的场景。SolrCloud适合数据量大,时不时会有更新的情形。那么solr的主从配置很简单。在solrconfig.xml中找到 <requestHandler name="/replication" class="solr.ReplicationHandler" > 。这里的replication主要解决主从复制的。它主要实现:在主进行数据写操做,在slave节点进行读操做。当并发量大些,能够经过扩展slave节点数来应对,多个slave作一个反向代理和负载均衡(在本文中,就不作说明了,若有须要,能够使用nginx或者apache等负载软件),供查询使用。好了,先看看主节点配置:java
<requestHandler name="/replication" class="solr.ReplicationHandler" > <!-- To enable simple master/slave replication, uncomment one of the sections below, depending on whether this solr instance should be the "master" or a "slave". If this instance is a "slave" you will also need to fill in the masterUrl to point to a real machine. --> <lst name="master"> <str name="replicateAfter">commit</str> <str name="replicateAfter">startup</str> <str name="confFiles">schema.xml,stopwords.txt,spellings.txt,synonyms.txt</str> </lst> <!-- <lst name="slave"> <str name="masterUrl">http://your-master-hostname:8983/solr</str> <str name="pollInterval">00:00:60</str> </lst> -->
master 标志该core 为主节点。复制的行为发生在commit、startup以后。cofFiles表示,向从节点复制的配置文件(记住,主从的solrconfig.xml配置不同,不要把solrconfig.xml也复制到从节点了)。
再看看slave节点的配置,slave配置很简单,把上面的配置文件中master那段注释掉。把slave那段放开便可。将masterUrl换成master的url,格式:http://your-master-host:port/solr/your_core_name。具体配置以下:
<requestHandler name="/replication" class="solr.ReplicationHandler" > <!-- To enable simple master/slave replication, uncomment one of the sections below, depending on whether this solr instance should be the "master" or a "slave". If this instance is a "slave" you will also need to fill in the masterUrl to point to a real machine. --> <!-- <lst name="master"> <str name="replicateAfter">commit</str> <str name="replicateAfter">startup</str> <str name="confFiles">schema.xml,stopwords.txt</str> </lst> --> <lst name="slave"> <str name="masterUrl">http://192.9.104.116:8090/solr/POI</str> <str name="pollInterval">00:00:20</str> </lst> </requestHandler>
pollInterval 表示多久向master同步一次数据,数据格式{时}:{分}:{秒}。这个要根据你的业务场景。若是更新比较频繁,就把这个值调小点,反之,就调大些。在同步数据时,根据网络和机器配置等不一样,slave之间的数据会存在不一样步的状况。若是,你对此有要求,须要注意了。总之,任何一种集群方案都不是万能的。solr的主从模式目前存在诸多问题,好比:主节点有单点故障等等,但愿后续的版本会有些改进。