学习淘淘商城第五十一课(搜索功能切换到集群)

本文介绍如何在Spring容器中配置Solr集群,并实现从数据库导入数据到Solr集群的过程。通过简单的配置更改,即可在单机版与集群版间切换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

      Solr单机版使用的是HttpSolrServer,集群版用的是CloudSolrServer,这两个类都是SolrServer的子类,我们在Dao层使用的SolrServer进行操作,从而我们可以不用改Dao层代码,只需要在Spring容器中切换单机版或集群版Solr即可。


      现在我们到applicationContext-solr.xml文件当中配置一下Solr集群,如下图所示。


      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:dubbo="http://code.alibabatech.com/schema/dubbo"
	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://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
    
   <!-- 配置单机版Solr -->
   <!-- <bean id="httpSolrServer" class="org.apache.solr.client.solrj.impl.HttpSolrServer">
   	   <constructor-arg name="baseURL" value="http://192.168.156.22:8080/solr/collection1"/>
   </bean> -->
   
   <!-- 集群版Solr连接 -->
   <bean id="cloudSolrServer" class="org.apache.solr.client.solrj.impl.CloudSolrServer">
       <constructor-arg name="zkHost" value="192.168.156.23:2181,192.168.156.24:2181,192.168.156.25:2181"/>
       <property name="defaultCollection" value="collection2"/>
   </bean>
</beans>

        由于还没有将数据库中的数据导入到集群版的索引库,因此我们需要到淘淘商城后台重新导入到索引库,不过在此之前,我们最好把上节课测试添加的那条数据给删除掉。如下图所示,我们点击"Documents",在Document Type类型中选择XML,在Document(s)中输入删除语句,然后点击提交按钮即可完成删除操作。


      删除操作代码如下:

<delete>
<query>id:"11111"</query>
</delete>
<commit/>

       下面我们把数据库中的数据导入到集群版索引库(整个过程耗费的时间比较长,一两分钟左右),如下图所示。


       导入成功后,我们访问淘淘商城首页,并在搜索框中输入"金立M2017"并按回车进行搜索,发现可以正常搜索到结果,如下图所示。说明我们切换到集群版后,代码完全没问题。


        

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值