活动
Activity 是 Temporal 中执行副作用和非确定性操作的单元。工作流负责编排, 活动负责执行具体业务动作。
核心边界
任何网络请求、数据库写入、系统时间读取、随机数等逻辑,都应放在 Activity 中, 不要直接写在 Workflow 里。
活动特性
副作用执行
负责外部 API、数据库、消息队列等非确定性操作。
可配置重试
Temporal 默认重试活动,并支持按活动覆盖重试策略。
失败可恢复
可通过超时、心跳、重试预算组合控制失败恢复行为。
状态隔离
活动不依赖工作流重放机制,便于封装底层 SDK 与外部客户端。
调用活动示例(Go)
Workflow ExecuteActivityGo
func PaymentWorkflow(ctx workflow.Context, orderID string) (string, error) {
ao := workflow.ActivityOptions{
StartToCloseTimeout: 30 * time.Second,
}
ctx = workflow.WithActivityOptions(ctx, ao)
var txID string
err := workflow.ExecuteActivity(ctx, ChargePaymentActivity, orderID).Get(ctx, &txID)
if err != nil {
return "", err
}
return txID, nil
}活动超时与心跳
StartToCloseTimeout
单次活动执行的最大时间。通常是最优先配置的活动超时。
HeartbeatTimeout
长运行活动应定期心跳。丢失心跳可触发超时并由重试策略接管。