使用的软件:
solr:http://lucene.apache.org/solr/
虽然solr里面自带了zookeeper,但是现在我们使用单独的zookeeper。
zookeeper:http://zookeeper.apache.org/
Java:oracle jdk 1.8

环境:Ubuntu16.04

安装zookeeper集群

将zookeeper-3.4.10.tar.gz解压到/home/zookeeper目录下。

[shell]
tar zvxf ./zookeeper-3.4.10.tar.gz
[/shell]

并且复制3份,最终我们将部署3份zookeeper实例,保持目录如下所示

[shell]
/home
|-/zookeeper
|-/zookeeper1
|-bin
|-conf
|-contrib
|-…
|-/zookeeper2
|-/zookeeper3
[/shell]

在每个zookeeper中,新建data目录,并在该目录下,新建一个文件,文件名为myid,写入各个zookeeper的标识。

[shell]
echo “1” > /home/zookeeper/zookeeper1/data/myid
echo “2” > /home/zookeeper/zookeeper2/data/myid
echo “3” > /home/zookeeper/zookeeper3/data/myid
[/shell]

创建zoo.cfg文件。

[shell]
#从sample文件中复制
cp /home/zookeeper/zookeeper1/conf/zoo_sample.cfg /home/zookeeper/zookeeper1/conf/zoo.cfg
cp /home/zookeeper/zookeeper1/conf/zoo_sample.cfg /home/zookeeper/zookeeper2/conf/zoo.cfg
cp /home/zookeeper/zookeeper1/conf/zoo_sample.cfg /home/zookeeper/zookeeper3/conf/zoo.cfg
[/shell]

修改zoo.cfg的内容

[shell]
#这个是zookeeper1文件的内容,zookeeper2和zookeeper3相应进行修改
#dataDir是数据存储的位置
#clientPort是提供客户端访问的端口
# server.1中的1,就是data/myid中写入的数字。后面的192.168.174.128:20002:20003,表示其他机器可以访问的ip,投票端口,选举端口

dataDir=/home/zookeeper/zookeeper1/data
# the port at which the clients will connect
clientPort=20001
server.1=192.168.174.128:20002:20003
server.2=192.168.174.128:20005:20006
server.3=192.168.174.128:20008:20009
[/shell]

新建一个shell文件,用来启动/停止所有的zookeeper服务器

[shell]
vi /home/zookeeper/start.sh
#文件内容如下
[/shell]

[shell]
#!/bin/sh
DIR=”$( cd “$( dirname “$0″ )” && pwd )”
cd ${DIR}

command=””;

case $1 in
start)
command=”start”;
;;
status)
command=”status”;
;;
stop)
command=”stop”;
;;
restart)
command=”restart”;
;;
clearlog)
command=”clearlog”;
;;
*)
command=””;
esac

operate() {
if [ “${command}” != “” ]; then
echo “we will do ${command}”

if [ “${command}” = “clearlog” ]; then
cd ${DIR}
cd $1
echo “” > ./zookeeper.out
else
cd ${DIR}
cd $1
./bin/zkServer.sh “${command}”
fi

fi
}

operate “./zookeeper1”
operate “./zookeeper2”
operate “./zookeeper3”
[/shell]

安装solr集群

下载solr二进制文件: https://lucene.apache.org/solr/downloads.html
创建solr_home,用来存储之后solr的数据和配置

[shell]
mkdir /home/solr/solr1/solr_data
#解压安装脚本
tar xzf solr-8.1.1.tgz solr-8.1.1/bin/install_solr_service.sh –strip-components=2
# 安装为service方式
sudo bash ./install_solr_service.sh ./solr-8.1.1.tgz -i /home/solr/solr1 -d /home/solr/solr1/solr-data -u ubuntu -s solr -p 8983 -n
[/shell]

修改solr相关参数,solr2和solr3的文件修改类似。

[shell]
# service方式安装的,修改下面的文件
sudo vi /etc/default/solr.in.sh
# 普通解压安装的,修改下面的文件
vi /home/solr/solr1/bin/solr.in.sh
[/shell]

修改下面的几个参数

[shell]
# Set the ZooKeeper connection string if using an external ZooKeeper ensemble
# e.g. host1:2181,host2:2181/chroot
# Leave empty if not using SolrCloud
ZK_HOST=”192.168.174.128:20001,192.168.174.128:20004,192.168.174.128:20007″

# Set the ZooKeeper client timeout (for SolrCloud mode)
ZK_CLIENT_TIMEOUT=”15000″

# By default the start script uses “localhost”; override the hostname here
# for production SolrCloud environments to control the hostname exposed to cluster state
SOLR_HOST=”192.168.174.128″
# Path to a directory for Solr to store cores and their data. By default, Solr will use server/solr

# By default the start script uses UTC; override the timezone if needed
SOLR_TIMEZONE=”Asia/Shanghai”

# If solr.xml is not stored in ZooKeeper, this directory needs to contain solr.xml
SOLR_HOME=/home/solr/solr1/solr_data

# Sets the port Solr binds to, default is 8983
SOLR_PORT=8983
[/shell]

好了,现在文件已经修改完毕。接下来是启动solr,zookeeper前面已经启动起来了。
创建solr启动脚本/home/solr/start.sh

[shell]
#!/bin/sh
DIR=”$( cd “$( dirname “$0″ )” && pwd )”
cd ${DIR}

command=””;

case $1 in
start)
command=”start”;
;;
status)
command=”status”;
;;
stop)
command=”stop”;
;;
restart)
command=”restart”;
;;
*)
command=””;
esac

operate() {
if [ “${command}” != “” ]; then
echo “we will do ${command}”

cd ${DIR}
cd $1
./bin/solr “${command}” -cloud

fi
}

operate “./solr1”
operate “./solr2”
operate “./solr3”
[/shell]

启动solr

[shell]
/home/solr/start.sh start
[/shell]

访问网站http://192.168.174.128:8984/solr/,开始创建collection。

创建collection成功,然后可以给这个collection创建replica备份。下图中的位置还可以选择是将replica创建到哪台机器上。

创建shard分片
注意,能够动态增加分片的collection,在创建的时候,route一定要选择implicit,否则以后动态增加shard的时候会报错。

动态增加shard的访问网址命令
http://192.168.174.128:8984/solr/admin/collections?action=CREATESHARD&shard=shardName&collection=collectionName
成功后会返回

{
"responseHeader":{
"status":0,
"QTime":2245},
"success":{
"192.168.174.128:8983_solr":{
"responseHeader":{
"status":0,
"QTime":525},
"core":"test2_shard2_replica_n3"}}}

如果是compositeId,则会失败,返回

{
"responseHeader":{
"status":400,
"QTime":1},
"error":{
"metadata":[
"error-class","org.apache.solr.common.SolrException",
"root-error-class","org.apache.solr.common.SolrException"],
"msg":"shards can be added only to 'implicit' collections",
"code":400}}

好了,接下来就可以在各个solr_home看到创建的collection目录了。

上传配置到zookeeper
#上传配置文件夹
#./bin/solr zk upconfig -n <name for configset> -d <path to directory with configset>
./bin/solr zk upconfig -n test -d /home/solr/solr1/solr_home/configsets/test
#下载配置文件夹
#./bin/solr zk downconfig -n <name for configset> -d <path to directory with configset>
./bin/solr zk downconfig -n test -d /home/solr/solr1/solr_home/configsets/test
#删除配置文件
#./bin/solr zk rm <path to directory with configset>
./bin/solr zk rm -r /configs/test
./bin/solr zk rm /configs/test/solrconfig.xml