本文共 10039 字,大约阅读时间需要 33 分钟。
SolrCloud(solr 云)是Solr提供的分布式搜索方案,当你需要大规模,容错,分布式索引和检索能力时使用 SolrCloud。
当一个系统的索引数据量少的时候是不需要使用SolrCloud的,当索引量很大,搜索请求并发很高,这时需要使用SolrCloud来满足这些需求。SolrCloud是基于Solr和Zookeeper的分布式搜索方案,它的主要思想是使用Zookeeper作为集群的配置信息中心。集中式的配置信息自动容错近实时搜索查询时自动负载均衡
集群管理:容错、负载均衡。配置文件的集中管理集群的入口
为实现zookeeper 高可用,需要搭建集群。建议是奇数节点,最少需要三个zookeeper服务器。
搭建伪分布式:
需要三个zookeeper节点 需要四个tomcat节点。CentOS-6.5-i386-bin-DVD1.isojdk-7u72-linux-i586.tar.gz(建议虚拟机的内存1G以上。)apache-tomcat-7.0.47.tar.gzzookeeper-3.4.6.tar.gzsolr-4.10.3.tgz
[root@localhost ~]# mkdir /usr/local/solr-cloud[root@localhost ~]# cp -r zookeeper-3.4.6 /usr/local/solr-cloud/zookeeper01[root@localhost ~]# cp -r zookeeper-3.4.6 /usr/local/solr-cloud/zookeeper02[root@localhost ~]# cp -r zookeeper-3.4.6 /usr/local/solr-cloud/zookeeper03
内容就是每个实例的id。这里分别为1、2、3[root@localhost data]# echo 1 >> myid(创建一个名为myid的文件,并向文件中加入1,另外两个同理,也可用touch myid创建文件,vim myid 向文件中写入)[root@localhost data]# lltotal 4-rw-r--r--. 1 root root 2 Apr 7 18:23 myid[root@localhost data]# cat myid 1
[root@Solr zookeeper01]# cd conf[root@Solr conf]# ll总用量 12-rw-r--r--. 1 root root 535 3月 22 08:43 configuration.xsl-rw-r--r--. 1 root root 2161 3月 22 08:43 log4j.properties-rw-r--r--. 1 root root 922 3月 22 08:43 zoo_sample.cfg修改:[root@Solr conf]# mv zoo_sample.cfg zoo.cfg[root@Solr conf]# ll总用量 12-rw-r--r--. 1 root root 535 3月 22 08:43 configuration.xsl-rw-r--r--. 1 root root 2161 3月 22 08:43 log4j.properties-rw-r--r--. 1 root root 922 3月 22 08:43 zoo.cfg修改每个实例的配置文件:zoo.cfg[root@Solr conf]# vim zoo.cfg # The number of milliseconds of each ticktickTime=2000# The number of ticks that the initial# synchronization phase can takeinitLimit=10# The number of ticks that can pass between# sending a request and getting an acknowledgementsyncLimit=5# the directory where the snapshot is stored.# do not use /tmp for storage, /tmp here is just# example sakes.dataDir=/usr/local/solr-cloud/zookeeper01/data/(当前实例的data目录)# the port at which the clients will connectclientPort=2181(端口号,不能重复2181,2182,2183)# the maximum number of client connections.# increase this if you need to handle more clients#maxClientCnxns=60## Be sure to read the maintenance section of the# administrator guide before turning on autopurge.## http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance## The number of snapshots to retain in dataDir#autopurge.snapRetainCount=3# Purge task interval in hours# Set to "0" to disable auto purge feature#autopurge.purgeInterval=1server.1=192.168.25.130:2881:3881server.2=192.168.25.130:2882:3882server.3=192.168.25.130:2883:3883(每个实例的都一样)~~
[root@Solr solr-cloud]# ll总用量 12drwxr-xr-x. 11 root root 4096 3月 22 08:46 zookeeper01drwxr-xr-x. 11 root root 4096 3月 22 08:46 zookeeper02drwxr-xr-x. 11 root root 4096 3月 22 08:46 zookeeper03启动脚本:[root@Solr solr-cloud]# vim start-zookeeper.shcd zookeeper01/bin./zkServer.sh startcd ../../cd zookeeper02/bin./zkServer.sh startcd ../../cd zookeeper03/bin./zkServer.sh startcd ../../"start-zookeeper.sh" [新] 9L, 147C 已写入 [root@Solr solr-cloud]# ll总用量 16-rw-r--r--. 1 root root 147 3月 22 09:24 start-zookeeper.shdrwxr-xr-x. 11 root root 4096 3月 22 08:46 zookeeper01drwxr-xr-x. 11 root root 4096 3月 22 08:46 zookeeper02drwxr-xr-x. 11 root root 4096 3月 22 08:46 zookeeper03[root@Solr solr-cloud]# chmod +x start-zookeeper.sh启动:[root@Solr solr-cloud]# ./start-zookeeper.sh JMX enabled by defaultUsing config: /usr/local/solr-cloud/zookeeper01/bin/../conf/zoo.cfgStarting zookeeper ... STARTEDJMX enabled by defaultUsing config: /usr/local/solr-cloud/zookeeper02/bin/../conf/zoo.cfgStarting zookeeper ... STARTEDJMX enabled by defaultUsing config: /usr/local/solr-cloud/zookeeper03/bin/../conf/zoo.cfgStarting zookeeper ... STARTED[root@Solr solr-cloud]#状态脚本:[root@Solr solr-cloud]# vim status-zookeeper.shcd zookeeper01/bin./zkServer.sh statuscd ../../cd zookeeper02/bin./zkServer.sh statuscd ../../cd zookeeper03/bin./zkServer.sh statuscd ../../"status-zookeeper.sh" [新] 9L, 150C 已写入 [root@Solr solr-cloud]# chmod +x status-zookeeper.sh 查看状态:[root@Solr solr-cloud]# ./status-zookeeper.sh JMX enabled by defaultUsing config: /usr/local/solr-cloud/zookeeper01/bin/../conf/zoo.cfgMode: followerJMX enabled by defaultUsing config: /usr/local/solr-cloud/zookeeper02/bin/../conf/zoo.cfgMode: followerJMX enabled by defaultUsing config: /usr/local/solr-cloud/zookeeper03/bin/../conf/zoo.cfgMode: leader[root@Solr solr-cloud]#
从上面的状态可以看出zookeeper03为内部选举出来的leadder。
zookeeper集群搭建成功!
[root@Solr ~]# cp -r apache-tomcat-7.0.52 /usr/local/solr-cloud/tomcat01[root@Solr ~]# cp -r apache-tomcat-7.0.52 /usr/local/solr-cloud/tomcat02[root@Solr ~]# cp -r apache-tomcat-7.0.52 /usr/local/solr-cloud/tomcat03[root@Solr ~]# cp -r apache-tomcat-7.0.52 /usr/local/solr-cloud/tomcat04[root@Solr solr-cloud]# vim tomcat01/conf/server.xml进行如下修改:
其他tomcat操作一样,端口不能冲突。vim tomcat02/conf/server.xml(8205、8280、8209)vim tomcat03/conf/server.xml(8305、8380、8309)vim tomcat04/conf/server.xml(8405、8480、8409)
cp -r ../tomcat/apache-tomcat-7.0.52/webapps/solr/ tomcat01/webapps/cp -r ../tomcat/apache-tomcat-7.0.52/webapps/solr/ tomcat02/webapps/cp -r ../tomcat/apache-tomcat-7.0.52/webapps/solr/ tomcat03/webapps/cp -r ../tomcat/apache-tomcat-7.0.52/webapps/solr/ tomcat04/webapps/
[root@Solr solr-cloud]# cp -r ../solrhome/solr/ solrhome01/[root@Solr solr-cloud]# cp -r ../solrhome/solr/ solrhome02/[root@Solr solr-cloud]# cp -r ../solrhome/solr/ solrhome03/[root@Solr solr-cloud]# cp -r ../solrhome/solr/ solrhome04/[root@Solr solr-cloud]# ll总用量 52drwxr-xr-x. 4 root root 4096 3月 22 10:07 solrhome01drwxr-xr-x. 4 root root 4096 3月 22 10:07 solrhome02drwxr-xr-x. 4 root root 4096 3月 22 10:07 solrhome03drwxr-xr-x. 4 root root 4096 3月 22 10:07 solrhome04-rwxr-xr-x. 1 root root 147 3月 22 09:24 start-zookeeper.sh-rwxr-xr-x. 1 root root 150 3月 22 09:31 status-zookeeper.shdrwxr-xr-x. 9 root root 4096 3月 22 10:04 tomcat01drwxr-xr-x. 9 root root 4096 3月 22 09:45 tomcat02drwxr-xr-x. 9 root root 4096 3月 22 09:45 tomcat03drwxr-xr-x. 9 root root 4096 3月 22 09:45 tomcat04drwxr-xr-x. 11 root root 4096 3月 22 08:46 zookeeper01drwxr-xr-x. 11 root root 4096 3月 22 08:46 zookeeper02drwxr-xr-x. 11 root root 4096 3月 22 08:46 zookeeper03
[root@Solr solr-cloud]# vim tomcat01/webapps/solr//WEB-INF/web.xml
其他三个同理
host:Linux的ip hostPort:分别为tomcat的端口号8180、8280、8380、8480
把此配置添加到配置文件中(每个tomcat中都加):JAVA_OPTS="-DzkHost=192.168.25.130:2181,192.168.25.130:2182,192.168.25.130:2183"
使用工具上传配置文件:/root/solr-4.10.3/example/scripts/cloud-scripts/zkcli.sh./zkcli.sh -zkhost 192.168.25.130:2181,192.168.25.130:2182,192.168.25.130:2183 -cmd upconfig -confdir /usr/local/solr-cloud/solrhome01/collection1/conf -confname myconf查看zookeeper上的配置文件:使用zookeeper目录下的bin/zkCli.sh命令查看zookeeper上的配置文件:[root@localhost bin]# ./zkCli.sh (默认连到2181,指定:./zkCli.sh -server 192.168.25.130:2182)[zk: localhost:2181(CONNECTED) 0] ls /[configs, zookeeper][zk: localhost:2181(CONNECTED) 1] ls /configs[myconf][zk: localhost:2181(CONNECTED) 2] ls /configs/myconf[admin-extra.menu-top.html, currency.xml, protwords.txt, mapping-FoldToASCII.txt, _schema_analysis_synonyms_english.json, _rest_managed.json, solrconfig.xml, _schema_analysis_stopwords_english.json, stopwords.txt, lang, spellings.txt, mapping-ISOLatin1Accent.txt, admin-extra.html, xslt, synonyms.txt, scripts.conf, update-script.js, velocity, elevate.xml, admin-extra.menu-bottom.html, clustering, schema.xml]退出:[zk: localhost:2181(CONNECTED) 3] quit
创建脚本启动:[root@Solr solr-cloud]# vim start-tomcat.sh/usr/local/solr-cloud/tomcat01/bin/startup.sh/usr/local/solr-cloud/tomcat02/bin/startup.sh/usr/local/solr-cloud/tomcat03/bin/startup.sh/usr/local/solr-cloud/tomcat04/bin/startup.sh
solr:/sbin/iptables -I INPUT -p tcp --dport 8180 -j ACCEPT/sbin/iptables -I INPUT -p tcp --dport 8280 -j ACCEPT/sbin/iptables -I INPUT -p tcp --dport 8380 -j ACCEPT/sbin/iptables -I INPUT -p tcp --dport 8480 -j ACCEPTzookeeper:/sbin/iptables -I INPUT -p tcp --dport 2181 -j ACCEPT/sbin/iptables -I INPUT -p tcp --dport 2182 -j ACCEPT/sbin/iptables -I INPUT -p tcp --dport 2183 -j ACCEPT/etc/rc.d/init.d/iptables save查看状态:/etc/init.d/iptables status
solr集群搭建成功!
地址:192.168.25.130:8180/solr
http://192.168.25.130:8180/solr/admin/collections?action=CREATE&name=collection2&numShards=2&replicationFactor=2成功:
http://192.168.25.130:8180/solr/admin/collections?action=DELETE&name=collection1
SolrCloud搭建成功……
@Test public void addDocument() throws Exception { // 创建一个SolrServer对象,需要使用CloudSolrServer子类。构造方法的参数是zookeeper的地址列表。 CloudSolrServer solrServer = new CloudSolrServer("192.168.25.130:2181,192.168.25.130:2182,192.168.25.130:2183"); // 设置DefaultCollection属性 solrServer.setDefaultCollection("collection2"); // 创建SolrInputDocument对象 SolrInputDocument document = new SolrInputDocument(); // 向文档对象中添加域 document.addField("id", "test001"); document.addField("item_title", "测试商品"); // 把文档对象写入索引库 solrServer.add(document); // 提交 solrServer.commit(); }原文地址