Dubbo介绍
Dubbo是:(简言之dubbo就是:一个框架 + 两个方案)
- 一款分布式服务框架
- 高性能和透明化的RPC远程服务调用方案
- SOA服务治理方案
每天为2千多个服务提供大于30亿次访问量支持,并被广泛应用于阿里巴巴集团的各成员站点以及别的公司的业务中。
Dubbo架构图

- Provider: 暴露服务的服务提供方。
- Consumer: 调用远程服务的服务消费方。
- Registry: 服务注册与发现的注册中心。
- Monitor: 统计服务的调用次数和调用时间的监控中心
调用流程:
0. 服务容器负责启动,加载,运行服务提供者。
- 服务提供者在启动时,向注册中心注册自己提供的服务。
- 服务消费者在启动时,向注册中心订阅自己所需的服务。
- 注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。
- 服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。
- 服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。

Dubbo注册中心
对于服务提供方,它需要发布服务,而且由于应用系统的复杂性,服务的数量、类型也不断膨胀;
对于服务消费方,它最关心如何获取到它所需要的服务,而面对复杂的应用系统,需要管理大量的服务调用。
而且,对于服务提供方和服务消费方来说,他们还有可能兼具这两种角色,即既需要提供服务,又需要消费服务。
通过将服务统一管理起来,可以有效地优化内部应用对服务发布/使用的流程和管理。服务注册中心可以通过特定协议来完成服务对外的统一。
Dubbo提供的注册中心有如下几种类型可供选择:
- Multicast注册中心
- Zookeeper注册中心
- Redis注册中心
- Simple注册中心
Dubbo在Zookeeper注册中心存储的目录格式

- 根节点:dubbo
- 一级子节点:提供服务的服务名
- 二级子节点:固定的四个子节点:分别为:consumers、configurators、routers、provider
Dubbo优缺点
优点:
- 透明化的远程方法调用
像调用本地方法一样调用远程方法;只需简单配置,没有任何API侵入。
- 软负载均衡及容错机制
可在内网替代nginx,lvs等硬件负载均衡器。
- 服务注册中心自动注册 & 配置管理
不需要写死服务提供者地址,注册中心基于接口名自动查询提供者ip。
使用类似zookeeper等分布式协调服务作为服务注册中心,可以将绝大部分项目配置移入zookeeper集群。
- 服务接口监控与治理
Dubbo-admin与Dubbo-monitor提供了完善的服务接口管理与监控功能,针对不同应用的不同接口,可以进行多版本,多协议,多注册中心管理。
缺点:
Dubbo的入门示例
Dubbo采用全Spring配置方式,透明化接入应用,对应用没有任何API侵入,只需用Spring加载Dubbo的配置即可。下面以官网给出的DemoService作为示例。
1.定义po
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| package com.lzumetal.dubbolearn.helloworld.entity;
import java.io.Serializable;
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private Long id; private String name; private Integer age;
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public Integer getAge() { return age; }
public void setAge(Integer age) { this.age = age; }
@Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + '}'; } }
|
2.定义服务接口
由于服务的提供者和消费者都依赖于相同的接口,所以强烈推荐把接口定义在一个单独的模块中,提供者模块和消费者模块可以共享这个模块。
1 2 3 4 5 6 7 8 9 10 11 12 13
| package com.lzumetal.dubbolearn.helloworld.service;
import com.lzumetal.dubbolearn.helloworld.entity.User;
public interface DemoService {
String sayHello(String name); User findUserById(Long id);
}
|
实现服务提供者:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| package com.lzumetal.dubbolearn.helloworld.service.impl;
import com.lzumetal.dubbolearn.helloworld.entity.User; import com.lzumetal.dubbolearn.helloworld.service.DemoService;
public class DemoServiceImpl implements DemoService {
@Override public String sayHello(String name) { return "Hello," + name; }
@Override public User findUserById(Long id) { User user = new User(); user.setId(id); user.setName("小明"); user.setAge(14); return user; } }
|
3.配置服务提供者
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| <?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:protocol name="dubbo" port="20880"/>
<dubbo:service interface="com.lzumetal.dubbolearn.helloworld.service.DemoService" ref="demoService"/>
<bean id="demoService" class="com.lzumetal.dubbolearn.helloworld.service.impl.DemoServiceImpl"></bean> </beans>
|
4.启动服务提供者
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| package com.lzumetal.dubbolearn.helloworld.provider;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
public class DemoProvider {
public static void main(String[] args) throws IOException { ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("dubbo-helloworld-provider.xml"); applicationContext.start(); System.in.read(); } }
|
5.配置服务消费者
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| <?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:registry address="multicast://224.5.6.7:1234"/>
<dubbo:reference interface="com.lzumetal.dubbolearn.helloworld.service.DemoService" id="demoService"/>
</beans>
|
6.运行服务消费者
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| package com.lzumetal.dubbolearn.helloworld.consumer;
import com.lzumetal.dubbolearn.helloworld.entity.User; import com.lzumetal.dubbolearn.helloworld.service.DemoService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.support.ClassPathXmlApplicationContext;
public class DemoConsumer {
private static final Logger log = LoggerFactory.getLogger(DemoConsumer.class);
public static void main(String[] args) { ClassPathXmlApplicationContext aplicationContext = new ClassPathXmlApplicationContext("dubbo-helloworld-consumer.xml"); aplicationContext.refresh(); aplicationContext.start();
DemoService demoService = (DemoService) aplicationContext.getBean("demoService");
String hello = demoService.sayHello("Lilei");
log.error("调用 sayHello 方法的结果:{}",hello);
System.out.println("-------------------------------"); User user = demoService.findUserById(3234L); log.error("调用 findUserById 方法的结果:{}",user);
} }
|
运行结果
可以看到控制台会输出如下信息:
1 2 3
| 调用 sayHello 方法的结果:Hello,Lilei ------------------------------- 调用 findUserById 方法的结果:User{id=3234, name='小明', age=14}
|