淘淘商城——搜索服务搭建

Solr服务配置好之后,接下来我们就要考虑一个问题,那就是我们要把商品数据导入到索引库里面才行,否则的话,我们是没有办法实现搜索这个功能的。接下来我们势必要搭建搜索工程了。首先,我们还是先看下淘淘商城的整体架构图,如下图所示,我们已经写完了后台管理系统、商品服务、商城门户、内容服务,现在需要搭建的是搜索系统和搜索服务。
这里写图片描述
下面我们便来搭建搜索服务工程。

搭建taotao-search工程

我们可参考taotao-manager工程的创建来搭建搜索服务工程,它是后台的服务层工程。这个工程里面需要很多模块,我们须把这些模块单独拆分,所以它应该是一个聚合工程。
首先点击【File】菜单选项,并在下拉框中选中【New】,接着点击【Other】,如下:
这里写图片描述
在输入框中输入maven,并选择Maven Project,如下:
这里写图片描述
点击【Next】,勾选Create a simple project复选框,如果你不打上这个勾,它会让你选择一个骨架,但骨架里面是没有pom这个模板的。
这里写图片描述
点击【Next】,出现如下对话框,在该对话框中定义maven工程的坐标,如下:
这里写图片描述
最后点击【Finish】,taotao-search工程即可创建完毕。

搭建taotao-search-interface模块

现在我们来搭建taotao-search-interface模块,方法是在taotao-search工程上右键→Maven→New Maven Module Project,如下图所示。
这里写图片描述
弹出如下对话框,勾选”Create a simple project”,在Module Name中输入taotao-search-interface,然后点击”Next”。
这里写图片描述
选择该模块的打包方式,我们使用默认的jar,直接点击”Finish”。
这里写图片描述

搭建taotao-search-service模块

搭建taotao-search-service模块,步骤基本上同上,只是打包方式换成war即可,如下图所示。
这里写图片描述
至于dao和pojo这两个模块我们不用在taotao-search工程再新建一遍了,因为我们在taotao-manager工程当中便创建好了,我们只需要引用这两个模块就可以了。

配置taotao-search工程的pom.xml文件

参考taotao-manager聚合工程,把它的pom.xml文件中的依赖拷过来,只是需要修改下tomcat插件的端口号,修改为8084(前面已经用到8083了)。
这里写图片描述
为方便大家复制,现把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>
    <groupId>com.taotao</groupId>
    <artifactId>taotao-search</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>
    <modules>
        <module>taotao-search-interface</module>
        <module>taotao-search-service</module>
    </modules>

    <dependencies>
        <dependency>
            <groupId>com.taotao</groupId>
            <artifactId>taotao-common</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
    <!-- 配置tomcat插件 -->
    <build>
        <plugins>
            <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文件

我们可参考taotao-manager-interface工程的pom.xml文件,由于我们的搜索服务也可能用到pojo,主要添加对taotao-manager-pojo的依赖,如下图所示。
这里写图片描述

配置taotao-search-service模块的pom.xml文件

我们可以参考taotao-manager-service工程的依赖。由于搜索服务要用到数据库,因此需要有taotao-manager-dao,把依赖的interface改为我们的taotao-search-interface,taotao-search-service所需要依赖的内容如下所示。

<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>
        <dependency>
            <groupId>com.taotao</groupId>
            <artifactId>taotao-manager-dao</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.taotao</groupId>
            <artifactId>taotao-search-interface</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <!-- 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相关 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <!-- 排除依赖 -->
            <exclusions>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.jboss.netty</groupId>
                    <artifactId>netty</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- zookeeper的客户端,你要连接zookeeper,需要把以下两个jar包加进来 -->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
        </dependency>
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
        </dependency>
    </dependencies>
</project>

框架整合

我们把taotao-manager-service的src/main/resources目录下的mybatis、properties、spring三个目录粘贴到taotao-search-service的src/main/resources目录中。SqlMapConfig.xml文件不用动,如下图所示。
这里写图片描述
properties目录下的db.properties配置文件也不用修改,如下图所示。
这里写图片描述
接着我们再看下spring目录下的文件,首先看applicationContext-dao.xml文件,这个配置文件用来操作数据库的,我们需要在该配置文件中做一点改动,如下图所示。
这里写图片描述
为了大家方便复制,现把该文件的内容黏贴出来。

<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/db.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>
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.taotao.mapper,com.taotao.search.mapper" />
    </bean>
</beans>

除此之外,我们还要到taotao-search-service的src/main/java目录下新建一个com.taotao.search.mapper包,如下图所示。
这里写图片描述
下面我们来看下spring目录下的第二个文件——applicationContext-service.xml,我们把包扫描器扫描的包修改为”com.taotao.search.service”,将对外发布的dubbo服务的端口改为”20882”,由于还没写服务接口,我们先把拷过来的暴露的服务接口注释掉(留个模板),并且将提供方应用信息名称改为”taotao-search”。
这里写图片描述
为了大家方便复制,现把该文件的内容黏贴出来。

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

    <context:component-scan base-package="com.taotao.search.service"></context:component-scan>

    <!-- 使用dubbo发布服务 -->
    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="taotao-search" />
    <dubbo:registry protocol="zookeeper" address="192.168.25.128:2181" />
    <!-- 用dubbo协议在20882端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20882" />
    <!-- 声明需要暴露的服务接口 -->
    <!-- <dubbo:service interface="com.taotao.service.ItemService" ref="itemServiceImpl" timeout="300000" /> -->

</beans>

由于上面配置的要扫描的包还没有创建,因此我们在taotao-search-interface工程中新建”com.taotao.search.service”包,在taotao-search-service工程中新建”com.taotao.search.service.impl”包,如下图所示。
这里写图片描述
我们再看下一个applicationContext-trans.xml配置文件,这个配置文件是用来配置事务的,由于搜索服务只是查数据库,不涉及到改数据库,因此我们用不到事务,照理来说,我们应该把这个配置文件删除掉,但我这里还是将其留下来了,而且还修改切面的包为com.taotao.search.service,如下图所示。
这里写图片描述
至于该文件删不删,全凭个人意愿。
最后,我们把taotao-manager-service下的WEB-INF及web.xml,粘贴到taotao-search-service的webapp目录下,修改web.xml文件中的<display-name>为”taotao-search”。
这里写图片描述
至此,我们的框架就整合好了。希望大家渡过愉快的一天!

猜你喜欢

转载自blog.csdn.net/a_blackmoon/article/details/80480015