手把手教你搭建分布式项目环境(MyEclipse)
搭建基于Duboo、ZooKeeper、RPC的分布式SpringMVC项目背景项目环境项目结构搭建思路整体展示知识补充: 如何创建聚合项目的子模块 ?搭建步骤创建父项目 ah-szxy (pom)创建其聚合子模块 ah-szxy-common (jar)创建其聚合子模块 ah-szxy-system (pom)创建 ah-szxy-system的聚合子模块 ah-szxy-system-p...
搭建基于Duboo、ZooKeeper、RPC的分布式SpringMVC项目
背景
根据电商项目模拟练习后 ,根据个人总结和经验重新进行了分布式项目的框架搭建 , 现总结过程步骤 ,为以后进行相关类型的开发做好基础~~~
项目环境
- 安装虚拟机
- 安装 Zookeeper集群( 搭建详情进本人其他博客 )
- 安装 Maven项目开发工具
- msql 或oracle数据库支持(本项目选择的是mysql)
项目结构
搭建思路
整体展示
注意:
可以先见这些项目都建好(如上图) ,然后逐一为其添加相关坐标依赖 ,以及相关的配置文件 .
然后根据配置文件创建相应的包名 ,这样就不会导致扫描不到相关的包
知识补充: 如何创建聚合项目的子模块 ?
点击需要聚合其他模块的项目 ,选中状态下 ctrl+n ,选中Maven Module
勾选创建简单项目 ,自定义项目名
选则你需要的Maven项目类型即可
搭建步骤
创建父项目 ah-szxy (pom)
添加该项目所用到的 jar包坐标
<!-- 集中定义依赖版本号 -->
<properties>
<junit.version>4.12</junit.version>
<spring.version>4.1.3.RELEASE</spring.version>
<mybatis.version>3.2.8</mybatis.version>
<mybatis.spring.version>1.2.2</mybatis.spring.version>
<mybatis.paginator.version>1.2.15</mybatis.paginator.version>
<mysql.version>5.1.32</mysql.version>
<slf4j.version>1.6.4</slf4j.version>
<jackson.version>2.8.2</jackson.version>
<druid.version>1.0.9</druid.version>
<jstl.version>1.2</jstl.version>
<servlet-api.version>2.5</servlet-api.version>
<jsp-api.version>2.0</jsp-api.version>
<joda-time.version>2.5</joda-time.version>
<commons-lang3.version>3.3.2</commons-lang3.version>
<commons-io.version>1.3.2</commons-io.version>
<commons-net.version>3.3</commons-net.version>
<pagehelper.version>4.0.3</pagehelper.version>
<jsqlparser.version>0.9.1</jsqlparser.version>
<commons-fileupload.version>1.3.1</commons-fileupload.version>
<jedis.version>2.7.2</jedis.version>
<solrj.version>4.10.3</solrj.version>
<dubbo.version>2.5.3</dubbo.version>
<zkclient.version>0.10</zkclient.version>
<httpcomponents.version>4.4.1</httpcomponents.version>
<noggit.version>0.6</noggit.version>
<zookeeper.version>3.4.6</zookeeper.version>
<log4j.version>1.2.17</log4j.version>
<aspectj.version>1.8.2</aspectj.version>
<aopalliance.version>1.0</aopalliance.version>
</properties>
<!-- 只定义依赖的版本,并不实际依赖 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>${aopalliance.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>${zookeeper.version}</version>
</dependency>
<!-- 时间操作组件 -->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>${joda-time.version}</version>
</dependency>
<!-- Apache工具组件 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>${commons-io.version}</version>
</dependency>
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>${commons-net.version}</version>
</dependency>
<!-- Jackson Json处理工具包 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<!-- dubbo:进行服务的输入和输出 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<exclusions>
<!-- 排除或者不适用dubbo中指定的spring版本 -->
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
</exclusions>
<version>${dubbo.version}</version>
</dependency>
<!-- 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!-- 日志处理 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<!-- Mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>${commons-fileupload.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis.spring.version}</version>
</dependency>
<!-- mybatis的分页插件 -->
<dependency>
<groupId>com.github.miemiedev</groupId>
<artifactId>mybatis-paginator</artifactId>
<version>${mybatis.paginator.version}</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>${pagehelper.version}</version>
</dependency>
<!-- MySql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!-- 连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</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-jdbc</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-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- JSP相关 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>${servlet-api.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>${jsp-api.version}</version>
<scope>provided</scope>
</dependency>
<!-- Redis客户端 -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>${jedis.version}</version>
</dependency>
<!-- solr客户端 -->
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>${solrj.version}</version>
</dependency>
<!-- zookeeper客户端 -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>${zkclient.version}</version>
</dependency>
<dependency>
<groupId>org.noggit</groupId>
<artifactId>noggit</artifactId>
<version>${noggit.version}</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>${httpcomponents.version}</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>${httpcomponents.version}</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>${httpcomponents.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<!-- 资源文件拷贝插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<!-- 资源打包插件 -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5</version>
</plugin>
<!-- java编译插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
<configuration>
<forkMode>once</forkMode>
<!-- 指定测试的时候的字符集编码 -->
<argLine>-Dfile.encoding=UTF-8</argLine>
<systemProperties>
<property>
<name>net.sourceforge.cobertura.datafile</name>
<value>target/cobertura/cobertura.ser</value>
</property>
</systemProperties>
</configuration>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<!-- 配置Tomcat插件 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
创建其聚合子模块 ah-szxy-common (jar)
存放相关的工具类 ,坐标需根据需要添加
相关坐标
<dependencies>
<!-- 为了支持FTPClient通信,以及文件上传与下载 -->
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
</dependency>
<!-- 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<!-- 时间操作组件 -->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
</dependency>
<!-- Jackson Json处理工具包 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
创建其聚合子模块 ah-szxy-system (pom)
用来聚合项目 ,无需添加jar包坐标
创建 ah-szxy-system的聚合子模块 ah-szxy-system-pojo (jar)
存放实体类 ,无需添加jar包坐标 , 但是请注意要实体类要实现序列化接口
创建 ah-szxy-system的聚合子模块 ah-szxy-system-mapper (jar)
存放dao层接口以及 Mapper文件 , 但是请注意二者所在位置
依赖 mybatis的查询 , 依赖实体类实体 , commo的公共方法 .请根据自己的命名修改第二个第三个依赖
<dependencies>
<!-- 依赖实体类实体\commo的公共方法\mybatis的查询 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</dependency>
<dependency>
<groupId>ah.szxy</groupId>
<artifactId>ah-szxy-system-pojo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>ah.szxy</groupId>
<artifactId>ah-szxy-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<!-- //发布该目录下的文件 -->
<includes>
<include>**/*.xml</include>
<!-- //发布所有以xml结尾的文件 -->
</includes>
<filtering>false</filtering>
</resource>
</resources>
创建 ah-szxy-system的聚合子模块 ah-szxy-system-service (jar)
需要添加依赖pojo,common,只负责定义业务层方法
<!-- 依赖pojo,common,只负责定义业务层方法 -->
<dependencies>
<dependency>
<groupId>ah.szxy</groupId>
<artifactId>ah-szxy-system-pojo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>ah.szxy</groupId>
<artifactId>ah-szxy-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
创建 ah-szxy-system的聚合子模块 ah-szxy-system-service-impl (jar)
由于该项目配置过多 ,现将该项目资源分享至百度云 .如果觉得迷糊直接根据所在位置复制即可1
项目结构
- 依赖dao层与业务层,mapper进行查询 service进行服务的发布.消费等
<dependencies>
<!-- 依赖dao层与业务层,mapper进行查询 service进行服务的发布.消费等 -->
<dependency>
<groupId>ah.szxy</groupId>
<artifactId>ah-szxy-system-mapper</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>ah.szxy</groupId>
<artifactId>ah-szxy-system-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- 提供servlet的相关的支持 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- 连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
<!-- mybatis和spring的整合包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
</dependency>
<!-- 支持mybatis的日志记录 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</dependency>
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
</dependency>
<!-- 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<!-- dubbo依赖 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<!-- zookeeper客户端依赖 -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
</dependency>
<!-- mybatis分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
</dependency>
<!-- Redis客户端 -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<!-- Apache的通用工具 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<!-- solr客户端 -->
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 指定项目的打包插件信息 -->
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<!-- 指定打包描述文件的位置:相对项目根目录的路径 -->
<!-- assembly打包的描述文件 -->
<descriptor>assembly/assembly.xml</descriptor>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<!--用于资源拷贝 -->
<resource>
<directory>src/main/resources/spring</directory>
<includes>
<include>**/*.xml</include>
</includes>
<!-- dubbo加载spring的配置文件的时候,是从META-INF/spring开始查找 -->
<targetPath>META-INF/spring/</targetPath>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
<!--<targetPath></targetPath> -->
</resource>
</resources>
- mtbatis配置文件,使用分页插件使用 ,一般可以没有
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- <settings>
指定mybatis的日志记录方式,需要添加log4j的依赖
需要在src/main/resources下添加log4j.properties
<setting name="logImpl" value="LOG4J" />
</settings>
-->
<typeAliases>
<!-- 将指定包下的所有的实体bean的别名取为类名首字母小写 -->
<!-- <package name="ah.szxy.system.pojo" /> -->
</typeAliases>
<!-- 配置分页插件 -->
<!-- <plugins>
<plugin interceptor="com.github.pagehelper.PageHelper">
设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库
<property name="dialect" value="mysql" />
</plugin>
</plugins>-->
</configuration>
- 指定数据库连接参数
jdbc.driver=com.mysql.jdbc.Driver
#jdbc.url=jdbc:mysql://192.168.179.128:3306/ego?useSSL=false
jdbc.url=jdbc:mysql://localhost:3306/exam
jdbc.user=root
jdbc.password=root
druid.maxActive=20
- dao层配置文件 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:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 加载数据库连接文件 -->
<context:property-placeholder location="classpath:resources/*.properties"/>
<!-- 获取数据源 德鲁伊 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
destroy-method="close">
<!-- driverClass -->
<property name="driverClassName" value="${jdbc.driver}"></property>
<!-- jdbc的url -->
<property name="url" value="${jdbc.url}"></property>
<!-- jdbc的用户名 -->
<property name="username" value="${jdbc.user}"></property>
<!-- jdbc的密码 -->
<property name="password" value="${jdbc.password}"></property>
<!-- 数据库连接池的最大连接数 -->
<property name="maxActive" value="${druid.maxActive}"></property>
</bean>
<!-- sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 指定数据源 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 指定mybatis配置文件的位置 -->
<property name="configLocation" value="classpath:mybatis/mybatisConfig.xml"></property>
</bean>
<!--
// 指定mybatis的配置文件
String resource = "mybatisConfig.xml";
// 获取mybatis配置文件的输入流
InputStream is = Resources.getResourceAsInputStream(resource);
// 获取SqlSessionFactory
SqlSessionFactory factory = new SqlSessionFactoryBuilder.build(is);
// 获取SqlSession
SqlSession session = factory.openSession();
TbItemMapper mapper = session.getMapper(TbItemMapper.class);
-->
<!-- MapperScannerConfigurer -->
<!-- 设置扫描mybatis接口和映射文件所在的包,生成mapper接口的实现类对象,放到spring的容器中 -->
<!-- 放到spring中的mapper实现类对象的id是mapper接口类名首字母小写 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 多个包用逗号或者分号隔开 -->
<property name="basePackage" value="ah.szxy.system.mapper"></property>
<!-- 如果有多个SqlSessionFactory的时候用于指定使用哪个,单个可以不用配置 -->
<!-- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> -->
</bean>
</beans>
- dubbo配置文件 applicationContext-dubbo.xml
自定义服务名称
根据自己的zookeeper配置指定注册中心地址
实例化自己需要发布的服务
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 添加服务提供者的标志 -->
<dubbo:application name="ah-szxy-system-provider" />
<!-- 指定注册中心 -->
<dubbo:registry address="192.168.179.128:2181,192.168.179.128:2182,192.168.179.128:2183" protocol="zookeeper" />
<!-- 指定当前项目发布dubbo服务的方式 -->
<!-- 指定服务发布的协议:dubbo协议 -->
<!-- 指定服务发布的端口:10000 -->
<!-- 指定项目发布的地方. 可以与注册中心不再同一个虚拟机: host -->
<!-- <dubbo:protocol name="dubbo" port="20000" host="192.168.179.129"/> -->
<!-- 发布dubbo服务 服务所在路径.实现的id -->
<dubbo:service interface="ah.szxy.system.service.TopicService" ref="topicServcieImpl"></dubbo:service>
</beans>
- 业务层配置文件 applicationContext-service.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:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="ah.szxy.system.service.impl" />
</beans>
- 事务配置文件applicationContext-tx.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
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.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 事务管理器 -->
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 事务的通知 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="insert*" isolation="DEFAULT" propagation="REQUIRED" />
<tx:method name="add*" isolation="DEFAULT" propagation="REQUIRED" />
<tx:method name="create*" isolation="DEFAULT" propagation="REQUIRED" />
<tx:method name="save*" isolation="DEFAULT" propagation="REQUIRED" />
<tx:method name="delete*" isolation="DEFAULT" propagation="REQUIRED" />
<tx:method name="remove*" isolation="DEFAULT" propagation="REQUIRED" />
<tx:method name="modify*" isolation="DEFAULT" propagation="REQUIRED" />
<tx:method name="update*" isolation="DEFAULT" propagation="REQUIRED" />
<!-- 只读事务 -->
<tx:method name="select*" isolation="DEFAULT" propagation="REQUIRED"
read-only="true" />
<tx:method name="get*" isolation="DEFAULT" propagation="REQUIRED"
read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 事务切面的配置 -->
<aop:config>
<!-- 切点 -->
<aop:pointcut expression="execution(* ah.szxy.system.service.impl.*.*(..))"
id="txPc" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPc" />
</aop:config>
</beans>
- 日志文件 log4j.properties
放在resources根目录下 ,与上面三个文件夹同级
修改包名
log4j.logger.ah.szxy.system.mapper=DEBUG
# Global logging configuration
log4j.rootLogger=INFO, stdout
log4j.logger.ah.szxy.system.mapper=DEBUG
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
- 测试类
用于通过java手动发布服务 ,在执行ah-szxy-manager-web 项目时 ,需要首先运行这段测试代码 ,代码放在test目录下
package an.szxy.system.test;
import java.io.IOException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class test {
public static void main(String[] args) {
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("spring/applicationContext-*.xml");
ac.start();
try {
System.in.read();
} catch (IOException e) {
e.printStackTrace();
} // 阻塞程序的运行
ac.stop();
}
}
- 资源打包插件见博文末尾分享1 ,需要项目的根目录下 ,具体请看上面截图
创建其聚合子模块 ah-szxy-manager-web (war)
项目结构
ftp配置文件需要有 vsftpd软件以及相关api的支持 ,暂时不需要 ,故省略不写
相关资料已分享至百度云
步骤
- 添加相关jar的坐标
依赖业务层接口,因为要调用相关的服务
<dependencies>
<!-- 依赖业务层接口,调用相关的服务 -->
<dependency>
<groupId>ah.szxy</groupId>
<artifactId>ah-szxy-system-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- dubbo依赖 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<!-- zookeeper客户端依赖 -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
</dependency>
<!-- spring依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</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>
<!-- json到java对象的序列化和反序列化 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<!-- 处理图片上传的 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 配置Tomcat插件 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<configuration>
<path>/</path>
<port>8080</port>
<!-- Maven中关联的tomcat账户的id,发布服务消费者时候使用 -->
<!-- <server>tomcat7</server>
<path>/ROOT</path>
<url>http://192.168.179.129:8080/manager/text</url> -->
</configuration>
</plugin>
</plugins>
</build>
- applicationContext-dubbo.xml
修改消费服务者标志
修改注册中心
修改远程调用的服务对象
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 添加服务消费者的标志 -->
<dubbo:application name="ah-szxy-system-consumer"/>
<!-- 指定注册中心 -->
<dubbo:registry address="192.168.179.128:2181,192.168.179.128:2182,192.168.179.128:2183" protocol="zookeeper" />
<!-- spring容器中存在一个远程服务的代理对象 -->
<dubbo:reference interface="ah.szxy.system.service.TopicService" id="topicServiceProxy"></dubbo:reference>
</beans>
- applicationContext-service.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:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 加载属性文件 -->
<!-- <context:property-placeholder location="classpath:resources/ftp.properties"/> -->
<context:component-scan base-package="ah.szxy.system.service.impl" />
</beans>
- springmvc.xml
修改controller包名
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 扫描controller -->
<context:component-scan base-package="ah.szxy.manager.controller" />
<!-- mvc:annotation-driven -->
<mvc:annotation-driven />
<!-- 静态资源映射 -->
<!-- location:表示资源在项目的真正位置 -->
<!-- mapping:访问路径 -->
<!-- /css/** -->
<!-- http://localhost:8080/css/a/b/c/hello.css -->
<!-- / = http://localhost:8080/ -->
<mvc:resources location="/WEB-INF/css/" mapping="/css/**"></mvc:resources>
<mvc:resources location="/WEB-INF/js/" mapping="/js/**"></mvc:resources>
<mvc:resources location="/WEB-INF/images/" mapping="/images/**"></mvc:resources>
<!-- 视图解析器 -->
<bean id="viewResovler"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 表示使用的视图技术是jsp -->
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView"></property>
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/jsp/"></property>
<!-- 后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 图片上传的解析器 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设定默认编码 -->
<property name="defaultEncoding" value="UTF-8"></property>
<!-- 设定文件上传的最大值5MB,5*1024*1024 -->
<property name="maxUploadSize" value="5242880"></property>
</bean>
</beans>
5 .web.xml (创建路径 :webapp/WEB-INF/web.xml)
主要修改如下的标识名(3个) :display-name与servlet的servlet-name和servlet_mapping的servlet-name
<?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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<display-name>ah-szxy-manager-web</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/favicon.ico</url-pattern>
</servlet-mapping>
<!-- 以监听器的方式启动spring容器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 指定spring的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext-*.xml</param-value>
</context-param>
<!-- POST请求的乱码过滤器 -->
<filter>
<filter-name>encodingFilter</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>
</filter>
<!-- 映射filter -->
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- springmvc的servlet -->
<servlet>
<servlet-name>ah-szxy-manager-web</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 指定springmvc的配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
<!-- 让springmvc随系统启动而启动 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ah-szxy-manager-web</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
6 .webapp目录
webapp下的文件需要自己创建 web.xml已经给出 , jsp页面只要自己写一个index.jsp的页面即可 .
其他资源根据自己需要添加 ,如有需要 ,可下载底部分享的资源
更多推荐
所有评论(0)