最近学完了maven和dubbo,在将之前的项目进行整合拆分成maven+dubbo的时候,遇到了很多麻烦,在这里将步骤粗略的写一下。以方便其他人或者日后在遇到此类问题。由与篇幅较长,本片先写maven拆分子项目。
项目demo:许木木/bookstore
整合后的项目(maven拆分+dubbo):许木木/bookshop-maven
maven拆分
1.创建maven父项目
父工程只有pom.xml,可以推断父工程不进行编码
作用
项目需要的以来信息,在父工程中定义,子模块继承父工程
将各个子模块聚合到一起
2. 导入依赖:
3. 将其发布到maven仓库中:
发布到本地仓库的目的
将来service、dao工程发布到本地仓库时会通过父工程进行相互调用
2.创建子模块
1. 在父工程基础上创建po子模块
将po层复制到项目中,maveninstall,更换jdk版本后要通过maven build ...输入安装jar包
2. 创建dao子模块
拆分spring配置文件
basic.xml:
2.dao.xml
将dao层复制到项目中
引入po依赖, maveninstall:
3.创建service子模块,同样的方法引入, maven install
4.创建action子模块
拆分spring配置文件。
修改web.xml,修改spring配置文件的加载路径
拆分时需要注意的是者当中的配置文件路径问题,见
这样就将原项目拆分成一个具有po、dao、service、action模块的maven项目,各模块之间的关系为:
dubbo分布式服务的整合
将拆分后的项目应用dubbo,以便于进行服务的治理,降低项目之间的耦合度,优点不必多说。此时各模块之间的关系如下:
在父pom.xml中引入dubbo与zookeeper:
<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>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
1.创建接口模块
将service模块作为服务提供者,action做为服务消费者,这样action层和serviceimpl都需要service接口,因此单独建立interface模块,文件结构如下:
2.服务提供方
service此时作为服务提供者,则根据rpc底层实现原理,进行远程调用的项目必须暴露端口(便于通过网络编程的流操作通过动态代理实现远程调用),因此将service模块打包方式改为war。则服务提供方的相关配置:
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="bootshop-service"/>
<dubbo:provider delay="-1" timeout="60000" retries="0"/>
<!-- 使用multicast广播注册中心暴露发现服务地址 -->
<dubbo:registry protocol="zookeeper" address="zookeeper://127.0.0.1:2181"/>
<dubbo:protocol id="book" name="dubbo" port="20880"/>
<dubbo:protocol id="authority" name="dubbo" port="20881"/>
<dubbo:protocol id="business" name="dubbo" port="20882"/>
<dubbo:protocol id="comment" name="dubbo" port="20883"/>
<dubbo:protocol id="login" name="dubbo" port="20884"/>
<dubbo:protocol id="order" name="dubbo" port="20885"/>
<dubbo:protocol id="shop" name="dubbo" port="20886"/>
<dubbo:protocol id="sort" name="dubbo" port="20887"/>
<dubbo:protocol id="userComment" name="dubbo" port="20888"/>
<dubbo:protocol id="user" name="dubbo" port="20889"/>
<dubbo:protocol id="manager" name="dubbo" port="20890"/>
<!-- 生成远程服务代理,对不同的服务通过不同的端口号进行暴露 -->
<dubbo:service interface="edu.hrbeu.service.BookService" ref="bookService" protocol="book"/>
<dubbo:service interface="edu.hrbeu.service.AuthorityService" ref="authorityService" protocol="authority"/>
<dubbo:service interface="edu.hrbeu.service.BusinessService" ref="businessService" protocol="business"/>
<dubbo:service interface="edu.hrbeu.service.CommentService" ref="commentService" protocol="comment"/>
<dubbo:service interface="edu.hrbeu.service.LoginService" ref="loginService" protocol="login"/>
<dubbo:service interface="edu.hrbeu.service.OrderService" ref="orderService" protocol="order"/>
<dubbo:service interface="edu.hrbeu.service.ShopService" ref="shopService" protocol="shop"/>
<dubbo:service interface="edu.hrbeu.service.SortService" ref="sortService" protocol="sort"/>
<dubbo:service interface="edu.hrbeu.service.UserCommentService" ref="userCommentService" protocol="userComment"/>
<dubbo:service interface="edu.hrbeu.service.UserService" ref="userService" protocol="user"/>
<dubbo:service interface="edu.hrbeu.service.ManagerService" ref="managerService" protocol="manager"/>
</beans>
以上配置中,我刚开始比较迷糊的时协议的配置,单个服务我会,但是多个协议配置时,总是报出:”Failed to check the status of the service . No provider available for the service“异常。着实让人摸不着头脑。最后通过对协议声明id,让后让不同的service占据不同的端口成功解决!!
bean-service.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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置组件扫描器 -->
<context:component-scan base-package="edu.hrbeu" />
<!-- AOP动态代理 -->
<aop:aspectj-autoproxy proxy-target-class="true" />
<import resource="dubbo-provider.xml"/>
</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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring/beans-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
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>
<parent>
<groupId>edu.hrbeu</groupId>
<artifactId>bookshop</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>bookshop-service</artifactId>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>edu.hrbeu</groupId>
<artifactId>bookshop-interface</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>edu.hrbeu</groupId>
<artifactId>bookshop-po</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>edu.hrbeu</groupId>
<artifactId>bookshop-dao</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>8060</port>
</configuration>
</plugin>
</plugins>
</build>
</project>
<port>8060</port>
</configuration>
</plugin>
</plugins>
</build>
</project>
3.服务消费方
action此时作为服务消费者,则其相关配置如下:
dubbo-consumer.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="bootshop-action"/>
<!-- 使用multicast广播注册中心暴露发现服务地址 -->
<dubbo:registry protocol="zookeeper" address="zookeeper://127.0.0.1:2181"/>
<!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
<dubbo:reference interface="edu.hrbeu.service.BookService" id="bookService" />
<dubbo:reference interface="edu.hrbeu.service.AuthorityService" id="authorityService"/>
<dubbo:reference interface="edu.hrbeu.service.BusinessService" id="businessService" />
<dubbo:reference interface="edu.hrbeu.service.CommentService" id="commentService"/>
<dubbo:reference interface="edu.hrbeu.service.LoginService" id="loginService" />
<dubbo:reference interface="edu.hrbeu.service.OrderService" id="orderService" />
<dubbo:reference interface="edu.hrbeu.service.ShopService" id="shopService"/>
<dubbo:reference interface="edu.hrbeu.service.SortService" id="sortService"/>
<dubbo:reference interface="edu.hrbeu.service.UserCommentService" id="userCommentService"/>
<dubbo:reference interface="edu.hrbeu.service.UserService" id="userService"/>
<dubbo:reference interface="edu.hrbeu.service.ManagerService" id="managerService"/>
</beans>
beans-mvc.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:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 配置组件扫描器 -->
<context:component-scan base-package="edu.hrbeu"/>
... ...
<import resource="dubbo-consumer.xml"/>
</beans>
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>
<parent>
<groupId>edu.hrbeu</groupId>
<artifactId>bookshop</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>bookshop-action</artifactId>
<packaging>war</packaging>
<properties>
<webVersion>3.1</webVersion>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>8070</port>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>edu.hrbeu</groupId>
<artifactId>bookshop-po</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>edu.hrbeu</groupId>
<artifactId>bookshop-interface</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
<port>8070</port>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>edu.hrbeu</groupId>
<artifactId>bookshop-po</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>edu.hrbeu</groupId>
<artifactId>bookshop-interface</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
分别启动service模块与action模块,可以看到项目运行!!
最最最头疼的异常!!
1. 项目整合后运行时抛出:
Could not obtain transaction-synchronized Session for current thread
这个问题的解决方法是在service层上也加上@Transactional注解
2.序列化,实体在远程调用时需要实现序列化
3.jstl,老生常谈的问题