SolrCloud搭建和使用

2 篇文章 0 订阅

SolrCloud(solr 云)是Solr提供的分布式搜索方案,当你需要大规模,容错,分布式索引和检索能力时使用 SolrCloud。当一个系统的索引数据量少的时候是不需要使用SolrCloud的,当索引量很大,搜索请求并发很高,这时需要使用SolrCloud来满足这些需求。

SolrCloud是基于Solr和Zookeeper的分布式搜索方案,它的主要思想是使用Zookeeper作为集群的配置信息中心。

3.3. 需要实现的solr集群架构

大体思路 :搭建zookeeper集群,搭建四个solr,solr是在tomcat下部署的,我们要修改tomcat,关联solr和zookeeper

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]# ll

total 4

-rw-r–r–. 1 root root 2 Apr 7 18:23 myid

[root@localhost data]# cat myid

1

第七步:修改配置文件。把conf目录下的zoo_sample.cfg文件改名为zoo.cfg

这里写图片描述

第八步:启动每个zookeeper实例。

启动bin/zkServer.sh start

查看zookeeper的状态:

bin/zkServer.sh status

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及端口号配置好。

这里写图片描述

第六步:让zookeeper统一管理配置文件。需要把solrhome/collection1/conf目录上传到zookeeper。上传任意solrhome中的配置文件即可。

使用工具上传配置文件:/root/solr-4.10.3/example/scripts/cloud-scripts/zkcli.sh

查看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

第七步:修改tomcat/bin目录下的catalina.sh 文件,关联solr和zookeeper。

把此配置添加到配置文件中:

JAVA_OPTS=”-DzkHost=192.168.25.154:2181,192.168.25.154:2182,192.168.25.154: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

这里写图片描述
这里写图片描述

使用solrJ管理solr服务

测试solrJ 添加文档和查询文档

package cn.e3mall.solrj;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.CloudSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.junit.Test;
import org.junit.experimental.theories.PotentialAssignment.CouldNotGenerateValueException;

public class TestSolrCloud {

    /**
     * solr集群添加测试
     * <p>Title: testAddDocument</p>
     * <p>Description: </p>
     * @throws Exception
     */
    @Test
    public void testAddDocument() throws Exception {
        //创建集群的连接
        CloudSolrServer cloudSolrServer = new CloudSolrServer("192.168.25.129:2181,192.168.25.129:2182,192.168.25.129:2183");
        cloudSolrServer.setDefaultCollection("collection2");
        //创建文档对象
        SolrInputDocument document = new SolrInputDocument();
        document.setField("id", "solrcloud01");
        document.setField("item_title", "测试商品01");
        document.setField("item_price", 123);
        //文件写入索引库
        cloudSolrServer.add(document);
        //提交
        cloudSolrServer.commit();
    }

    /**
     * solr集群查询
     * <p>Title: testQueryDocument</p>
     * <p>Description: </p>
     * @throws Exception
     */
    @Test
    public void testQueryDocument() throws Exception {
        CloudSolrServer cloudSolrServer = new CloudSolrServer("192.168.25.129:2181,192.168.25.129:2182,192.168.25.129:2183");
        cloudSolrServer.setDefaultCollection("collection2");
        SolrQuery solrQuery = new SolrQuery();
        solrQuery.setQuery("*:*");
        QueryResponse queryResponse = cloudSolrServer.query(solrQuery);
        SolrDocumentList solrDocumentList = queryResponse.getResults();
        System.out.println("总记录数:"+solrDocumentList.getNumFound());
        for (SolrDocument solrDocument : solrDocumentList) {
            System.out.println(solrDocument.get("id"));
            System.out.println(solrDocument.get("title"));
            System.out.println(solrDocument.get("item_title"));
            System.out.println(solrDocument.get("item_price"));
        }
    }
}

applicationContext-solr.xml 中配置:

<?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">

    <!-- 单机版solrJ -->
    <!-- <bean id="httpSolrServer" class="org.apache.solr.client.solrj.impl.HttpSolrServer">
        <constructor-arg index="0" value="http://192.168.25.128:8080/solr/collection1"></constructor-arg>
    </bean> -->
    <!-- 集群版solrJ -->
    <bean id="cloudSolrServer" class="org.apache.solr.client.solrj.impl.CloudSolrServer">
        <constructor-arg index="0" value="192.168.25.129:2181,192.168.25.129:2182,192.168.25.129:2183"></constructor-arg>
        <property name="defaultCollection" value="collection2"></property>
    </bean>
</beans>

然后和单机版一样使用

(开发人员搭环境不重要 会使用solrJ就够了 嘿嘿)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值