Dubbo是Alibaba开源的分布式服务框架,它最大的特点是按照分层的方式来架构,使用这种方式可以使各个层之间解耦合(或者最大限度地松耦合)。从服务模型的角度来看,Dubbo采用的是一种非常简单的模型,要么是提供方提供服务,要么是消费方消费服务,所以基于这一点可以抽象出服务提供方(Provider)和服务消费方(Consumer)两个角色。

先上一个小例子进行分析:

首先新建2个maven项目来充当消费方,服务方

服务方:

pom.xml文件主要配置内容:

<!-- spring begin -->  
  <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-context</artifactId>  
            <version>4.1.6.RELEASE</version>  
    </dependency>  
    
    <!-- dubbo begin -->  
    <dependency>  
            <groupId>com.alibaba</groupId>  
            <artifactId>dubbo</artifactId>  
            <version>2.5.3</version>  
    </dependency>
    
        <!-- 注册中心zookeeper begin -->
        <dependency>  
            <groupId>org.apache.zookeeper</groupId>  
            <artifactId>zookeeper</artifactId>  
            <version>3.3.6</version>  
        </dependency>
        
         <!-- log begin -->  
        <dependency>  
            <groupId>commons-logging</groupId>  
            <artifactId>commons-logging</artifactId>  
            <version>1.1.1</version>  
        </dependency>  
        <dependency>  
            <groupId>log4j</groupId>  
            <artifactId>log4j</artifactId>  
            <version>1.2.15</version>  
            <exclusions>  
                <exclusion>  
                    <groupId>com.sun.jdmk</groupId>  
                    <artifactId>jmxtools</artifactId>  
                </exclusion>  
                <exclusion>  
                    <groupId>com.sun.jmx</groupId>  
                    <artifactId>jmxri</artifactId>  
                </exclusion>  
                <exclusion>  
                    <artifactId>jms</artifactId>  
                    <groupId>javax.jms</groupId>  
                </exclusion>  
                <exclusion>  
                    <artifactId>mail</artifactId>  
                    <groupId>javax.mail</groupId>  
                </exclusion>  
            </exclusions>  
        </dependency>
         
        <!-- 其他配置begin -->
        <dependency>  
            <groupId>org.jboss.netty</groupId>  
            <artifactId>netty</artifactId>  
            <version>3.2.0.Final</version>  
        </dependency>  
        <dependency>  
            <groupId>com.101tec</groupId>  
            <artifactId>zkclient</artifactId>  
            <version>0.8</version>  
        </dependency>  
        
      <!-- junit begin -->     
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

配置文件截图如下:

applicationProvider.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="server-dubbo-demo" />  
    <!-- zookeeper注册中心 -->  
   <!--  <dubbo:registry address="zookeeper://127.0.0.1:2181" />  -->
    
    <!-- 使用multicast广播注册中心暴露服务地址 -->
    <dubbo:registry address="multicast://224.5.6.7:1234" />
    
    <dubbo:protocol name="dubbo" port="20880" />  
      
    <!-- 和本地bean一样实现服务 -->   
    <bean id="demoService" class="serviceImp.DemoServiceImp" />  
    <!-- 向注册中心注册暴漏服务地址,注册服务 -->  
    <dubbo:service interface="service.DemoService"  
        ref="demoService" executes="10" />  
</beans>

接下来编写我们提供服务的接口service :

public interface DemoService {

String sayHello(String name);

String PrintString();
}

接下来编写我们的实现类serviceImp:

import service.DemoService;

public class DemoServiceImp implements DemoService {

@Override
    public String sayHello(String name) {
        System.out.println("start....");
        return "你好  " + name;
    }

@Override
    public String PrintString() {
        return "Hello World";
    }
}

这样就定义好了我们提供的服务,接下来就是启动服务方的服务,进行发布,提供给消费方进行调用

这里本地使用的是junit进行测试:

import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ProviderTest {
    @Test
    public void mainTest() throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationProvider.xml" });
        context.start();
        System.out.println("输入任意按键退出 ~ ");
        System.in.read();
        context.close();
    }
}

文件的目录结构如下:

消费方:

pom.xml主要配置文件内容:

