分布式基础理论
什么是分布式系统
《分布式系统原理与范型》定义:“分布式系统是若干独立计算机的集合,这些计算机合并起来对于用户来说就像单个相关系统”
分布式系统(distributed system)是建立在网络之上的软件系统。
随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,亟需一个治理系统确保架构有条不紊的演进。
发展演变

单一应用架构
当网站流量很小时,只需一个应用,将所有功能都部署在一起,以减少部署节点和成本。此时,用于简化增删改查工作量的数据访问框架(ORM)是关键。

适用于小型网站,小型管理系统,将所有功能都部署到一个功能里,简单易用。
缺点: 1.性能扩展比较难 2.协同开发问题 3.不利于升级维护
垂直应用架构
当访问量逐渐增大,单一应用增加机器带来的加速度越来越小,将应用拆成互不相干的几个应用,以提升效率。此时,用于加速前端页面开发的Web框架(MVC)是关键。

通过切分业务来实现各个模块独立部署,降低了维护和部署的难度,团队各司其职更易管理,性能扩展也更方便,更有针对性。
缺点: 公用模块无法重复利用,开发性的浪费
分布式应用架构
当垂直应用越来越多,应用之间交互不可避免,将核心业务抽取出来,作为独立的服务,逐渐形成稳定的服务中心,使前端应用能更快速的响应多变的市场需求。此时,用于提高业务复用及整合的**分布式服务框架(RPC)**是关键。

流动计算架构
当服务越来越多,容量的评估,小服务资源的浪费等问题逐渐显现,此时需增加一个调度中心基于访问压力实时管理集群容量,提高集群利用率。此时,用于**提高机器利用率的资源调度和治理中心(SOA)[ Service Oriented Architecture]**是关键。

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

时序图

影响rpc框架的性能有两点
- 能否快速在多态服务器之间建立连接,通讯效率
- 序列化和反序列化速度快慢,eg 二进制流>json>xml快
RPC两个核心模块:通讯,序列化。
dubbo核心概念
http://dubbo.apache.org/
简介
Apache Dubbo (incubating) |ˈdʌbəʊ| 是一款高性能、轻量级的开源Java RPC框架,它提供了三大核心能力:面向接口的远程方法调用,智能容错和负载均衡,以及服务自动注册和发现。

设计架构

角色说明
- 服务提供者(Provider):暴露服务的服务提供方,服务提供者在启动时,向注册中心注册自己提供的服务。
- 服务消费者(Consumer): 调用远程服务的服务消费方,服务消费者在启动时,向注册中心订阅自己所需的服务,服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。
- 注册中心(Registry):注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者
- 监控中心(Monitor):服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心
调用关系说明
- 服务容器负责启动,加载,运行服务提供者。
- 服务提供者在启动时,向注册中心注册自己提供的服务。
- 服务消费者在启动时,向注册中心订阅自己所需的服务。
- 注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。
- 服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。
- 服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。
dubbo环境搭建
注册中心zk
zk安装
版本:3.5.8
dubbo管理控制台
dubbo管理控制台安装
dubbo-helloworld
需求
某个电商系统,订单服务需要调用用户服务获取某个用户的所有地址;
我们现在 需要创建两个服务模块进行测试
模块 |
功能 |
订单服务web模块 |
创建订单等 |
用户服务service模块 |
查询用户地址等 |
测试预期结果:
订单服务web模块在A服务器,用户服务模块在B服务器,A可以远程调用B的功能。
工程架构
根据 dubbo《服务化最佳实践》
分包
建议将服务接口,服务模型,服务异常等均放在 API 包中,因为服务模型及异常也是 API 的一部分,同时,这样做也符合分包原则:重用发布等价原则(REP),共同重用原则(CRP)。
如果需要,也可以考虑在 API 包中放置一份 spring 的引用配置,这样使用方,只需在 spring 加载过程中引用此配置即可,配置建议放在模块的包目录下,以免冲突,如:com/alibaba/china/xxx/dubbo-reference.xml。
粒度
服务接口尽可能大粒度,每个服务方法应代表一个功能,而不是某功能的一个步骤,否则将面临分布式事务问题,Dubbo 暂未提供分布式事务支持。
服务接口建议以业务场景为单位划分,并对相近业务做抽象,防止接口数量爆炸。
不建议使用过于抽象的通用接口,如:Map query(Map),这样的接口没有明确语义,会给后期维护带来不便。
工程目录
gmall:
- gmall-interface 公共接口层
- gmall-order-web 订单web模块
- gmall-user 用户服务模块
创建模块
gmall-interface
model、service、exception…
UserAddress
1
2
3
4
5
6
7
8
9
10
11
12
13
|
@NoArgsConstructor
@AllArgsConstructor
@Data
public class UserAddress implements Serializable {
private static final long serialVersionUID = 2063964547378314522L;
private Integer id;
private String userAddress;
private String userId;
private String consignee;
private String phoneNum;
private String isDefault;
}
|
UserService
1
2
3
|
public interface UserService {
List<UserAddress> getUserAddressList(String userId);
}
|
gmall-user
pom
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>
<dependency>
<groupId>com.eh</groupId>
<artifactId>gmall-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>29.0-jre</version>
</dependency>
<!--
dubbo 2.6以前的版本引入zkclient操作zookeeper
dubbo 2.6及以后的版本引入curator操作zookeeper
下面两个zk客户端根据dubbo版本2选1即可
-->
<!-- 引入dubbo -->
<!-- https://mvnrepository.com/artifact/com.alibaba/dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.2</version>
</dependency>
<!-- 注册中心使用的是zookeeper,引入操作zookeeper的客户端端 -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.12.0</version>
</dependency>
</dependencies>
|
配置文件
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
25
|
<?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://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/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> -->
<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"></dubbo:registry>
<!-- 3、指定通信规则(通信协议?通信端口) -->
<dubbo:protocol name="dubbo" port="20882"></dubbo:protocol>
<!-- 4、暴露服务 ref:指向服务的真正的实现对象 -->
<dubbo:service interface="com.eh.gmall.service.UserService" ref="userServiceImpl"></dubbo:service>
<!-- 服务的实现 -->
<bean id="userServiceImpl" class="com.eh.user.service.UserServiceImpl"></bean>
</beans>
|
业务类
UserServiceImpl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
package com.eh.user.service;
import com.eh.gmall.model.UserAddress;
import com.eh.gmall.service.UserService;
import com.google.common.collect.Lists;
import java.util.List;
public class UserServiceImpl implements UserService {
@Override
public List<UserAddress> getUserAddressList(String userId) {
UserAddress address1 = new UserAddress(1, "北京市昌平区宏福科技园综合楼3层", "1", "李老师", "010-56253825", "Y");
UserAddress address2 = new UserAddress(2, "深圳市宝安区西部硅谷大厦B座3层(深圳分校)", "1", "王老师", "010-56253825", "N");
return Lists.newArrayList(address1, address2);
}
}
|
启动类
1
2
3
4
5
6
7
8
9
10
11
12
13
|
package com.eh.user.service.com.eh.user;
import lombok.SneakyThrows;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApplication {
@SneakyThrows
public static void main(String[] args) {
ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("provider.xml");
ioc.start();
System.in.read();
}
}
|
启动主程序,可以在dubbo管理界面看到服务提供者注册进注册中心了

