Dubbo学习笔记(三)--- 消费Dubbo服务

本文参照dubbo-2.6.0的dubbo-demo源码:

https://github.com/apache/dubbo/tree/dubbo-2.6.0/dubbo-demo 

 

本节源码下载:

https://github.com/icat-xb/dubbo-parent/tree/dubbo-demo-consumer-ver1.0  

 

工具包下载: 

链接:https://pan.baidu.com/s/1FoOoXLQXnHIKhALlGQcZ4w 

提取码:fm18 

上一篇文章我们搞了发布Dubbo服务,今天我们来调用服务,消费一下!

 

消费Dubbo服务

首先创建一个maven项目

命名dubbo-demo-consumer

创建方法参考 Dubbo学习笔记(二)--- 发布Dubbo服务和启动管理控制台

 

pom.xml添加依赖:

pom.xml:

<dependencies>
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>dubbo</artifactId>
      <version>2.6.0</version>
    </dependency>

    <dependency>
      <groupId>com.101tec</groupId>
      <artifactId>zkclient</artifactId>
      <version>0.10</version>
    </dependency>
    <dependency>
      <groupId>org.apache.curator</groupId>
      <artifactId>curator-framework</artifactId>
      <version>4.0.1</version>
    </dependency>

    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.2.46</version>
    </dependency>

    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.17</version>
    </dependency>

    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.7.25</version>
    </dependency>

    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-lang3</artifactId>
      <version>3.4</version>
    </dependency>

    <dependency>
      <groupId>io.netty</groupId>
      <artifactId>netty-all</artifactId>
      <version>4.0.35.Final</version>
    </dependency>
  </dependencies>

 

定义一个服务消费者接口 

DemoProviderService:

package com.icat.service;

/**
 * 服务消费者接口
 * @author icat
 *
 */

public interface DemoProviderService {
  
  public String sayHello(String name);

}

 

 

创建dubbo-demo-consumer.xml

在src/main/resources 下创建配置文件 dubbo > dubbo-demo-consumer.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
  
  <!-- 服务消费方应用名称,用于计算依赖关系,不是匹配条件,不能与服务提供方名称一样 -->
  <dubbo:application name="demo-consumer"/>

  <!-- 使用zookeeper注册中心暴露服务地址 -->
  <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
  
  <!-- 生成远程服务代理,可与本地bean一样使用 check:启动时候是否坚持 -->
  <dubbo:reference id="demoProviderService" interface="com.icat.service.DemoProviderService"/>
</beans>

 

 

创建测试类

ConsumerTest:

package test;

import java.io.IOException;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.icat.service.DemoProviderService;

public class ConsumerTest {

  public static void main(String[] args) throws IOException {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"dubbo/dubbo-demo-consumer.xml"});
    context.start();
    DemoProviderService demoProviderService = (DemoProviderService)context.getBean("demoProviderService");
    String result = demoProviderService.sayHello("Hello Dubbo!");
    
    System.out.println("服务消费者远程调用的结果:" + result);
    System.in.read(); // 按任意键退出
    context.close();
  }
}

 

 

测试

1.首先启动zookeeper服务,

2.运行服务提供者测试类 ProviderTest,发布服务注册到zookeeper注册中心,

3.最后运行服务消费者测试类 ConsumerTest;

运行结果:

服务消费者远程调用的结果: 服务提供者 -------------- Hello Dubbo!

远程消费服务成功!

 

Dubbo 管理控制台

这里右边显示状态 正常  说明消费dubbo服务成功!

 

最后

我们可以看到dubbo-demo-consumer和dubbo-demo-provider有重复接口DemoProviderService,我们会在下一篇文章中抽取重复接口以及统一依赖版本。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值