00-springmvc分布式项目项目结构
Posted tpf386
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了00-springmvc分布式项目项目结构相关的知识,希望对你有一定的参考价值。
01模块名字:p2p-parent
作为父模块,对其他模块进行管理。
1,idea新建空项目,设置项目保存位置。
2,选择新建模块,选择maven项目类型,不选择具体的模板,建出来是一个有maven基本结构的项目,这个项目作为父模块,可以起名为:xxx(项目名)-parent。
3,修改配置文件,在配置文件添加节点packaging,文本内容为pom:
<packaging>pom</packaging>
4,删除src目录,parent项目不需要src目录。
5,配置文件内容:
<?xml version="1.0" encoding="UTF-8"?> <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>com.bjpowernode.p2p</groupId> <artifactId>p2p-parent</artifactId> <version>1.0.0</version> <!----> <packaging>pom</packaging> <properties> <!--自定义标称名称--> <spring-version>4.3.9.RELEASE</spring-version> <servlet-api-version>3.1.0</servlet-api-version> <jstl-api-version>1.2</jstl-api-version> <taglibs-standard-version>1.2.1</taglibs-standard-version> <mysql-connector-java-version>5.1.43</mysql-connector-java-version> <druid-version>1.1.1</druid-version> <mybatis-version>3.4.1</mybatis-version> <mybatis-spring-version>1.3.0</mybatis-spring-version> <aspectjweaver-version>1.8.9</aspectjweaver-version> <zkclient-version>0.10</zkclient-version> <log4j-version>2.3</log4j-version> <spring-data-redis-version>1.8.4.RELEASE</spring-data-redis-version> <jedis-versino>2.9.0</jedis-versino> <dubbo-version>2.6.0</dubbo-version> <commons-lang3-version>3.4</commons-lang3-version> <jackson-version>2.7.3</jackson-version> <commons-io-version>2.5</commons-io-version> <httpclient-version>4.5.2</httpclient-version> <fastjson-version>1.2.36</fastjson-version> </properties> <dependencyManagement> <dependencies> <!--dataservice 工程依赖--> <!-- Spring 框架依赖的 JAR 配置 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</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-core</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-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-web</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-oxm</artifactId> <version>$spring-version</version> </dependency> <!-- servlet 及 jstl 标签库依赖的 JAR 配置 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>$servlet-api-version</version> </dependency> <dependency> <groupId>javax.servlet.jsp.jstl</groupId> <artifactId>jstl-api</artifactId> <version>$jstl-api-version</version> </dependency> <dependency> <groupId>org.apache.taglibs</groupId> <artifactId>taglibs-standard-spec</artifactId> <version>$taglibs-standard-version</version> </dependency> <dependency> <groupId>org.apache.taglibs</groupId> <artifactId>taglibs-standard-impl</artifactId> <version>$taglibs-standard-version</version> </dependency> <!-- MySQL 数据库连接驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>$mysql-connector-java-version</version> </dependency> <!-- JDBC 数据源连接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>$druid-version</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>$mybatis-spring-version</version> </dependency> <!-- Spring AOP 支持 start --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>$aspectjweaver-version</version> </dependency> <!-- zookeeper 客户端依赖 --> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>$zkclient-version</version> </dependency> <!-- Log4j2 依赖的 JAR 配置 --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>$log4j-version</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>$log4j-version</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-jcl</artifactId> <version>$log4j-version</version> </dependency> <!-- spring-data-redis 依赖的 JAR 配置 --> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>$spring-data-redis-version</version> </dependency> <!-- jedis 依赖的 JAR 配置 --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>$jedis-versino</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>$dubbo-version</version> <!--排除依赖冲突项--> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> </exclusion> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> </exclusion> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> </exclusion> </exclusions> </dependency> <!-- 加载 apache commons 包 start --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>$commons-lang3-version</version> </dependency> <!-- 加载 jackson 包 --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>$jackson-version</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>$jackson-version</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>$jackson-version</version> </dependency> <!-- 文件 IO 操作包 start --> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>$commons-io-version</version> </dependency> <!-- httpclient4.5 操作包 start --> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>$httpclient-version</version> </dependency> <!-- 阿里巴巴 json 解析包 start --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>$fastjson-version</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.2</version> <!-- provided:编译运行时生效,打包时不将该包打包 --> <scope>provided</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <!-- 编译插件 --> <plugin> <artifactId>maven-compiler-plugin</artifactId> <!-- 插件的版本 --> <version>3.5.1</version> <!-- 编译级别 --> <configuration> <source>1.8</source> <target>1.8</target> <!-- 编码格式 --> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build> </project>
02模块名字:p2p-exterface
该模块是接口项目,新建模块的时候,需要继承自parent模块,同时选择继承GroupId。
选择模板,可以选择quickstart模板或者不选择模板。
dubbo推荐这种开发模式,该模板存放接口和bean、vo等。
com.bjpowernode.p2p 顶级包package
com.bjpowernode.p2p.model 存放bean的包,包名也可以是domain、beans等
com.bjpowernode.p2p.service 存放业务接口的包。
com.bjpowernode.p2p.vo 存放vo类(全称为Value Object,就是一个普通的JavaBean。一般配合DAO来使用,用于实例化对象。)
继承自parent模块,会在pom文件生成一段配置,这段配置如下,假如新建模块时没有继承自parent,可以直接在配置文件里面写上配置,达到一样的效果。
<parent> <artifactId>p2p-parent</artifactId> <groupId>com.bjpowernode.p2p</groupId> <version>1.0.0</version> <relativePath>../p2p-parent/pom.xml</relativePath> </parent>
具体的配置文件内容:
<?xml version="1.0" encoding="UTF-8"?> <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"> <parent> <artifactId>p2p-parent</artifactId> <groupId>com.bjpowernode.p2p</groupId> <version>1.0.0</version> <relativePath>../p2p-parent/pom.xml</relativePath> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>p2p-exterface</artifactId> <dependencies> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.2</version> <!-- provided:编译运行时生效,打包时不将该包打包 --> <scope>provided</scope> </dependency> </dependencies> </project>
03模块:p2p-dataservice
该模块是dubbo服务提供者。
它要去实现p2p-exterface工程的业务接口,并连接数据库实现数据的持久层
新建模块,继承自parent,模板选择maven项目webapp
com.bjpowernode.p2p 顶级包
com.bjpowernode.p2p.mapper 存放dao接口和mybatis的xml映射文件的包
com.bjpowernode.p2p.service 存放exterface的Service中接口的实现类
resources中存放配置文件
applicationContext.xml 主要:加载系统配置文件 配置扫描service包的注解 导入数据库相关配置 导入redis相关配置 导入服务提供者配置
applicationContext-datasource.xml 主要:配置数据库连接,阿里数据源 druid 连接池 MyBatis sqlSessionFactory 配置 mybatis的MapperScannerConfigurer自动装配配置、事务控制等。
applicationContext-dubbo-provider.xml 主要:服务提供者应用名称, 配置 zookeeper 注册中心 暴露具体的业务服务
applicationContext-redis.xml 主要:jedis Connection Factory redis template definition
log4j2.xml 主要:log4j的日志配置
mybatis-configuration.xml 主要: log4j2 与 mybatis 集成,目的是打印出 sql 语句
redis.properties 主要:redis的连接数据。
WEB_INF配置文件:
web.xml 主要:spring 监听器加载 applicationContext.xml 配置文件 spring 字符过滤器
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: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:task="http://www.springframework.org/schema/task" 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/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"> <!-- 加载系统配置文件 --> <context:property-placeholder location="classpath:*.properties" /> <!-- 扫描注解 --> <context:component-scan base-package="com.bjpowernode.p2p.service" /> <!-- 导入数据相关配置 --> <import resource="applicationContext-datasource.xml" /> <!-- 导入 redis 配置 --> <import resource="applicationContext-redis.xml" /> <!-- 导入服务提供者配置 --> <import resource="applicationContext-dubbo-provider.xml"/> </beans>
applicationContext-datasource.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: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:task="http://www.springframework.org/schema/task" 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/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"> <!-- 配置数据库连接,阿里数据源 druid 连接池 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="url" value="jdbc:mysql://192.168.127.128:3306/p2p?useSSL=false&useUnicode=true&characterEncoding=UTF-8"/> <property name="username" value="root"/> <property name="password" value="123456"/> </bean> <!-- MyBatis sqlSessionFactory 配置 mybatis --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="classpath:mybatis-configuration.xml"/> <property name="dataSource" ref="dataSource"/> </bean> <!-- scan for mappers and let them be autowired --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.bjpowernode.p2p.mapper"/> <property name="sqlSessionFactory" ref="sqlSessionFactory"/> </bean> <!-- 事务相关控制 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <tx:annotation-driven transaction-manager="transactionManager"/> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <!-- 对业务层所有方法添加事务,除了以 get、find、select 开始的 --> <tx:method name="*" isolation="DEFAULT" propagation="REQUIRED" rollback-for="java.lang.Exception"/> <!-- 查询操作没有必要开启事务,给只读事务添加一个属性 read-only --> <tx:method name="get*" read-only="true"/> <tx:method name="find*" read-only="true"/> <tx:method name="select*" read-only="true"/> <tx:method name="query*" read-only="true"/> </tx:attributes> </tx:advice> <!-- Service 层事务控制 --> <aop:config> <aop:pointcut id="pointcut" expression="execution(* com.bjpowernode.p2p.service.**.*.*(..))"/> <aop:advisor pointcut-ref="pointcut" advice-ref="txAdvice"/> </aop:config> </beans>
applicationContext-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="dataservice"/> <!-- 配置 zookeeper 注册中心 --> <dubbo:registry protocol="zookeeper" address="192.168.127.128:2181"/> <!--产品业务--> <dubbo:service interface="com.bjpowernode.p2p.service.loan.LoanInfoService" ref="loanInfoServiceImpl" version="1.0.0" timeout="15000"></dubbo:service> <!--用户业务--> <dubbo:service interface="com.bjpowernode.p2p.service.loan.UserService" ref="userServiceImpl" version="1.0.0" timeout="15000"></dubbo:service> <!--投资业务--> <dubbo:service interface="com.bjpowernode.p2p.service.loan.BidInfoService" ref="bidInfoServiceImpl" version="1.0.0" timeout="15000"></dubbo:service> <!--帐户业务--> <dubbo:service interface="com.bjpowernode.p2p.service.user.FinanceAccountService" ref="financeAccountServiceImpl" version="1.0.0" timeout="15000"></dubbo:service> <!--redis业务--> <dubbo:service interface="com.bjpowernode.p2p.service.loan.RedisService" ref="redisServiceImpl" version="1.0.0" timeout="15000"></dubbo:service> <!--收益业务--> <dubbo:service interface="com.bjpowernode.p2p.service.loan.IncomeRecordService" ref="incomeRecordServiceImpl" version="1.0.0" timeout="15000"></dubbo:service> <!--充值业务--> <dubbo:service interface="com.bjpowernode.p2p.service.loan.RechargeRecordService" ref="rechargeRecordServiceImpl" version="1.0.0" timeout="15000"></dubbo:service> </beans>
applicationContext-redis.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: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"> <!-- jedis Connection Factory --> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:usePool="$redis.usePool" p:hostName="$redis.hostName" p:port="$redis.port" p:timeout="$redis.timeout" p:password="$redis.password"/> <!-- redis template definition --> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="jedisConnectionFactory"/> </bean> </beans>
log4j2.xml 配置文件内容:
<?xml version="1.0" encoding="UTF-8"?> <configuration status="debug"> <appenders> <Console name="Console" target="SYSTEM_OUT"> <ThresholdFilter level="debug" onMatch="ACCEPT" onMismatch="ACCEPT"/> <PatternLayout pattern="[dataservice] %dyyyy-MM-dd HH:mm:ss.SSS %-5level %class36 %L %M - %msg%xEx%n"/> </Console> <RollingFile name="RollingFile" fileName="/opt/tomcat_dataservice/logs/dataservice.log" filePattern="/opt/tomcat_dataservice/logs/$$date:yyyy-MM/dataservice-%dMM-dd-yyyy-%i.log.gz"> <PatternLayout pattern="[dataservice] %dyyyy-MM-dd ‘at‘ HH:mm:ss z %-5level %class36 %L %M - %msg%xEx%n"/> <SizeBasedTriggeringPolicy size="100MB"/> </RollingFile> </appenders> <loggers> <logger name="com.bjpowernode.p2p.mapper" level="debug"/> <root level="debug"> <appender-ref ref="Console"/> <appender-ref ref="RollingFile"/> </root> </loggers> </configuration>
mybatis-configuration.xml配置文件内容:
<?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> <!-- log4j2 与 mybatis 集成,目的是打印出 sql 语句 --> <settings> <setting name="logImpl" value="LOG4J2"/> <setting name="logImpl" value="STDOUT_LOGGING"/> </settings> </configuration>
redis.properties配置文件主要内容:
#redis config
redis.usePool=true
redis.hostName=192.168.127.128
redis.port=6379
redis.timeout=8000
redis.password=123456
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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="dataservice" version="3.0"> <display-name>dataservice application</display-name> <!-- spring 监听器加载 applicationContext.xml 配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- spring 字符过滤器 --> <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-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
以上是关于00-springmvc分布式项目项目结构的主要内容,如果未能解决你的问题,请参考以下文章