Dubbo初体验

Dubbo初体验

  • Apache Dubbo 是一款高性能、轻量级的开源服务框架。
  • 提供了六大核心能力:面向接口代理的高性能RPC调用,智能容错和负载均衡,服务自动注册和发现,高度可扩展能力,运行期流量调度,可视化的服务治理与运维。

一、Dubbo如何实现远程通信?

在这里插入图片描述
服务消费者去注册中心订阅到服务提供者的信息。然后通过dubbo进行远程调用。

二、demo

1. 创建maven项目,命名为my-dubbo-demo

2. 创建my-dubbo-demo的module,类型为maven

  1. 命名my-service-interfaces
  2. 创建接口DeviceService
    在这里插入图片描述

3. 创建my-dubbo-demo的module,类型为maven

  1. 命名device-service-provider
  2. pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>my-dubbo-demo</artifactId>
        <groupId>com.hao</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>device-service-provider</artifactId>
    <dependencies>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.8.0-alpha2</version>
        </dependency>

        <!--dubbo-->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.7.3</version>
        </dependency>
        <!--zk-->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>4.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-client</artifactId>
            <version>4.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>4.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.13</version>
        </dependency>
        <dependency>
            <groupId>com.hao</groupId>
            <artifactId>my-service-interfaces</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>


</project>
  1. 编写具体的提供服务的实现类
    在这里插入图片描述

  2. 编写bean配置文件,将dubbo和spring ioc整合,把服务提供到dubbo中.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="device-service"/>
<!--注册中心的信息:服务要注册到这个注册中心上-->
<dubbo:registry address="zookeeper://192.168.100.105:2181"/>
<!--配置当前这个服务在dubbo容器中的端口号,每个dubbo容器内部的服务的端口号必须是不一样的-->
<dubbo:protocol name="dubbo" port="20881"/>
<!--暴露出服务,指明该服务具体的实现bean是deviceService-->
<dubbo:service interface="com.hao.service.api.device.DeviceService" ref="deviceService"/>
<!--指明服务的实现类,将服务提供者的bean注入到ioc容器中-->
<bean id="deviceService" class="com.hao.device.service.impl.DeviceServiceImpl"/>

</beans>
        <!--注册服务-->
  1. 启动ioc容器,关联bean配置文件,provider.java
package com.hao.device.service.impl;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

public class Provider {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"provider.xml"});
        context.start();
        System.out.println("服务已注册!");
        try {
            // 按任意键退出
            System.in.read();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

4. 创建my-dubbo-demo的module,类型为maven

  1. 命名device-service-consumer
  2. pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>my-dubbo-demo</artifactId>
        <groupId>com.hao</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>device-service-consumer</artifactId>

    <dependencies>
        <dependency>
            <artifactId>my-service-interfaces</artifactId>
            <groupId>com.hao</groupId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <!--dubbo-->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.7.3</version>
        </dependency>
        <!--zk-->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>4.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-client</artifactId>
            <version>4.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>4.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.13</version>
        </dependency>
    </dependencies>

</project>
  1. 编写bean配置文件,将dubbo和spring ioc整合,把服务提供到dubbo中.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="device-consumer"/>
    <!--注册中心的信息:服务要注册到这个注册中心上-->
    <dubbo:registry address="zookeeper://192.168.100.105:2181"/>
    <!--要订阅的服务,生成一个远程的服务代理对象-->
    <dubbo:reference interface="com.hao.service.api.device.DeviceService" id="deviceService"/>
</beans>
<!--注册服务-->
  1. 启动ioc容器,关联bean配置文件 Consumer.java
package com.hao.device.consumer;

import com.hao.service.api.device.DeviceService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Consumer {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"consumer.xml"});
        context.start();
        DeviceService deviceService = (DeviceService) context.getBean("deviceService");
        String result = deviceService.getDeviceNameByID(1001);
        System.out.println(result);
    }
}



三、服务代理过程

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值