要搭建項目,先要將項目拆分成N層,將服務(service)層獨自放在A臺服務器專門提供服務,zookeepek也可以單獨放在一臺服務器B,A服務器運行的服務要註冊到B的zk註冊中心,將界面層和邏輯處理放在同一層,該層只運行(springMvc+spring),該層也單獨放在C臺服務器。請求到C服務器,請求去B註冊中心找服務。找到對應服務,返回結果到界面。
![](http://static.javashuo.com/static/loading.gif)
一個完整的項目可以簡單拆分成:實體Po、servicedao、服務(service)、web呈現界面。簡單點理解就是提供者和消費者的關係。提供者:服務(service),消費者:web呈現界面。
1、 建立實體Po Maven項目層。
該項目pom.xml不引用其他jar包,默認配置。
2、 建立servicedaoMaven項目層。
![](http://static.javashuo.com/static/loading.gif)
該項目要引用實體Po項目中的實體po,還要引用其他jar包,所以都在該項目的pom.xml中配置jar包的引用。
該項目的pom.xml文件配置
3、 建立提供者(service)MavenWeb項目
![](http://static.javashuo.com/static/loading.gif)
該項目要引用實體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項目
![](http://static.javashuo.com/static/loading.gif)
該項目要引用實體Po和servicedao,還要引用其他jar包。還有各種配置文件要正確配置。
先是pom.xml配置: