Solr集群,
Solr服务搭建
1.Solr的环境
Solr是java开发。
需要安装jdk。
安装环境Linux。
需要安装Tomcat。
2.搭建步骤
第一步:把solr 的压缩包上传到Linux系统
第二步:解压solr。
第三步:安装Tomcat,解压缩即可。
第四步:把solr部署到Tomcat下。
第五步:解压缩war包。启动Tomcat解压。
第六步:把/root/solr-4.10.3/example/lib/ext目录下的所有的、中。
[root@localhost ext]# pwd
/root/solr-4.10.3/example/lib/ext
[root@localhost ext]# cp */usr/local/solr/tomcat/webapps/solr/WEB-INF/lib/
第七步:创建一个solrhome。/example/solr目录就是一个solrhome。复制此目录到/usr/local/solr/solrhome
[root@localhost example]# pwd
/root/solr-4.10.3/example
[root@localhost example]# cp -r solr/usr/local/solr/solrhome
[root@localhost example]#
第八步:关联solr及solrhome。需要修改solr工程的web.xml文件。
第九步:启动Tomcat
http://192.168.25.154:8080/solr/
Solr配置中文解析器
创建对应的业务域。需要制定中文分析器。
创建步骤:
第一步:把中文分析中。
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_desc" type="text_ik" indexed="true" stored="false" />
<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"/> <copyField source="item_desc" dest="item_keywords"/> |
第四步:重启tomcat
以上单个solr配置
Solr集群配置步骤
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
第七步:修改配置文件。把每个zookeeper下的conf目录下的zoo_sample.cfg文件改名为zoo.cfg
server.1=192.168.25.133:2881:3881 server.2=192.168.25.133:2882:3882 server.3=192.168.25.133:2883:3883 |
第八步:启动每个zookeeper实例。
启动bin/zkServer.sh start
查看zookeeper的状态:
bin/zkServer.sh status
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
./zkcli.sh -zkhost 192.168.25.133:2181,192.168.25.133:2182,192.168.25.133:2183 -cmd upconfig -confdir /usr/local/taotao/solr-cluder/solrhome01/collection1/conf -confname myconf |
查看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.133,192.168.25.133:2182,192.168.25.133: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
1. 使用solrJ管理集群
添加文档
使用步骤:
第一步:把solrJ相关的jar包添加到工程中。
第二步:创建一个SolrServer对象,需要使用CloudSolrServer子类。构造方法的参数是zookeeper的地址列表。
第三步:需要设置DefaultCollection属性。
第四步:创建一SolrInputDocument对象。
第五步:向文档对象中添加域
第六步:把文档对象写入索引库。
第七步:提交。
@Test publicvoid testSolrCloudAddDocument() throws Exception { // 第一步:把solrJ相关的jar包添加到工程中。 // 第二步:创建一个SolrServer对象,需要使用CloudSolrServer子类。构造方法的参数是zookeeper的地址列表。 //参数是zookeeper的地址列表,使用逗号分隔 CloudSolrServer solrServer = new CloudSolrServer("192.168.25.154:2181,192.168.25.154:2182,192.168.25.154:2183"); // 第三步:需要设置DefaultCollection属性。 solrServer.setDefaultCollection("collection2"); // 第四步:创建一SolrInputDocument对象。 SolrInputDocument document = new SolrInputDocument(); // 第五步:向文档对象中添加域 document.addField("item_title", "测试商品"); document.addField("item_price", "100"); document.addField("id", "test001"); // 第六步:把文档对象写入索引库。 solrServer.add(document); // 第七步:提交。 solrServer.commit();
} |
查询文档
创建一个CloudSolrServer对象,其他处理和单机版一致。
2. 把搜索功能切换到集群版
<?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-beans4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx4.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util4.2.xsd">
<!-- 单机版solr服务配置 --> <!-- <bean id="httpSolrServer" class="org.apache.solr.client.solrj.impl.HttpSolrServer"> <constructor-arg name="baseURL" value="http://192.168.25.154:8080/solr"></constructor-arg> </bean> --> <!-- 集群版solr服务 --> <bean id="cloudSolrServer" class="org.apache.solr.client.solrj.impl.CloudSolrServer"> <constructor-arg name="zkHost" value="192.168.25.154:2181,192.168.25.154:2182,192.168.25.154:2183"></constructor-arg> <property name="defaultCollection" value="collection2"></property> </bean> </beans> |