首先我们还是先看一眼淘淘商城的系统架构,如下图所示,可以看到订单系统是一个单独的模块,分服务层和表现层,服务层负责存储订单,表现层负责展示订单。
下面我们便要开始搭建工程了,可以先搭建订单系统的服务层工程,例如taotao-order。
搭建taotao-order服务层工程
我们可参考taotao-sso工程的创建来搭建订单系统的服务层工程。这个工程是一个pom(聚合)工程,包含有两个子模块(taotao-order-interface和taotao-order-service)。
我们该如何来搭建该工程呢?很简单,按照这样的步骤即可。第一步,右键名为taotao
的Working Set,然后在下拉列表选中New
,接着再点击Other...
,如下图所示。
第二步,这时会弹出一个窗口,在该窗口的输入框中输入maven,并选择Maven Project
,然后再点击Next
按钮。
第三步,这时会弹出一个如下窗口,在该窗口中勾选Create a simple project
复选框,如果你不打上这个勾,那么它会让你选择一个骨架,但骨架里面是没有pom这个模板的。然后再点击Next
按钮。
第四步,出现如下窗口,在该窗口中定义maven工程的坐标,如下图所示。
第五步,点击Finish
按钮,taotao-order工程即可创建完毕。
taotao-order工程创建成功之后,我们接下来就要配置该工程的pom文件了,主要是添加对taotao-common工程的依赖。另外,在启动工程时,我们最好启动聚合工程,因此我们还需要在聚合工程中配置tomcat7插件,注意这儿我使用的是8090端口,如下所示。
<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>
<!-- 依赖taotao-common -->
<dependencies>
<dependency>
<groupId>com.taotao</groupId>
<artifactId>taotao-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 配置tomcat7插件 -->
<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模块
现在我们来搭建taotao-order-interface模块。你可以按照如下图所示的步骤来搭建该模块。
这时,会弹出如下窗口,在该窗口中勾选Create a simple project
前面的框框,并在Module Name一栏中输入模块的名称,例如taotao-order-interface,然后点击Next
按钮。
接着在弹出的窗口中选择该模块的打包方式,我们使用默认的jar即可,最后点击Finish
按钮即可成功创建taotao-order-interface模块。
taotao-order-interface模块创建成功之后,我们接下来就要配置该工程的pom文件了。由于我们的订单服务也有可能用到pojo,因此在该pom文件中主要添加对taotao-manager-pojo的依赖,如下所示。
<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-interface</artifactId>
<!-- 依赖taotao-manager-pojo -->
<dependencies>
<dependency>
<groupId>com.taotao</groupId>
<artifactId>taotao-manager-pojo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
搭建taotao-order-service模块
搭建taotao-order-service模块的步骤基本上同上,只是打包方式换成war了,如下图所示。
taotao-order-service模块创建成功之后,我们接下来就要配置该工程的pom文件了,主要是添加对taotao-manager-dao、taotao-order-interface、Spring以及Dubbo的依赖,如下所示。
<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>
<!-- 依赖taotao-manager-dao -->
<dependency>
<groupId>com.taotao</groupId>
<artifactId>taotao-manager-dao</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>
<!-- 与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>
<build>
<plugins>
<!-- 配置打包时跳过测试(一般打包的时候,会使用Maven install命令,如果你写了一些测试类,
它会执行这个测试类的,但是我们不希望它们执行,所以就需要有这样一个插件) -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
</project>
至于dao和pojo这两个模块,我们不必在taotao-order工程中再新建一遍了,因为我们在taotao-manager工程当中便创建好了,我们只需要引用这两个模块就可以了。
框架整合
我们把taotao-manager-service工程的src/main/resources目录下的mybatis、properties以及spring这三个目录粘贴复制到taotao-order-service工程的src/main/resources目录中,其中SqlMapConfig.xml文件不用动,如下图所示。
properties目录下的db.properties配置文件也不用修改,如下图所示。
properties目录下的resource.properties文件内容我们先清空,如下图所示。
接下来我们再看下spring目录中的文件,由于applicationContext-activemq.xml配置文件在订单服务中可能用不到,所以我们在这里将其删除掉。
然后再看一下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">
<!-- 加载db.properties配置文件 -->
<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文件,由于现在暂时还没想到订单服务用不用得到redis,所以我们在这里将其删除掉。
紧接着看一下spring目录中的applicationContext-service.xml文件。我们要对该文件进行修改,把包扫描器扫描的包修改为com.taotao.order.service
,而且还应将对外发布Dubbo服务的端口改为20884
,由于还没写服务接口,所以我们先把拷过来的暴露的服务接口注释掉(留个模板就行),此外还要将提供方应用信息名称改为taotao-order
。
为了大家方便复制,现把该文件的内容黏贴出来,如下所示。
<?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.81.131:2181" />
<!-- 用dubbo协议在20880端口(端口可以随便写,但默认是20880)暴露服务 -->
<dubbo:protocol name="dubbo" port="20884" />
<!-- 声明需要暴露的服务接口 -->
<!-- <dubbo:service interface="com.taotao.order.service.OrderService" ref="orderServiceImpl" timeout="300000" /> -->
</beans>
由于上面配置的要扫描的包还没有创建,因此我们还要在taotao-order-interface工程中新建一个com.taotao.order.service包,在taotao-order-service工程中新建一个com.taotao.order.service.impl包,如下图所示。
再接着看一下spring目录中的applicationContext-transaction.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">
<!-- 配置一个事务管理器 -->
<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-manager-service工程下的WEB-INF目录及web.xml文件粘贴到taotao-order-service工程的webapp目录下,并修改web.xml文件中<display-name>
标签里面的内容为taotao-order-service。
至此,我们的框架就算是整合好了!
搭建taotao-order-web服务层工程
taotao-order服务层工程创建好了之后,接下来咱们就要新建一个taotao-order-web表现层工程了,该工程可参考taotao-sso-web工程来搭建!
第一步,右键名为taotao
的Working Set,然后在下拉列表选中New
,接着再点击Other...
,如下图所示。
第二步,这时会弹出如下一个窗口,然后在该窗口的输入框中输入maven,并选择Maven Project
,接着点击Next
按钮。
第三步,在弹出的如下窗口中勾选上Create a simple project
复选框,如果你不打上这个勾,那么它会让你选择一个骨架,但骨架里面是没有pom这个模板的。然后再点击Next
按钮。
第四步,这时会出现如下窗口,我们要在该窗口中定义maven工程的坐标,如下图所示。
温馨提示:taotao-order-web工程的打包方式是war,且须依赖父工程。
第五步,点击Finish
按钮,taotao-order-web工程就创建好了,但是新建的web工程由于缺少web.xml文件而报错,解决这个错误最好的方法是直接利用Eclipse来帮我们创建该文件,而且我们只须按照下图所示的步骤进行操作即可。
taotao-order-web工程搭建好了之后,我们还要配置一下该工程的pom文件,而且我们可以参考taotao-sso-web工程的pom文件来配置,仅仅只需要稍作修改即可,所做的修改有两处,一是将依赖的interface修改为taotao-order-interface,二是将最下面的tomcat插件端口号配置为8091,修改之后的pom文件的内容如下所示。
<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-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>
<build>
<plugins>
<!-- 配置tomcat7插件 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<configuration>
<port>8091</port>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
</project>
接着我们来配置一下资源文件,配置时也可以参考taotao-sso-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/*.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.81.131:2181"/>
<!-- <dubbo:reference interface="com.taotao.sso.service.UserLoginService" id="userLoginService" /> -->
</beans>
最后来配置一下web.xml文件,我们依然可参考taotao-sso-web工程的web.xml文件来进行配置,且仅仅只需稍作修改即可。我们需要修改的地方是名字,即把原来所有的taotao-sso-web
都更改为taotao-order-web
(可以使用全文替换)。除此之外,配置只拦截以.html
结尾的请求,做一个伪静态化。
至此,taotao-order-web表现层工程便算是搭建完了。