淘淘商城系列——搜索系统搭建

来源:互联网 发布:淘宝素材网站有哪些 编辑:程序博客网 时间:2024/06/04 18:28

上文我们一起搭建了搜索服务工程,本文我将带领大家一起搭建搜索系统这个表现层工程。
现在我们就来新建一个taotao-search-web工程,该工程可参考taotao-portal-web工程来搭建哟!
首先点击【File】菜单选项,并在下拉框中选中【New】,接着点击【Other】,如下:
这里写图片描述
在输入框中输入maven,并选择Maven Project,如下:
这里写图片描述
点击【Next】,勾选Create a simple project复选框,如果你不打上这个勾,它会让你选择一个骨架,但骨架里面是没有pom这个模板的。
这里写图片描述
点击【Next】,出现如下对话框,在该对话框中定义maven工程的坐标,如下:
这里写图片描述
注意:taotao-search-web工程的打包方式是war,且须依赖父工程。
最后点击【Finish】,taotao-search-web工程就创建好了,但是新建的web工程由于缺少web.xml文件而报错,解决方法是在webapp目录下新建一个WEB-INF目录,并在该目录下新建web.xml文件,至于该文件的内容具体是什么,后面会具体给出,这里我们并不着急。
接着配置taotao-search-web工程的pom.xml文件,我们可参考taotao-portal-web工程的pom.xml文件来配置,只需稍作修改,将依赖的interface修改为taotao-search-interface(第二个<dependency>),最下面的tomcat插件端口号配置为8085,修改好的依赖如下:

<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-web</artifactId>    <version>0.0.1-SNAPSHOT</version>    <packaging>war</packaging>    <dependencies>        <!-- 依赖taotao-common -->        <dependency>              <groupId>com.taotao</groupId>              <artifactId>taotao-common</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 -->        <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>        <!-- JSP相关 -->        <dependency>            <groupId>jstl</groupId>            <artifactId>jstl</artifactId>        </dependency>        <dependency>            <groupId>javax.servlet</groupId>            <artifactId>servlet-api</artifactId>            <scope>provided</scope>        </dependency>        <dependency>            <groupId>javax.servlet</groupId>            <artifactId>jsp-api</artifactId>            <scope>provided</scope>        </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>        <dependency>            <groupId>junit</groupId>            <artifactId>junit</artifactId>        </dependency>    </dependencies>    <!-- 配置tomcat插件 -->    <build>        <plugins>            <plugin>                <groupId>org.apache.tomcat.maven</groupId>                <artifactId>tomcat7-maven-plugin</artifactId>                <configuration>                    <port>8085</port>                    <path>/</path>                </configuration>            </plugin>        </plugins>    </build></project>

紧接着来配置资源文件,我们亦可参考taotao-portal-web工程,将src/main/resources目录下的两个文件夹拷贝过来。先看resource目录下的resource.properties文件,该文件是用来配置常量的,目前我们还没有写业务代码,该文件暂时保持为空。
这里写图片描述
下面再看下spring目录下的springmvc.xml,修改要扫描的包和引用dubbo服务两项配置,要扫描的包”com.taotao.search.controller”我们是需要新建的,如下图所示。
这里写图片描述
为了大家方便复制,现把该文件的内容贴出。

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"    xmlns:context="http://www.springframework.org/schema/context"    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"     xmlns:mvc="http://www.springframework.org/schema/mvc"    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">    <!-- 加载属性文件 -->    <context:property-placeholder location="classpath:resource/resource.properties" />    <context:component-scan base-package="com.taotao.search.controller" />    <mvc:annotation-driven />    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <property name="prefix" value="/WEB-INF/jsp/" />        <property name="suffix" value=".jsp" />    </bean>    <!-- 引用dubbo服务 -->    <dubbo:application name="taotao-search-web"/>    <dubbo:registry protocol="zookeeper" address="192.168.25.128:2181"/>        <!-- <dubbo:reference interface="com.taotao.content.service.ContentService" id="contentService" /> --></beans>

最后来配置web.xml,我们依然可参考taotao-portal-web工程的web.xml,首先需要在webapp目录下新建一个WEB-INF目录,并拷贝taotao-portal-web工程的web.xml文件到WEB-INF目录下,我们需要修改的地方是名字,把原来所有的”taotao-portal-web”都更改为”taotao-search-web”(可以使用全文替换)。
至此,搜索系统便搭建完了。