RuleGo RuleGo
🏠首页
  • 快速入门
  • 规则链
  • 标准组件
  • 扩展组件
  • 自定义组件
  • 可视化
  • RuleGo-Server
  • RuleGo-MCP-Server
  • AOP
  • 触发器
  • 高级主题
  • 性能
  • 标准组件
  • 扩展组件
  • 自定义组件
  • 组件市场
  • 概述
  • 快速入门
  • 路由
  • DSL
  • API
  • Options
  • 组件
🔥编辑器 (opens new window)
  • 可视化编辑器 (opens new window)
  • RuleGo-Server (opens new window)
  • ❓问答

    • FAQ
💖支持
👥加入社区
  • Github (opens new window)
  • Gitee (opens new window)
  • GitCode (opens new window)
  • 更新日志 (opens new window)
  • English
  • 简体中文
🏠首页
  • 快速入门
  • 规则链
  • 标准组件
  • 扩展组件
  • 自定义组件
  • 可视化
  • RuleGo-Server
  • RuleGo-MCP-Server
  • AOP
  • 触发器
  • 高级主题
  • 性能
  • 标准组件
  • 扩展组件
  • 自定义组件
  • 组件市场
  • 概述
  • 快速入门
  • 路由
  • DSL
  • API
  • Options
  • 组件
🔥编辑器 (opens new window)
  • 可视化编辑器 (opens new window)
  • RuleGo-Server (opens new window)
  • ❓问答

    • FAQ
💖支持
👥加入社区
  • Github (opens new window)
  • Gitee (opens new window)
  • GitCode (opens new window)
  • 更新日志 (opens new window)
  • English
  • 简体中文

广告采用随机轮播方式显示 ❤️成为赞助商
  • 快速入门

  • 规则链

  • 标准组件

  • 扩展组件

  • 自定义组件

  • 组件市场

  • 可视化

  • AOP

    • AOP概述
    • Before 增强点
    • After 增强点
    • Around 增强点
      • 接口
      • 示例
    • Start 增强点
    • End 增强点
    • Completed 增强点
    • OnChainBeforeInit 增强点
    • OnNodeBeforeInit 增强点
    • OnCreated 增强点
    • OnReload 增强点
    • OnDestroy 增强点
    • 内置切片

  • 触发器

  • 高级主题

  • RuleGo-Server

  • 问题

目录

Around 增强点

节点环绕通知(Around Advice):在节点OnMsg方法执行之前和之后执行。该类型增强点,可以替换原节点逻辑。可以实现节点故障转移、重试、缓存等功能。

Before、After、Around Advice 执行顺序:Around->Before->Around

# 接口

// Order 返回执行顺序,值越小,优先级越高
Order() int
// 规则链初始化时候会调用该方法创建新的实例,如果字段值需要继承,在这里处理
New() Aspect
// PointCut 声明一个切入点,用于判断是否需要执行增强点
//例如:指定某些组件类型或者relationType才执行切面逻辑;return ctx.Self().Type()=="mqttClient"
PointCut(ctx RuleContext, msg RuleMsg, relationType string) bool
//Around 节点 OnMsg 方法执行环绕的增强点。
//如果返回false:引擎不会调用当前节点的OnMsg方法,需要Aspect手动触发,如:ctx.Self().OnMsg(ctx, msg),或者通过 ctx.TellNext(msg, relationType) 控制流程。
//如果返回true:引擎会调用当前节点的OnMsg方法。
Around(ctx RuleContext, msg RuleMsg, relationType string) (RuleMsg, bool)
1
2
3
4
5
6
7
8
9
10
11

Around msg返回值无法影响下一个节点入参,请用 before 切面或者通过 ctx.TellNext(msg, relationType) 控制。

# 示例

type AroundAspect struct {
	Name string
}

func (aspect *AroundAspect) Order() int {
	return 5
}

func (aspect *AroundAspect) New() types.Aspect {
	return &AroundAspect{}
}

func (aspect *AroundAspect) PointCut(ctx types.RuleContext, msg types.RuleMsg, relationType string) bool {
	return true
}

func (aspect *AroundAspect) Around(ctx types.RuleContext, msg types.RuleMsg, relationType string) (types.RuleMsg, bool) {
	fmt.Printf("debug Around before ruleChainId:%s,flowType:%s,nodeId:%s,msg:%+v,relationType:%s \n", ctx.RuleChain().GetNodeId().Id, "Around", ctx.Self().GetNodeId().Id, msg, relationType)
	msg.Metadata.PutValue(ctx.GetSelfId()+"_before", ctx.GetSelfId()+"_before")
	// 执行当前节点逻辑
	ctx.Self().OnMsg(ctx, msg)
	//获取执行结果
	//ctx.GetOut()
    fmt.Printf("debug Around after ruleChainId:%s,flowType:%s,nodeId:%s,msg:%+v,relationType:%s \n", ctx.RuleChain().GetNodeId().Id, "Around", ctx.Self().GetNodeId().Id, msg, relationType)
	//返回false,脱离框架不重复执行该节点逻辑
	return msg, false
}

func TestAroundAspect(t *testing.T) {
var chain = `
{
  "ruleChain": {
    "id": "rule8848",
    "name": "测试规则链",
    "root": true
  },
  "metadata": {
    "nodes": [
      {
        "id": "s1",
        "type": "jsFilter",
        "name": "过滤",
        "debugMode": true,
        "configuration": {
          "jsScript": "return msg.role=='admin';"
        }
      },
      {
        "id": "s2",
        "type": "jsTransform",
        "name": "转换",
        "debugMode": true,
        "configuration": {
          "jsScript": "msg.userName=msg.userName+'NO-1';\n return {'msg':msg,'metadata':metadata,'msgType':msgType};"
        }
      }
    ],
    "connections": [
         {
        "fromId": "s1",
        "toId": "s2",
        "type": "False"
      }
    ]
  }
}
`
chainId := "test_around_aspect"

config := NewConfig()

ruleEngine, err := DefaultPool.New(chainId, []byte(chain), WithConfig(config), types.WithAspects(
&AroundAspect{Name: "AroundAspect1"},
))
if err != nil {
t.Error(err)
}
metaData := types.NewMetadata()
metaData.PutValue("productType", "test01")
msg := types.NewMsg(0, "TEST_MSG_TYPE1", types.JSON, metaData, "{\"temperature\":41}")

ruleEngine.OnMsg(msg)
time.Sleep(time.Millisecond * 200)
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
在 GitHub 上编辑此页 (opens new window)
上次更新: 2025/04/02, 01:29:50
After 增强点
Start 增强点

← After 增强点 Start 增强点→

Theme by Vdoing | Copyright © 2023-2025 RuleGo Team | Apache 2.0 License

  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式