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. Vue 常用属性汇总

    1.Vue实例常用属性 (1)数据 data:Vue 实例的数据对象 components:Vue实例配置局部注册组件 (2)类方法computed:计算属性 watch:侦听属性 filters:过 ...

  2. 理解shell中的atime,mtime,ctime

    所有文件都有3个时间信息,保存在文件系统中 atime (Access time)是文件最后一此读的时间 或者执行文件的时间 mtime (Modified time)是文件最后一次写的时间(是在写入 ...

  3. __block 和__weak 区别及使用

    API Reference对__block变量修饰符有如下几处解释: //A powerful feature of blocks is that they can modify variables ...

  4. CentOS6.5配置PHP CI程序

    步骤: 1.安装CentOS6.5系统:     1.选择PHP+Mysql环境 2.关闭防火墙和SeLinux     1.chkconfig --level 35 iptables off     ...

  5. 8.1.2 绑定Activity和Service

    8.1.2 绑定Activity和Service 2010-06-21 16:57 李宁 中国水利水电出版社 字号:T | T <Android/OPhone开发完全讲义>第8章Andro ...

  6. CentOS统的7个运行级别的含义

    原文: http://blog.csdn.net/liansehai/article/details/45370965 CentOS系统有7个运行级别(runlevel) 运行级别就是操作系统当前正在 ...

  7. oracle 插入记录,字段自动获取当前系统时间(YYYY-MM-DD HH24:MI:SS)

    需求: 插入一条记录,要求自动获取当前日期,并且格式为(YYYY-MM-DD HH24:MI:SS) sql语句: insert into SY_COMM_CONFIG(CONF_ID, S_MTIM ...

  8. Java并发编程(十二)线程安全性的委托

    在组合对象中如果每个组件都已经是线程安全的,是否需要再加一个额外的"线程安全层",需要视情况而定. final可以修饰未复制的属性,只要在静态代码块或者构造函数中赋值了即可. 独立 ...

  9. PHP 几种 序列化/反序列化的方法

    序列化是将变量转换为可保存或传输的字符串的过程:反序列化就是在适当的时候把这个字符串再转化成原来的变量使用.这两个过程结合起来,可以轻松地存储和传输数据,使程序更具维护性. 1. serialize和 ...

  10. JIRA /mnt/server/atlassian-jira-6.3.6-standalone/bin/start-jira.sh

    JIRA 敏捷开发平台部署记录   分类: 敏捷开发   1.1 jira说明 JIRA是Atlassian公司出品的项目与事务跟踪工具,被广泛应用于缺陷跟踪.客户服务.需求收集.流程审批.任务跟踪. ...