Dubbo调用示例

用dubbo做一个“hello world”。

此次demo十分简单,旨在对Dubbo有个整体上的初步了解。服务提供者(程序)和服务消费者(程序)虽然都是运行在同个服务器上(本地tomcat),但是调用是通过Dubbo的RPC。

注册中心是redis,部署在本地虚拟机,地址为192.168.1.66:6379(在配置文件中需要用到)。最终达到效果是服务消费者(Consumer)调用服务提供者(Provider)的sayHello()方法在控制台输出“Hello world”。

需要做的事情:

  • 在pom.xml 当中引入Dubbo 依懒
  • 编写服务接口类
  • 编写服务实现类
  • 编写服务提供者 xml 配置
  • 编写服务消费者 xml 配置
  • 启动服务提供者
  • 编写并执行远程服务调用

pom.xml

	<dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>dubbo</artifactId>
      <version>2.5.3</version>
    </dependency>

    <dependency>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
      <version>2.2.0</version>
    </dependency>

服务接口DemoService

public interface DemoService {
    String sayHello(String str);
}

服务接口实现DemoService

public class DemoServiceImpl implements DemoService {
    public String sayHello(String str) {
        return "Hello " + str;
    }
}

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="demo-consumer"/>
    <dubbo:registry protocol="redis" address="192.168.1.66:6379" check="true"/>
    <dubbo:consumer timeout="5000" retries="2"
                    group="snowman"
                    version="1.0.0"/>
    <dubbo:reference
            timeout="3000" retries="1"
            id="demoService"
            version="*"
            interface="com.snowman.service.DemoService"/>
</beans>

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="demo-provider"/>
    <dubbo:registry protocol="redis" address="192.168.1.66:6379" check="true"/>
    <dubbo:protocol name="dubbo" port="20880"/>
    <dubbo:provider group="snowman"
                    threadpool="fixed"
                    threads="500"
                    timeout="5000"
                    retries="2"
    />
    <dubbo:service interface="com.snowman.service.DemoService"
                   timeout="5000"
                   retries="1"
                   version="3.0.0"
                   ref="demoService">
        <dubbo:method name="sayHello" timeout="2000"/>
    </dubbo:service>
    <bean id="demoService" class="com.snowman.service.DemoServiceImpl"/>
</beans>

服务提供者Provider

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Provider {
    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
               "config/spring-dubbo-redis-provider.xml");
        context.start();
        System.out.println("dubbo redis 服务启动成功 ");
        System.in.read();
    }
}

服务消费者Consumer

import com.snowman.server.DemoService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Consumer {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                "config/spring-dubbo-redis-consumer.xml");
        context.start();
        DemoService demoService = (DemoService) context.getBean("demoService"); //获取远程服务
        String hello = demoService.sayHello("world"); //执行远程服务
        System.out.println(hello);
    }
}

项目工程结构
在这里插入图片描述
先启动redis(如何安装可参考《Redis之在Linux上安装和简单的使用》),因为它是注册中心(在两个xml都配置了redis的地址),没起来会报错,也没办法通知Consumer调用。

运行Provider,可以在控制台看到输出“dubbo redis 服务启动成功 ”
在这里插入图片描述

再运行Consumer,可以在控制台看到输出“Hello world ”,远程调用Provider的服务成功。
在这里插入图片描述

再去看redis,可以看到Provider和Consumer都把相关信息发送到注册中心
在这里插入图片描述
(RedisDesktopManager,一个redis可视化工具)

流程简述:

  1. 启动Provider,Provider根据配置文件找到要提供的服务接口及其相关参数(版本号、超时时间等),向提供的注册中心发送信息进行注册。
  2. 启动Consumer,Consumer根据配置文件找到所需服务接口及其相关参数,向注册中心发送信息进行注册。
  3. 注册中心接收到注册信息后尽心匹配,将服务和接口信息发送给Consumer。
  4. Consumer接到注册信息的信息进行缓存,并根据信息进行接口的远程调用。
    (调用过程跟注册中心没半毛钱关系,信息是从缓存取得,取得注册中心的信息后,就算注册中心挂掉可以正常调用)

demo:https://github.com/soarsnowman/dubbo.git

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值