<!-- spring begin -->  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-context</artifactId>  
            <version>4.1.6.RELEASE</version>  
        </dependency>  
        <!-- spring end -->  
 
        <!-- dubbo begin -->  
        <dependency>  
            <groupId>com.alibaba</groupId>  
            <artifactId>dubbo</artifactId>  
            <version>2.5.3</version>  
        </dependency>  
        <!-- dubbo end -->  
 
        <!-- 注册中心zookeeper begin -->  
        <dependency>  
            <groupId>org.apache.zookeeper</groupId>  
            <artifactId>zookeeper</artifactId>  
            <version>3.3.6</version>  
        </dependency>  
        <!-- 注册中心zookeeper end -->  
 
        <!-- log begin -->  
        <dependency>  
            <groupId>log4j</groupId>  
            <artifactId>log4j</artifactId>  
            <version>1.2.15</version>  
            <exclusions>  
                <exclusion>  
                    <groupId>com.sun.jdmk</groupId>  
                    <artifactId>jmxtools</artifactId>  
                </exclusion>  
                <exclusion>  
                    <groupId>com.sun.jmx</groupId>  
                    <artifactId>jmxri</artifactId>  
                </exclusion>  
                <exclusion>  
                    <artifactId>jms</artifactId>  
                    <groupId>javax.jms</groupId>  
                </exclusion>  
                <exclusion>  
                    <artifactId>mail</artifactId>  
                    <groupId>javax.mail</groupId>  
                </exclusion>  
            </exclusions>  
        </dependency>  
        <!-- log end -->  
 
        <!-- other begin -->  
        <dependency>  
            <groupId>com.101tec</groupId>  
            <artifactId>zkclient</artifactId>  
            <version>0.8</version>  
        </dependency>  
        
<-- 消费方这里要进行引用服务方服务,所以需要引入服务方工程jar -->
        <dependency>  
            <groupId>test.mvnproject</groupId>
            <artifactId>dubboProvider</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

<!-- other end -->
          
    <!-- junit begin -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

pom.xml文件截图如下:

applicationConsumer.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="consumer-dubbo-demo" />  
    <!-- zookeeper注册中心 -->  
    <!-- <dubbo:registry address="zookeeper://127.0.0.1:2181" />  -->
    
    <dubbo:registry address="multicast://224.5.6.7:1234" />
      
      <!-- 向注册中心订阅服务 -->  
    <dubbo:reference id="demoService" interface="service.DemoService" />
</beans>

applicationConsumer.xml配置文件如下

消费方这边我们也编写一个测试类调用服务:

import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import service.DemoService;

public class ConsumerTest{
    @Test
    public void consumerTest() {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationConsumer.xml" });
        context.start();

//加载服务方的bean
        DemoService service = (DemoService) context.getBean("demoService");

//这里调用服务方的服务
        System.out.println(service.sayHello("小明"));
        System.out.println(service.PrintString());
        context.close();
    }
}

接下来把我们的demo跑起来,首先发布我们服务方的服务,直接run  as ProviderTest,之后启动消费方的测试类进行服务调用: run  as consumerTest

结果显示:  start.....

你好  小明

Hello  World

到此完成一个demo的创建.

备注:这里主要使用的是多播模式实现服务自动注册与发现:主要用于applicationProvider跟applicationConsumer配置文件中进行注册中心配置:     <dubbo:registry address="multicast://224.5.6.7:1234" />

这里我们也可以使用  zoookeper作为服务中心,进行服务的发布,注册,订阅 ,使用.

说明一点使用zookeper作为注册中心的话,服务方,消费方  配置文件中需要修改注册中心地址:<!-- <dubbo:registry address="zookeeper://127.0.0.1:2181" />  -->

接下来先在本机上启动zookeper注册中心(直接解压到英文路径下直接运行bin/zkServer.cmd)

启动成功截图如下:

运行服务方服务,线程阻塞成功,运行消费方,输出结果:

你好  小明

Hello  World

附上zookeper下载链接:

zookeeper即下即用版:http://download.csdn.net/detail/xingbaozhen1210/9532177

具体参考链接阿里的dubbo官网demo:   http://dubbo.io/#tutorial

文章参考链接:       http://blog.csdn.net/xingbaozhen1210/article/details/51507974

