搭建一个简单的dubbo服务

参考地址:

dubbo官网:http://dubbo.apache.org/zh-cn/docs/user/references/registry/zookeeper.html

博客:http://www.cnblogs.com/lighten/p/6828026.html

以上两个教程经实践发现都有部分谬误,本教程做了一定更正

1.简介

dubbo是一个分布式服务框架,由阿里巴巴的工程师开发,致力于提供高性能和透明化的RPC远程服务调用。可惜的是该项目在2012年之后就没有再更新了,之后由当当基于dubbo开发了dubbox。这里对dubbo的入门构建进行简单的介绍。不涉及dubbo的运行机制,只是搭建过程,方便学习者快速构建项目,运行、熟悉该框架。

dubbo提供了两种构建项目的方法:

  1. 通过Spring容器快速构建,其中还有注解的方式;
  2. 通过直接使用API(不推荐)。以下对其一一说明。

2.spring配置的形式

1.导入maven依赖

<!--如果引入的是2.6.2以下版本,xml文件头引入的xsd地址会报错-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.3</version>
<exclusions>
<exclusion>
<!-- 排除传递spring依赖 -->
<artifactId>spring</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency> <!--下面分别是两种不同的zookeeper客户端实现,选择一种就好-->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.4</version>
</dependency> <dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.12.0</version>
</dependency> <!--后期可能还会用到如下zookeeper客户端jar包依赖,但是目前这个demo不引入也没有问题-->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.3.3</version>
</dependency> <!--spring配置方式,自然需要spring相关的依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.17.RELEASE</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency> <!--打印日志相关-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.4</version>
</dependency>

zkclient和curator

curator和zkclient分别为两种不同的zookeeper实现,两者使用的同时都需要开启先zookeeper注册中心的服务端。(关于如何下载配置并启动zookeeper服务端,参考win10环境下搭建zookeeper伪集群)

在本项目中,它默认是使用了curator实现的,要改成zkclient实现,需要在<dubbo:registry address="zookeeper://127.0.0.1:2181/" client="zkclient" />标签中加入 client="zkclient",并换成zkclient的依赖。

(在dubbo官网中称2.5以上的dubbo版本默认使用zookeeper实现,但是实测结果却相反)

2.定义服务接口(对提供方和调用方都可见)

package dubboXml;

public interface DemoService {
String sayHello(String name);
}

3.服务提供方配置

  1. 实现服务接口

    package dubboXml;
    
    public class DemoServiceImpl implements DemoService{
    public String sayHello(String name) {
    return "Hello " + name;
    }
    }
  2. spring配置

    <?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://dubbo.apache.org/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
    http://dubbo.apache.org/schema/dubbo
    http://dubbo.apache.org/schema/dubbo/dubbo.xsd"> <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="hello-world-app" /> <!-- 使用zookeeper注册中心暴露服务地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" client="zkclient" /> <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880" /> <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="dubboXml.DemoService" ref="demoService" /> <!-- 和本地bean一样实现服务 -->
    <bean id="demoService" class="dubboXml.DemoServiceImpl" />
    </beans>
  3. 读取配置,提供服务

    package dubboXml;
    
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Provider {
    public static void main(String[] args) throws Exception {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"provider.xml"});
    context.start();
    System.in.read(); // 按任意键退出
    }
    }

4.服务调用方配置

  1. spring配置

    <?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://dubbo.apache.org/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
    http://dubbo.apache.org/schema/dubbo
    http://dubbo.apache.org/schema/dubbo/dubbo.xsd"> <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
    <dubbo:application name="consumer-of-helloworld-app" /> <!-- 使用zookeeper注册中心暴露发现服务地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" /> <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
    <dubbo:reference id="demoService" interface="dubboXml.DemoService" />
    </beans>
  2. 读取配置,调用服务

    package dubboXml;
    
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Consumer {
    public static void main(String[] args) throws Exception {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"consumer.xml"});
    context.start();
    DemoService demoService = (DemoService)context.getBean("demoService"); // 获取远程服务代理
    String hello = demoService.sayHello("nihao"); // 执行远程方法
    System.out.println( hello ); // 显示调用结果
    }
    }

最终结果:

5.其他说明

  • 如果涉及传输对象,那么被传输的对象类应该实现serializable接口。因为对象远程传输是以二进制的形式进行的,那么在传输之前需要实现序列化。
  • 我的demo地址:demoXml

我的目录结构:

3.注解配置的方式

maven配置同上。

1.服务接口和传输对象

服务接口

package com.common.server;

