solr 5.0 集群搭建


一,搭建zookeeper集群
1.下载zookeeper解压3份,分别重命名为zookeeper-3.4.10-42181,zookeeper-3.4.10-42182,zookeeper-3.4.10-42183(4218* 表示连接端口)
2. 进入D:\mysoftsetup\solr-colud\zookeeper\zookeeper-3.4.10-42181\conf目录修改
(zookeeper-3.4.10-42182,zookeeper-3.4.10-42183操作类似)
zoo_sample.cfg 修改为:zoo.cfg
修改如下:
#保存数据路径
dataDir=D:/mysoftsetup/solr-colud/zookeeper/zookeeper-3.4.10-42181/data
### log file 保存日志文件
dataLogDir=D:/mysoftsetup/solr-colud/zookeeper/zookeeper-3.4.10-42181/log
# the port at which the clients will connect 端口号(注意:如果为zookeeper-3.4.10-42182,那么clientPort=42182)
clientPort=42181

#集群通信接口配置(1为serverid,与myid文件内容对应,等号后面分别为ip、zk通讯端口、选举端口)
server.1=127.0.0.1:4888:5888
server.2=127.0.0.1:4889:5889
server.3=127.0.0.1:4890:5890

3.在D:\mysoftsetup\solr-colud\zookeeper\zookeeper-3.4.10-42181\data目录建立myid文件,写入1。(zookeeper-3.4.10-42182,zookeeper-3.4.10-42183操作类似)(注意:如果为zookeeper-3.4.10-42182,myid文件写入2)

4.zookeeper-3.4.10-42181/conf/zoo.cfg 配置如下:
# 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=/tmp/zookeeper

dataDir=D:/mysoftsetup/solr-colud/zookeeper/zookeeper-3.4.10-42181/data
### log file 保存日志文件
dataLogDir=D:/mysoftsetup/solr-colud/zookeeper/zookeeper-3.4.10-42181/log
# the port at which the clients will connect
clientPort=42181
# 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=127.0.0.1:4888:5888
server.2=127.0.0.1:4889:5889
server.3=127.0.0.1:4890:5890

zookeeper-3.4.10-42182/conf/zoo.cfg 配置如下:

# 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=/tmp/zookeeper

dataDir=D:/mysoftsetup/solr-colud/zookeeper/zookeeper-3.4.10-42182/data
### log file 保存日志文件
dataLogDir=D:/mysoftsetup/solr-colud/zookeeper/zookeeper-3.4.10-42182/log
# the port at which the clients will connect
clientPort=42182
# 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=127.0.0.1:4888:5888
server.2=127.0.0.1:4889:5889
server.3=127.0.0.1:4890:5890

zookeeper-3.4.10-42183/conf/zoo.cfg 配置如下:
# 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=/tmp/zookeeper

dataDir=D:/mysoftsetup/solr-colud/zookeeper/zookeeper-3.4.10-42183/data
### log file 保存日志文件
dataLogDir=D:/mysoftsetup/solr-colud/zookeeper/zookeeper-3.4.10-42183/log
# the port at which the clients will connect
clientPort=42183
# 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=127.0.0.1:4888:5888
server.2=127.0.0.1:4889:5889
server.3=127.0.0.1:4890:5890

5.依次启动,不报错就可以了。

第二 solrCloud集群搭建。
2.1将下载来的solr-5.0.0.zip解压。
创建D:\mysoftsetup\solr-colud\solr_home1目录,复制solr-5.0.0\server\solr下所有文件到D:\mysoftsetup\solr-colud\solr_home1,修改solr.xml。
需要把solr.xml中的端口改为容器服务端口,例如tomcat的28983

<int name="hostPort">${jetty.port:28983}</int>
也可以通过修改host配置一个固定的ip
    <str name="host">127.0.0.1</str>


把solr-5.0.0\server\webapps\solr.war放到tomcat的webapps目录下;启动tomcat自动解压得到solr文件夹。
把solr-5.0.0\server\lib\ext下所有的jar,复制到刚才得到的solr文件夹下的WEB-INF\lib目录下。
把solr-5.0.0\server\resources\log4j.properties复制到webapps\solr\WEB-INF\classes目录下。
修改webapps\solr\WEB-INF\web.xml的:
   <env-entry>
       <env-entry-name>solr/home</env-entry-name>
       <env-entry-value>D:\mysoftsetup\solr-colud\solr_home1</env-entry-value>
       <env-entry-type>java.lang.String</env-entry-type>
    </env-entry>
