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
  • 简体中文

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

  • 规则链

  • 标准组件

  • 扩展组件

    • 扩展组件概述
    • 过滤器

      • lua脚本过滤器
        • 配置
        • Relation Type
        • 执行结果
        • 配置示例
        • 自定义函数
        • 加载Lua模块
    • 转换器

    • 外部的

    • AI

    • CI

    • IoT

  • 自定义组件

  • 组件市场

  • 可视化

  • AOP

  • 触发器

  • 高级主题

  • RuleGo-Server

  • 问题

目录

lua脚本过滤器

luaFilter组件:lua脚本过滤器。可以使用Lua脚本对msg、metadata、msgType进行过滤。根据脚本返回值路由到True或者False链。也可以启动加载lua第三方库,进行例如:加解密、I/O、网络、文件等高级操作。

Lua脚本使用加解密、I/O、网络、文件等高级操作库,需要通过以下配置开启:config.Properties.PutValue(luaEngine.LoadLuaLibs, "true")

Lua脚本支持Lua5.1语法规范,请参考gopher-lua (opens new window) 。

# 配置

字段 类型 说明 默认值
script string lua脚本,或者与.lua后缀的lua脚本文件路径 无
  • script:可以对msg、metadata、msgType进行过滤。只需要提供函数体内容,如果是文件路径,则需要提供完整的脚本函数:

        function Filter(msg, metadata, msgType) 
            ${script} 
         end
    
    1
    2
    3
    • msg:消息内容,如果dataType=JSON,可以使用msg.temperature 方式操作。如果dataType是其他类型,该字段类型是:string
    • metadata:消息元数据,类型:jsonObject
    • msgType:消息类型
    • 函数返回值类型:boolean,决定和下一个节点的连接关系(Relation Type)

# Relation Type

  • True: 把消息发送到True链
  • False: 把消息发送到False链
  • Failure: 执行失败,把消息发送到Failure链

# 执行结果

该组件不会改变msg、metadata和msgType内容。

# 配置示例

  {
    "id": "s1",
    "type": "x/luaFilter",
    "name": "过滤",
    "configuration": {
      "script": "return msg.temperature > 50"
    }
  }
1
2
3
4
5
6
7
8

# 自定义函数

可以通过config.RegisterUdf注册golang函数,然后在Lua脚本中使用,其注册方式和用法和Js一致。详情参考Udf 。

# 加载Lua模块

可以通过加载Lua自定义或者第三方模块库,扩展Lua能力,使用以下方式把Lua模块注册到引擎中:

import luaEngine "github.com/rulego/rulego-components/pkg/lua_engine"

luaEngine.Preloader.Register(func(state *lua.LState) {
	//加载模块
	//libs.Preload(state)
})
1
2
3
4
5
6

使用Golang实现Lua模块参考 (opens new window)

另外框架内置了大量的Lua第三方库模块,使用以下方式启用,默认不加载:

config.Properties.PutValue(luaEngine.LoadLuaLibs, "true")
1

启动后可以进行加解密、I/O、网络、数据库、文件等高级操作,第三方库列表:

  • argparse (opens new window) argparse CLI parsing https://github.com/luarocks/argparse (opens new window)
  • base64 (opens new window) encoding/base64 (opens new window) api
  • cloudwatch (opens new window) aws cloudwatch log access
  • cert_util (opens new window) monitoring ssl certs
  • chef (opens new window) chef client api
  • cmd (opens new window) cmd port
  • crypto (opens new window) calculate md5, sha256 hash for string
  • db (opens new window) access to databases
  • filepath (opens new window) path.filepath port
  • goos (opens new window) os port
  • http (opens new window) http.client && http.server
  • humanize (opens new window) humanize github.com/dustin/go-humanize (opens new window) port
  • inspect (opens new window) pretty print github.com/kikito/inspect.lua (opens new window)
  • ioutil (opens new window) io/ioutil port
  • json (opens new window) json implementation
  • log (opens new window) log port
  • plugin (opens new window) run lua code in lua code
  • pprof (opens new window) pprof http-server for golang from lua
  • prometheus (opens new window) prometheus exporter
  • regexp (opens new window) regexp port
  • runtime runtime port
  • pb (opens new window) https://github.com/cheggaaa/pb (opens new window) port (v3)
  • shellescape (opens new window) shellescape https://github.com/alessio/shellescape (opens new window) port
  • stats (opens new window) stats https://github.com/montanaflynn/stats (opens new window) port
  • storage (opens new window) package for store persist data and share values between lua states
  • strings (opens new window) strings port (utf supported)
  • tac (opens new window) tac line-by-line scanner (from end of file to up)
  • tcp (opens new window) raw tcp client lib
  • telegram (opens new window) telegram bot
  • template (opens new window) template engines
  • time (opens new window) time port
  • xmlpath (opens new window) gopkg.in/xmlpath.v2 (opens new window) port
  • yaml (opens new window) gopkg.in/yaml.v2 (opens new window) port
  • zabbix (opens new window) zabbix bot
在 GitHub 上编辑此页 (opens new window)
上次更新: 2025/03/31, 01:52:11
扩展组件概述
lua脚本转换器

← 扩展组件概述 lua脚本转换器→

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

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