共享数据
- 节点间数据传递
建议使用msg的Metadata
或者Data
在节点间传递数据,但如果需要传递信号量或者指针类,也支持context.Context
方式传递。 使用示例:
ruleEngine.OnMsg(msg, types.WithContext(context.WithValue(context.Background(), shareKey, shareValue)))
1
组件获取共享数据:
func (n *TimeNode) OnMsg(ctx types.RuleContext, msg types.RuleMsg) {
//context 方式获取共享数据
v1 := ctx.GetContext().Value(shareKey)
//context 方式修改共享数据
modifyCtx := context.WithValue(ctx.GetContext(), addShareKey, addShareValue)
ctx.SetContext(modifyCtx)
//msg.Metadata 方式获取共享数据
v2 :=msg.Metadata.GetValue("timestamp")
//msg.Metadata 方式修改共享数据
msg.Metadata.PutValue("timestamp", time.Now().Format(time.RFC3339))
ctx.TellSuccess(msg)
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
- 规则链间或者跨节点间共享数据
可以使用[Cache][/pages/d59341/#cache] 或者Cache Set组件 Cache Get组件 Cache Get组件
在 GitHub 上编辑此页 (opens new window)
上次更新: 2025/06/12, 01:18:51