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 ...
随机推荐
- 【codeforces 801D】Volatile Kite
[题目链接]:http://codeforces.com/contest/801/problem/D [题意] 给你一个凸多边形的n个点; 然后允许你将每个点移动到距离不超过D的范围内; 要求无论如何 ...
- JavaWeb+MySql分页封装
前段时间因为需要所以写一个JavaWeb+MySql的分页封装类,附上代码.技术有限写得不好请多多指教. 1.首先贴上Eneity类 package com.zx.pageUtil; import j ...
- Java和JS MD5加密-附盐值加密demo
JAVA和JS的MD5加密 经过测试:字母和数据好使,中文不好使. 源码如下: ** * 类MD5Util.java的实现描述: * */public class MD5Util { // 获得MD5 ...
- mongodb之安装
前言 系统环境是CentOS,linux只支持64位版本 yum源安装 rpm包说明 mongodb-org-server 包含mongod进程,关联配置,初始化脚本mongodb-org-mongo ...
- PHP array_count_values()
定义和用法 array_count_values() 函数用于统计数组中所有值出现的次数. 本函数返回一个数组,其元素的键(索引)是原数组的值,元素的值是该值在原数组中出现的次数. 语法 array_ ...
- org.hibernate.PropertyValueException: not-null property references a null or transient value: model.
今天在写一个SSH整合的项目时,首先将数据库操作部分单独分离出来,写完后使用Junit进行測试,经过測试.发现没有不论什么问题,对数据库中的内容进行增删改查没有问题,可是将他整合到SSH的项目中时,报 ...
- [RxJS] Get current value out of Subject (BehaviorSubject)
When you want to get the current value of a subject, you need to switch BehaviorSubject, it always e ...
- [RxJS] exhaustMap vs switchMap vs concatMap
exhaustMap: It drop the outter observable, just return the inner observable, and it waits until prev ...
- CF # 296 C Glass Carving (并查集 或者 multiset)
C. Glass Carving time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- Android Back键和Home键的区别
back键 Android的程序无需刻意的去退出,当你一按下手机的back键的时候,系统会默认调用程序栈中最上层Activity的Destroy()方法来,销毁当前Activity.当此Activit ...