dubbo介绍及实战
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介绍及实战的更多相关文章
- android MVP模式介绍与实战
android MVP模式介绍与实战 描述 MVP模式是什么?MVP 是从经典的模式MVC演变而来,它们的基本思想有相通的地方:Controller/Presenter负责逻辑的处理,Model提供数 ...
- Jenkins高级用法 - Jenkinsfile 介绍及实战经验
系列目录 1.Jenkins 安装 2.Jenkins 集群 3.Jenkins 持续集成 - ASP.NET Core 持续集成(Docker&自由风格&Jenkinsfile) 4 ...
- dubbo系列一:dubbo介绍、dubbo架构、dubbo的官网入门使用demo
一.dubbo介绍 Dubbo是阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的RPC实现服务的输出和输入功能,可以和Spring框架无缝集成.简单地说,dubbo是一个基于Spri ...
- dubbo介绍及其使用案例
dubbo介绍及其使用案例 1. Dubbo是什么? Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案.简单的说,dubbo就是个服务框架,如果 ...
- Dubbo官网实战使用技巧
原文链接:Dubbo官网实战使用技巧 1.启动时检查: 我们检查依赖的服务是否启动,可利用下面三个属性,优先级从左到右逐渐降低. 如果服务不是强依赖,或者说服务之间可能存在死循环依赖,我们应该将 ch ...
- 高性能优秀的服务框架-dubbo介绍
先来了解一下这些年架构的变化,下面的故事是我编的.... "传统架构":很多年前,刚学完JavaWeb开发的我凭借一人之力就开发了一个网站,网站 所有的功能和应用都集中在一起,方便 ...
- Dubbo入门到实战
前沿:在当下流行的分布式架构中Dubbo是非常流行的一门技术,借着这几天有空学习学习,并在后面的项目中进行实战,为后面的分布式项目做铺垫. Dubbox简介 Dubbox 是一个分布式服务框架,其前身 ...
- 深度学习框架Keras介绍及实战
Keras 是一个用 Python 编写的高级神经网络 API,它能够以 TensorFlow, CNTK, 或者 Theano 作为后端运行.Keras 的开发重点是支持快速的实验.能够以最小的时延 ...
- Dubbo安装及其实战1
一.Dubbo安装 (1)安装zk和tomcat yum 安装tomcat 默认路径为 /usr/share/tomcat zookeeper 我这里采用的是使用zookeeper管理的.所以要安装z ...
随机推荐
- 第十五节:pandas之concat()级联
Pandas 提供了concat()函数可以轻松的将Series.DataFrame对象进行合并在一起. pandas.concat(obj , axis=0 , join="inner&q ...
- GeoTrust 企业(OV)型 通配符(Wildcard) SSL证书
GeoTrust 企业(OV)型 通配符(Wildcard)SSL证书(GeoTrust True BusinessID Wildcard SSL Certificates),支持通配符(Wild ...
- hdu 4975 最大流解决行列和求矩阵问题,用到矩阵dp优化
//刚开始乱搞. //网络流求解,如果最大流=所有元素的和则有解:利用残留网络判断是否唯一, //方法有两种,第一种是深搜看看是否存在正边权的环,见上一篇4888 //至少四个点构成的环,第二种是用矩 ...
- windows 2013(codevs 1695)
题目描述 Description 话说adamyi编的Windows 2013超时了(- -!),所以他不得不在自己家门口亲眼见证这个电影般的场景.虽然他不想错过这个美妙的时刻,但是他的肚子一再抗议, ...
- asp.net--webservice--安全验证SoapHeader
服务端的设置 客户端设置,需要生成一个服务端的代理,调用服务端的方法
- Java多线程之如何确定线程数
关于多线程的线程数的确定,最近研读过几篇paper,在此做一下笔记,方便使用时翻看. 1.<Java 虚拟机并发编程>中介绍 就是说:线程数 = CPU的核心数 * (1 - 阻塞系数) ...
- 【Unity3D】 KeyCode 键码
Key codes returned by Event.keyCode. These map directly to a physical key on the keyboard. KeyCode是由 ...
- MSP430WARE++的使用2:RSP1 driver的调用方法
MSP430WARE是一套基于C++语言的开源的MSP430层次化软件架构,支持多种外设.本文将介绍雷达測速芯片RSP1驱动程序的调用方法. 1.硬件原理图 採用下图所看到的 ...
- runloop简单介绍
runloop是iOS底层机制中保持我们的程序一直运行的机制.他可以让线程一直循环不退出.而在我们正常的编程中.线程其实是线性的,当线程处理完我们的代码以后就自动退出了.runloop就是保证我们的应 ...
- codeforces Looksery Cup 2015 D. Haar Features
The first algorithm for detecting a face on the image working in realtime was developed by Paul Viol ...