CentOS-6.4-i386-bin-DVD1.iso
jdk-7u72-linux-i586.tar.gz
apache-tomcat-7.0.47.tar.gz
zookeeper-3.4.6.tar.gz
solr-4.10.3.tgz
-
-
- zookeeper集群安装
-
第一步:解压zookeeper,tar -zxvf zookeeper-3.4.6.tar.gz将zookeeper-3.4.6拷贝到/usr/local/solrcloud下,复制三份分别并将目录名改为zookeeper1、zookeeper2、zookeeper3
第二步:进入zookeeper1文件夹,创建data目录。并在data目录中创建一个myid文件内容为“1”(echo 1 >> data/myid)。
第三步:进入conf文件夹,把zoo_sample.cfg改名为zoo.cfg
第四步:修改zoo.cfg。
修改:
dataDir=/usr/local/solrcloud/zookeeper1/data
clientPort=2181(zookeeper2中为2182、zookeeper3中为2183)
添加:
server.1=192.168.25.150:2881:3881
server.2=192.168.25.150:2882:3882
server.3=192.168.25.150:2883:3883
# The number of milliseconds of each tick tickTime=2000 # The number of ticks that the initial # synchronization phase can take initLimit=10 # The number of ticks that can pass between # sending a request and getting an acknowledgement syncLimit=5 # the directory where the snapshot is stored. # do not use /tmp for storage, /tmp here is just # example sakes. dataDir=/usr/local/solrcloud/zookeeper1/data # the port at which the clients will connect clientPort=2181 # 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=1 server.1=192.168.25.150:2881:3881 server.2=192.168.25.150:2882:3882 server.3=192.168.25.150:2883:3883 |
第五步:对zookeeper2、3中的设置做第二步至第四步修改。
zookeeper2:
myid内容为2
dataDir=/usr/local/solrcloud/zookeeper2/data
clientPort=2182
Zookeeper3:
的myid内容为3
dataDir=/usr/local/solrcloud/zookeeper3/data
clientPort=2183
第六步:启动三个zookeeper
/usr/local/solrcloud/zookeeper1/bin/zkServer.sh start
/usr/local/solrcloud/zookeeper2/bin/zkServer.sh start
/usr/local/solrcloud/zookeeper3/bin/zkServer.sh start
查看集群状态:
/usr/local/solrcloud/zookeeper1/bin/zkServer.sh status
/usr/local/solrcloud/zookeeper2/bin/zkServer.sh status
/usr/local/solrcloud/zookeeper3/bin/zkServer.sh status
第七步:开启zookeeper用到的端口,或者直接关闭防火墙。
service iptables stop
-
- tomcat安装
第一步:将apache-tomcat-7.0.47.tar.gz解压
tar -zxvf apache-tomcat-7.0.47.tar.gz
第二步:把解压后的tomcat复制到/usr/local/solrcloud/目录下复制四份。
/usr/local/solrcloud/tomcat1
/usr/local/solrcloud/tomcat2
/usr/local/solrcloud/tomcat3
/usr/local/solrcloud/tomcat4
第三步:修改tomcat的server.xml
vim tomcat2/conf/server.xml,把其中的端口后都加一。保证两个tomcat可以正常运行不发生端口冲突。
-
- solr单机部署
- 全新部署
- solr单机部署
参考solr教案单机部署方法。
-
- solrCloud部署
- 启动zookeeper
- solrCloud部署
solrCloud部署依赖zookeeper,需要先启动每一台zookeeper服务器。
-
-
- zookeeper管理配置文件
-
由于zookeeper统一管理solr的配置文件(主要是schema.xml、solrconfig.xml), solrCloud各各节点使用zookeeper管理的配置文件。
将上边部署的solr单机的conf拷贝到/home/solr下。
执行下边的命令将/home/solr/conf下的配置文件上传到zookeeper(此命令为单条命令,虽然很长o(╯□╰)o)。此命令在solr-4.10.3/example/scripts/cloud-scripts/目录下:
./zkcli.sh -zkhost 192.168.25.150:2181,192.168.25.150:2182,192.168.25.150:2183 -cmd upconfig -confdir /usr/local/solrcloud/solrhome1/collection1/conf -confname myconf |
登陆zookeeper服务器查询配置文件:
cd /usr/local/zookeeper/bin/
./zkCli.sh
-
-
- 修改SolrCloud监控端口为8080:
-
修改每个solrhome的solr.xml文件。
-
-
- 每一台solr和zookeeper关联
-
修改每一台solr的tomcat 的 bin目录下catalina.sh文件中加入DzkHost指定zookeeper服务器地址:
JAVA_OPTS="-DzkHost=192.168.25.150:2181,192.168.25.150:2182,192.168.25.150:2183"
(可以使用vim的查找功能查找到JAVA_OPTS的定义的位置,然后添加)
-
-
- 启动所有的solr服务
-
启动每一台solr的tomcat服务。
-
-
- 访问solrcloud
-
访问任意一台solr,左侧菜单出现Cloud:
-
-
- SolrCloud集群配置
-
上图中的collection1集群只有一片,可以通过下边的方法配置新的集群。
如果集群中有四个solr节点创建新集群collection2,将集群分为两片,每片两个副本。
http://192.168.25.150:8080/solr/admin/collections?action=CREATE&name=collection2&numShards=2&replicationFactor=2
删除集群命令;
http://192.168.25.150:8080/solr/admin/collections?action=DELETE&name=collection1
执行后原来的collection1删除,如下:
更多的命令请参数官方文档:apache-solr-ref-guide-4.10.pdf
-
-
- 启动solrCloud注意
-
启动solrCloud需要先启动solrCloud依赖的所有zookeeper服务器,再启动每台solr服务器。
- solrJ访问solrCloud
public class SolrCloudTest {
// zookeeper地址
private static String zkHostString = "192.168.25.150:2181,192.168.25.150:2182,192.168.25.150:2183";
// collection默认名称,比如我的solr服务器上的collection是collection2_shard1_replica1,就是去掉“_shard1_replica1”的名称
private static String defaultCollection = "collection1";
// cloudSolrServer实际
private CloudSolrServer cloudSolrServer;
// 测试方法之前构造 CloudSolrServer
@Before
public void init() {
cloudSolrServer = new CloudSolrServer(zkHostString);
cloudSolrServer.setDefaultCollection(defaultCollection);
cloudSolrServer.connect();
}
// 向solrCloud上创建索引
@Test
public void testCreateIndexToSolrCloud() throws SolrServerException,
IOException {
SolrInputDocument document = new SolrInputDocument();
document.addField("id", "100001");
document.addField("title", "李四");
cloudSolrServer.add(document);
cloudSolrServer.commit();
}
// 搜索索引
@Test
public void testSearchIndexFromSolrCloud() throws Exception {
SolrQuery query = new SolrQuery();
query.setQuery("*:*");
try {
QueryResponse response = cloudSolrServer.query(query);
SolrDocumentList docs = response.getResults();
System.out.println("文档个数:" + docs.getNumFound());
System.out.println("查询时间:" + response.getQTime());
for (SolrDocument doc : docs) {
ArrayList title = (ArrayList) doc.getFieldValue("title");
String id = (String) doc.getFieldValue("id");
System.out.println("id: " + id);
System.out.println("title: " + title);
System.out.println();
}
} catch (SolrServerException e) {
e.printStackTrace();
} catch (Exception e) {
System.out.println("Unknowned Exception!!!!");
e.printStackTrace();
}
}
// 删除索引
@Test
public void testDeleteIndexFromSolrCloud() throws SolrServerException, IOException {
// 根据id删除
UpdateResponse response = cloudSolrServer.deleteById("zhangsan");
// 根据多个id删除
// cloudSolrServer.deleteById(ids);
// 自动查询条件删除
// cloudSolrServer.deleteByQuery("product_keywords:教程");
// 提交
cloudSolrServer.commit();
}
}