一个最简单的dubbo例子实现

来源:互联网 发布:康丝0.4数据 编辑:程序博客网 时间:2024/05/17 07:04

Dubbo作为淘宝搞出的框架,居然没提供相关文档,这样学习这个框架变得比较困难,尤其是入门这步,看了网上零零种种的关于Dubbo的总结,总感觉说的太罗嗦了,没有抓住重点,简单来说dubbo是种非侵入式的RPC(远程访问)框架。关键是两个词非侵入式RPC。

所谓的非侵入性是指dubbo并没有参杂入实现代码中,实现代码并不直接依赖dubbo的相关类。而是通过Spring XML的配置文件的形式进行完成RPC(远程访问)操作。

RPC(远程访问)框架是指在客户端这边调用和服务端同名的接口,效果相当于调用了远程服务端实现的同名服务功能,虽然服务功能的实现是在服务端实现运行的,在客户端并无实现,但是通过RPC框架内部的网络通讯机制使客户端能够透明地使用服务端同名服务而不需要客户端实现。RPC对客户端使用者而言是透明的。

先用一个最简单的dubbo实现例子说说dubbo的非侵入性

服务端:实现一个helloworld的服务功能,功能为打印一行hello world!

客户端:不知道helloworld服务的具体实现,只知道接口规范,在客户端上调用该功能,通过dubbo的rpc机制使用该服务。

要实现上面的简单例子,需要几点:

1.       服务端的Spring XML文件配置(里面包含了dubbo的相关配置信息)

provider-bean-context.xml:

<?xmlversion="1.0" encoding="UTF-8"?>

<beansxmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:jee="http://www.springframework.org/schema/jee"

xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"xmlns:p="http://www.springframework.org/schema/p"

xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:util="http://www.springframework.org/schema/util"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/jeehttp://www.springframework.org/schema/jee/spring-jee.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd

http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.2.xsd

http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd

http://code.alibabatech.com/schema/dubbohttp://code.alibabatech.com/schema/dubbo/dubbo.xsd">

<dubbo:applicationname="dubboTest-provider" />

<!--zookeeper注册中心--> 

<dubbo:registry protocol="zookeeper"address="127.0.0.1:2181" client="curator" />

<!--使用multicast广播注册中心暴露服务地址 --> 

<dubbo:protocol name="dubbo" port="20880"/> 

<dubbo:serviceinterface="com.tisson.zrftest.DubboTestProvider" 

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

         <beanid="dubboTestProvider"class="com.tisson.zrftest.DubboTestProviderImp" /> 

</beans> 

 

--重点看粗体字相关内容,里面配置了注册中心使用的zookeeper的ip,端口等信息,另外配置了服务端暴露给外界的端口,服务接口名以及服务的实现类。

2. 客户端的Spring XML文件配置(里面包含了dubbo的相关配置信息)

consumer-bean-context.xml:

<?xml version="1.0"encoding="UTF-8"?>

<beansxmlns="http://www.springframework.org/schema/beans"

         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:jee="http://www.springframework.org/schema/jee"

         xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"

         xmlns:context="http://www.springframework.org/schema/context"

         xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"xmlns:p="http://www.springframework.org/schema/p"

         xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:util="http://www.springframework.org/schema/util"

         xsi:schemaLocation="http://www.springframework.org/schema/beans

         http://www.springframework.org/schema/beans/spring-beans.xsd

         http://www.springframework.org/schema/jeehttp://www.springframework.org/schema/jee/spring-jee.xsd

         http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.2.xsd

         http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.2.xsd

         http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd

         http://code.alibabatech.com/schema/dubbohttp://code.alibabatech.com/schema/dubbo/dubbo.xsd">

        

         <dubbo:applicationname="dubboTest-consumer" />

         <!--zookeeper注册中心 --> 

         <dubbo:registry  protocol="zookeeper"address="127.0.0.1:2181" client="curator" /> 

         <!--使用multicast广播注册中心暴露的服务地址--> 

        <!--<dubbo:registryaddress="multicast://10.57.41.19:1234"/> --> 

          <!-- 生成远程服务代理,可以和本地bean一样使用demoService--> 

         <dubbo:referenceid="dubboTestProvider"interface="com.tisson.zrftest.DubboTestProvider" />

</beans>

--重点看粗体字相关内容,里面配置了注册中心使用的zookeeper的ip,端口等信息,客户端使用到的接口名

 

3.服务接口的定义:

DubboTestProvider.java:

public interfaceDubboTestProvider{

    public  void helloWorld();

}

4.接口实现类:

public classDubboTestProviderImp implements DubboTestProvider{

    @Override

    public void helloWorld() {

        // TODO Auto-generated method stub

        System.out.println("hello world!");

    }

}

 

5.服务端启动类:

LuncherProvider.java:

public classLuncherProvider{

    public static void main(String[] args){

        String[]springConfigLocation=newString[]{"resource/dubboTestProvider/provider-bean-context.xml"};

        ClassPathXmlApplicationContext  springContext =newClassPathXmlApplicationContext(springConfigLocation);

        springContext.start();

        try {

            System.in.read();

        }catch(IOExceptione) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

    }

}

--简单来说就是加载Spring的xml配置文件,调用start启动,并且用System.in.read();阻塞主流程,让服务端一直处于等待访问状态。

6.客户端启动类:

LuncherConsumer.java:

public classLuncherConsumer{

    public static void main(String[] args){

        String[]springConfigLocation=newString[]{"resource/dubboTestConsumer/consumer-bean-context.xml"};

        ClassPathXmlApplicationContext  springContext =newClassPathXmlApplicationContext(springConfigLocation);

        DubboTestProviderdubboTestProvider=(DubboTestProvider)springContext.getBean("dubboTestProvider");

        dubboTestProvider.helloWorld();

    }

}

--简单来说就是加载Spring的xml配置文件,并通过springContext.getBean("dubboTestProvider")获取接口引用,并调用接口中的方法,通过远程调用,最终使用到了远程服务端的功能

 

---------启动服务端后,再启动客户端,可以发现,服务端的后台输出了一行字符串:

hello world!

如果我们这时候去查看zookeeper会发现,在zookeeper那边的根路径下多了一个dubbo的目录

[zk: localhost:2181(CONNECTED) 0] ls /

[host, dubbo, conf, zookeeper, zrf]

Dubbo下面有一个子目录

[zk:localhost:2181(CONNECTED) 1] ls /dubbo

[com.tisson.zrftest.DubboTestProvider]

--这就是服务接口名,是服务端和客户端联系起来的纽带,再进去一层看有四个子目录

[zk:localhost:2181(CONNECTED) 2] ls /dubbo/com.tisson.zrftest.DubboTestProvider

[consumers,configurators, routers, providers]

--里面主要是保存了服务端的ip和端口信息,以便客户端能够获取该信息,使用rpc机制进行远程调用。

 

总结:以上就是dubbo非侵人特征的最简单直观的介绍,可以看到在具体的java代码中丝毫看不到任何与dubbo的一点依赖,通过Spring的XML配置就能很简单地使用dubbo框架。至于dubbo的RPC机制,表面上理解很容易就是客户端调用远程服务端功能的同名接口,实际上通过dubbo的rpc中的网络通讯能力透明地调用了服务端的代码。但是内部详细实现流程还待探索。

 

0 0
原创粉丝点击