1.首先创建一个maven父项目。
2.创建一个父类项目。
这样就成功创建一个dubbo的父类项目了!
3.点击dubbotest项目,右键new→module新建一个远程接口(api)项目,如下图所示。
接下去依次创建服务端项目(server)和客户端项目(client)。完整项目结构如下:
4.在父项目的POM文件添加依赖管理,把远程接口(api)这个项目交给父项目管理(在dubbotest项目的POM文件添加下面dependencyManagement依赖管理)。这样父项目就把api项目的版本号管理起来,其他端的服务要调用的时候就不必再输版本号。
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.easythink</groupId>
<artifactId>api</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</dependencyManagement>
5.分别在服务端(server)和客户端(client)中引入远程接口(api)项目的依赖。这样就完成了分布式项目之间的依赖管理。
<dependencies>
<dependency>
<groupId>com.easythink</groupId>
<artifactId>api</artifactId>
</dependency>
</dependencies>
6.添加Dubbo项目的依赖库,在父类项目POM文件中添加下列的依赖。由于Dubbo本身使用的Spring版本太低,所以在引用的时候排除了对Spring的依赖。
<properties>
<spring-version>4.2.4.RELEASE</spring-version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.easythink</groupId>
<artifactId>api</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.15</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.6</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.6</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.8</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
7.在server项目和client项目中添加依赖库,如下代码所示。
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
</dependency>
8.在api项目中编写接口类。在dubbo项目中,远程服务接口均在api项目中定义。
9.编写服务器程序,首先实现服务端的程序。在server项目中实现该远程服务接口。
10.然后在server项目中使用Spring配置声明暴露服务,在resources目录下创建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"
xmlns:dubbol="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-server"/>
<!--使用zookeeper注册中心的地址-->
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<!--用Dubbo中的RPC协议在20880端口暴露服务-->
<dubbol:protocol name="dubbo" port="20880"/>
<!--声明需要暴露的服务接口-->
<dubbo:service interface="com.easythink.api.IHelloService" ref="helloService"/>
<!--和本地bean一样实现服务-->
<bean id="helloService" class="com.easythink.api.impl.HelloServiceImpl"/>
</beans>
11.加载Spring配置,创建一个Server启动类,通过该类来加载Spring配置并提供远程服务:
12.编写客户端程序,首先在client项目中通过Spring配置引用远程服务,在resources目录下创建一个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="dubbo-client"/>
<!--使用zookeeper注册中心的地址-->
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<!--生成远程服务代理,可以和本地bean一样使用helloService-->
<dubbo:reference id="helloService" interface="com.easythink.api.IHelloService"/>
</beans>
13.加载Spring配置,创建Client启动类,并实现客户端的远程调用:
14.先启动server项目,再启动client项目,可得到以下结果,这就是一个入门级的Dubbo项目。