1. 程式人生 > >day73_淘淘商城專案_06_搜尋工程的搭建 + linux下solr索引庫的搭建 + 把商品資料匯入到索引庫中(後臺) + 商品搜尋功能實現(前臺) + 圖片顯示等問題解決_匠心筆記

day73_淘淘商城專案_06_搜尋工程的搭建 + linux下solr索引庫的搭建 + 把商品資料匯入到索引庫中(後臺) + 商品搜尋功能實現(前臺) + 圖片顯示等問題解決_匠心筆記

淘淘商城專案_06


課程計劃

  • 第六天:
    • 1、搜尋工程的搭建
    • 2、linux下solr服務的搭建
    • 3、測試使用solrJ管理索引庫
    • 4、把商品資料匯入到索引庫中(後臺功能)
    • 5、商品搜尋功能實現(前臺功能)

1、搜尋工程的搭建


要實現搜尋功能,需要搭建solr服務搜尋服務工程搜尋系統(表現層的工程)

1.1、Solr服務搭建

1.1.1、solr的環境

  solr是java開發的。
  solr的安裝檔案。
  推薦在Linux環境下使用Solr,需要安裝環境Linux。
  需要安裝jdk。參考連結:https://www.cnblogs.com/chenmingjun/p/9931593.html
  需要安裝tomcat。

1.1.2、solr的搭建步驟

第一步:使用SecureCRT的SFTP功能,把solr-4.10.3.tgz.tgz的壓縮包上傳到Linux系統。
第二步:解壓縮solr後,刪除該安裝包。

[[email protected] ~]# tar -zxvf solr-4.10.3.tgz.tgz
......
......
[[email protected] ~]# ll
總用量 146496
drwxr-xr-x. 8 root root       218 11月 20 17:07 solr-4.10.3
-rw-r--r--. 1 root root 150010621 9月  26 23:16 solr-4.10.3.tgz.tgz
[[email protected] ~]# rm -rf solr-4.10.3.tgz.tgz
[[email protected] ~]# ll
總用量 0
drwxr-xr-x. 8 root root 218 11月 20 17:07 solr-4.10.3
[[email protected] ~]# 

第三步:使用SecureCRT的SFTP功能,把apache-tomcat-7.0.47.tar.gz的壓縮包上傳到Linux系統,解壓後刪除安裝包。

[[email protected] ~]# tar -zxvf apache-tomcat-7.0.47.tar.gz
[[email protected] ~]# # rm -rf apache-tomcat-7.0.47.tar.gz

第四步:建立solr存放的目錄,複製apache-tomcat-7.0.47目錄/usr/local/solr/tomcat目錄

[[email protected] ~]# mkdir /usr/local/solr
[[email protected] ~]# cp -r apache-tomcat-7.0.47/ /usr/local/solr/tomcat

第五步:把solr-4.10.3/dist/solr-4.10.3.war檔案部署(複製)到tomcat目錄下,並重新命名為solr.war

[[email protected] ~]# cp solr-4.10.3/dist/solr-4.10.3.war /usr/local/solr/tomcat/webapps/solr.war

注意:複製目錄(資料夾)的時候需要加-r,複製檔案的時候不需要加-r
第六步:解壓縮solr.war包。啟動tomcat即可自動解壓war包,並檢視tomcat啟動日誌

[[email protected] ~]# cd /usr/local/solr/tomcat/bin/
[[email protected] bin]# ./startup.sh 
Using CATALINA_BASE:   /usr/local/solr/tomcat
Using CATALINA_HOME:   /usr/local/solr/tomcat
Using CATALINA_TMPDIR: /usr/local/solr/tomcat/temp
Using JRE_HOME:        /usr/local/java/jdk1.7.0_80/jre
Using CLASSPATH:       /usr/local/solr/tomcat/bin/bootstrap.jar:/usr/local/solr/tomcat/bin/tomcat-juli.jar
[[email protected] bin]# cd ..
[[email protected] tomcat]# tail -f logs/catalina.out
十一月 20, 2018 5:23:24 下午 org.apache.catalina.startup.HostConfig deployDirectory
資訊: Deploying web application directory /usr/local/solr/tomcat/webapps/host-manager
十一月 20, 2018 5:23:25 下午 org.apache.catalina.startup.HostConfig deployDirectory
資訊: Deploying web application directory /usr/local/solr/tomcat/webapps/manager
十一月 20, 2018 5:23:25 下午 org.apache.coyote.AbstractProtocol start
資訊: Starting ProtocolHandler ["http-bio-8080"]
十一月 20, 2018 5:23:25 下午 org.apache.coyote.AbstractProtocol start
資訊: Starting ProtocolHandler ["ajp-bio-8009"]
十一月 20, 2018 5:23:25 下午 org.apache.catalina.startup.Catalina start
資訊: Server startup in 11144 ms

第六步:關閉tomcat後,刪除掉沒用的solr.war包。原則:沒用的東西及時刪掉。
注意:要是想刪掉沒用的solr.war包,必須在關閉tomcat的情況下,否則解壓縮後的solr包也會一併刪除掉。

