1. dubbo是什么?

Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案。
其核心部分包含:

  • 远程通讯: 提供对多种基于长连接的NIO框架抽象封装,包括多种线程模型,序列化,以及“请求-响应”模式的信息交换方式。
  • 集群容错: 提供基于接口方法的透明远程过程调用,包括多协议支持,以及软负载均衡,失败容错,地址路由,动态配置等集群支持。
  • 自动发现: 基于注册中心目录服务,使服务消费方能动态的查找服务提供方,使地址透明,使服务提供方可以平滑增加或减少机器。

2. dubbo的架构

节点角色说明:

Provider: 暴露服务的服务提供方。

Consumer: 调用远程服务的服务消费方。

Registry: 服务注册与发现的注册中心。

Monitor: 统计服务的调用次调和调用时间的监控中心。

Container: 服务运行容器。

调用关系说明:

0. 服务容器负责启动,加载,运行服务提供者。

1. 服务提供者在启动时,向注册中心注册自己提供的服务。

2. 服务消费者在启动时,向注册中心订阅自己所需的服务。

3. 注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。

4. 服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。

5. 服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。

3. dubbo使用方法

Dubbo采用全Spring配置方式,透明化接入应用,对应用没有任何API侵入,只需用Spring加载Dubbo的配置即可,Dubbo基于Spring的Schema扩展进行加载。如果不想使用Spring配置,而希望通过API的方式进行调用(不推荐)。

服务提供者

1. 定义服务接口: (该接口需单独打包,在服务提供方和消费方共享)
package m.dubbo.demo.api;

public interface DemoService {
String sayHello();
}
2.在服务提供方实现接口:
package m.dubbo.demo.provider;

import m.dubbo.demo.api.DemoService;

public class DemoServiceImpl implements DemoService{

    @Override
public String sayHello() {
return "Hello, World!"; }
}
3. 用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"
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="demo-provider"/>
<dubbo:registry address="multicast://224.5.6.7:1234"></dubbo:registry>
<dubbo:protocol name="dubbo" port="20880" host="10.0.0.5"></dubbo:protocol>
<bean id="userService" class="m.dubbo.demo.provider.UserServiceImpl"/>
<bean id="demoService" class="m.dubbo.demo.provider.DemoServiceImpl"/>
<dubbo:service interface="m.dubbo.demo.api.UserService" ref="userService"></dubbo:service>
<dubbo:service interface="m.dubbo.demo.api.DemoService" ref="demoService"></dubbo:service>
</beans>
注意:如果服务提供者所在机器是有多网卡的,需要设置dubbo:protocol的host属性,不然服务消费者就有可能条用不到(报错误no provider available for the service....)。
4. 加载Spring配置,启动服务:
package m.dubbo.demo.provider;

import java.io.IOException;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class DemoProvider {
public static void main(String[] args) throws IOException{
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:*.xml");
context.start();
System.out.println("服务已经启动...");
System.in.read();
}
}

消费者

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://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="demo-consumer"></dubbo:application>
<dubbo:registry address="multicast://224.5.6.7:1234"></dubbo:registry>
<dubbo:reference check="false" id="userService" interface="m.dubbo.demo.api.UserService"/>
<dubbo:reference check="false" id="demoService" interface="m.dubbo.demo.api.DemoService"/>
</beans>
1. 加载Spring配置,并调用远程服务:
package m.dubbo.demo.consumer;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import m.dubbo.demo.api.DemoService;
import m.dubbo.demo.api.UserService; public class DemoConsumer { public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:*.xml");
context.start();
UserService userService = context.getBean("userService", UserService.class);
System.out.println(userService.getUser(1L));
DemoService demoService = context.getBean("demoService", DemoService.class);
System.out.println(demoService.sayHello());
} }

以下为基于gradle创建的项目源代码https://files.cnblogs.com/files/jmbkeyes/dubbo_demo.zip