import java.util.List;

public interface DemoService {

    public String greet(String name);
public List<User> getUsers(); }

传输对象:实现序列化接口

package com.common.server;

import java.io.Serializable;

public class User implements Serializable {

    private static final long serialVersionUID = 1L;

    private String name;
private int age;
private String sex; public User(String name, int age, String sex) {
this.name = name;
this.age = age;
this.sex = sex;
} public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
} @Override
public String toString() {
return "User [name=" + name + ", age=" + age + ", sex=" + sex + "]";
}
}

2.服务提供方

  1. 实现接口(注意加上相应注解)

    package com.provider;
    
    import com.alibaba.dubbo.config.annotation.Service;
    import com.common.server.DemoService;
    import com.common.server.User; import java.util.ArrayList;
    import java.util.List; /**
    * 实现类上打上service注解:注意,是dubbo的service注解
    */
    @Service
    public class DemoServiceImpl implements DemoService { @Override
    public String greet(String name) {
    return "Hello " + name;
    } @Override
    public List<User> getUsers() {
    List<User> list = new ArrayList<>(); User user1 = new User("张三",10,"男");
    User user2 = new User("李四",11,"女");
    User user3 = new User("王五",12,"男"); list.add(user1);
    list.add(user2);
    list.add(user3); return list;
    } }
  2. xml配置

    (与全xml配置服务的方式相比,这里去掉了bean和<dubbo:service>,使用<dubbo:anotition>取代了,相当于自动扫描并暴露服务)

    <?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="provider" /> <!-- 使用zookeeper注册中心暴露服务地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" client="zkclient" />
    <!--<dubbo:registry address="N/A"/>--> <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880" /> <!-- 注解实现,指向服务提供者实现类的包扫描-->
    <dubbo:annotation package="com.provider" />
    </beans>
  3. 开启服务提供

    package com.provider;
    
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import java.io.IOException;
    
    /**
    * 服务提供者
    */
    public class Provider { public static void main(String[] args) throws IOException {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"provider.xml"});
    context.start();
    System.in.read();
    }
    }

3.服务调用方

  1. 类中注入并使用远程服务代理(为什么不在加载spring文件的类中注入?因为那是入口)

    package com.consumer;
    import com.alibaba.dubbo.config.annotation.Reference;
    import com.common.server.DemoService;
    import org.springframework.stereotype.Component; /**
    * 服务消费者:service是spring的注解
    */
    @Component("anotationConsumer")
    public class AnotationConsumer { /**
    * 远程服务代理,可以和本地bean一样使用demoService
    */
    @Reference(check = false)
    private DemoService demoService; public void print(){
    System.out.println(demoService.greet("张三"));
    System.out.println(demoService.getUsers());
    }
    }
  2. consumer.xml spring配置文件

    <?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"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://code.alibabatech.com/schema/dubbo
    http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <dubbo:application name="consumer"/> <dubbo:registry address="zookeeper://127.0.0.1:2181" client="zkclient" /> <!--指向服务调用者所在的包-->
    <dubbo:annotation package="com.consumer" /> <!-- 配置spring注解包扫描 -->
    <context:component-scan base-package="com.consumer"></context:component-scan> </beans>
  3. 启动服务

    package com.consumer;
    
    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(); AnotationConsumer anotationConsumer = (AnotationConsumer) context.getBean("anotationConsumer");
    anotationConsumer.print();
    }
    }

调用结果:

4.附加信息

  • 无论是哪种配置方式,如果使用了注册中心,那么注册和调用的时候必须先开启注册中心。
  • demo地址:dubboAnotation
  • 还有一种api调用的形式,由于不常用所以这里没有提供教程。如果有兴趣可以查看参考帖。

