關於solr的集群主要分為主從和SolrCloud兩種。主從,比較適合以讀為主的場景。SolrCloud適合數據量大,時不時會有更新的情形。那么solr的主從配置很簡單。在solrconfig.xml中找到 <requestHandler name="/replication" class="solr.ReplicationHandler" > 。這里的replication主要解決主從復制的。它主要實現:在主進行數據寫操作,在slave節點進行讀操作。當並發量大些,可以通過擴展slave節點數來應對,多個slave做一個反向代理和負載均衡(在本文中,就不做說明了,如有需要,可以使用nginx或者apache等負載軟件),供查詢使用。好了,先看看主節點配置:
<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的主從模式目前存在諸多問題,比如:主節點有單點故障等等,希望后續的版本會有些改進。