dubbo介绍及实战的更多相关文章

  1. android MVP模式介绍与实战

    android MVP模式介绍与实战 描述 MVP模式是什么?MVP 是从经典的模式MVC演变而来,它们的基本思想有相通的地方:Controller/Presenter负责逻辑的处理,Model提供数 ...

  2. Jenkins高级用法 - Jenkinsfile 介绍及实战经验

    系列目录 1.Jenkins 安装 2.Jenkins 集群 3.Jenkins 持续集成 - ASP.NET Core 持续集成(Docker&自由风格&Jenkinsfile) 4 ...

  3. dubbo系列一:dubbo介绍、dubbo架构、dubbo的官网入门使用demo

    一.dubbo介绍 Dubbo是阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的RPC实现服务的输出和输入功能,可以和Spring框架无缝集成.简单地说,dubbo是一个基于Spri ...

  4. dubbo介绍及其使用案例

    dubbo介绍及其使用案例 1. Dubbo是什么? Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案.简单的说,dubbo就是个服务框架,如果 ...

  5. Dubbo官网实战使用技巧

    原文链接:Dubbo官网实战使用技巧 1.启动时检查: 我们检查依赖的服务是否启动,可利用下面三个属性,优先级从左到右逐渐降低. 如果服务不是强依赖,或者说服务之间可能存在死循环依赖,我们应该将 ch ...

  6. 高性能优秀的服务框架-dubbo介绍

    先来了解一下这些年架构的变化,下面的故事是我编的.... "传统架构":很多年前,刚学完JavaWeb开发的我凭借一人之力就开发了一个网站,网站 所有的功能和应用都集中在一起,方便 ...

  7. Dubbo入门到实战

    前沿:在当下流行的分布式架构中Dubbo是非常流行的一门技术,借着这几天有空学习学习,并在后面的项目中进行实战,为后面的分布式项目做铺垫. Dubbox简介 Dubbox 是一个分布式服务框架,其前身 ...

  8. 深度学习框架Keras介绍及实战

    Keras 是一个用 Python 编写的高级神经网络 API,它能够以 TensorFlow, CNTK, 或者 Theano 作为后端运行.Keras 的开发重点是支持快速的实验.能够以最小的时延 ...

  9. Dubbo安装及其实战1

    一.Dubbo安装 (1)安装zk和tomcat yum 安装tomcat 默认路径为 /usr/share/tomcat zookeeper 我这里采用的是使用zookeeper管理的.所以要安装z ...

随机推荐

  1. Emacs的undo与redo

    在Emacs的手册16.1节中有这样一句话, Any command other than an undo command breaks the sequence of undo commands. ...

  2. 手机访问pc版网站自动跳转为手机版页面

    1.PC版首页</head>标签前加上以下脚本 <script src="/tools/browser_redirect.ashx"></script ...

  3. 调用ms自带的合成语音TTS

    通过import of Component导入封装TTS引擎,然后选择: 最后调用: MyVoce := CoSpVoice.Create; MyVoce.Pause;//暂停 MyVoce.Stat ...

  4. codeforeces近日题目小结

    题目源自codeforeces的三场contest contest/1043+1055+1076 目前都是solved 6/7,都差了最后一题 简单题: contest/1043/E: 先不考虑m个限 ...

  5. RestEasy用户指南---第6章.@QueryParam

    转载说明出处:http://blog.csdn.net/nndtdx/article/details/6870391 原文地址 http://docs.jboss.org/resteasy/docs/ ...

  6. DOM对象属性(property)与HTML标签特性(attribute)

    HTML中property与attribute是极易混淆的两个概念.大多数时候这两个单词都翻译为"属性",为了区分二者,一般将property翻译为"属性",a ...

  7. 认识GIT之入门

    前言 GIT是非常优秀的源代码版本管理工具,经过几年的发展,已经变得非常成熟以及流行,不同于其他的源代码管理系统,值得使用.GIT官网下载在线安装包,经常会中途退出,很有可能的原因是被墙了,所以建议使 ...

  8. JVM内存管理和垃圾回收机制介绍

    http://backend.blog.163.com/blog/static/20229412620128233285220/     内存管理和垃圾回收机制是JVM最核心的两个组成部分,对其内部实 ...

  9. 最小生成树模板(poj3625)

    Building Roads Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9360   Accepted: 2690 De ...

  10. OllyDbg 使用笔记 (七)

    OllyDbg 使用笔记 (七) 參考 书:<加密与解密> 视频:小甲鱼 解密系列 视频 演示样例程序下载:http://pan.baidu.com/s/1gvwlS 暴力破解 观察这个程 ...