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 ...
随机推荐
- hadoop手工移块
1.关于磁盘使用策略,介绍参考http://www.it165.net/admin/html/201410/3860.html 在hadoop2.0中,datanode数据副本存放磁盘选择策略有两种方 ...
- 【codeforces 801B】Valued Keys
[题目链接]:http://codeforces.com/contest/801/problem/B [题意] 定义一个对两个字符串x,y的f(x,y)函数; 返回的是一个字符串; 这个返回的字符串的 ...
- 【codeforces 510C】Fox And Names
[题目链接]:http://codeforces.com/contest/510/problem/C [题意] 给你n个字符串; 问你要怎么修改字典序; (即原本是a,b,c..z现在你可以修改每个字 ...
- IDEA 工具使用报错总结
读前语:此文章仅给非入门级观看 1.使用Debug 无法运行,而使用Run 则正常启动,报错代码如下 1 Error running 'jx_web': Unable to open debugge ...
- noip模拟赛 公交车
题目描述LYK在玩一个游戏.有k群小怪兽想乘坐公交车.第i群小怪兽想从xi出发乘坐公交车到yi.但公交车的容量只有M,而且这辆公交车只会从1号点行驶到n号点.LYK想让小怪兽们尽可能的到达自己想去的地 ...
- ESdata节点脱离集群,系统日志报120秒超时
ES信息:Centos7.2,ES6.2.2 , MASTER:16核/128G物理 * 3 ,DATA:16核/128G/12块HDD6T组成RAID0 * 40, JVM开了30G, 目前只有一 ...
- poj 1698
题意:爱丽丝喜欢拍电影,现在正好有N个公司找她拍电影,每部电影都指定在每周的星期几拍,要用D天拍完电影,最迟M个星期要拍完.问爱丽丝能不能拍完所有电影. 分析:350天各为一个顶点,每个顶点都有且只有 ...
- bootstrap-table设置height后表头与内容无法对齐的问题
bootstrap-table项目官网:https://github.com/wenzhixin/bootstrap-table bootstrap-table各版本下载:https://github ...
- iOS: 两句话给UILabel添加下划线
1. 将UILabel控件的Text属性设为Attributed 2. 在viewDidLoad方法中添加如下语句: NSDictionary *underlineAttribute = @{NSUn ...
- iOS:解决pod的Insecure world writable dir问题
当我们运行pod setup的命令的时候,有时候会碰到这个警告: /Library/Ruby/Gems/2.0.0/gems/cocoapods-0.33.1/lib/cocoapods/execut ...