要搭建项目,先要将项目拆分成N层,将服务(service)层独自放在A台服务器专门提供服务,zookeepek也可以单独放在一台服务器B,A服务器运行的服务要注册到B的zk注册中心,将界面层和逻辑处理放在同一层,该层只运行(springMvc+spring),该层也单独放在C台服务器。请求到C服务器,请求去B注册中心找服务。找到对应服务,返回结果到界面。

一个完整的项目可以简单拆分成:实体Po、servicedao、服务(service)、web呈现界面。简单点理解就是提供者和消费者的关系。提供者:服务(service),消费者:web呈现界面。
1、 建立实体Po Maven项目层。
该项目pom.xml不引用其他jar包,默认配置。
2、 建立servicedaoMaven项目层。

该项目要引用实体Po项目中的实体po,还要引用其他jar包,所以都在该项目的pom.xml中配置jar包的引用。
该项目的pom.xml文件配置
3、 建立提供者(service)MavenWeb项目

该项目要引用实体Po和servicedao和其他jar包,最主要的是各种配置文件要配置正确。
先是pom.xml的配置:
接着applicationContext.xml配置:
- <?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:aop="http://www.springframework.org/schema/aop"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xsi:schemaLocation="
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.1.xsd
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
- <!-- 开启注解 -->
- <context:annotation-config />
- <!-- 自动扫描(service),(dao) -->
- <context:component-scan base-package="end.gx.dao,end.gx.service">
- <context:include-filter type="annotation" expression="org.springframework.stereotype.Service" />
- <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository" />
- </context:component-scan>
- </beans>
再接着spring-mybatis.xml和
jdbc.properties
配置:
jdbc.properties:
- driverClassName=com.mysql.jdbc.Driver
- url=jdbc:mysql://192.168.3.16:3306/hotelmanagementsystem?useUnicode=true&characterEncoding=utf-8
- username=root
- password=123
- validationQuery=SELECT 1
spring-mybatis.xml:
再有spring-dubbo-provider.xml配置:
- <?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:dubbo="http://code.alibabatech.com/schema/dubbo"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd
- http://code.alibabatech.com/schema/dubbo
- http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
- <dubbo:application name="dubbo-service" />
- <!-- zookeeper所在服务器IP地址,本地的就是127.0.0.1 -->
- <dubbo:registry address="zookeeper://127.168.3.16:2181" />
- <dubbo:protocol name="dubbo" port="20880" />
- <!-- 可以有多个Service -->
- <dubbo:service interface="end.gx.service.UserService" ref="userService" />
- <!-- 可以有多个ServiceImpl -->
- <bean id="userService" class="end.gx.service.impl.UserServiceImpl" />
- </beans>
最后web.xml配置:
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
- id="WebApp_ID" version="2.5">
-
- <display-name>Archetype Created Web Application</display-name>
-
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>
- classpath:applicationContext.xml,
- classpath:spring-mybatis.xml,
- classpath:spring-dubbo-provider.xml
- </param-value>
- </context-param>
-
- <listener>
- <description>spring监听器</description>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- </web-app>
4、建立消费方(web) MavenWeb项目

该项目要引用实体Po和servicedao,还要引用其他jar包。还有各种配置文件要正确配置。
先是pom.xml配置: