Dubbo入门demo

Dubbo是阿里开源的分布式RPC通信框架,目前在国内使用范围比较广,本文详细描述Dubbo的入门级使用过程,希望能够给想了解dubbo的人提供一点帮助。

前期准备(括号内为我使用的版本)

  • JDK 1.5+(1.8.0_u161)
  • eclipse(4.7.2)
  • zookeeper(3.4.11)
  • maven(3.5.2)
  1. 我们在eclipse中创建maven项目,命名为dubbo-demo。
  2. 创建三个子模块,分别为dubbo-provider,dubbo-consumer,dubbo-api,从命名可以看出这三个模块分别为服务提供者,服务消费者和API接口项目。

《Dubbo入门demo》 项目初始化结构

  1. 在 dubbo-demo 的 pom.xml中添加公共依赖,主要是添加dubbo和zookeeper客户端依赖。代码如下:
<dependencies>
    <!-- dubbo当前最新版 -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>dubbo</artifactId>
        <version>2.6.0</version>
    </dependency>
    <!-- zookeeper客户端 -->
    <dependency>
        <groupId>com.101tec</groupId>
        <artifactId>zkclient</artifactId>
        <version>0.10</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.7.0</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <encoding>utf-8</encoding>
            </configuration>
        </plugin>
    </plugins>
</build>
  1. 首先我们先处理 api,此项目为其他两个项目提供接口,保证生产者和消费者所使用的接口统一。我们在 dubbo-api 项目中创建包 com.aotian.demo.dubbo.api,并添加 DemoService 接口。

《Dubbo入门demo》 API项目结构及代码

  1. 接口类定义后我们需要在服务端和客户端项目中引用,保证两端接口一致,分别在两个项目的 pom.xml 中添加如下代码:
<dependencies>
    <!-- API接口引用 -->
    <dependency>
        <groupId>com.aotian</groupId>
        <artifactId>dubbo-api</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
</dependencies>
  1. 引用接口之后,我们需要在服务端添加接口实现类,在 dubbo-provider 项目中创建包 com.aotian.demo.dubbo.provider,并添加 DemoService 接口的实现类。

《Dubbo入门demo》 服务端项目结构及代码实现.png

  1. 接口实现类添加后,我们需要将其注册到注册中心,只有这样,其他应用才有可能请求到,接下来我们在 src / main / resources 下创建 dubbo-provider.xml 文件,用于配置dubbo服务端注册,代码如下:
<?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应用程序命名-->
    <dubbo:application name="dubbo-demo-provider"/>

    <!--dubbo注册地址-->
    <dubbo:registry address="zookeeper://localhost:2181"/>

    <!--dubbo协议地址-->
    <dubbo:protocol name="dubbo" port="20880"/>

    <!--接口声明-->
    <dubbo:service interface="com.aotian.demo.dubbo.api.DemoService" ref="demoService"/>
    <bean id="demoService" class="com.aotian.demo.dubbo.provider.DemoServiceImpl"/>
</beans>
  1. 接下来我们准备对服务端进行注册,所以需要准备注册中心,这里用的是 zookeeper,官网下载 zookeeper 后解压,将conf文件夹下的 zoo_sample.cfg 重命名为 zoo.cfg,之后在命令提示符下切换至bin目录下,执行 .\zkServer.cmd,看到如下提示后表示启动成功。
2018-03-13 23:12:59,007 [myid:] - INFO  [main:NIOServerCnxnFactory@89] - binding to port 0.0.0.0/0.0.0.0:2181
  1. 启动服务端。本 Demo 用于测试,所以使用 main 方法加载的形式加载服务端,实际使用过程中可能会在 web 环境中使用。在服务端下添加 Provider.java 并执行 main 方法即可,其他 main 方法实现如下:
public static void main(String[] args) throws IOException {
    ClassPathXmlApplicationContext context 
        = new ClassPathXmlApplicationContext("classpath:dubbo-provider.xml");
    context.start();
    // 阻塞当前进程,否则程序会直接停止
    System.in.read();
}
  1. 处理好服务端之后,我们再进行客户端的处理,首先添加客户端配置文件 dubbo-consumer.xml 放于 src / main / resources 下,内容与服务端相似,只不过不需要暴露接口,而是引用接口,代码如下:
<?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应用程序命名-->
    <dubbo:application name="dubbo-demo-provider"/>

    <!--dubbo注册地址-->
    <dubbo:registry address="zookeeper://localhost:2181"/>

    <!--接口引用-->
    <dubbo:reference interface="com.aotian.demo.dubbo.api.DemoService" id="demoService"/>
</beans>
  1. 处理完配置文件,我们可以在客户端进行测试了,在 src / main / java 下创建包 com.aotian.demo.dubbo.api,并创建文件 consumer.java,main 方法代码如下:
public static void main(String[] args) {
    ClassPathXmlApplicationContext context 
        = new ClassPathXmlApplicationContext("classpath:dubbo-consumer.xml");
    context.start();
    
    String username = "aotian";
    DemoService demoService = (DemoService) context.getBean("demoService");
    System.out.println(demoService.changeUsername(username));
}
  1. 执行后便可在控制台查看结果,如下图所示,表示客户端和服务端已经成功调用。

《Dubbo入门demo》 执行结果

示例代码:https://github.com/Aotian-GitHub/dubbo-basic-demo.git

    原文作者:Aotian
    原文地址: https://www.jianshu.com/p/b492ef5d4b98
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