github.com/uber/cadence是公使衔的一个流程编排引擎。分布式、伸缩、高可靠的异步执行业务逻辑,工具比较丰富,同时提供了可视化UI。业务逻辑被建模为“工作流workflow”和“活动activities”。“工作流workflow”是业务协调逻辑的实现,其唯一目的是协调“活动activities”执行。“活动activities”是用于实现业务逻辑的任务。用户只需要根据自己的业务场景定义workflow和activity,就可以实现类似aws step function的功能。
Cadence内部又分为两个服务:Matching Service是负责分派任务,保证了at-most-once语义,这意味着工作流的每个活动要么只执行一次,要么(在失败的情况下)根本不执行。History Service管理队列,处理事件,存储和改变工作流状态,保证至少一次的语意。
下我们先体验下如何使用然后分析下例子的源码。首先我们使用默认的配置,采用docker-compose启动server端
% cd cadence/docker
% docker-compose up
Creating network "docker_default" with the default driver
Pulling cassandra (cassandra:3.11)...
3.11: Pulling from library/cassandra
Pulling node-exporter (prom/node-exporter:)...
latest: Pulling from prom/node-exporter
Pulling cadence (ubercadence/server:master-auto-setup)...
master-auto-setup: Pulling from ubercadence/server
Pulling cadence-web (ubercadence/web:latest)...
latest: Pulling from ubercadence/web
当然,如果希望后端存储是mysql可以使用
%docker-compose -f docker-compose-mysql.yml up
然后我们就可以查看后台,因为没有起任何任务所以什么都没有
http://localhost:8088/news/2021/02/25/cadence-go-0-16-0
然后查看监控
http://localhost:3000/d/1B0efRyGy2/cadence-client-basic?orgId=1
我们可以下载官方的样例任务来进行学习github.com/uber-common/cadence-samples,下载下来后make
% make
go build -o bin/helloworld cmd/samples/recipes/helloworld/*.go
直接执行会报域名找不到的错误,因为我们没有创建域名
% ./bin/helloworld -m worker
2023-06-03T18:09:09.692+0800 INFO common/sample_helper.go:111 Logger created.
2023-06-03T18:09:09.692+0800 DEBUG common/factory.go:162 Creating RPC dispatcher outbound {"ServiceName": "cadence-frontend", "HostPort": "127.0.0.1:7833"}
2023-06-03T18:09:09.753+0800 INFO common/sample_helper.go:162 Domain doesn't exist {"Domain": "samples-domain", "error": "EntityNotExistsError{Message: Domain samples-domain does not exist., CurrentCluster: , ActiveCluster: }"}
2023-06-03T18:09:09.819+0800 ERROR internal/internal_worker.go:254 domain does not exist
到cadence目录下执行
% git submodule update --init
子模组 'idls'(https://github.com/uber/cadence-idl.git)已对路径 'idls' 注册
正克隆到 'cadence/idls'...
% make tools
Makefile:41: Caution: you are not using CI's go version. Expected: go1.17, current: go version go1.19 darwin/amd64
make: Nothing to be done for `tools'.
我们改下Makefile里的golang版本
% vi cadence/Makefile
EXPECTED_GO_VERSION := go1.17
EXPECTED_GO_VERSION := go1.19
然后编译
% make tools make: Nothing to be done for `tools'.
% make bins
compiling cadence-server with OS: darwin, ARCH: amd64
注册我们的域名samples-domain
% ./cadence --domain samples-domain domain register
Domain samples-domain successfully registered.
查看下
% ./cadence --domain samples-domain domain describe
Name: samples-domain
UUID: 8910febf-d84a-460d-a5a8-63b0754ebd7c
Description:
OwnerEmail:
DomainData: map[]
Status: REGISTERED
RetentionInDays: 3
EmitMetrics: true
IsGlobal(XDC)Domain: true
ActiveClusterName: cluster0
Clusters: [cluster0]
HistoryArchivalStatus: DISABLED
VisibilityArchivalStatus: DISABLED
指定任务列表运行一下
% ./cadence --domain samples-domain workflow run --tl helloWorldGroup --wt main.Workflow --et 60 -i '"cadence"' Running execution: Workflow Id : 4ff92e57-6c35-43a6-975a-b0f16f25283d Run Id : e5119817-814c-480f-a9bb-abf58571afc1 Type : main.Workflow Domain : samples-domain Task List : helloWorldGroup Args : "cadence" Progress: 1, 2023-06-03T19:43:15+08:00, WorkflowExecutionStarted
Time elapse: 8s
其中参数解释如下:
Tasklist name (--tl)任务列表
Workflow type (--wt)
Execution start to close timeout in seconds (--et)
Input in JSON format (--i) (optional)
也可以启动官方例子里的worker
% ./bin/helloworld -m worker
2023-06-03T19:44:54.468+0800 INFO common/sample_helper.go:111 Logger created.
2023-06-03T19:44:54.469+0800 DEBUG common/factory.go:162 Creating RPC dispatcher outbound {"ServiceName": "cadence-frontend", "HostPort": "127.0.0.1:7833"}
2023-06-03T19:44:54.503+0800 INFO common/sample_helper.go:164 Domain successfully registered. {"Domain": "samples-domain"}
2023-06-03T19:44:54.589+0800 INFO internal/internal_worker.go:834 Started Workflow Worker {"Domain": "samples-domain", "TaskList": "helloWorldGroup", "WorkerID": "36869@xiazemindeMacBook-Pro.local@helloWorldGroup@f06a06ee-786b-405f-8932-d48340666531"}
2023-06-03T19:44:54.664+0800 INFO internal/internal_worker.go:859 Started Activity Worker {"Domain": "samples-domain", "TaskList": "helloWorldGroup", "WorkerID": "36869@xiazemindeMacBook-Pro.local@helloWorldGroup@f06a06ee-786b-405f-8932-d48340666531"}
或者直接trigger下
% ./bin/helloworld -m trigger
2023-06-03T19:49:19.128+0800 INFO common/sample_helper.go:111 Logger created.
2023-06-03T19:49:19.128+0800 DEBUG common/factory.go:162 Creating RPC dispatcher outbound {"ServiceName": "cadence-frontend", "HostPort": "127.0.0.1:7833"}
2023-06-03T19:49:19.148+0800 INFO common/sample_helper.go:164 Domain successfully registered. {"Domain": "samples-domain"}
2023-06-03T19:49:19.208+0800 INFO common/sample_helper.go:198 Started Workflow {"WorkflowID": "helloworld_45fcb8f4-6556-448a-ac8c-86386bbf4ff9", "RunID": "d0576e1d-3d8d-46e8-8126-042378b98427"}
官方例子还有很多就不一一列举了,然后我们到后台就可以看到如下界面了: