關於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的主從模式目前存在諸多問題,好比:主節點有單點故障等等,但願後續的版本會有些改進。