dubbo入门

时间:2025-03-22 08:33:50

一、介绍

Dubbo是阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的 RPC 实现服务的输出和输入功能,可以和 Spring框架无缝集成。简单来说,就是远程服务提供接口,隐藏具体实现,调用方通过dubbo连接到远程服务,通过接口可以调用方法进行操作或者获取数据,而不用关心具体实现细节。

二、代码实现

1、Maven配置

搭建java maven项目,引入对应jar包

    <dependency>
        <groupId></groupId>
        <artifactId>spring-context</artifactId>
        <version>4.1.</version>
    </dependency>
    <dependency>
        <groupId></groupId>
        <artifactId>dubbo</artifactId>
        <version>2.5.3</version>
    </dependency>

注:dubbo是阿里开源的一个项目,但只维护到2.5.3版本,现在maven仓库里面最新的也只有这个版本,后续由dangdang团队维护到了2.8.3版本,并命名为dubbox
/dubbox/


切记,这两个版本并不兼容,笔者之前项目里面由于服务商用了dubbox,而本地一直在用dubbo2.5.3版本,一直出错,花了很长时间才解决。

2、代码:

接口:

package ;

public interface DemoService {

    String sayHello(String name);  
}

实现类:

package ;

import ;

public class DemoServiceImpl implements DemoService {

public String sayHello(String name) {
    return "Hello " + name;  
}

}

3、配置文件

dubbo是跟spring无缝集成的,这里我们新建两个配置文件,分别为:

:模拟服务提供商

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans"
xmlns:xsi="http:///2001/XMLSchema-instance"
xmlns:dubbo="/schema/dubbo"
xsi:schemaLocation="/schema/beans        /schema/beans/        /schema/dubbo        /schema/dubbo/">

<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="hello-world-app"  />

<!-- 使用multicast广播注册中心暴露服务地址 -->
<dubbo:registry address="multicast://224.5.6.7:1234" />

<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />

<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="" ref="demoService" />

<!-- 和本地bean一样实现服务 -->
<bean  class="" />

</beans>

dubbo:registry定义注册中心和对外暴露的地址(给调用方使用),默认集成的是multicast注册中心,一般正式环境中会用zookeeper来做,这里我们简单就用默认的。
dubbo:service声明暴露的服务接口,这里是我们自己写的一个DemoService接口和DemoServiceImpl实现类,后面接口提供给调用方。

:模拟服务调用方

    <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans"
    xmlns:xsi="http:///2001/XMLSchema-instance"
    xmlns:dubbo="/schema/dubbo"
    xsi:schemaLocation="/schema/beans        /schema/beans/        /schema/dubbo        /schema/dubbo/">

<!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
<dubbo:application name="consumer-of-helloworld-app"  />

<!-- 使用multicast广播注册中心暴露发现服务地址 -->
<dubbo:registry protocol="multicast" address="224.5.6.7:1234"/>

<!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
<dubbo:reference  interface="" />

</beans>

dubbo:registry指定服务提供方的地址和协议
dubbo:reference根据提供方提供的接口,生成远程服务代理

4、服务运行

:模拟服务提供方

package ;

import ;

public class Provider {

 public static void main(String[] args) throws Exception {  
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {""});  
        ();  

        (); // 为保证服务一直开着,利用输入流的阻塞来模拟  
    }  

}

:模拟服务调用方

package ;

import ;

import ;

public class Consumer {

 public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {""});
        //();

        DemoService api = (DemoService)("demoService"); // 获取远程服务代理
        (("你好"));

        ();

    }

}

先运行Provider类,让服务方运行,再运行Consumer类,控制台打印“Hello 你好”信息,说明consumer通过Dubbo调用服务方的代码成功。