gmall-order-web
pom
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<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的客户端端 -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.12.0</version>
</dependency>
<dependency>
<groupId>com.eh</groupId>
<artifactId>gmall-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
|
配置文件
consumer.xml
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:context="http://www.springframework.org/schema/context"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<context:component-scan base-package="com.eh.order.service"></context:component-scan>
<dubbo:application name="order-service-consumer"></dubbo:application>
<dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry>
<!--声明需要调用的远程服务的接口;生成远程服务代理 -->
<dubbo:reference interface="com.eh.gmall.service.UserService" id="userService"></dubbo:reference>
</beans>
|
业务类
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
|
package com.eh.order.service;
import com.eh.gmall.model.UserAddress;
import com.eh.gmall.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class OrderService {
@Autowired
private UserService userService;
/**
* 初始化订单,查询用户的所有地址并返回
*
* @param userId
* @return
*/
public List<UserAddress> initOrder(String userId) {
return userService.getUserAddressList(userId);
}
}
|
主程序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
package com.eh.order;
import com.eh.gmall.model.UserAddress;
import com.eh.order.service.OrderService;
import lombok.SneakyThrows;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
public class MainApplication {
@SneakyThrows
public static void main(String[] args) {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("consumer.xml");
OrderService orderService = applicationContext.getBean(OrderService.class);
List<UserAddress> userAddresses = orderService.initOrder("1");
System.out.println("调用完成...., userAddresses:" + userAddresses);
System.in.read();
}
}
|
控制台输出:
1
|
调用完成...., userAddresses:[UserAddress(id=1, userAddress=北京市昌平区宏福科技园综合楼3层, userId=1, consignee=李老师, phoneNum=010-56253825, isDefault=Y), UserAddress(id=2, userAddress=深圳市宝安区西部硅谷大厦B座3层(深圳分校), userId=1, consignee=王老师, phoneNum=010-56253825, isDefault=N)]
|
dubbo管理后台也看到提供者注册到了注册中心

dubbo监控中心
-
下载dubbo-admin, 参考dubbo管理控制台安装
-
在父工程下打包,mvn clean package
, 进入到子工程dubbo-monitor-simple下target下
-
解压缩dubbo-monitor-simple-2.0.0-assembly.tar.gz,将解压缩后的目录dubbo-monitor-simple-2.0.0拷贝到自己的软件目录下,我这里是:/Users/david/soft/custom/dubbo-monitor-simple-2.0.0/
-
修改配置文件conf/dubbo.properties
,我这里只将jetty服务器端口改成了8081,其他默认即可(如果你前面都是默认过来的话)
-
启动monitor,在assembly.bin
目录下执行./start.sh
,查看日志$ cat ../logs/stdout.log
-
访问http://localhost:8081/

让监控中心能够监控到我们的服务还得在应用里增加如下配置:
分别在消费者consumer.xml和提供者provider.xml中增加如下配置:
1
2
3
4
5
|
<!--连接监控中心-->
<!--使用协议,让应用自动找到监控中心地址-->
<dubbo:monitor protocol="registry"></dubbo:monitor>
<!--直连监控中心-->
<!-- <dubbo:monitor address="127.0.0.1:7070"></dubbo:monitor> -->
|
重启提供者和服务者,观察监控中心变化,如下

springboot整合dubbo
说明
2018年2月16日,Apache Dubbo 加入 Apache 基金会孵化器。2019年5月16日,Apache 软件基金会董事会决议通过了 Apache Dubbo 的毕业申请,这意味着 Apache Dubbo 正式成为 Apache 的顶级项目。
Dubbo 捐精给 Apache 以后,也就意味着之前 Dubbo 以及 spring-boot-starter 坐标也变了,当然之前的还可以用,我这里使用新版本来实现前面的业务逻辑。
mall-api
同gmall-interface
mall-user
pom
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
|
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>dubbo2020</artifactId>
<groupId>com.eh</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>mall-user</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--dubbo-->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>2.7.3</version>
</dependency>
<!-- Zookeeper -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.9</version>
<!--解决日志冲突,排除self4j-log4j12-->
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>4.2.0</version>
</dependency>
<!--公共依赖、工具-->
<dependency>
<groupId>com.eh</groupId>
<artifactId>mall-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>29.0-jre</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>
|
yml
1
2
3
4
5
6
7
8
9
10
11
12
13
|
dubbo:
application:
name: mall-user
registry:
address: localhost:2181
protocol: zookeeper
# 指定通信协议,有dubbo、http等
protocol:
name: dubbo
# 通信端口
port: 20881
monitor:
protocol: registry
|
业务类
UserServiceImpl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
package com.eh.dubbo2020.user.service;
import com.eh.mall.api.model.UserAddress;
import com.eh.mall.api.service.UserService;
import com.google.common.collect.Lists;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
@Service
public class UserServiceImpl implements UserService {
@Override
public List<UserAddress> getUserAddressList(String userId) {
UserAddress address1 = new UserAddress(1, "北京市昌平区宏福科技园综合楼3层", "1", "李老师", "010-56253825", "Y");
UserAddress address2 = new UserAddress(2, "深圳市宝安区西部硅谷大厦B座3层(深圳分校)", "1", "王老师", "010-56253825", "N");
return Lists.newArrayList(address1, address2);
}
}
|
主启动
如果在yml配置文件中配置了包扫描dubbo.scan.base-packages,那么主启动上也可以不配置@EnableDubbo
1
2
3
4
5
6
7
8
9
10
11
12
13
|
package com.eh.dubbo2020.user;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnableDubbo(scanBasePackages = "com.eh.dubbo2020.user.service")
@SpringBootApplication
public class UserMainApplication {
public static void main(String[] args) {
SpringApplication.run(UserMainApplication.class, args);
}
}
|
mall-order
pom
同mall-user,唯一区别是使用spring-boot-starter-web用于在web端测试
yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
server:
port: 8082
dubbo:
# 指定当前服务/应用的名字
application:
name: mall-order
# 指定注册中心的位置
registry:
address: localhost:2181
protocol: zookeeper
# 指定通信协议,有dubbo、http等
protocol:
name: dubbo
# 通信端口
port: 20881
# 使用协议,让应用自动找到监控中心地址,等同于dubbo.monitor.address: localhost:7070
monitor:
protocol: registry
|
业务类
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
|
package com.eh.mall.order.service;
import com.eh.mall.api.model.UserAddress;
import com.eh.mall.api.service.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class OrderService {
@Reference
private UserService userService;
/**
* 初始化订单,查询用户的所有地址并返回
*
* @param userId
* @return
*/
public List<UserAddress> initOrder(String userId) {
return userService.getUserAddressList(userId);
}
}
|
主启动
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
|
package com.eh.mall.order;
import com.eh.mall.api.model.UserAddress;
import com.eh.mall.order.service.OrderService;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@EnableDubbo
@SpringBootApplication
public class OrderMainApplication {
public static void main(String[] args) {
SpringApplication.run(OrderMainApplication.class, args);
}
@RestController
class OrderController {
@Autowired
private OrderService orderService;
@GetMapping("/")
public List<UserAddress> getOrderAddress() {
return orderService.initOrder("1");
}
}
}
|
总结
- 启动zk
- 提供端得用org.apache.dubbo.config.annotation.Service 的注解:格式:@Service (version=xxx)
- 消费端使用远程调用得用@Reference(version=xxx)
- 依次启动服务提供者,消费者
- 注意版本的使用