淘购:淘淘商城——订单系统服务端和客户端工程搭建

首先我们还是先看一眼淘淘商城的系统架构,如下图所示,可以看到订单模块是单独的模块,有服务端还有客户端,服务端负责存储订单,客户端负责展示订单。
这里写图片描述
下面我们便开始搭建工程,首先来搭建订单服务工程——taotao-order。

搭建taotao-order工程

我们可参考taotao-sso工程的创建来搭建订单服务工程,它是后台的服务层工程。这个工程是个pom(聚合)工程,包含两个子模块——taotao-order-interface和taotao-order-service。
首先点击【File】菜单选项,并在下拉框中选中【New】,接着点击【Other】,如下:
这里写图片描述
在输入框中输入maven,并选择Maven Project,如下:
这里写图片描述
点击【Next】,勾选Create a simple project复选框,如果你不打上这个勾,它会让你选择一个骨架,但骨架里面是没有pom这个模板的。
这里写图片描述
点击【Next】,出现如下对话框,在该对话框中定义maven工程的坐标,如下:
这里写图片描述
最后点击【Finish】,taotao-order工程即可创建完毕。

搭建taotao-order-interface模块

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

搭建taotao-order-service模块

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

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

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

配置taotao-order-interface模块的pom.xml文件

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

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

我们可以参考taotao-content-service工程的依赖。由于订单服务要用到数据库,因此需要有taotao-manager-dao,把依赖的interface改为我们的taotao-order-interface,taotao-order-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-order</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>taotao-order-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-order-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> <!-- Redis客户端 --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </dependency> </dependencies></project>

框架整合

我们把taotao-content-service的src/main/resources目录下的mybatis、properties、spring三个目录粘贴到taotao-order-service的src/main/resources目录中。SqlMapConfig.xml文件不用动,如下图所示。
这里写图片描述
properties目录下的db.properties配置文件也不用修改,如下图所示。
这里写图片描述
properties目录下的resource.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/*.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" /> </bean></beans>

spring目录下的applicationContext-redis.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:annotation-config /> --> <!-- redis单机版 --> <bean id="jedisPool" class="redis.clients.jedis.JedisPool"> <constructor-arg name="host" value="192.168.25.128" /> <constructor-arg name="port" value="6379" /> </bean> <bean id="jedisClientPool" class="com.taotao.content.jedis.JedisClientPool"></bean> <!-- 集群版,注意:单机版和集群版不能共存!!! --> <!-- <bean id="jedisCluster" class="redis.clients.jedis.JedisCluster"> <constructor-arg name="nodes"> <set> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.25.128" /> <constructor-arg name="port" value="7001" /> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.25.128" /> <constructor-arg name="port" value="7002" /> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.25.128" /> <constructor-arg name="port" value="7003" /> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.25.128" /> <constructor-arg name="port" value="7004" /> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.25.128" /> <constructor-arg name="port" value="7005" /> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.25.128" /> <constructor-arg name="port" value="7006" /> </bean> </set> </constructor-arg> </bean> <bean id="jedisClientCluster" class="com.taotao.content.jedis.JedisClientCluster"></bean> --></beans>

既然要保留applicationContext-redis.xml配置文件,那么就需要把相关包和类拷贝过来,我们参考taotao-content-service工程,将taotao-content-service工程下的com.taotao.content.jedis包整个拷贝到taotao-order-service工程的src/main/java目录下,如下图所示。
这里写图片描述
接下来我们修改一下spring目录下的applicationContext-service.xml文件,如下图所示,在taotao-order-interface工程下新建扫描包”com.taotao.order.service”,在taotao-order-service工程下新建”com.taotao.order.service.impl”包,发布的dubbo服务名称修改为”taotao-order”,dubbo服务的端口修改为”20884”。
这里写图片描述
为了大家方便复制,现把该文件的内容黏贴出来。

<?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.order.service"></context:component-scan> <!-- 使用dubbo发布服务 --> <!-- 提供方应用信息,用于计算依赖关系 --> <dubbo:application name="taotao-order" /> <dubbo:registry protocol="zookeeper" address="192.168.25.128:2181" /> <!-- 用dubbo协议在20881端口暴露服务 --> <dubbo:protocol name="dubbo" port="20884" /> <!-- 声明需要暴露的服务接口 --> <!-- <dubbo:service interface="com.taotao.content.service.ContentCategoryService" ref="contentCategoryServiceImpl" timeout="300000" /> --></beans>

最后来看一下spring目录下的applicationContext-trans.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"> <!-- 事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 数据源 --> <property name="dataSource" ref="dataSource" /> </bean> <!-- 通知 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <!-- 传播行为 --> <tx:method name="save*" propagation="REQUIRED" /> <tx:method name="insert*" propagation="REQUIRED" /> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="create*" propagation="REQUIRED" /> <tx:method name="delete*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="find*" propagation="SUPPORTS" read-only="true" /> <tx:method name="select*" propagation="SUPPORTS" read-only="true" /> <tx:method name="get*" propagation="SUPPORTS" read-only="true" /> </tx:attributes> </tx:advice> <!-- 切面 --> <aop:config> <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.taotao.order.service.*.*(..))" /> </aop:config></beans>

下面我们把taotao-content-service工程下的WEB-INF及web.xml,粘贴到taotao-order-service的webapp目录下,修改web.xml文件中的<display-name>为”taotao-order”。
这里写图片描述
至此,我们的框架就整合好了!

搭建taotao-order-web表现层工程

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

<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-order-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-order-interface --> <dependency> <groupId>com.taotao</groupId> <artifactId>taotao-order-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>8091</port> <path>/</path> </configuration> </plugin> </plugins> </build></project>

紧接着来配置资源文件,我们亦可参考taotao-cart-web工程,将src/main/resources目录下的两个文件夹拷贝过来。先看resource目录下的resource.properties文件,该文件是用来配置常量的,目前我们还没有写业务代码,该文件暂时保持为空。
这里写图片描述
下面再看下spring目录下的springmvc.xml,修改要扫描的包和引用dubbo服务两项配置,要扫描的包”com.taotao.order.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.order.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-order-web"/> <dubbo:registry protocol="zookeeper" address="192.168.25.128:2181"/> <!-- <dubbo:reference interface="com.taotao.service.ItemService" id="itemService" /> --></beans>

最后来配置web.xml,我们依然可参考taotao-cart-web工程的web.xml,首先需要在webapp目录下新建一个WEB-INF目录,并拷贝taotao-cart-web工程的web.xml文件到WEB-INF目录下,我们需要修改的地方是名字,把原来所有的”taotao-cart-web”都更改为”taotao-order-web”(可以使用全文替换),如下图所示。
这里写图片描述
这样,我们的服务端和客户端工程便都搭建好了。

相关推荐

相关文章