辉夜的博客

繁花似锦,辉夜如昼

0%

RPC框架设计侧重点-特色和架构调研2.md

Thrift

Thrift 是用于点对点 RPC 实现的轻量级、跨语言的软件栈。Thrift,为传输、序列化、应用级逻辑都提供了实现,同时具有从idl生成代码的功能。

Thrift将架构分为6层,每层都可以自由组合,以适应不同的需求。
1

架构

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
86
87
88
89
90
91
92
93
94
95
96
97
├── ...(一些构建相关)
├── compiler
│   └── cpp
├── debian
├── lib #代码实现
│   ├── c_glib/src/thrift
| ├── processor #处理请求
| │   ├── thrift_dispatch_processor.c #实现,使用glib-object完成面向对象
| │   ├── thrift_dispatch_processor.h
| │   ├── thrift_multiplexed_processor.c #提供多个处理器
| │   ├── thrift_multiplexed_processor.h
| │   ├── thrift_processor.c #接口
| │   └── thrift_processor.h
| ├── protocol
| │   ├── thrift_binary_protocol.c #二进制协议的实现,把数据转化为合适的形式(bool转化为uint8, double转化为IEEE int64等)
| │   ├── thrift_binary_protocol.h
| │   ├── thrift_binary_protocol_factory.c
| │   ├── thrift_binary_protocol_factory.h
| │   ├── thrift_compact_protocol.c
| │   ├── thrift_compact_protocol.h
| │   ├── thrift_compact_protocol_factory.c
| │   ├── thrift_compact_protocol_factory.h
| │   ├── thrift_multiplexed_protocol.c
| │   ├── thrift_multiplexed_protocol.h
| │   ├── thrift_protocol.c #写入数据,`THRIFT_PROTOCOL_GET_CLASS (protocol)->write`
| │   ├── thrift_protocol.h
| │   ├── thrift_protocol_decorator.c #用decorator模式提供了多协议`THRIFT_PROTOCOL_GET_CLASS (self->concrete_protocol)->write_struct_begin (self->concrete_protocol,name, error)`
| │   ├── thrift_protocol_decorator.h
| │   ├── thrift_protocol_factory.c
| │   ├── thrift_protocol_factory.h
| │   ├── thrift_stored_message_protocol.c
| │   └── thrift_stored_message_protocol.h
| ├── server
| │   ├── thrift_server.c #服务器抽象类
| │   ├── thrift_server.h
| │   ├── thrift_simple_server.c #简单实现
| │   └── thrift_simple_server.h
| ├── thrift.c
| ├── thrift.h
| ├── thrift_application_exception.c
| ├── thrift_application_exception.h
| ├── thrift_configuration.c
| ├── thrift_configuration.h
| ├── thrift_struct.c
| ├── thrift_struct.h
| └── transport
| ├── thrift_buffered_transport.c
| ├── thrift_buffered_transport.h
| ├── thrift_buffered_transport_factory.c
| ├── thrift_buffered_transport_factory.h
| ├── thrift_fd_transport.c
| ├── thrift_fd_transport.h
| ├── thrift_framed_transport.c
| ├── thrift_framed_transport.h
| ├── thrift_framed_transport_factory.c
| ├── thrift_framed_transport_factory.h
| ├── thrift_memory_buffer.c
| ├── thrift_memory_buffer.h
| ├── thrift_platform_socket.h
| ├── thrift_server_socket.c
| ├── thrift_server_socket.h
| ├── thrift_server_transport.c
| ├── thrift_server_transport.h
| ├── thrift_socket.c
| ├── thrift_socket.h
| ├── thrift_ssl_socket.c
| ├── thrift_ssl_socket.h
| ├── thrift_transport.c
| ├── thrift_transport.h
| ├── thrift_transport_factory.c
| ├── thrift_transport_factory.h
| ├── thrift_zlib_transport.c
| ├── thrift_zlib_transport.h
| ├── thrift_zlib_transport_factory.c
| └── thrift_zlib_transport_factory.h
| ...
|
├── contrib #一些实例,但不是官方测试用例
├── test #测试用例
├── doc
│   ├── specs #一些格式信息
| ├── HeaderFormat.md #请求头格式
| ├── SequenceNumbers.md #序列号(用于在一个链接中异步处理多个请求)
| ├── idl.md #接口定义语言
| ├── thrift-binary-protocol.md #二进制协议, TLV格式
| ├── thrift-compact-protocol.md #压缩方式 zigzag压缩整数, 其他的似乎编码没有太大不同?
| ├── thrift-parameter-validation-proposal.md #参数验证,也就是通过某些方式限定参数的取值范围.比如int32类型必须取1,2,4,或者限定某个字符串长度必须大于4
| ├── thrift-protocol-spec.md #BNF
| ├── thrift-rpc.md #框架整体简介
| ├── thrift-sasl-spec.txt #(Simple Authentication and Security Layer)
| ├── thrift-tconfiguration.md #一些设置
| └── thrift.tex
│   └── ... #没啥用
└── tutorial #教程
├── c_glib
...
└── swift

特色

Thrift 专门设计用于支持跨客户端和服务器代码的非原子版本更改。这使您可以升级服务器,同时仍然能够为旧客户端提供服务;或让较新的客户端向较旧的服务器发出请求。

Motan

Motan是一套基于java开发的RPC框架,除了常规的点对点调用外,Motan还提供服务治理功能,包括服务节点的自动发现、摘除、高可用和负载均衡等。

Motan具有良好的扩展性,主要模块都提供了多种不同的实现,例如支持多种注册中心,支持多种rpc协议等。

架构概述

2
默认序列化: Hessian2, 默认传输层: Netty NIO TCP长连接

  • register:
    用来和注册中心进行交互,包括注册服务、订阅服务、服务变更通知、服务心跳发送等功能;Server端会在系统初始化时通过register模块注册服务,Client端在系统初始化时会通过register模块订阅到具体提供服务的Server列表,当Server 列表发生变更时也由register模块通知Client。

  • Cluster:
    cluster是一组可用的Server在逻辑上的封装,包含若干可以提供RPC服务的Server,实际请求时会根据不同的高可用与负载均衡策略选择一个可用的Server发起远程调用。

    在进行RPC请求时,Client通过代理机制调用cluster模块,cluster根据配置的HA和LoadBalance选出一个可用的Server,通过serialize模块把RPC请求转换为字节流,然后通过transport模块发送到Server端。

代码结构

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
├── closable
├── cluster
│   ├── ha
│   ├── loadbalance
│   └── support
├── codec
├── common
├── config
│   ├── annotation
│   └── handler
├── core
│   └── extension
├── exception
├── filter
├── log
├── protocol
│   ├── injvm
│   ├── mock
│   ├── rpc
│   ├── support
│   └── v2motan
├── proxy
│   └── spi
├── registry #服务注册:发现注册/变更通知/流量配置/失败返回
│   └── support
│   └── command
├── rpc #奇怪的工具类 Callbackable/Future/Node/Refer/Request/...Response
│   └── init
├── serialize #FastJson, Hessian2, Breeze, Simple
├── switcher
├── transport #Channel/Transport/Server绑定/Clent心跳/..
│   ├── async
│   └── support
└── util #Math/Net/Stats/Reflect/....

Dubbo

  • 基于透明接口的 RPC
  • 智能负载均衡
  • 自动服务注册和发现
  • 高扩展性
  • 运行时流量路由
  • 可视化服务治理

架构

3

代码架构和设计内容像详解