要搭建项目,先要将项目拆分成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文件配置
- <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>
- <groupId>end.gx.servicedao</groupId>
- <artifactId>servicedao</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>jar</packaging>
- <name>servicedao</name>
- <url>http://maven.apache.org</url>
- <properties>
- <!-- spring版本号 -->
- <spring.version>3.2.4.RELEASE</spring.version>
- <!-- mybatis版本号 -->
- <mybatis.version>3.2.4</mybatis.version>
- <!-- log4j日志文件管理包版本 -->
- <slf4j.version>1.6.6</slf4j.version>
- <log4j.version>1.2.17</log4j.version>
- <fastjson.version>1.1.33</fastjson.version>
- <commons-codec.version>1.6</commons-codec.version>
- <commons-lang.version>2.5</commons-lang.version>
- <zookeeper_version>3.3.3</zookeeper_version>
- <zkclient_version>0.1</zkclient_version>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- </properties>
- <dependencies>
- <!-- 引入实体po -->
- <dependency>
- <groupId>end.gx.entity</groupId>
- <artifactId>po</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- </dependency>
- <!-- spring框架 -->
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-core</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-web</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-oxm</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-tx</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-webmvc</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-aop</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-jdbc</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context-support</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-test</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-aspects</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <!-- springframe end -->
- <!-- mybatis核心包 -->
- <dependency>
- <groupId>org.mybatis</groupId>
- <artifactId>mybatis</artifactId>
- <version>${mybatis.version}</version>
- </dependency>
- <!-- mybatis/spring包 -->
- <dependency>
- <groupId>org.mybatis</groupId>
- <artifactId>mybatis-spring</artifactId>
- <version>1.2.2</version>
- </dependency>
- <!-- mysql驱动包 -->
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>5.1.22</version>
- </dependency>
- <!-- junit测试包 -->
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.11</version>
- <scope>test</scope>
- </dependency>
- <!-- 阿里巴巴数据源包 -->
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>druid</artifactId>
- <version>1.0.2</version>
- </dependency>
- <!-- json数据 -->
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>fastjson</artifactId>
- <version>${fastjson.version}</version>
- </dependency>
- <dependency>
- <groupId>org.codehaus.jackson</groupId>
- <artifactId>jackson-mapper-asl</artifactId>
- <version>1.9.13</version>
- </dependency>
- <dependency>
- <groupId>com.google.code.gson</groupId>
- <artifactId>gson</artifactId>
- <version>2.8.0</version>
- </dependency>
- <!--json-lib -->
- <dependency>
- <groupId>net.sf.json-lib</groupId>
- <artifactId>json-lib</artifactId>
- <version>2.4</version>
- <classifier>jdk15</classifier>
- </dependency>
- <!-- 日常使用 -->
- <dependency>
- <groupId>commons-fileupload</groupId>
- <artifactId>commons-fileupload</artifactId>
- <version>1.3.1</version>
- </dependency>
- <dependency>
- <groupId>commons-lang</groupId>
- <artifactId>commons-lang</artifactId>
- <version>${commons-lang.version}</version>
- </dependency>
- <dependency>
- <groupId>org.apache.commons</groupId>
- <artifactId>commons-lang3</artifactId>
- <version>3.4</version>
- </dependency>
- <dependency>
- <groupId>commons-codec</groupId>
- <artifactId>commons-codec</artifactId>
- <version>${commons-codec.version}</version>
- </dependency>
- <!-- 解决tomcat没有此包bug -->
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>jstl</artifactId>
- <version>1.2</version>
- <scope>runtime</scope>
- </dependency>
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>servlet-api</artifactId>
- <version>2.5</version>
- <scope>provided</scope>
- </dependency>
- <dependency>
- <groupId>javax.servlet.jsp</groupId>
- <artifactId>jsp-api</artifactId>
- <version>2.0</version>
- <scope>provided</scope>
- </dependency>
- <!-- 日志文件管理包 -->
- <!-- log start -->
- <dependency>
- <groupId>log4j</groupId>
- <artifactId>log4j</artifactId>
- <version>${log4j.version}</version>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-api</artifactId>
- <version>${slf4j.version}</version>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-log4j12</artifactId>
- <version>${slf4j.version}</version>
- </dependency>
- <!-- log end -->
- <!-- freemarker -->
- <dependency>
- <groupId>org.freemarker</groupId>
- <artifactId>freemarker</artifactId>
- <version>2.3.22</version>
- </dependency>
- <!-- 分页工具 -->
- <dependency>
- <groupId>jsptags</groupId>
- <artifactId>pager-taglib</artifactId>
- <version>2.0</version>
- </dependency>
- <!-- 邮件 -->
- <dependency>
- <groupId>javax.mail</groupId>
- <artifactId>mail</artifactId>
- <version>1.4</version>
- </dependency>
- <!-- 使用缓存解决JSSDK -->
- <dependency>
- <groupId>net.sf.ehcache</groupId>
- <artifactId>ehcache</artifactId>
- <version>2.9.1</version>
- </dependency>
- </dependencies>
- </project>
3、 建立提供者(service)MavenWeb项目
该项目要引用实体Po和servicedao和其他jar包,最主要的是各种配置文件要配置正确。
先是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/maven-v4_0_0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>end.gx.service</groupId>
- <artifactId>service</artifactId>
- <packaging>war</packaging>
- <version>0.0.1-SNAPSHOT</version>
- <name>service Maven Webapp</name>
- <url>http://maven.apache.org</url>
- <properties>
- <!-- spring版本号 -->
- <spring.version>3.2.4.RELEASE</spring.version>
- <!-- mybatis版本号 -->
- <mybatis.version>3.2.4</mybatis.version>
- <!-- log4j日志文件管理包版本 -->
- <slf4j.version>1.6.6</slf4j.version>
- <log4j.version>1.2.17</log4j.version>
- <fastjson.version>1.1.33</fastjson.version>
- <commons-codec.version>1.6</commons-codec.version>
- <commons-lang.version>2.5</commons-lang.version>
- <zookeeper_version>3.3.3</zookeeper_version>
- <zkclient_version>0.1</zkclient_version>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- </properties>
- <dependencies>
- <!-- po -->
- <dependency>
- <groupId>end.gx.entity</groupId>
- <artifactId>po</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- </dependency>
- <!-- servicedao -->
- <dependency>
- <groupId>end.gx.servicedao</groupId>
- <artifactId>servicedao</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- </dependency>
- <!-- spring框架 -->
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-core</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-web</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-oxm</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-tx</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-webmvc</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-aop</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-jdbc</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context-support</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-test</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-aspects</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <!-- springframe end -->
- <!-- dubbo -->
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>dubbo</artifactId>
- <version>2.5.3</version>
- <exclusions>
- <exclusion>
- <groupId>org.springframework</groupId>
- <artifactId>spring</artifactId>
- </exclusion>
- </exclusions>
- </dependency>
- <!--zookeeper -->
- <dependency>
- <groupId>org.apache.zookeeper</groupId>
- <artifactId>zookeeper</artifactId>
- <version>3.3.3</version>
- </dependency>
- <dependency>
- <groupId>com.github.sgroschupf</groupId>
- <artifactId>zkclient</artifactId>
- <version>0.1</version>
- </dependency>
- <dependency>
- <groupId>org.codehaus.xfire</groupId>
- <artifactId>xfire-all</artifactId>
- <version>1.2.6</version>
- <exclusions>
- <exclusion>
- <groupId>org.springframework</groupId>
- <artifactId>spring</artifactId>
- </exclusion>
- </exclusions>
- </dependency>
- <!-- mybatis核心包 -->
- <dependency>
- <groupId>org.mybatis</groupId>
- <artifactId>mybatis</artifactId>
- <version>${mybatis.version}</version>
- </dependency>
- <!-- mybatis/spring包 -->
- <dependency>
- <groupId>org.mybatis</groupId>
- <artifactId>mybatis-spring</artifactId>
- <version>1.2.2</version>
- </dependency>
- <!-- mysql驱动包 -->
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>5.1.22</version>
- </dependency>
- <!-- junit测试包 -->
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.11</version>
- <scope>test</scope>
- </dependency>
- <!-- 阿里巴巴数据源包 -->
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>druid</artifactId>
- <version>1.0.2</version>
- </dependency>
- <!-- json数据 -->
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>fastjson</artifactId>
- <version>${fastjson.version}</version>
- </dependency>
- <dependency>
- <groupId>org.codehaus.jackson</groupId>
- <artifactId>jackson-mapper-asl</artifactId>
- <version>1.9.13</version>
- </dependency>
- <dependency>
- <groupId>com.google.code.gson</groupId>
- <artifactId>gson</artifactId>
- <version>2.8.0</version>
- </dependency>
- <!--json-lib -->
- <dependency>
- <groupId>net.sf.json-lib</groupId>
- <artifactId>json-lib</artifactId>
- <version>2.4</version>
- <classifier>jdk15</classifier>
- </dependency>
- <!-- 日常使用 -->
- <dependency>
- <groupId>commons-fileupload</groupId>
- <artifactId>commons-fileupload</artifactId>
- <version>1.3.1</version>
- </dependency>
- <dependency>
- <groupId>commons-lang</groupId>
- <artifactId>commons-lang</artifactId>
- <version>${commons-lang.version}</version>
- </dependency>
- <dependency>
- <groupId>org.apache.commons</groupId>
- <artifactId>commons-lang3</artifactId>
- <version>3.4</version>
- </dependency>
- <dependency>
- <groupId>commons-codec</groupId>
- <artifactId>commons-codec</artifactId>
- <version>${commons-codec.version}</version>
- </dependency>
- <!-- 解决tomcat没有此包bug -->
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>jstl</artifactId>
- <version>1.2</version>
- <scope>runtime</scope>
- </dependency>
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>servlet-api</artifactId>
- <version>2.5</version>
- <scope>provided</scope>
- </dependency>
- <dependency>
- <groupId>javax.servlet.jsp</groupId>
- <artifactId>jsp-api</artifactId>
- <version>2.0</version>
- <scope>provided</scope>
- </dependency>
- <!-- 日志文件管理包 -->
- <!-- log start -->
- <dependency>
- <groupId>log4j</groupId>
- <artifactId>log4j</artifactId>
- <version>${log4j.version}</version>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-api</artifactId>
- <version>${slf4j.version}</version>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-log4j12</artifactId>
- <version>${slf4j.version}</version>
- </dependency>
- <!-- log end -->
- <!-- freemarker -->
- <dependency>
- <groupId>org.freemarker</groupId>
- <artifactId>freemarker</artifactId>
- <version>2.3.22</version>
- </dependency>
- <!-- 分页工具 -->
- <dependency>
- <groupId>jsptags</groupId>
- <artifactId>pager-taglib</artifactId>
- <version>2.0</version>
- </dependency>
- <!-- 邮件 -->
- <dependency>
- <groupId>javax.mail</groupId>
- <artifactId>mail</artifactId>
- <version>1.4</version>
- </dependency>
- <!-- 使用缓存解决JSSDK -->
- <dependency>
- <groupId>net.sf.ehcache</groupId>
- <artifactId>ehcache</artifactId>
- <version>2.9.1</version>
- </dependency>
- </dependencies>
- <build>
- <finalName>service</finalName>
- </build>
- </project>
接着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>
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:
- <?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">
- <!-- properties配置文件 -->
- <bean id="dbProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <!-- 是否忽略不可解析的 -->
- <property name="ignoreUnresolvablePlaceholders" value="true" />
- <property name="locations">
- <list>
- <value>classpath:jdbc.properties</value>
- </list>
- </property>
- </bean>
- <!-- ========================================配置数据源========================================= -->
- <!-- 配置数据源,使用的是alibaba的Druid(德鲁伊)数据源 -->
- <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
- <property name="url" value="${url}" />
- <property name="username" value="${username}" />
- <property name="password" value="${password}" />
- <!-- 初始化连接大小 -->
- <property name="initialSize" value="0" />
- <!-- 连接池最大使用连接数量 -->
- <property name="maxActive" value="20" />
- <!-- 连接池最大空闲 -->
- <property name="maxIdle" value="20" />
- <!-- 连接池最小空闲 -->
- <property name="minIdle" value="0" />
- <!-- 获取连接最大等待时间 -->
- <property name="maxWait" value="60000" />
- <property name="validationQuery" value="${validationQuery}" />
- <property name="testOnBorrow" value="false" />
- <property name="testOnReturn" value="false" />
- <property name="testWhileIdle" value="true" />
- <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
- <property name="timeBetweenEvictionRunsMillis" value="60000" />
- <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
- <property name="minEvictableIdleTimeMillis" value="25200000" />
- <!-- 打开removeAbandoned功能 -->
- <property name="removeAbandoned" value="true" />
- <!-- 1800秒,也就是30分钟 -->
- <property name="removeAbandonedTimeout" value="1800" />
- <!-- 关闭abanded连接时输出错误日志 -->
- <property name="logAbandoned" value="true" />
- <!-- 监控数据库 -->
- <!-- <property name="filters" value="stat" /> -->
- <property name="filters" value="mergeStat" />
- </bean>
- <!-- ========================================针对myBatis的配置项============================== -->
- <!-- 配置sqlSessionFactory -->
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <!-- 实例化sqlSessionFactory时需要使用上述配置好的数据源以及SQL映射文件 -->
- <property name="dataSource" ref="dataSource" />
- <property name="configLocation" value="classpath:mybatis.cofig.xml"/>
- <!-- 自动扫描Mybatis的Mapper.xml文件 -->
- <property name="mapperLocations" value="classpath:end/gx/mapping/*.xml" />
- </bean>
- <!-- 配置扫描器 -->
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <!-- 扫描com.fangjian.dao这个包以及它的子包下的所有映射接口类 -->
- <property name="basePackage" value="end.gx.dao" />
- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
- </bean>
- <!-- 配置事务管理器 -->
- <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="dataSource" />
- </bean>
- <!-- 拦截器方式配置事物 -->
- <tx:advice id="transactionAdvice" transaction-manager="transactionManager">
- <tx:attributes>
- <tx:method name="add*" propagation="REQUIRED" />
- <tx:method name="append*" propagation="REQUIRED" />
- <tx:method name="insert*" propagation="REQUIRED" />
- <tx:method name="save*" propagation="REQUIRED" />
- <tx:method name="update*" propagation="REQUIRED" />
- <tx:method name="modify*" propagation="REQUIRED" />
- <tx:method name="edit*" propagation="REQUIRED" />
- <tx:method name="delete*" propagation="REQUIRED" />
- <tx:method name="remove*" propagation="REQUIRED" />
- <tx:method name="repair" propagation="REQUIRED" />
- <tx:method name="delAndRepair" propagation="REQUIRED" />
- <tx:method name="get*" propagation="SUPPORTS" />
- <tx:method name="find*" propagation="SUPPORTS" />
- <tx:method name="load*" propagation="SUPPORTS" />
- <tx:method name="search*" propagation="SUPPORTS" />
- <tx:method name="datagrid*" propagation="SUPPORTS" />
- <tx:method name="*" propagation="SUPPORTS" />
- </tx:attributes>
- </tx:advice>
- <aop:config>
- <aop:pointcut id="transactionPointcut" expression="execution(* com.gx.service_dao.service..*Impl.*(..))" />
- <aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" />
- </aop:config>
- <!-- 配置druid监控spring jdbc -->
- <bean id="druid-stat-interceptor" class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor"></bean>
- <bean id="druid-stat-pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut" scope="prototype">
- <property name="patterns">
- <list>
- <value>com.gx.service_dao.service.*</value>
- </list>
- </property>
- </bean>
- <aop:config>
- <aop:advisor advice-ref="druid-stat-interceptor" pointcut-ref="druid-stat-pointcut" />
- </aop:config>
- </beans>
再有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配置:
- <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/maven-v4_0_0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>end.gx.service</groupId>
- <artifactId>serviceweb</artifactId>
- <packaging>war</packaging>
- <version>0.0.1-SNAPSHOT</version>
- <name>serviceweb Maven Webapp</name>
- <url>http://maven.apache.org</url>
- <properties>
- <!-- spring版本号 -->
- <spring.version>3.2.4.RELEASE</spring.version>
- <!-- mybatis版本号 -->
- <mybatis.version>3.2.4</mybatis.version>
- <!-- log4j日志文件管理包版本 -->
- <slf4j.version>1.6.6</slf4j.version>
- <log4j.version>1.2.17</log4j.version>
- <fastjson.version>1.1.33</fastjson.version>
- <commons-codec.version>1.6</commons-codec.version>
- <commons-lang.version>2.5</commons-lang.version>
- <zookeeper_version>3.3.3</zookeeper_version>
- <zkclient_version>0.1</zkclient_version>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- </properties>
- <dependencies>
- <!-- 引用servicedao -->
- <dependency>
- <groupId>end.gx.servicedao</groupId>
- <artifactId>servicedao</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- </dependency>
- <!-- 引用实体po -->
- <dependency>
- <groupId>end.gx.entity</groupId>
- <artifactId>po</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- </dependency>
- <!-- spring框架 -->
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-core</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-web</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-oxm</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-tx</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-webmvc</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-aop</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-jdbc</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context-support</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-test</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-aspects</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <!-- springframe end -->
- <!-- dubbo -->
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>dubbo</artifactId>
- <version>2.5.3</version>
- <exclusions>
- <exclusion>
- <groupId>org.springframework</groupId>
- <artifactId>spring</artifactId>
- </exclusion>
- </exclusions>
- </dependency>
- <!--zookeeper -->
- <dependency>
- <groupId>org.apache.zookeeper</groupId>
- <artifactId>zookeeper</artifactId>
- <version>3.3.3</version>
- </dependency>
- <dependency>
- <groupId>com.github.sgroschupf</groupId>
- <artifactId>zkclient</artifactId>
- <version>0.1</version>
- </dependency>
- <!-- mybatis核心包 -->
- <dependency>
- <groupId>org.mybatis</groupId>
- <artifactId>mybatis</artifactId>
- <version>${mybatis.version}</version>
- </dependency>
- <!-- mybatis/spring包 -->
- <dependency>
- <groupId>org.mybatis</groupId>
- <artifactId>mybatis-spring</artifactId>
- <version>1.2.2</version>
- </dependency>
- <!-- mysql驱动包 -->
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>5.1.22</version>
- </dependency>
- <!-- junit测试包 -->
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.11</version>
- <scope>test</scope>
- </dependency>
- <!-- 阿里巴巴数据源包 -->
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>druid</artifactId>
- <version>1.0.2</version>
- </dependency>
- <!-- json数据 -->
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>fastjson</artifactId>
- <version>${fastjson.version}</version>
- </dependency>
- <dependency>
- <groupId>org.codehaus.jackson</groupId>
- <artifactId>jackson-mapper-asl</artifactId>
- <version>1.9.13</version>
- </dependency>
- <dependency>
- <groupId>com.google.code.gson</groupId>
- <artifactId>gson</artifactId>
- <version>2.8.0</version>
- </dependency>
- <!--json-lib -->
- <dependency>
- <groupId>net.sf.json-lib</groupId>
- <artifactId>json-lib</artifactId>
- <version>2.4</version>
- <classifier>jdk15</classifier>
- </dependency>
- <!-- 日常使用 -->
- <dependency>
- <groupId>commons-fileupload</groupId>
- <artifactId>commons-fileupload</artifactId>
- <version>1.3.1</version>
- </dependency>
- <dependency>
- <groupId>commons-lang</groupId>
- <artifactId>commons-lang</artifactId>
- <version>${commons-lang.version}</version>
- </dependency>
- <dependency>
- <groupId>org.apache.commons</groupId>
- <artifactId>commons-lang3</artifactId>
- <version>3.4</version>
- </dependency>
- <dependency>
- <groupId>commons-codec</groupId>
- <artifactId>commons-codec</artifactId>
- <version>${commons-codec.version}</version>
- </dependency>
- <!-- 解决tomcat没有此包bug -->
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>jstl</artifactId>
- <version>1.2</version>
- <scope>runtime</scope>
- </dependency>
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>servlet-api</artifactId>
- <version>2.5</version>
- <scope>provided</scope>
- </dependency>
- <dependency>
- <groupId>javax.servlet.jsp</groupId>
- <artifactId>jsp-api</artifactId>
- <version>2.0</version>
- <scope>provided</scope>
- </dependency>
- <!-- 日志文件管理包 -->
- <!-- log start -->
- <dependency>
- <groupId>log4j</groupId>
- <artifactId>log4j</artifactId>
- <version>${log4j.version}</version>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-api</artifactId>
- <version>${slf4j.version}</version>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-log4j12</artifactId>
- <version>${slf4j.version}</version>
- </dependency>
- <!-- log end -->
- <!-- freemarker -->
- <dependency>
- <groupId>org.freemarker</groupId>
- <artifactId>freemarker</artifactId>
- <version>2.3.22</version>
- </dependency>
- <!-- 分页工具 -->
- <dependency>
- <groupId>jsptags</groupId>
- <artifactId>pager-taglib</artifactId>
- <version>2.0</version>
- </dependency>
- <!-- 邮件 -->
- <dependency>
- <groupId>javax.mail</groupId>
- <artifactId>mail</artifactId>
- <version>1.4</version>
- </dependency>
- <!-- 使用缓存解决JSSDK -->
- <dependency>
- <groupId>net.sf.ehcache</groupId>
- <artifactId>ehcache</artifactId>
- <version>2.9.1</version>
- </dependency>
- </dependencies>
- <build>
- <finalName>serviceweb</finalName>
- </build>
- </project>
接着spring-mvc.xml配置:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- 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"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-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/mvc
- http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
- <!-- 自动扫描controller包下的所有类,使其认为spring mvc的控制器 -->
- <context:component-scan base-package="edu.gx.web">
- <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
- </context:component-scan>
- <!-- 日期转换 必须放在<mvc:annotation-driven />前面 -->
- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean>
- <mvc:annotation-driven />
- <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
- <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
- <property name="prefix" value="/WEB-INF/jsp"></property>
- <property name="suffix" value=".jsp"></property>
- </bean>
- </beans>
再有spring-dubbo-customer.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-client" />
- <!-- zookeeper安装所在服务器IP地址,本机是127.0.0.1 -->
- <dubbo:registry address="zookeeper://192.168.3.16:2181" />
- <!-- 可以有多个Service -->
- <dubbo:reference id="userService" interface="end.gx.service.UserService" check="false" timeout="10000" retries="0"/>
- </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">
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>
- classpath:spring-dubbo-customer.xml
- </param-value>
- </context-param>
- <listener>
- <description>spring监听器</description>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <filter>
- <filter-name>characterEncodingFilter</filter-name>
- <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
- <init-param>
- <param-name>encoding</param-name>
- <param-value>UTF-8</param-value>
- </init-param>
- <init-param>
- <param-name>forceEncoding</param-name>
- <param-value>true</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>characterEncodingFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <servlet>
- <description>spring mvc servlet</description>
- <servlet-name>springMvc</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <init-param>
- <description>spring mvc 配置文件</description>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:spring-mvc.xml</param-value>
- </init-param>
- <init-param>
- <param-name>activeReverseAjaxEnabled</param-name>
- <param-value>true</param-value>
- </init-param>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>springMvc</servlet-name>
- <url-pattern>*.do</url-pattern>
- </servlet-mapping>
- </web-app>
简单的分别式架构搭建完成,接下来就是将提供者(service)运行成功,接着将service打包成war包,放在tomcat下运行。在运行service的前提是zookeeper要运行。这样service服务才能正常注册到zk服务中心。
接着在Eclipse运行消费方(web),当界面能把数据查询出来,证明分布式架构才是完全搭建成功。