1. Dubbo的简介

Apache Dubbo是一款高性能的Java RPC框架。其前身是阿里巴巴公司开源的一个高性能、轻量级的开源Java RPC框架,可以和Spring框架无缝集成。

什么是RPC ?

RPC【Remote Procedure Call】是指远程过程调用,是一种进程间通信方式,他是一种技术的思想,而不是规范。它允许程序调用另一个地址空间(通常是共享网络的另一台机器上)的过程或函数,而不用程序员显式编码这个远程调用的细节。即程序员无论是调用本地的还是远程的函数,本质上编写的调用代码基本相同。

RPC基本原理

根据以上的架构图,做一下解释 :

客户端消费者的一个功能想要去调用服务端提供者的一个功能时,client functions会去找到一个小助手client stub,而这个小助手会跟服务器建立一个sockets连接,然后将需要调用服务器的方法或者参数传给服务器,服务器端的server stub小助手就会收到这些信息,就会知道客户端要调用服务端的哪个方法以及需要的参数信息也会通过网络传输进服务器端。服务端的小助手就会帮调用需求的方法,获取返回值后通过网络传输给小助手,小助手交给客户端的功能代码。

总结 :其实RPC核心就是两个A、B服务器之间建立连接,并进行通信。

Dubbo官网地址 : http://dubbo.apache.org/en-us/

Dubbo提供了三大核心能力 : 面向接口的远程方法调用,智能容错和负载均衡,以及服务自动注册和发现。

2.Dubbo的架构说明

架构图 :

服务提供者(Provider):暴露服务的服务提供方,服务提供者在启动时,向注册中心注册自己提供的服务。

服务消费者(Consumer): 调用远程服务的服务消费方,服务消费者在启动时,向注册中心订阅自己所需的服务。服务消费者,从服务提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。

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

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

调用关系说明 :

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

3. Dubbo的Hello World

Dubbo作为一个RPC框架,其最核心的功能就是要实现跨网络的远程调用。现在要创建两个应用,一个作为服务的提供者,一个作为服务的消费者。通过Dubbo来实现服务消费者远程调用服务提供者的方法。

需要创建两个服务模块进行测试 :

模块 功能
订单服务web模块 创建订单等
用户服务service模块 查询用户地址等

测试预期结果:

​ 订单服务模块在A服务器,用户服务模块在B服务器,当订单模块需要下订单时,会生成用户地址信息,达到A可以远程调用B的功能。

项目目录结构图 :

注意:看上面的目录结构图中并没有实体类与接口,那是因为已经将其抽取出来做成一个Module,只需要通过Maven去install成jar,然后通过导入依赖的形式写入module即可。

然后每一个模块都加入相关依赖即可,

1
2
3
4
5
<dependency>
<groupId>com.self</groupId>
<artifactId>interface-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>

3.1 公共代码块

创建一个空的工程,然后将src目录删除,在新建一个Module :interface-api ,

实体类 :

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package com.self.dubbo.bean;

import java.io.Serializable;