初步认识dubbo--小案例的更多相关文章

  1. DUBBO+Zookeeper在Centos7中本地搭建及小案例

    环境: 1.centos7 2.jdk-7u76-linux-x64.tar.gz 2.tomcat:apache-tomcat-7.0.59.tar.gz 3.zookeeper-3.4.6.tar ...

  2. 机械表小案例之transform的应用

    这个小案例主要是对transform的应用. 时钟的3个表针分别是3个png图片,通过setInterval来让图片转动.时,分,秒的转动角度分别是30,6,6度. 首先,通过new Date函数获取 ...

  3. shell讲解-小案例

    shell讲解-小案例 一.文件拷贝输出检查 下面测试文件拷贝是否正常,如果cp命令并没有拷贝文件myfile到myfile.bak,则打印错误信息.注意错误信息中basename $0打印脚本名.如 ...

  4. [jQuery学习系列六]6-jQuery实际操作小案例

    前言最后在这里po上jQuery的几个小案例. Jquery例子1_占位符使用需求: 点击第一个按钮后 自动去check 后面是否有按钮没有选中, 如有则提示错误消息. <html> &l ...

  5. 02SpringMvc_springmvc快速入门小案例(XML版本)

    这篇文章中,我们要写一个入门案例,去整体了解整个SpringMVC. 先给出整个项目的结构图:

  6. React.js入门小案例

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title&g ...

  7. SqlDependency缓存数据库表小案例

    SqlDependency的简介: SqlDependency是outputcache网页缓存的一个参数,它的作用是指定缓存失效的数据库依赖项,可以具体到数据库和表. SqlDependency能解决 ...

  8. JavaScript apply函数小案例

    //回调函数1 function callback(a,b,c) { alert(a+b+c); } //回调函数2 function callback2(a,b) { alert(a+b); } / ...

  9. Session小案例------完成用户登录

    Session小案例------完成用户登录     在项目开发中,用户登陆功能再平常只是啦,当用户完毕username和password校验后.进入主界面,须要在主界面中显示用户的信息,此时用ses ...

  10. ch1-vuejs基础入门(hw v-bind v-if v-for v-on v-model 应用组件简介 小案例)

    1 hello world 引入vue.min.js 代码: ----2.0+版本 <div id="test"> {{str}} </div> <s ...

随机推荐

  1. 解决apt-get的E: Could not get lock /var/lib/dpkg/lock方法

    使用apt-get进行软件的install或update时,有时会出现以下提示信息: E: Could not get lock /var/lib/dpkg/lock - open (11 Resou ...

  2. 改动图片exif信息

    我们先了解一下EXIF: EXIF能够附加于JPEG.TIFF.RIFF等文件之中.为其添加有关数码相机拍摄信息的内容和索引图或图像处理软件的版本号信息. 全部的JPEG文件以字符串"0xF ...

  3. android推送方式

    本文介绍在Android中实现推送方式的基础知识及相关解决方案.推送功能在手机开发中应用的场景是越来起来了,不说别的,就我们手机上的新闻客户端就时不j时的推送过来新的消息,很方便的阅读最新的新闻信息. ...

  4. GoogLeNet模型的微调

    我从零开始训练了GoogLeNet模型. 但它没有给我带来希望的结果. 作为替代,我想对我的数据集中的GoogLeNet模型进行微调. 有谁知道我应该遵循什么步骤? 采纳答案: 假设你正在尝试做图像分 ...

  5. 妙味云课堂之css:其它知识点汇总

    一. 热区 map 热区.area 点击区域 shape="circle" 圆型,coords="圆心点X.圆心点Y,圆的半径" shape="rec ...

  6. I2S与pcm的区别

    I2S仅仅是PCM的一个分支,接口定义都是一样的, I2S的采样频率一般为44.1KHZ和48KHZ做,PCM采样频率一般为8K,16K.有四组信号: 位时钟信号,同步信号,数据输入,数据输出. I2 ...

  7. valgrind的编译和使用

    ubuntu 平台: valgrind 3.8.1 一. 编译 ./configure --prefix=/home/frank/test/valgrind/PC/local 报错:checking ...

  8. virtualenv下使用matplotlib

    Unable to “import matplotlib.pyplot as plt” in virtualenv   (PyMVPA) SimilarFacedeMacBook-Pro:PyMVPA ...

  9. ../lib//libscsdblog.so: undefined reference to `pthread_atfork'

    代码中遇到这个问题,但是在makefile中已经添加了-lpthread. 最后发现问题时,引入库的顺序,把-lpthread放在最后就可以了.

  10. Java Resource路径小结

    首先一点很重要,Java中不存在标准的相对路径,各种相对路径取资源的方式都是基于某种规则转化为绝对路劲 然后一点也很重要,绝对不要直接使用绝对路径,否则死得很难看 基于以上两点,总结Resource路 ...