专注Java教育14年 全国咨询/投诉热线:444-1124-454
赢咖4LOGO图
始于2009,口口相传的Java黄埔军校
首页 hot资讯 Dubbo调用示例分析

Dubbo调用示例分析

更新时间:2021-06-11 15:29:17 来源:赢咖4 浏览828次

用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();
    }
}

项目工程结构

先启动redis,因为它是注册中心(在两个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接到注册信息的信息进行缓存,并根据信息进行接口的远程调用。

(调用过程跟注册中心没半毛钱关系,信息是从缓存取得,取得注册中心的信息后,就算注册中心挂掉可以正常调用)

以上就是赢咖4小编介绍的"学IT什么培训学校好",希望对大家有帮助,如有疑问,请在线咨询,有专业老师随时为您服务。

提交申请后,顾问老师会电话与您沟通安排学习

免费课程推荐 >>
技术文档推荐 >>