黑马程序员技术交流社区

标题: 【南京校区】linux自动脚本搭建solr平台 [打印本页]

作者: 大蓝鲸Java    时间: 2018-4-6 11:10
标题: 【南京校区】linux自动脚本搭建solr平台
本帖最后由 大蓝鲸Java 于 2018-4-6 11:12 编辑

       淘淘商城学习的时候,想必solr的集群搭建 应该是个非常头疼的事情,报错信息不够明确,调试起来是相当困难。那么我们如何解决这样的难题呢?学生无法搭建这样的集群的时候,如果快速将其搭建成功,避免影响整个学习的进度呢?说到这里啊,不得不在说一下,集群定然不是一台机器,假设现在solr的集群有100台,我们一台一台的安装吗?肯定不会这么做。是不是好多同学已经想到了使用自动化的脚本来搭建这样的一个集群呢。
下面就给大家介绍一下自动化脚本搭建solr集群
1,首先先将 提供的软件复制到root用户目录下(复制到其他路径也可以,但是记得修改脚本)
       1.    apache-tomcat-7.0.47.tar.gz
       2.    jdk-7u55-linux-i586.tar.gz
       3.    IK Analyzer 2012FF_hf1 (这是个文件夹,这个复制过来)
       4.    solr-4.10.3.tgz.tgz
       5.    zookeeper-3.4.6.tar.gz
2、在root用户目录下执行如下脚本

   
[AppleScript] 纯文本查看 复制代码
<pre><code>
#!/bin/sh

service iptables stop
chkconfig iptables off

#jdk是否已经安装了,默认0表示不安装
isJDK=0

#软件包路径
softPath=/root
#tomcat名称
tomcatName=apache-tomcat-7.0.47.tar.gz
#jdk名称
jdkName=jdk-7u55-linux-i586.tar.gz
#solr名称
solrName=solr-4.10.3.tgz.tgz

#zookeeper名称
zookeeperName=zookeeper-3.4.6.tar.gz

#ip
ip=`ifconfig eth0 | grep "inet addr" | awk '{ print $2}' | awk -F: '{print $2}'`

#解压路径
jieyaPath=/root/soft/solrCluster
jieyaPath2=$(echo $jieyaPath |sed -e 's/\//\\\//g')

#判断解压路径是否存在,不存在就创建,存在就删除
if [ ! -d "$jieyaPath" ]; then
  mkdir $jieyaPath
else
  rm -rf $jieyaPath
  mkdir $jieyaPath
fi

echo -e "--------------------------开始解压软件-------------------------------\n"


echo -e "zookeeper安装并启动,端口号2281-2283"

echo -e "安装jdk"
if [ isJDK==0 ]
then
        echo -e "jdk已经安装"
else
        echo -e "2、解压jdk到$jieyaPath\n"
        tar -zxf $softPath/$jdkName -C $jieyaPath
        mv jdk* jdk
        echo "export JAVA_HOME=$jieyaPath/jdk" >> /etc/profile
        echo 'export PATH=$PATH:$JAVA_HOME/bin' >> /etc/profile
        source /etc/profile
fi

echo -e "解压zookeeper"
tar -zxvf $softPath/$zookeeperName -C $jieyaPath
mv $jieyaPath/zoo* $jieyaPath/zookeeper

for i in 1 2 3
do
        cp -r $jieyaPath/zookeeper $jieyaPath/zookeeper$i
        mkdir $jieyaPath/zookeeper$i/data
        echo $i >> $jieyaPath/zookeeper$i/data/myid
        mv $jieyaPath/zookeeper$i/conf/zoo_sample.cfg $jieyaPath/zookeeper$i/conf/zoo.cfg
        sed -i "s/dataDir=\/tmp\/zookeeper/dataDir=$jieyaPath2\/zookeeper$i\/data/g" $jieyaPath/zookeeper$i/conf/zoo.cfg
        sed -i "s/clientPort=2181/clientPort=228$i/g" $jieyaPath/zookeeper$i/conf/zoo.cfg
        echo "server.1=$ip:2881:3881" >> $jieyaPath/zookeeper$i/conf/zoo.cfg
        echo "server.2=$ip:2882:3882" >> $jieyaPath/zookeeper$i/conf/zoo.cfg
        echo "server.3=$ip:2883:3883" >> $jieyaPath/zookeeper$i/conf/zoo.cfg
         
        cd $jieyaPath/zookeeper$i
        bin/zkServer.sh start
         