[[email protected] tomcat]# bin/shutdown.sh 
Using CATALINA_BASE:   /usr/local/solr/tomcat
Using CATALINA_HOME:   /usr/local/solr/tomcat
Using CATALINA_TMPDIR: /usr/local/solr/tomcat/temp
Using JRE_HOME:        /usr/local/java/jdk1.7.0_80/jre
Using CLASSPATH:       /usr/local/solr/tomcat/bin/bootstrap.jar:/usr/local/solr/tomcat/bin/tomcat-juli.jar
[[email protected] tomcat]# rm -rf webapps/solr.war 

第七步:想要啟動solr工程,還需要新增solr的擴充套件服務包
/root/solr-4.10.3/example/lib/ext目錄下的所有的jar包,新增到solr工程中。

[[email protected] ext]# pwd
/root/solr-4.10.3/example/lib/ext
[[email protected] ext]# cp * /usr/local/solr/tomcat/webapps/solr/WEB-INF/lib/

第八步:建立一個solrhome目錄。
我們知道:/root/solr-4.10.3/example/solr目錄就是一個solrhome目錄。
複製此目錄中所有內容到/usr/local/solr/solrhome目錄下

[[email protected] example]# pwd
/root/solr-4.10.3/example
[[email protected] example]# cp -r solr /usr/local/solr/solrhome

第九步:關聯solr工程solrhome。需要修改solr工程web.xml檔案。

[[email protected] ~]# cd /usr/local/solr/tomcat/webapps/solr/WEB-INF/
[[email protected] WEB-INF]# vim web.xml

修改如下圖所示:

第九步:再次啟動tomcat

[[email protected] ~]# cd /usr/local/solr/tomcat/
[[email protected] tomcat]# bin/startup.sh 

第十步:修改防火牆配置
CentOS 7.X 預設的防火牆不是iptables,而是firewalld。我們可以試一下systemctl stop firewalld關閉防火牆,但是不推薦該方式。
CentOS 6.X 是iptables,可以使用vim /etc/sysconfig/iptables修改配置即可。
本博主的是CentOS7,防火牆使用的是firewalld,我們使用命令的方式來新增埠(修改後需要重啟firewalld服務):

[[email protected] ~]# cd /etc/firewalld/zones/
[[email protected] zones]# firewall-cmd --permanent --add-port=8080/tcp
success
[[email protected] zones]# service firewalld restart
Redirecting to /bin/systemctl restart firewalld.service
[[email protected] zones]#

第十一步:測試連線
訪問地址:http://192.168.25.154:8080/solr/
其實和在windows下的配置完全一樣。
瀏覽器介面如下:

點選按鈕“collection1”

1.1.3、solr的使用

  • 新增文件時必須有id域,其他域必須在solr的schema.xml中進行定義。

1.2、配置業務域

1.2.1、在schema.xml中需要定義以下欄位

  1、商品id(根據id查詢商品描述頁(詳情頁))
  2、商品標題title
  3、商品賣點sell_point
  4、商品價格price
  5、商品圖片image
  6、分類名稱category_name(不是分類id,我們一般不會根據商品分類id去查詢商品,而是根據商品分類名稱去查)
  7、商品描述tb_item_desc(商品詳情)(實際開發中不需要搜尋商品描述(商品詳情),為了練習需要,我們也把該表加入索引庫)
  一共涉及到三張表:tb_item、tb_item_cat、tb_item_desc。
  建立對應的業務域。同時需要指定中文分析器。

1.2.2、建立自定義業務域步驟

第一步:把中文分析器新增到solr工程中。
  0、把資料夾IK Analyzer 2012FF_hf1上傳至linux中。
  1、把IKAnalyzer2012FF_u1.jar拷貝到solr工程的lib目錄下。
  2、把擴充套件詞詞典停用詞字典配置檔案拷貝到solr工程的WEB-INF/classes目錄下。(沒有classes目錄就先建立該目錄)

[[email protected] IK Analyzer 2012FF_hf1]# pwd
/root/IK Analyzer 2012FF_hf1
[[email protected] IK Analyzer 2012FF_hf1]# cp IKAnalyzer2012FF_u1.jar /usr/local/solr/tomcat/webapps/solr/WEB-INF/lib/
[[email protected] IK Analyzer 2012FF_hf1]# mkdir /usr/local/solr/tomcat/webapps/solr/WEB-INF/classes
[[email protected] IK Analyzer 2012FF_hf1]# cp mydict.dic ext_stopword.dic IKAnalyzer.cfg.xml /usr/local/solr/tomcat/webapps/solr/WEB-INF/classes
[[email protected] IK Analyzer 2012FF_hf1]# ll /usr/local/solr/tomcat/webapps/solr/WEB-INF/classes
總用量 12
-rw-r--r--. 1 root root 168 11月 20 19:29 ext_stopword.dic
-rw-r--r--. 1 root root 419 11月 20 19:29 IKAnalyzer.cfg.xml
-rw-r--r--. 1 root root  34 11月 20 19:29 mydict.dic
[[email protected] IK Analyzer 2012FF_hf1]# 

第二步:配置一個自定義的fieldType,使用指定的中文分詞器IKAnalyzer
  修改solr工程下的schema.xml檔案,在檔案末尾新增一個自定義的fieldType,注意:要在標籤<schema></schema>裡面新增。

[[email protected] conf]# pwd
/usr/local/solr/solrhome/collection1/conf
[[email protected] conf]# vim schema.xml

  新增內容如下:

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

第三步:配置自定義業務域,type指定使用自定義的業務域型別fieldType。
  設定自定義業務域的field

<schema>
......
......
    <!-- 配置自定義的業務域型別 -->
    <fieldType name="text_ik" class="solr.TextField">
      <analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/>
    </fieldType>
    
    <!-- 配置自定義的業務域 -->
      <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"/>
</schema>

  注意:分類名稱是不分詞只建立索引。商品描述是分詞但是不儲存。

第四步:重啟tomcat,測試我們自定義的業務域是否好使。
測試結果如下:

1.3、搜尋服務層工程的搭建

1.3.1、搜尋服務工程的建立可以參考taotao-content的建立

taotao-search(聚合工程pom)
  |–taotao-search-interface(jar)
  |–taotao-search-service(war)
這裡不再贅圖了。
目錄結構如下:

1.3.2、pom.xml的配置可以參考taotao-content的配置

/taotao-search/pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
	http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>com.taotao</groupId>
		<artifactId>taotao-parent</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<artifactId>taotao-search</artifactId>
	<packaging>pom</packaging>
	<modules>
		<module>taotao-search-interface</module>
		<module>taotao-search-service</module>
	</modules>
	<dependencies>
		<!-- 配置對taotao-common的依賴 -->
		<dependency>
			<groupId>com.taotao</groupId>
			<artifactId>taotao-common</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
	</dependencies>
    <build>
        <plugins>
            <!-- 配置Tomcat外掛  -->
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <configuration>
                    <port>8084</port>
                    <path>/</path>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

/taotao-search-interface/pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
	http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>com.taotao</groupId>
		<artifactId>taotao-search</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<artifactId>taotao-search-interface</artifactId>
    <dependencies>
        <!-- 配置對taotao-manager-pojo的依賴 -->
        <dependency>
            <groupId>com.taotao</groupId>
            <artifactId>taotao-manager-pojo</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

/taotao-search-service/pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
	http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>com.taotao</groupId>
		<artifactId>taotao-search</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<artifactId>taotao-search-service</artifactId>
	<packaging>war</packaging>
    <dependencies>
        <!-- 配置對taotao-manager-dao的依賴 -->
        <dependency>
            <groupId>com.taotao</groupId>
            <artifactId>taotao-manager-dao</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <!-- 配置對taotao-search-interface的依賴:服務層釋出服務要通過該介面 -->
        <dependency>
            <groupId>com.taotao</groupId>
            <artifactId>taotao-search-interface</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <!-- 配置對spring的依賴 -->
        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jms</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
        </dependency>
        <!-- 配置對dubbo的依賴 -->
		<!-- dubbo相關 -->
		<dependency>
		    <groupId>com.alibaba</groupId>
		    <artifactId>dubbo</artifactId>
		    <!-- 排除對低版本jar包的依賴 -->
		    <exclusions>
		    	<exclusion>
		    		<artifactId>spring</artifactId>
		    		<groupId>org.springframework</groupId>
		    	</exclusion>
		    	<exclusion>
		    		<artifactId>netty</artifactId>
		    		<groupId>org.jboss.netty</groupId>
		    	</exclusion>
		    </exclusions>
		</dependency>
		<dependency>
		    <groupId>org.apache.zookeeper</groupId>
		    <artifactId>zookeeper</artifactId>
		</dependency>
		<dependency>
		    <groupId>com.github.sgroschupf</groupId>
		    <artifactId>zkclient</artifactId>
		</dependency>
    </dependencies>
</project>

1.3.3、框架整合的配置檔案可以參考taotao-content-service的配置


由於搜尋的資料涉及到3張表,所以需要自己定義mapper。
而mapper的使用,只在搜尋服務工程中,所以mapper介面及對映檔案需要放在taotao-search-service工程中。
applicationContext-dao.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">
	<!-- 配置資料庫連線池 -->
	<!-- 載入配置檔案 -->
	<context:property-placeholder location="classpath:properties/*.properties" />
	<!-- 資料庫連線池 -->
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
		destroy-method="close">
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<property name="driverClassName" value="${jdbc.driver}" />
		<property name="maxActive" value="10" />
		<property name="minIdle" value="5" />
	</bean>	
	<!-- 配置讓spring管理sqlsessionfactory,使用mybatis和spring整合包中的 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 資料庫連線池 -->
		<property name=