指定solr/home为D:\mysoftsetup\solr-colud\solr_home1目录。
修改D:\mysoftsetup\solr-colud\solr_home1\solr.xml,修改hostPort为该tomcat的端口号。
拷贝solr-5.0.0\server\solr\configsets\basic_configs\conf目录到D:\mysoftsetup\solr-colud\solr_home1中


2.2. 配置tomcat

修改tomcat的server.xml,服务的口号我改为了28983。
修改tomcat的catalina.bat文件,在

setlocal
下增加这一行内容:

set “JAVA_OPTS=%JAVA_OPTS% -DzkHost=127.0.0.1:28983,127.0.0.1:28986,127.0.0.1:28990”
把tomcat再复制2份,注意修改端口号。
我的3个tomcat路径分别为:
E:\Solr_Workspace\apache-tomcat-7.0.62_28983
E:\Solr_Workspace\apache-tomcat-7.0.62_28986
E:\Solr_Workspace\apache-tomcat-7.0.62_28990

把solr_home1拷贝2份:solr_home2,solr_home3
然后把tomcat2、3下的webapps下的solr文件夹按照步骤3再配置一遍,修改1、2、3有区别的地方,使之分别与tomcat1、2、3对应。(注意:solr_home2和solr_home3下的collection1目录删除,不需要)。
tomcat2和3的catalina.bat的set JAVA_OPTS分别为:
set “JAVA_OPTS=%JAVA_OPTS% -DzkHost=127.0.0.1:28983,127.0.0.1:28986,127.0.0.1:28990”
注意:此处也可不修改catalina.bat,
而直接在solr/home变量配置的目录下的solr.xml文件的solr>solrcloud节点下添加:
<str name="zkHost">1127.0.0.1:28983,127.0.0.1:28986,127.0.0.1:28990</str>

2.3登录solr建立core

http://localhost:28983/solr/

