基于连接与组合的微服务架构剖析 xuli@qiniu.com
Qiniu 1.0 POST GET Object Storage
Qiniu 2.0 POST GET Object Storage File Operation Process
Qiniu 2.0 / POST GET Object Storage Raw File Processed File File Operation Process (File Operation Process)
early Bandwidth LB More LB? 6
early Bottlenecks Network IO Data Flow Gate Bandwidth OMG! Daemon Disk IO 7
early Bottlenecks Fix Network IO Control Router Disk IO Data Control Agent Data 8
nanoservices Request with URL Load Balancer Pull raw File Storage AGENT Local AGENT IMAGE IMAGE IMAGE 9
API Gate (Logic Server) Supervisor Router [ACTION] http://domain/key?fop1 fop2 Qiniu Architecture 配置管理 / 服务发现 / 负载均衡 HOST-1 HOST-2 Agent Agent Docker Daemon Docker Daemon 图像处理 Container1 音视频转码 Container2 图像处理 Container1 音视频转码 Container2 文档转换 Container3 自定义处理 Container4 文档转换 Container3 自定义处理 Container4 Object Cache Object Storage 10
How Works? API Gate [GET] http://domain/key?fop1 (Logic Server) 1 2 3 8 Object Cache 5 Agent NO 4 Supervisor 6 7 Object Storage Router Data IO Daemon CONTAINER 11
Questions» 离线计算怎么做» 同 一个 并发请求怎么办» 多个 组合 工作流如何处理» 程序 IMAGE 可以 自定义吗 12
How Offline Works? [POST] http://customized.example.com/notify Callback API Gate [POST] http://domain/key?fop1 (Logic Server) 1 2 3 8 Object Cache 5 Agent NO 4 Supervisor 6 7 Object Storage Router Data IO Daemon CONTAINER 13
How Concurrency Works? 1 [GET] http://domain/key?fop1 [GET] http://domain/key?fop1 [GET] http://domain/key?fop1 Object Cache API Gate (Logic Server) 3 8 5 4 NO 7 Object Storage 2 Router Agent Memcached Data IO Supervisor 6 Daemon CONTAINER 14
Waiting for results done := make(chan Result, 1) go func() {... done <- Result{} }()... select { case result := <-done: do(result) case <- time.after(3 * time.second): fmt.println("timeout") } 15
How Multi- Works? [GET] http://domain/key?fop1 fop2 1 API Gate (Logic Server) 2 3 8 Object Cache 5 Agent NO 4 Supervisor 6 7 Object Storage Router Data IO Daemon CONTAINER 16
What is Multi-s? [ACTION] http://domain/key?fop1 fop2 fop3... 17
从 Unix 谈起» Unix 的连接和组合» app1 params1 app2 params2» App 接 口» 输 入 stdin, params» 输出 stdout» 协议 text (data stream)» Pipeline» 将 一个 app 的输出(stdout) 转为另 一个 app 的输 入 (stdin) 18
论 awk 编程与 Unix 管道艺术 200 GET... 500 POST... 200 GET... 500 GET... HTTP CODE COUNTING awk '{h[$1]++} END {for(k in h) print h[k], k}' test.log cut -d' ' -f1 test.log sort uniq -c 19
Unix Pipelines awk - pattern-directed scanning and processing language cut -- cut out selected portions of each line of a file sort - sort lines of text files uniq -- report or filter out repeated lines in a file 20
Pipeline 关键点» 多个 app 是并 行执 行的» 上游每产 生 一段output 会 立即交由下游 处理» app 间的协议是松散耦合的» 上游 app 的 ouput 是 xml 还是 json 下 游 app 需要知晓 但是属于 一种松散的 耦合关系 并 无任何强制的约束 21
Go 对 Unix Pipeline 的仿真 App func(in io.reader, out io.writer, args []string) app1 params1 app2 params2 bind(app1, params1) bind(app2, params2) pipe( bind(app1, params1), bind(app2, params2) ) 22
Go 对 Unix Pipeline 的仿真 func bind( app func(in io.reader, out io.writer, args []string), args []string ) func(in io.reader, out io.writer) { return func(in io.reader, out io.writer) { app(in, out, args) } } 23
Go 对 Unix Pipeline 的仿真 func pipe( app1 func(in io.reader, out io.writer), app2 func(in io.reader, out io.writer) ) func(in io.reader, out io.writer) { return func(in io.reader, out io.writer) { pr, pw := io.pipe() defer pw.close() go func() { defer pr.close() app2(pr, out) }() app1(in, pw) } } 24
Go 对 Unix Pipeline(s) 的仿真 func pipe(apps...func(in io.reader, out io.writer)) func(in io.reader, out io.writer) { if len(apps) == 0 { return nil } app := apps[0] for i := 1; i < len(apps); i++ { app1, app2 := app, apps[i] app = func(in io.reader, out io.writer) { pr, pw := io.pipe() defer pw.close() go func() { defer pr.close() app2(pr, out) }() app1(in, pw) } } return app 难以想象的优雅 } 25
Multi- Works Pipelines pipe(bind(fop1),bind(fop2)) [GET] http://domain/key?fop1 fop2 1 API Gate (Logic Server) 2 3 8 Object Cache 5 Agent NO 4 Supervisor 6 7 Object Storage Router Data IO Daemon CONTAINER 26
程序可以 自定义吗» Pipelined services» User-defined service (Docker) 27
自定义数据处理 U» U 用户 自定义的数据处理进程» 基于 Docker» 面向对象存储做就近计算处理» U 程序可以是 自 行部署 也可以选 用七 牛 U 公共平台上已有的服务程序» Docker 为部署 自定义 Image 提供了可能性 28
U 案例 https://hd.qiniu.com/nrop2015/ 29
U 不可以做什么» 不可以作为 网站主机 因为 U 没有 IP:PORT 只能通过 URL?ufop_api_args 形式调 用 其中 ufop_api_args 由 U 程序 自定义» 只要数据存在七 牛 针对该数据的各种特征 计算处理 都可以使 用 U 实现» U 也可与 七 牛云 平台上的其他 无缝集成 管道链式处理: fop1 fop2 app3 appn 30
Why Docker Product Compose Software Code Process Architecture Modularization Tool RevisionControl (git) Microservices Container (Docker) 31
软件架构变迁 Microservices Monolithic Application Introduction to Microservices, Chris Richardson 32
Why not Docker» 容器技术提供的隔离性 物理边界机制和微服务的理 念是契合的» 拆细/封装了环境和实现 不同的技术 不需要知道 里 面是怎样的 只关 心接 口» 然 而还不够 没有解决服务运维问题 是以出现官 方 的 工具链» 没有运维的解决 方案 如同空中楼阁» Kubernetes, 谷歌去年公开的容器编排系统 领先 十 年 omega/borg 论 文分量» 旨在解决容器运维问题 定义状态 而 非关 心过程» 其他问题 存储 网络 硬伤 33
Kubernetes»»»»»» Master Minion(Kubelet) Pods Replication Controller Labels - Proxy 34
as-nanoservices Storage DocFormat U Image Transcoding Customized HLS CDN Log & Data 35