Cascading Zippers

源代码: https://github.com/yomorun/yomo/tree/master/example/4-cascading-zipper

该示例展示 YoMo 如何使用多个 Zipper 级联传输,构建 Mesh 网络。

代码结构

  • source: 模拟生成随机数并发送给 zipper-1
  • zipper-1: 接收 source 的流数据, 并广播给下游另一个地区的 zipper-2
  • zipper-2: 接收上游 zipper-1 的流数据。
  • sfn: 接收 zipper-2 的数据并在终端打印出来。

准备

安装 YoMo CLI

Binary (推荐)

$ curl -fsSL "https://bina.egoist.sh/yomorun/cli?name=yomo" | sh

  ==> Resolved version latest to v0.1.8
  ==> Downloading asset for darwin amd64
  ==> Installing yomo to /usr/local/bin
  ==> Installation complete

或者从源代码编译

$ go install github.com/yomorun/cli/yomo@latest

选择 1: 自动运行

task run

$ task run

task: [zipper-2] go run zipper_2.go
task: [zipper-1] go run zipper_1.go
task: [sfn-build] go build -o ./bin/sfn sfn/sfn_echo.go
task: [source-build] go build -o ./bin/source source/source.go
task: [source] ./bin/source
task: [sfn] ./bin/sfn
[zipper-1] 2022-02-20 17:24:29.822  [core:client] use credential: [None]
[zipper-1] 2022-02-20 17:24:29.822  Server has started!, pid: 49449
[zipper-1] 2022-02-20 17:24:29.823  [yomo:zipper] Listening SIGUSR1, SIGUSR2, SIGTERM/SIGINT...
[zipper-1] 2022-02-20 17:24:29.824  [core:server][Zipper-1] Listening on: [::]:9001, QUIC: [v1 draft-29], AUTH: [None]
[zipper-2] 2022-02-20 17:24:29.912  Server has started!, pid: 49450
[zipper-2] 2022-02-20 17:24:29.912  [yomo:zipper] Listening SIGUSR1, SIGUSR2, SIGTERM/SIGINT...
[zipper-2] 2022-02-20 17:24:29.914  [core:server][zipper-2] Listening on: 127.0.0.1:9002, QUIC: [v1 draft-29], AUTH: [None]
[zipper-1] 2022-02-20 17:24:30.032  [core:client] ❤️  [zipper-2]([::]:58661) is connected to YoMo-Zipper localhost:9002
[zipper-2] 2022-02-20 17:24:30.032  [core:server] ❤️  <Upstream Zipper> [::zipper-2](127.0.0.1:58661) is connected!
[source] 2022-02-20 17:24:30.229    [core:client] use credential: [None]
[source] 2022-02-20 17:24:30.234    [core:client] ❤️  [yomo-source]([::]:64006) is connected to YoMo-Zipper localhost:9001
[source] 2022-02-20 17:24:30.234    [source] ✅ Emit 2437998737 to YoMo-Zipper
[zipper-1] 2022-02-20 17:24:30.234  [core:server] ❤️  <Source> [::yomo-source](127.0.0.1:64006) is connected!
[sfn] 2022-02-20 17:24:30.549   [core:client] use credential: [None]
[sfn] 2022-02-20 17:24:30.554   [core:client] ❤️  [echo-sfn]([::]:62720) is connected to YoMo-Zipper localhost:9002
[zipper-2] 2022-02-20 17:24:30.555  [core:server] ❤️  <Stream Function> [::echo-sfn](127.0.0.1:62720) is connected!
[source] 2022-02-20 17:24:31.235    [source] ✅ Emit 432890138 to YoMo-Zipper
[sfn] 2022-02-20 17:24:31.238   >> [sfn] got tag=0x33, data=432890138
[source] 2022-02-20 17:24:32.235    [source] ✅ Emit 1245807400 to YoMo-Zipper
[sfn] 2022-02-20 17:24:32.240   >> [sfn] got tag=0x33, data=1245807400
[source] 2022-02-20 17:24:33.236    [source] ✅ Emit 3329942892 to YoMo-Zipper
[sfn] 2022-02-20 17:24:33.239   >> [sfn] got tag=0x33, data=3329942892
[source] 2022-02-20 17:24:34.236    [source] ✅ Emit 2733970616 to YoMo-Zipper
[sfn] 2022-02-20 17:24:34.239   >> [sfn] got tag=0x33, data=2733970616
[source] 2022-02-20 17:24:35.238    [source] ✅ Emit 3313499294 to YoMo-Zipper
[sfn] 2022-02-20 17:24:35.243   >> [sfn] got tag=0x33, data=3313499294

选择 2: 手动运行

运行 zipper-1

cd zipper-1
go run zipper_1.go

ℹ️   Running YoMo-Zipper...
2021/11/11 16:09:54 [yomo:zipper] Listening SIGUSR1, SIGUSR2, SIGTERM/SIGINT..
2021/11/11 16:09:54 [core:server][Zipper-1] Listening on: [::]:9001, QUIC: [v1 draft-29], AUTH: [None]

运行 zipper-2

cd zipper-2
go run zipper_2.go

ℹ️   Running YoMo-Zipper...
2021/11/11 16:09:54 [yomo:zipper] Listening SIGUSR1, SIGUSR2, SIGTERM/SIGINT..
2021/11/11 16:09:54 [core:server][zipper-2] Listening on: [::]:9002, QUIC: [v1 draft-29], AUTH: [None]

运行 stream-function

go run ./sfn/sfn_echo.go

2021/11/11 16:11:05 [core:client] use credential: [None]
2021/11/11 16:11:05 [core:client] ❤️  [echo-sfn]([::]:56245) is connected to YoMo-Zipper localhost:9002

运行 yomo-source

go run ./source/source.go

2021/11/11 16:12:01 [core:client] use credential: [None]
2021/11/11 16:12:01 [core:client] ❤️  [yomo-source] is connected to YoMo-Zipper localhost:9001
2021/11/11 16:12:01 [source] ✅ Emit 1385416436 to YoMo-Zipper
2021/11/11 16:12:01 [source] ✅ Emit 837377611 to YoMo-Zipper

结果

stream-function

2021/11/11 16:12:01 >> [sfn] got tag=0x33, data=1385416436
2021/11/11 16:12:01 >> [sfn] got tag=0x33, data=837377611
在 GitHub 上编辑本页面 更新时间: Sat, May 28, 2022