2.3.1使用命令上传core的配置文件(注意:可以上传多个配置文件,哪一个core对应哪一个配置文件需要在创建时使用collection.configName 参数指定)
上传配置文件命令(windows版,jar 包含:D:\mysoftsetup\solr-colud\solr-5.0.0\server\solr-webapp\webapp\WEB-INF\lib )
java -classpath .;D:/mysoftsetup/solr-colud/solrcould/solr-war-lib/* ;D:\mysoftsetup\solr-colud\solr-5.0.0\server\lib\ext  org.apache.solr.cloud.ZkCLI -cmd upconfig -zkhost 127.0.0.1:42181,127.0.0.1:42182,127.0.0.1:42183 -confdir D:/mysoftsetup/solr-colud/solrcould/solr-config -confname shop_goods
或者
进入D:\mysoftsetup\solr-5.0.0\server\scripts\cloud-scripts 目录
执行:
zkcli.bat -zkhost 127.0.0.1:42181,127.0.0.1:42182,127.0.0.1:42183 -cmd upconfig -confdir D:/mysoftsetup/solr-colud/solrcould/solr-config -confname shop_goods
执行:



2.3.2使用命令建立core,并且指定使用哪一个配置文件名称
1.建立core名称为collection3;使用配置文件名称为:shop_goods;逻辑碎片的个数为:3;分片的副本数为:1
http://127.0.0.1:28983/solr/admin/collections?action=CREATE&name=collection3&numShards=3&replicationFactor=1&collection.configName=shop_goods
在http://localhost:28983/solr/页面点击左侧的cloud 显示



2.建立core名称为shop_goods2;使用配置文件名称为:shop_goods;逻辑碎片的个数为:2;分片的副本数为:2

http://127.0.0.1:28983/solr/admin/collections?action=CREATE&name=shop_goods2&numShards=2&replicationFactor=2&maxShardsPerNode=2&collection.configName=shop_goods&createNodeSet=127.0.0.1:28983_solr,127.0.0.1:28986_solr,127.0.0.1:28990_solr 
在http://localhost:28983/solr/页面点击左侧的cloud 显示



3.如果想删除collection3接口,
http://localhost:8983/solr/admin/collections?action=DELETE&name=collection3

4.重新加载 collection3接口
http://localhost:8983/solr/admin/collections?action=RELOAD&name=collection3


注意:solr通过api调用失败,有可能没有关联配置文件或关联的配置文件不正确。





 通过集群api 管理集群(Core Admin)
1).创建接口(第一种自动分配)
Java代码  
1.http://127.0.0.1:28983/solr/admin/collections?action=CREATE&name=collection1&numShards=3&replicationFactor=1&maxShardsPerNode=1&collection.configName=myconf    
2.http://127.0.0.1:28983/solr/admin/collections?action=CREATE&name=collection1&numShards=3&replicationFactor=1&maxShardsPerNode=1&collection.configName=myconf&createNodeSet=127.0.0.1:28986_solr,127.0.0.1:28983_solr,127.0.0.1:28990_solr  
   这样会出来一个collection,它有3个shard,每个shard有1个数据节点,1个备份节点,即该collection共有6个core
 
 参数:
name:将被创建的集合的名字
numShards:集合创建时需要创建逻辑碎片的个数
replicationFactor:分片的副本数。replicationFactor(复制因子)为 3 意思是每个逻辑碎片将有 3 份副本。
maxShardsPerNode:默认值为1,每个Solr服务器节点上最大分片数(4.2新增的)
注意三个数值:numShards、replicationFactor、liveSolrNode(当前存活的solr节点),一个正常的solrCloud集群不容许同一个liveSolrNode上部署同一个shard的多个replic,因此当maxShardsPerNode=1时,numShards*replicationFactor>liveSolrNode时,报错。因此正确时因满足以下条件:numShards*replicationFactor<liveSolrNode*maxShardsPerNode
createNodeSet:如果不提供该参数,那么会在所有活跃节点上面创建core,如果提供该参数就会在指定的solr节点上创建core
例如我现在在3台tomcat上面创建3个片,1个副本,不提供该参数结果是这样的


 
提供该参数例如:createNodeSet=127.0.0.1:28986_solr,127.0.0.1:28983_solr,127.0.0.1:28990_solr
结果是这样的



  collection.configName:用于新集合的配置文件的名称。如果不提供该参数将使用集合名称作为配置文件的名称。
 
创建接口2(手动分配)实例:通过下面多个链接进行创建(3个分片,每个节点上面一个备份)推荐使用,因为这种方式你想创建多少次就多少次 
Java代码  
1.http://127.0.0.1:28983/solr/admin/cores?action=CREATE&name=shard1_replica1&instanceDir=shard1_replica1&dataDir=data&collection=collection1&shard=shard1&collection.configName=myconf  
2.http://127.0.0.1:28990/solr/admin/cores?action=CREATE&name=shard1_replica2&instanceDir=shard1_replica2&dataDir=data&collection=collection1&shard=shard1&collection.configName=myconf  
3.  
4.  
5.http://127.0.0.1:28990/solr/admin/cores?action=CREATE&name=shard2_replica1&instanceDir=shard2_replica1&dataDir=data&collection=collection1&shard=shard2&collection.configName=myconf  
6.http://127.0.0.1:28986/solr/admin/cores?action=CREATE&name=shard2_replica2&instanceDir=shard2_replica2&dataDir=data&collection=collection1&shard=shard2&collection.configName=myconf  
7.  
8.http://127.0.0.1:28986/solr/admin/cores?action=CREATE&name=shard3_replica1&instanceDir=shard3_replica1&dataDir=data&collection=collection1&shard=shard3&collection.configName=myconf  
9.http://127.0.0.1:28983/solr/admin/cores?action=CREATE&name=shard3_replica2&instanceDir=shard3_replica2&dataDir=data&collection=collection1&shard=shard3&collection.configName=myconf  
 参数含义:
name:新建core的名称
创建的core的命名规则:
coreName_shardName_replicaN
例如:创建pscp的集合,2个分片,每个分片上面有两个备份
则命名如下:
pscp_shard1_replica1
pscp_shard1_replica2
pscp_shard2_replica1
pscp_shard2_replica2
shard:指定一个分配id,这个core将挂在那个分片上(随便写,如果还没有这个id,第一次会帮你创建)
collection.configName:从zookeeper中指定一份配置文件
instanceDir和dataDir:从下图看出他的含义
命名规则:instanceDir与name的名称相同,dataDir:统一建议命名为data

 
总结一:在一个集群中添加一个副本的两种方式
Java代码  
1.http://127.0.0.1:28983/solr/admin/collections?action=ADDREPLICA&collection=collection1&shard=shard2&node=192.168.66.128:8085_solr  
2.上面这句话的意思就是在collection1这个集合的shard2分片上添加一个副本,副本的地址在192.168.66.128:8085_solr台机子上面  
3.http://127.0.0.1:28986/solr/admin/cores?action=CREATE&name=shard3_replica1&instanceDir=shard3_replica1&dataDir=data&collection=collection1&shard=shard3&collection.configName=myconf  
 
2).删除接口
Java代码  
1.http://localhost:8983/solr/admin/collections?action=DELETE&name=mycollection  
   参数:
name:将被创建的集合别名的名字
collections:逗号分隔的一个或多个集合别名的列表
 
3).重新加载接口,这个时候,相应的core会重新加载配置文件
Java代码  
1.http://localhost:8983/solr/admin/collections?action=RELOAD&name=mycollection  
 参数:
name:将被重载的集合的名字
 
4).分割碎片接口
Java代码  
1.http://localhost:8983/solr/admin/collections?action=SPLITSHARD&collection=&lt;collection_name&gt;&shard=shardId  
 collection:集合的名字
shard:将被分割的碎片 ID
 这个命令不能用于使用自定义哈希的集群,因为这样的集群没有一个明确的哈希范围。 它只用于具有plain 或 compositeid 路由的集群。该命令将分割给定的碎片索引对应的那个碎片成两个新碎片。通过将碎片范围划分成两个相等的分区和根据新碎片范围分割出它在父碎片(被分的碎片)中的文档。新碎片将被命名为 appending_0 和_1。例如:shard=shard1 被分割,新的碎片将被命名为 shard1_0 和 shard1_1。一旦新碎片被创建,它们就被激活同时父碎片(被分的碎片)被暂停因此将没有新的请求到父碎片(被分的碎片)。该特征达到了无缝分割和无故障时间的要求。原来的碎片数据不会被删除。使用新 API 命令重载碎片用户自己决定。该特性发布始于 Solr4.3,由于 4.3 发布版本发现了一些 bugs,所以要使用该特性推荐等待 4.3.1
 
三.通过命令行工具将文件上传到Zookeeper进行管理
之所以能分布式是因为引入ZooKeeper来统一保存配置文件,故而需要将SolrCloud的配置文件上传到ZooKeeper中,这里演示命令行进行上传
要使用命令行管理管理工具,必须要先有包,这些包就是solr.war里面/WEB-INF/lib下面的所有jar包
第一步:新建文件夹
在可以和Zookeeper集群通讯的任意一台机子上面,新建两个文件夹,例如如下是我的目录
/usr/solrCloud/conf/files  /usr/solrCloud/conf/lib
files:用来保存配置文件   lib:用来存放jar包
第二步:上传需要使用的jar和配置文件
上传jar到lib目录,将solr发布包下面的jar(solr-4.8.0\example\solr-webapp\webapp\WEB-INF\lib\ 和 solr-4.8.0\example\lib\ext\ 下面包都要)全部上传到上面的lib目录
将solr的配置文件上传到上面的files目录下面
第三步:将文件上传Zookeeper进行统一管理
Java代码  
1.java -classpath .:/usr/solrCloud/conf/lib/* org.apache.solr.cloud.ZkCLI -cmd upconfig -zkhost 192.168.27.18:2181,192.168.27.18:2182,192.168.27.18:2183 -confdir /usr/solrCloud/conf/files  -confname myconf  
-cmd upconfig:上传配置文件
-confdir:配置文件的目录 
-confname:指定对应的名称
查看文件是否已经上传到Zookeeper服务器:
Java代码  
1.sh zkCli.sh -server localhost:2181  
2.ls /configs/myconf  
第四步:将上传到ZooKeeper中配置文件与collection相关联
Java代码  
1.java -classpath .:/usr/solrCloud/conf/lib/* org.apache.solr.cloud.ZkCLI -cmd linkconfig -collection collection1 -confname myconf -zkhost 192.168.27.18:2181,192.168.27.18:2182,192.168.27.18:2183  
-cmd linkconfig:为指定collection"绑定"配置文件
-collection:上面指定的collection的名称
-confname:zookeeper上面的配置文件名称
上面这句代码的意思就是:创建的core(collection1)将使用myconf这个配置文件
例如:执行下面这个请求将创建一个core为collection1,那么他使用的配置文件为zookeeper中的myconf这个配置
Java代码  
1.http://localhost:8983/solr/admin/collections?action=CREATE&name=collection1&numShards=3&replicationFactor=1  
话又说回来,如果zookeeper管理的集群上面仅有一份配置,那么创建的core都会用这份默认的配置。如果有多份,如果没有执行第四步,随便创建一个core将抛出异常,构建失败!
例如执行:
Java代码  
1.http://127.0.0.1:28983/solr/admin/collections?action=CREATE&name=sdf&numShards=3&replicationFactor=1  
 将抛出:因为上面有两份配置,但是并没有执行第四步,将配置与即将创建core(name=sdf)关联起来

.Solr集群的操作
  以下的操作均是以http://172.16.20.101:8983/solr这个开头访问的,然后跟在这个url后面才是完整的路径。如:我写的/admin/collections?action=DELETE&name=article,则访问路径是:http://172.16.20.101:8983/solr/admin/collections?action=DELETE&name=article
  4.1 创建collection
  两种方式创建collection,根据路由方式的不同:
第一种implicit路由方式
/admin/collections?action=CREATE&name=123456&router.name=implicit&shards=shard1,shard2,shard3&maxShardsPerNode=3&collection.configName=123456
  /admin/collections?action=CREATE&name=article&router.name=implicit&shards=shard1,shard2,shard3&maxShardsPerNode=5&collection.configName=collection3Conf
  第二种compositeId路由方式
  /admin/collections?action=CREATE&name=article&router.name=compositeId&numShards=5&replicationFactor=1&maxShardsPerNode=5&collection.configName=collection3Conf
  或者
  /admin/collections?action=CREATE&name=application&router.name=compositeId&numShards=1&replicationFactor=2&maxShardsPerNode=2&collection.configName=application&createNodeSet=172.16.20.102:8983_solr,172.16.20.103:8983_solr
  4.2删除collection
  /admin/collections?action=DELETE&name=article
  4.3增加分片shard
  implicit路由实现该需求比较简单,只要创建Shard即可。
  /admin/collections?action=CREATESHARD&collection=article&shard=shard1&name=article_shard1_replica2
  compositeId路由实现该需求稍微麻烦一下,通过分裂(SPLITSHARD)操作实现。
  /admin/collections?action=SPLITSHARD&collection=article&shard=shard1
  4.4删除分片shard
  /admin/collections?action=DELETESHARD&collection=article&shard=shard1
  4.5上传SolrCloud的配置文件到zookeeper,统一保存配置文件
 
  java -classpath .:/data/solr/solr-5.2.1/server/solr-webapp/webapp/WEB-INF/lib/* org.apache.solr.cloud.ZkCLI -cmd upconfig -zkhost 172.16.20.101:2181,172.16.20.102:2181,172.16.20.102:2181 -confdir /data/solr/solr-5.2.1/server/solr/configsets/sample_techproducts_configs/conf/ -confname collection1Conf
  4.6将上传到ZooKeeper中配置文件与Collection相关联
  java -classpath .:/data/solr/solr-5.2.1/server/solr-webapp/webapp/WEB-INF/lib/* org.apache.solr.cloud.ZkCLI -cmd linkconfig -collection collection1 -confname collection1Conf -zkhost 172.16.20.101:2181,172.16.20.102:2181,172.16.20.103:2181
  4.7将配置上传到zookeeper,如果要让正在运行的solr同步加载这些文件,需要让solr重新加载一下配置文件
/admin/collections?action=RELOAD&name=article























猜你喜欢

转载自gjp014.iteye.com/blog/2407658