public class UserAddress implements Serializable {

private Integer id;
private String userAddress; //用户地址
private String userId; //用于ID
private String consignee; //收货人
private String phoneNum; //电话号码
private String isDefault; //是否为默认地址 Y-是 N-否

public UserAddress() {
}

public UserAddress(Integer id, String userAddress, String userId, String consignee, String phoneNum, String isDefault) {
this.id = id;
this.userAddress = userAddress;
this.userId = userId;
this.consignee = consignee;
this.phoneNum = phoneNum;
this.isDefault = isDefault;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getUserAddress() {
return userAddress;
}

public void setUserAddress(String userAddress) {
this.userAddress = userAddress;
}

public String getUserId() {
return userId;
}

public void setUserId(String userId) {
this.userId = userId;
}

public String getConsignee() {
return consignee;
}

public void setConsignee(String consignee) {
this.consignee = consignee;
}

public String getPhoneNum() {
return phoneNum;
}

public void setPhoneNum(String phoneNum) {
this.phoneNum = phoneNum;
}

public String getIsDefault() {
return isDefault;
}

public void setIsDefault(String isDefault) {
this.isDefault = isDefault;
}

@Override
public String toString() {
return "UserAddress{" +
"id=" + id +
", userAddress='" + userAddress + '\'' +
", userId='" + userId + '\'' +
", consignee='" + consignee + '\'' +
", phoneNum='" + phoneNum + '\'' +
", isDefault='" + isDefault + '\'' +
'}';
}
}

业务层接口 :

订单 :

1
2
3
4
5
6
7
8
9
10
package com.self.dubbo.service;

public interface OrderService {

/**
* 根据用户ID初始化订单
* @param userId
*/
void initOrder(String userId);
}

用户 :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.self.dubbo.service;

import com.self.dubbo.bean.UserAddress;

import java.util.List;

/**
* 用户服务接口
*/
public interface UserService {

/**
* 根据ID获取所有收货地址
* @param userId
* @return
*/
List<UserAddress> getUserAddressList(String userId);
}

3.2 服务提供者

第一步: 再新建一个Module : user-service-provider,在pom.xml文件中导入坐标 :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<dependencies>

<!-- 导入Dubbo依赖 -->
<!-- https://mvnrepository.com/artifact/com.alibaba/dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.2</version>
</dependency>
<!-- 注册中心使用的是Zookeeper,引入Zookeeper客户端 -->
<!-- https://mvnrepository.com/artifact/org.apache.curator/curator-framework -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.0.1</version>
</dependency>

<dependency>
<groupId>com.self</groupId>
<artifactId>interface-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>

第二步 : 编写用户接口UserService的实现类 :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.self.dubbo.service.impl;

import com.self.dubbo.bean.UserAddress;
import com.self.dubbo.service.UserService;

import java.util.Arrays;
import java.util.List;

public class UserServiceImpl implements UserService {
@Override
public List<UserAddress> getUserAddressList(String userId) {

UserAddress address1 = new UserAddress(1,"广西省防城港","1","张三","123456","Y");
UserAddress address2 = new UserAddress(2,"广西省桂林市","1","李四","456789","Y");


return Arrays.asList(address1,address2);
}
}

第三步: 编写Spring与Dubbo整合的provider.xml配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

<!-- 1.指定当前服务/应用的名称 -->
<dubbo:application name="user-service-provider"></dubbo:application>

<!-- 2.指定注册中心的位置 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry>

<!-- 3.指定服务消费者与服务提供者的通信规则(通信协议?通信端口) -->
<dubbo:protocol name="dubbo" port="20880"></dubbo:protocol>

<!-- 4.暴露服务(暴露的接口全类名,接口的真正实现对象用ref指定) -->
<dubbo:service interface="com.self.dubbo.service.UserService" ref="userService"></dubbo:service>

<!-- 5.服务的真正实现对象 -->
<bean id="userService" class="com.self.dubbo.service.impl.UserServiceImpl"></bean>

</beans>

第四步 : 编写启动服务提供者的Main方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.self.dubbo;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

public class MainApplication {

public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("provider.xml");
ioc.start();

System.in.read();

}
}

运行Main方法后,打开dubbo-admin管理控制台就会发现服务提供者已经注册进Zookeeper

3.3 服务消费者

第一步: 再新建一个Module :order-service-consumer ,在pom.xml文件中导入坐标 :

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
<dependencies>

<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>

<!-- 导入Dubbo依赖 -->
<!-- https://mvnrepository.com/artifact/com.alibaba/dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.2</version>
</dependency>
<!-- 注册中心使用的是Zookeeper,引入Zookeeper客户端 -->
<!-- https://mvnrepository.com/artifact/org.apache.curator/curator-framework -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.0.1</version>
</dependency>


<dependency>
<groupId>com.self</groupId>
<artifactId>interface-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>

第二步 : 编写订单接口OrderService的实现类

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
package com.self.dubbo.service.impl;

import com.self.dubbo.bean.UserAddress;
import com.self.dubbo.service.OrderService;
import com.self.dubbo.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* 使用Dubbo实现RPC的步骤 :
* 1. 将服务提供者注册到服务注册中心(暴露服务)
* 1). 导入Dubbo依赖(2.6.2),操作Zookeeper的客户端(curator)
* 2). 配置服务提供者
*
* 2. 让服务消费者去注册中心订阅服务提供者的服务地址
*/
@Service("orderService")
public class OrderServiceImpl implements OrderService {

@Autowired(required = true)
private UserService userService;

@Override
public void initOrder(String userId) {

System.out.println("用户ID : " + userId);

//1.查询用户的收货地址
List<UserAddress> addressList = userService.getUserAddressList(userId);

for (UserAddress address : addressList) {
System.out.println(address.getUserAddress());
}

}
}

第三步: 编写Spring与Dubbo整合的consumer.xml配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://dubbo.apache.org/schema/dubbo
http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

<context:component-scan base-package="com.self.dubbo.service.impl"></context:component-scan>

<!-- 1. 指定当前应用的名称 -->
<dubbo:application name="order-service-consumer"></dubbo:application>

<!-- 2.指定注册中心的位置 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry>

<!-- 3. 声明需要调用的远程服务接口,生成远程服务代理 -->
<dubbo:reference id="userService" interface="com.self.dubbo.service.UserService" ></dubbo:reference>

</beans>

第四步 : 编写启动服务消费者的Main方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.self.dubbo;

import com.self.dubbo.service.OrderService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

public class MainApplication {

public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("consumer.xml");

OrderService orderService = ioc.getBean("orderService", OrderService.class);

orderService.initOrder("1");
System.out.println("调用完成...");
System.in.read();//阻塞
}
}

运行Main方法后,就会发现idea控制台有输出结果,说明订单模块已经成功调用用户模块:

打开dubbo-admin管理控制台 :