dubbo学习笔记:快速搭建的更多相关文章

  1. Hadoop学习笔记(10) ——搭建源码学习环境

    Hadoop学习笔记(10) ——搭建源码学习环境 上一章中,我们对整个hadoop的目录及源码目录有了一个初步的了解,接下来计划深入学习一下这头神象作品了.但是看代码用什么,难不成gedit?,单步 ...

  2. Hadoop学习笔记(4) ——搭建开发环境及编写Hello World

    Hadoop学习笔记(4) ——搭建开发环境及编写Hello World 整个Hadoop是基于Java开发的,所以要开发Hadoop相应的程序就得用JAVA.在linux下开发JAVA还数eclip ...

  3. 学习笔记 - 快速傅里叶变换 / 大数A * B的另一种解法

    转: 学习笔记 - 快速傅里叶变换 / 大数A * B的另一种解法 文章目录 前言 ~~Fast Fast TLE~~ 一.FFT是什么? 二.FFT可以干什么? 1.多项式乘法 2.大数乘法 三.F ...

  4. MongoDB学习笔记:快速入门

    MongoDB学习笔记:快速入门   一.MongoDB 简介 MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统.在高负载的情况下,添加更多的节点,可以保证服务器性能.M ...

  5. Dubbo -- 系统学习 笔记 -- 快速启动

    Dubbo -- 系统学习 笔记 -- 目录 快速启动 服务提供者 服务消费者 快速启动 Dubbo采用全Spring配置方式,透明化接入应用,对应用没有任何API侵入,只需用Spring加载Dubb ...

  6. NuGet学习笔记3——搭建属于自己的NuGet服务器

    文章导读 创建NuGetServer Web站点 发布站点到IIS 添加本地站点到包包数据源 在上一篇NuGet学习笔记(2) 使用图形化界面打包自己的类库 中讲解了如何打包自己的类库,接下来进行最重 ...

  7. NuGet学习笔记(3) 搭建属于自己的NuGet服务器

    文章导读 创建NuGetServer Web站点 发布站点到IIS 添加本地站点到包包数据源 在上一篇NuGet学习笔记(2) 使用图形化界面打包自己的类库 中讲解了如何打包自己的类库,接下来进行最重 ...

  8. NuGet学习笔记(3)——搭建属于自己的NuGet服务器(转)

    在上一篇NuGet学习笔记(2) 使用图形化界面打包自己的类库 中讲解了如何打包自己的类库,接下来进行最重要的一步,从零开始搭建属于自己的NuGet服务器,诚然园子里及其它很多地方已经有完全写好的Nu ...

  9. flink学习笔记-快速生成Flink项目

    说明:本文为<Flink大数据项目实战>学习笔记,想通过视频系统学习Flink这个最火爆的大数据计算框架的同学,推荐学习课程: Flink大数据项目实战:http://t.cn/EJtKh ...

随机推荐

  1. 设置div中的div居中显示

    设置div中的div居中显示 方法一. <div class='big'> <div class='small'>box1</div> </div> s ...

  2. Kubernetes 中的pv和pvc

    原文地址:http://www.cnblogs.com/leidaxia/p/6485646.html 持久卷 PersistentVolumes 本文描述了 Kubernetes 中的 Persis ...

  3. kalilinux、parrotsecos没有声音

    Kali Linux系统默认状态下,root用户是无法使用声卡的,也就没有声音.启用的方法如下: (1)在终端执行命令:systemctl --user enable pulseaudio (2)在/ ...

  4. .NET基础 (16)事件

    事件1 请解释事件的基本使用方法2 事件和委托有何联系3 如何设计一个带有很多事件的类型4 用代码表示如下情景:猫叫.老鼠逃跑.主人惊醒 事件1 请解释事件的基本使用方法 事件时一种使对象或类能够提供 ...

  5. Codeforces766B Mahmoud and a Triangle 2017-02-21 13:47 113人阅读 评论(0) 收藏

    B. Mahmoud and a Triangle time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  6. Mixin模式

    Mixin是JavaScript中用的最普遍的模式,几乎所有流行类库都会有Mixin的实现. Mixin是掺合,混合,糅合的意思,即可以就任意一个对象的全部或部分属性拷贝到另一个对象上. 从提供的接口 ...

  7. ADB server didn't ACK问题,连上手机问题(转)

    出现如下情况 ADB server didn't ACK* failed to start daemon * 解决办法: 方法一: (1)查看任务管理器,关闭所有adb.exe,或者运行->cm ...

  8. [转载]灵动思绪EF(Entity FrameWork)

    很久之前就想写这篇文章了,但是由于种种原因,没有将自己学习的EF知识整理成一片文章.今天我就用CodeFirst和ModelFirst两种方式的简单案例将自己学习的EF知识做个总结. 在讲解EF之前, ...

  9. C#基础笔记(第二十天)

    1.复习属性:保护字段的构造函数:初始化对象初始化对象:给对象的每个属性去赋值什么时候会调用构造函数:当我们new的时候面向对象中需要注意的两个关键字this 1.代表当前类的对象 2.调用自己的构造 ...

  10. html开发基础

    1 Doctype Doctype告诉浏览器使用什么样的html或xhtml规范来解析html文档 有和无的区别 BackCompat:标准兼容模式未开启(或叫怪异模式[Quirks mode].混杂 ...