done

#rm -r $jieyaPath/zookeeper





echo -e "3、解压solr到$jieyaPath\n"
tar -zxf /$softPath/$solrName -C $jieyaPath
mv $jieyaPath/solr* $jieyaPath/solr

echo -e "1、解压tomcat到$jieyaPath\n"
tar -zxf /$softPath/$tomcatName -C $jieyaPath
mv $jieyaPath/apache-* $jieyaPath/tomcat


echo -e "移动solr的war包到tomcat\n"
cp $jieyaPath/solr/dist/solr-4.10.3.war $jieyaPath/tomcat/webapps/solr.war


echo -e "启动tomcat"
sh $jieyaPath/tomcat/bin/startup.sh

cd $jieyaPath/tomcat/logs

num=0
while [ "1" == "1" ]
do
        isStart=$(cat catalina.out |grep "Server startup in" |awk '{print $2}')
        sleep 1s
         
        if [ -z $isStart ]
        then
                num=`expr $num + 1`
                echo "第$num次扫描"
        else
                echo -e "tomcat启动成功\n"
                break
        fi
done

tomcatPort=$(ps -aux |grep tomcat|awk 'NR==1{print $2}')
kill -9 $tomcatPort



echo -e "\n移动solr的jar包到solr的lib中\n"
cp $jieyaPath/solr/example/lib/ext/* $jieyaPath/tomcat/webapps/solr/WEB-INF/lib/

echo -e "创建一个solrhome\n"
cp -r $jieyaPath/solr/example/solr $jieyaPath/solrhome


echo -e "修改catalina.sh"
sed -i "s/#JAVA_OPTS=\"\$JAVA_OPTS -Dorg.apache.catalina.security.SecurityListener.UMASK=\`umask\`\"/JAVA_OPTS=\"-DzkHost=$ip:2281,$ip:2282,$ip:2283\"/g"   $jieyaPath/tomcat/bin/catalina.sh


sed -i "46d" $jieyaPath/tomcat/webapps/solr/WEB-INF/web.xml
sed -i "40d" $jieyaPath/tomcat/webapps/solr/WEB-INF/web.xml



if [ ! -d "$softPath/IKAnalyzer" ]; then
  mkdir $softPath/IKAnalyzer
else
  rm -rf mkdir $softPath/IKAnalyzer
  mkdir $softPath/IKAnalyzer
fi


cp -r $softPath/IK\ Analyzer\ 2012FF_hf1/* $softPath/IKAnalyzer/

echo -e "创建$jieyaPath/tomcat/webapps/solr/WEB-INF/classes文件夹\n"
mkdir $jieyaPath/tomcat/webapps/solr/WEB-INF/classes

echo -e "将IKAnalyzer2012FF_u1.jar移动到tomcat中solr项目里面的lib文件夹\n"
cp $softPath/IKAnalyzer/IKAnalyzer2012FF_u1.jar $jieyaPath/tomcat/webapps/solr/WEB-INF/lib/

echo -e "将IKAnalyzer.cfg.xml,mydict.dic移动到$jieyaPath/tomcat/webapps/solr/WEB-INF/classes文件夹下\n"
cp $softPath/IKAnalyzer/IKAnalyzer.cfg.xml $jieyaPath/tomcat/webapps/solr/WEB-INF/classes/
cp $softPath/IKAnalyzer/mydict.dic $jieyaPath/tomcat/webapps/solr/WEB-INF/classes/


sed -i '$d' $jieyaPath/solrhome/collection1/conf/schema.xml

echo -e "修改schema.xml文件\n"
echo "<fieldType name=\"text_ik\" class=\"solr.TextField\">">>$jieyaPath/solrhome/collection1/conf/schema.xml
echo "  <analyzer class=\"org.wltea.analyzer.lucene.IKAnalyzer\"/>">>$jieyaPath/solrhome/collection1/conf/schema.xml
echo "</fieldType>">>$jieyaPath/solrhome/collection1/conf/schema.xml
echo "<field name=\"item_title\" type=\"text_ik\" indexed=\"true\" stored=\"true\"/>">>$jieyaPath/solrhome/collection1/conf/schema.xml
echo "<field name=\"item_sell_point\" type=\"text_ik\" indexed=\"true\" stored=\"true\"/>">>$jieyaPath/solrhome/collection1/conf/schema.xml
echo "<field name=\"item_price\"  type=\"long\" indexed=\"true\" stored=\"true\"/>">>$jieyaPath/solrhome/collection1/conf/schema.xml
echo "<field name=\"item_image\" type=\"string\" indexed=\"false\" stored=\"true\" />">>$jieyaPath/solrhome/collection1/conf/schema.xml
echo "<field name=\"item_category_name\" type=\"string\" indexed=\"true\" stored=\"true\" />">>$jieyaPath/solrhome/collection1/conf/schema.xml
echo "<field name=\"item_desc\" type=\"text_ik\" indexed=\"true\" stored=\"false\" />">>$jieyaPath/solrhome/collection1/conf/schema.xml
echo " ">>$jieyaPath/solrhome/collection1/conf/schema.xml
echo "<field name=\"item_keywords\" type=\"text_ik\" indexed=\"true\" stored=\"false\" multiValued=\"true\"/>">>$jieyaPath/solrhome/collection1/conf/schema.xml
echo "<copyField source=\"item_title\" dest=\"item_keywords\"/>">>$jieyaPath/solrhome/collection1/conf/schema.xml
echo "<copyField source=\"item_sell_point\" dest=\"item_keywords\"/>">>$jieyaPath/solrhome/collection1/conf/schema.xml
echo "<copyField source=\"item_category_name\" dest=\"item_keywords\"/>">>$jieyaPath/solrhome/collection1/conf/schema.xml
echo "<copyField source=\"item_desc\" dest=\"item_keywords\"/>">>$jieyaPath/solrhome/collection1/conf/schema.xml
echo "</schema>">>$jieyaPath/solrhome/collection1/conf/schema.xml



for i in 1 2 3 4
do
        #创建一个tomcat
        cp -r $jieyaPath/tomcat $jieyaPath/tomcat$i
                 
        #创建一个solrhome
        cp -r $jieyaPath/solrhome $jieyaPath/solrhome$i
        sed -i "s/\${host:}/$ip/g" $jieyaPath/solrhome$i/solr.xml
        sed -i "s/\${jetty.port:8983}/8${i}80/g" $jieyaPath/solrhome$i/solr.xml
         
        #替换修改tomcat
        sed -i "s/\/put\/your\/solr\/home\/here/$jieyaPath2\/solrhome$i/g" $jieyaPath/tomcat$i/webapps/solr/WEB-INF/web.xml
         
        #修改tomcat端口号
        sed -i "s/Server port=\"8005\"/Server port=\"8${i}05\"/g" $jieyaPath/tomcat$i/conf/server.xml
        sed -i "s/Connector port=\"8080\"/Connector port=\"8${i}80\"/g" $jieyaPath/tomcat$i/conf/server.xml
        sed -i "s/Connector port=\"8009\"/Connector port=\"8${i}09\"/g" $jieyaPath/tomcat$i/conf/server.xml

done

echo -e "第六步:让zookeeper统一管理配置文件\n"
cd $jieyaPath/solr/example/scripts/cloud-scripts
./zkcli.sh -zkhost $ip:2281,$ip:2282,$ip:2283 -cmd upconfig -confdir $jieyaPath/solrhome1/collection1/conf -confname myconf

echo -e "启动所有的tomcat"
for i in 1 2 3 4
do
         
        sh  $jieyaPath/tomcat$i/bin/startup.sh  
done

</code></pre>




3、执行完以上的脚本,solr集群就已经搭建好了,并且已经启动了,输入  http://{你自己的ip}:8180/solr 就可以直接访问了。












欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2