day73_淘淘商城項(xiàng)目_06_solr索引庫搭建 + solr搜索功能實(shí)現(xiàn) + 圖片顯示等問題解一決
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
后,删除该安装包。
[root@itheima ~]# tar -zxvf solr-4.10.3.tgz.tgz
......
......
[root@itheima ~]# 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
[root@itheima ~]# rm -rf solr-4.10.3.tgz.tgz
[root@itheima ~]# ll
总用量 0
drwxr-xr-x. 8 root root 218 11月 20 17:07 solr-4.10.3
[root@itheima ~]#
第三步:使用SecureCRT的SFTP功能,把apache-tomcat-7.0.47.tar.gz
的压缩包上传到Linux系统,解压后删除安装包。
[root@itheima ~]# tar -zxvf apache-tomcat-7.0.47.tar.gz
[root@itheima ~]# # rm -rf apache-tomcat-7.0.47.tar.gz
第四步:创建solr存放的目录
,复制apache-tomcat-7.0.47目录
到/usr/local/solr/tomcat目录
下
[root@itheima ~]# mkdir /usr/local/solr
[root@itheima ~]# cp -r apache-tomcat-7.0.47/ /usr/local/solr/tomcat
第五步:把solr-4.10.3/dist/solr-4.10.3.war文件
部署(复制)到tomcat
目录下,并重命名为solr.war
。
[root@itheima ~]# 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启动日志
。
[root@itheima ~]# cd /usr/local/solr/tomcat/bin/
[root@itheima 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
[root@itheima bin]# cd ..
[root@itheima 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包也会一并删除掉。
[root@itheima 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
[root@itheima tomcat]# rm -rf webapps/solr.war
第七步:想要启动solr工程
,还需要添加solr的扩展服务包
。
把/root/solr-4.10.3/example/lib/ext
目录下的所有的jar包,添加到solr工程
中。
[root@itheima ext]# pwd
/root/solr-4.10.3/example/lib/ext
[root@itheima ext]# cp * /usr/local/solr/tomcat/webapps/solr/WEB-INF/lib/
第八步:创建一个solrhome
目录。
我们知道:/root/solr-4.10.3/example/solr
目录就是一个solrhome
目录。
复制此目录中所有内容到/usr/local/solr/solrhome
目录下
[root@itheima example]# pwd
/root/solr-4.10.3/example
[root@itheima example]# cp -r solr /usr/local/solr/solrhome
第九步:关联solr工程
及solrhome
。需要修改solr工程
的web.xml
文件。
[root@itheima ~]# cd /usr/local/solr/tomcat/webapps/solr/WEB-INF/
[root@itheima WEB-INF]# vim web.xml
修改如下图所示:
第九步:再次启动tomcat
[root@itheima ~]# cd /usr/local/solr/tomcat/
[root@itheima tomcat]# bin/startup.sh
第十步:修改防火墙配置
CentOS 7.X 默认的防火墙不是iptables,而是firewalld。我们可以试一下systemctl stop firewalld关闭防火墙,但是不推荐该方式。
CentOS 6.X 是iptables,可以使用vim /etc/sysconfig/iptables
修改配置即可。
本博主的是CentOS7,防火墙使用的是firewalld,我们使用命令的方式来添加端口(修改后需要重启firewalld服务):
[root@itheima ~]# cd /etc/firewalld/zones/
[root@itheima zones]# firewall-cmd --permanent --add-port=8080/tcp
success
[root@itheima zones]# service firewalld restart
Redirecting to /bin/systemctl restart firewalld.service
[root@itheima 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、商品描述item_desc(实际开发中不需要搜索商品描述)
一共涉及到三张表:tb_item、item_cat、item_desc。
创建对应的业务域。同时需要指定中文分析器。
1.2.2、创建业务域步骤
第一步:把中文分析器添加到solr工程中。
0、把文件夹IK Analyzer 2012FF_hf1
上传至linux中。
1、把IKAnalyzer2012FF_u1.jar
拷贝到solr工程的lib目录
下。
2、把扩展词词典
、停用词字典
、配置文件
拷贝到solr工程的WEB-INF/classes
目录下。(没有classes目录就先创建该目录)
[root@itheima IK Analyzer 2012FF_hf1]# pwd
/root/IK Analyzer 2012FF_hf1
[root@itheima IK Analyzer 2012FF_hf1]# cp IKAnalyzer2012FF_u1.jar /usr/local/solr/tomcat/webapps/solr/WEB-INF/lib/
[root@itheima IK Analyzer 2012FF_hf1]# mkdir /usr/local/solr/tomcat/webapps/solr/WEB-INF/classes
[root@itheima IK Analyzer 2012FF_hf1]# cp mydict.dic ext_stopword.dic IKAnalyzer.cfg.xml /usr/local/solr/tomcat/webapps/solr/WEB-INF/classes
[root@itheima 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
[root@itheima IK Analyzer 2012FF_hf1]#
第二步:配置一个自定义的fieldType
,使用指定的中文分词器IKAnalyzer
。
修改solr工程下的schema.xml
文件,在文件末尾添加一个自定义的fieldType
,注意:要在标签<schema></schema>
里面添加。
[root@itheima conf]# pwd
/usr/local/solr/solrhome/collection1/conf
[root@itheima 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>
<!-- 配置对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>8085</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>
<!-- 配置对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>
<!-- 配置对dao的依赖 -->
<dependency>
<groupId>com.taotao</groupId>
<artifactId>taotao-manager-dao</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- 配置对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="dataSource" ref="dataSource" />
<!-- 加载mybatis的全局配置文件 -->
<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />
</bean>
<!-- 配置Mapper映射文件的包扫描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.taotao.mapper" />
</bean>
</beans>
其他配置文件,参考taotao-content-service的配置。
} // 5、把文档对象添加到索引库中。 由于把 在taotao-manager-web工程中的pom.xml中添加如下:
}
solrServer.add(document);
}
// 6、提交修改。
solrServer.commit();
// 7、返回TaotaoResult。
return TaotaoResult.ok();
}
}3.3.4、在taotao-search-service中发布服务
特别注意:
由于我们的dubbo和zooKeeper是安装在虚拟机CentOS 7.5 上的,CentOS 7.X 默认的防火墙不是iptables,而是firewalld。我们可以试一下systemctl stop firewalld关闭防火墙,但是不推荐该方式。CentOS 6.X 是iptables,可以使用vim /etc/sysconfig/iptables
修改配置即可。
本博主的是CentOS7,防火墙使用的是firewalld,我们使用命令的方式来添加端口20882(修改后需要重启firewalld服务):[root@itheima ~]# cd /etc/firewalld/zones/
[root@itheima zones]# firewall-cmd --permanent --add-port=20882/tcp
success
[root@itheima zones]# service firewalld restart
Redirecting to /bin/systemctl restart firewalld.service
[root@itheima zones]#3.4、表现层
3.4.1、引用服务
从数据库中查询到的新的商品数据导入到索引库中
属于后台功能
,所以我们在taotao-manager-web中引用服务。
在/taotao-manager-web/src/main/resources/spring/springmvc.xml中引用服务:3.4.2、添加对taotao-search-interface的依赖
原文出处:https://www.cnblogs.com/chenmingjun/p/10002204.html
共同學(xué)習(xí),寫下你的評論
評論加載中...
作者其他優(yōu)質(zhì)文章