代码准备
本次作者建立了一个小Demo来说明如何使用Dubbo实现解耦和的项目部署方式
作者使用的开发工具是idea,大家也可以使用eclipse或者myeclipse.
首先使用idea创建一个maven项目,
其中包括了5个子模块,分别是
- dao
- daoimpl
- service
- serviceimpl
- web
项目结构如下图,请原谅画的比较…..(额..抽象)
最上面是dubbodemo的项目,旗下分为5个子模块.
他们的依赖关系分别如图所示
- daoimpl→dao
- serviceimpl→service
- serviceimpl→dao
- web→service
具体需要的jar包pom文件如下
<?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>
<properties>
<spring.version>4.3.9.RELEASE</spring.version> <!--自己写的 -->
<jedis.version>2.9.0</jedis.version>
<dubbo.version>2.5.5</dubbo.version>
<junit.version>4.12</junit.version>
<jackson.version>2.9.1</jackson.version>
</properties>
<groupId>com.dsj</groupId>
<artifactId>dubbodemo</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<module>dao</module>
<module>daoimpl</module>
<module>service</module>
<module>serviceimpl</module>
<module>web</module>
</modules>
<packaging>pom</packaging>
<dependencyManagement>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>${jedis.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
Dao
首先我们来编写一下dao,比如用户使用dao提供的服务
public interface UserDao {
int add(int i,int n);
}
显而易见,我们的dao只提供了一个add,下面编写daoimpl
DaoImpl
注:所有的模块均使用maven,包括打包
接下来是daoimpl的pom文件
<?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>dubbodemo</artifactId>
<groupId>com.dsj</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dao-impl</artifactId>
<dependencies>
<dependency>
<groupId>com.dsj</groupId>
<artifactId>dao</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<d