Solr集群搭建

3 篇文章 0 订阅

Solr集群搭建(Solr单机在下面)

1.环境准备

这里写图片描述
Zookeeper作为集群的管理工具。
1、集群管理:容错、负载均衡。
2、配置文件的集中管理
3、集群的入口

需要实现zookeeper 高可用。需要搭建集群。建议是奇数节点。需要三个zookeeper服务器。

搭建solr集群需要7台服务器。

搭建伪分布式:
需要三个zookeeper节点,实现zookeeper高可用
需要四个tomcat节点,分别运行4个Solr实例

建议虚拟机的内容1G以上。

CentOS-6.5-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

1.1.安装步骤

1.1.1.Zookeeper集群搭建

第一步:需要安装jdk环境。
第二步:把zookeeper的压缩包上传到服务器。
第三步:解压缩。
第四步:把zookeeper复制三份。

[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

第五步:在每个zookeeper目录下创建一个data目录。
第六步:在data目录下创建一个myid文件,文件名就叫做“myid”。内容就是每个实例的
id。例如1、2、3

[root@localhost data]# echo 1 >> myid
[root@localhost data]# cat myid 
1

第七步:修改配置文件。把conf目录下的zoo_sample.cfg文件改名为zoo.cfg
这里写图片描述

server.1=192.168.25.130:2881:3881
server.2=192.168.25.130:2882:3882
server.3=192.168.25.130:2883:3883

批量操作

./zookeeper01/bin/zkServer.sh start
./zookeeper02/bin/zkServer.sh start
./zookeeper03/bin/zkServer.sh start
修改文件为可执行文件
chmod u+x 文件名

第八步:启动每个zookeeper实例。
启动bin/zkServer.sh start

查看zookeeper的状态:
bin/zkServer.sh status

1.1.2.Solr集群的搭建

第一步:创建四个tomcat实例。每个tomcat运行在不同的端口。8180、8280、8380、8480
第二步:部署solr的war包。把单机版的solr工程复制到集群中的tomcat中。
第三步:为每个solr实例创建一个对应的solrhome。使用单机版的solrhome复制四份。
第四步:需要修改solr的web.xml文件。把solrhome关联起来。
第五步:配置solrCloud相关的配置。每个solrhome下都有一个solr.xml,把其中的ip及端口号配置好。Ip为tomcat运行的ip,端口号对应Tomcat的端口号
这里写图片描述
第六步:让zookeeper统一管理配置文件。需要把solrhome/collection1/conf目录上传到zookeeper。上传任意solrhome中的配置文件即可。
使用工具上传配置文件:/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

注:ip以及端口为zookeeper的ip和端口
查看zookeeper上的配置文件:
使用zookeeper目录下的bin/zkCli.sh命令查看zookeeper上的配置文件:

[root@localhost bin]# ./zkCli.sh 
[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] 
退出:
[zk: localhost:2181(CONNECTED) 3] quit

使用以下命令连接指定的zookeeper服务:

./zkCli.sh -server 192.168.25.154:2183

第七步:修改tomcat/bin目录下的catalina.sh 文件,关联solr和zookeeper。
把此配置添加到配置文件中:

JAVA_OPTS="-DzkHost=192.168.25.130:2181,192.168.25.130:2182,192.168.25.130:2183"

这里写图片描述
第八步:启动每个tomcat实例。要保证zookeeper集群是启动状态。
第九步:访问集群
这里写图片描述
第十步:创建新的Collection进行分片处理。
http://192.168.25.154:8180/solr/admin/collections?action=CREATE&name=collection2&numShards=2&replicationFactor=2
这里写图片描述
这里写图片描述
第十一步:删除不用的Collection。
http://192.168.25.154:8180/solr/admin/collections?action=DELETE&name=collection1
这里写图片描述

2.0 Solr单机版的搭建

第一步:将solr和tomcat的压缩包上传到linux服务器
解压tomcat

tar -zxvf  apache-tomcat-7.0.47 /usr/local/solr/tomcat

将solr解压到当前文件夹

tar zxvf solr-4.10.3.tgz.tgz

第二步:将 /root/myhome/solr-4.10.3/dist目录下的solr-4.10.3.war拷贝到tomcat的webapp下

cp  /root/myhome/solr-4.10.3/dist/solr-4.10.3.war  /usr/local/solr/tomcat/webapp/solr.war

运行tomcat,对war包进行解压
第三步: 将需要的jar包拷贝到solr的工程中

cp /root/myhome/solr-4.10.3/example/lib/ext/*  /usr/local/solr/tomcat/webapp/solr/WEB-INF/lib/

第四部:将example目录下的solr拷贝到solr目录下
cp solr /usr/local/solr/tomcat/webapp/solr/solrhome -r
第五步:在solr的WEB-INF下的web.xml中配置solrhome的位置

<env-entry>
       <env-entry-name>solr/home</env-entry-name>
       <env-entry-value>/usr/local/solr/solrhome/</env-entry-value>
       <env-entry-type>java.lang.String</env-entry-type>
</env-entry>

运行tomcat,可直接在浏览器访问

2.1.分词器的配置(此处使用IK分词器)

第一步:把中文分析器添加到工程中。
1、把IKAnalyzer2012FF_u1.jar添加到solr工程的lib目录下
2、把扩展词典、配置文件放到solr工程的WEB-INF/classes目录下。
第二步:配置一个FieldType,制定使用IKAnalyzer
修改schema.xml文件
修改Solr的schema.xml文件,添加FieldType:

<fieldType name="text_ik" class="solr.TextField">
  <analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/>
</fieldType>

第三步:配置业务域,type制定使用自定义的FieldType。
设置业务系统Field

<field name="item_title" type="text_ik" indexed="true" stored="true"/>
<field name="item_sell_point" type="text_ik" indexed="true" stored="true"/>
<field name="item_price"  type="long" indexed="true" stored="true"/>
<field name="item_image" type="string" indexed="false" stored="true" />
<field name="item_category_name" type="string" indexed="true" stored="true" />

<field name="item_keywords" type="text_ik" indexed="true" stored="false" multiValued="true"/>
<copyField source="item_title" dest="item_keywords"/>
<copyField source="item_sell_point" dest="item_keywords"/>
<copyField source="item_category_name" dest="item_keywords"/>

第四步:重启tomcat

2.2.使用SolrJ操作索引库

    /**
     * 添加索引(修改)
     * 
     * @throws Exception
     */
    // @Test
    public void testSave() throws Exception {
        // 创建SolrServer
        SolrServer solrServer = new HttpSolrServer("http://192.168.25.128:8080/solr/collection1");
        // 创建SolrInputDocument
        SolrInputDocument document = new SolrInputDocument();
        // 向文档中添加域
        document.addField("id", "test01");
        document.addField("item_title", "测试标题");
        document.addField("item_price", 1000);
        // 将文档写入索引库
        solrServer.add(document);
        // 提交
        solrServer.commit();
    }
    /**
     * 删除索引
     * 
     * @throws Exception
     */

    // @Test
    public void testDel() throws Exception {
        // 创建SolrServer
        SolrServer solrServer = new HttpSolrServer("http://192.168.25.128:8080/solr/collection1");
        // 根据id删除索引
        solrServer.deleteById("test01");
        solrServer.commit();
}
    /**
     * 查询索引
     * 
     * @throws Exception
     */

   @Test
    public void queryIndexFuza() throws Exception {
        SolrServer solrServer = new HttpSolrServer("http://192.168.25.163:8080/solr/collection1");
        //创建一个查询对象
        SolrQuery query = new SolrQuery();
        //查询条件
        query.setQuery("手机");
        query.setStart(0);
        query.setRows(20);
        query.set("df", "item_title");
        query.setHighlight(true);
        query.addHighlightField("item_title");
        query.setHighlightSimplePre("<em>");
        query.setHighlightSimplePost("</em>");
        //执行查询
        QueryResponse queryResponse = solrServer.query(query);
        //取文档列表。取查询结果的总记录数
        SolrDocumentList solrDocumentList = queryResponse.getResults();
        System.out.println("查询结果总记录数:" + solrDocumentList.getNumFound());
        //遍历文档列表,从取域的内容。
        Map<String, Map<String, List<String>>> highlighting = queryResponse.getHighlighting();
        for (SolrDocument solrDocument : solrDocumentList) {
            System.out.println(solrDocument.get("id"));
            //取高亮显示
            List<String> list = highlighting.get(solrDocument.get("id")).get("item_title");
            String title = "";
            if (list !=null && list.size() > 0 ) {
                title = list.get(0);
            } else {
                title = (String) solrDocument.get("item_title");
            }
            System.out.println(title);
            System.out.println(solrDocument.get("item_sell_point"));
            System.out.println(solrDocument.get("item_price"));
            System.out.println(solrDocument.get("item_image"));
            System.out.println(solrDocument.get("item_category_name"));
        }
    }
 /**
     * 使用Solr集群操作索引库
     * 
     * @throws Exception
     */
    @Test
    public void testCloudSolr() throws Exception {
        // 创建CloudSolr,指定注册中心端口
        CloudSolrServer solrServer = new CloudSolrServer("192.168.25.130:2181,192.168.25.130:2182,192.168.25.130:2183");
        // 设置默认Collection
        solrServer.setDefaultCollection("collection2");
        // 创建文档对象
        SolrInputDocument document = new SolrInputDocument();
        document.setField("id", "test01");
        document.setField("name", "测试Solr集群");
        solrServer.add(document);
        solrServer.commit();
    }
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">

    <!-- 注册Solr服务 -->
    <!-- <bean id="solrServer" class="org.apache.solr.client.solrj.impl.HttpSolrServer">
        <constructor-arg index="0" value="http://192.168.25.128:8080/solr/collection1"/>
    </bean> -->

    <!-- 配置Solr集群服务 -->
    <bean id="solrServer" class="org.apache.solr.client.solrj.impl.CloudSolrServer">
        <constructor-arg index="0" value="192.168.25.130:2181,192.168.25.130:2182,192.168.25.130:2183"></constructor-arg>
        <property name="defaultCollection" value="collection2"></property>
    </bean>
</beans>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Allen-xs

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值