1 Star 0 Fork 47

dean / rulego

forked from RuleGo / rulego 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

RuleGo

GoDoc Go Report codecov test build

English| 中文

RuleGo是一个基于Go语言的轻量级、高性能、嵌入式、可编排组件式的规则引擎。也一个灵活配置和高度定制化的事件处理框架。支持异构系统数据集成,可以对输入消息进行聚合、分发、过滤、转换、丰富和执行各种动作。

文档

官网文档托管在: rulego.cc

特性

  • 轻量级: 无外部中间件依赖,在低成本设备中也能高效对数据进行处理和联动,适用于物联网边缘计算。
  • 高性能: 得益于Go的高性能特性,另外RuleGo采用协程池和对象池等技术。
  • 嵌入式: 支持把RuleGo嵌入到现有项目,非入侵式利用其特性。
  • 组件化: 所有业务逻辑都是组件,并能灵活配置和重用它们。
  • 规则链: 可以灵活地组合和重用不同的组件,实现高度定制化和可扩展性的业务流程。
  • 流程编排: 支持对规则链组件进行动态编排,不重启应用情况下,替换或者新增业务逻辑。
  • 扩展简单: 提供丰富灵活的扩展接口,可以很容易地实现自定义组件或者引入第三方组件。
  • 动态加载: 支持通过Go plugin 动态加载组件和扩展组件。
  • 规则链嵌套: 支持子规则链嵌套,实现流程复用。
  • 内置大量组件: 消息类型Switch,JavaScript Switch,JavaScript过滤器,JavaScript转换器,HTTP推送MQTT推送发送邮件日志记录 等组件。可以自行扩展其他组件。
  • 上下文隔离机制: 可靠的上下文隔离机制,无需担心高并发情况下的数据串流。
  • AOP机制: 允许在不修改规则链或节点的原有逻辑的情况下,对规则链的执行添加额外的行为,或者直接替换原规则链或者节点逻辑。

使用场景

RuleGo是一款编排式的规则引擎,最擅长去解耦你的系统。

  • 如果你的系统业务复杂,并且代码臃肿不堪
  • 如果你的业务场景高度定制化或者经常变动
  • 如果你的系统需要对接大量的第三方应用或者协议
  • 或者需要端对端的物联网解决方案
  • 或者需要对异构系统数据集中处理
  • 或者你想尝试在Go语言实现热部署......
    RuleGo框架会是一个非常好的解决方案。

典型使用场景

  • 边缘计算: 可以在边缘服务器部署RuleGo,对数据进行预处理,筛选、聚合或者计算后再上报到云端。数据的处理规则和分发规则可以通过规则链动态配置和修改,而不需要重启系统。
  • 物联网: 收集设备数据上报,经过规则链的规则判断,触发一个或者多个动作,例如:发邮件、发告警、和其他设备或者系统联动。
  • 数据分发: 可以根据不同的消息类型,调用HTTP、MQTT或者gRPC把数据分发到不同系统。
  • 应用集成:RuleGo当做胶水连接各种系统或者协议,例如:ssh、webhook、kafka、消息队列、数据库、chatGPT、第三方应用系统。
  • 异构系统数据集中处理: 从不同的数据源(如 MQTT、HTTP、WS、TCP/UDP 等)接收数据,然后对数据进行过滤、格式转换、然后分发到数据库、业务系统或者仪表板。
  • 高度定制化业务: 把高度定制化或者经常变化的业务解耦出来,交给RuleGo规则链进行管理。业务需求变化而不需要重启主程序。
  • 复杂业务编排: 把业务封装成自定义组件,通过RuleGo编排和驱动这些自定义的组件,业务逻辑并支持动态调整和替换。
  • 微服务编排: 通过RuleGo编排和驱动微服务,或者动态调用第三方服务处理业务,并返回结果。
  • 业务代码和业务逻辑解耦: 例如:用户积分计算系统、风控系统。
  • 自动化: 例如:流程自动化系统、营销自动化系统、对接大模型提取用户意图,然后触发规则链与其他系统进行联动或者进行业务处理。
  • 灵活配置和高度定制化的事件处理框架: 对不同的消息类型,进行异步或者同步的处理。

架构图

RuleGo架构图

安装

使用go get命令安装RuleGo

go get github.com/rulego/rulego

使用

首先使用Json格式定义规则链。规则链的定义不需要学习特定的规则语法或者DSL,只要配置组件,并把他们通过一定的关系连接起来,即可实现你的功能需求。规则链定义:参考规则链

RuleGo 使用极其简单和轻量级。只需以下2步:

  1. 导入RuleGo包,并使用规则链定义,创建一个规则引擎实例:
import "github.com/rulego/rulego"

//使用规则链定义,创建一个规则引擎实例
ruleEngine, err := rulego.New("rule01", []byte(ruleFile))
  1. 把消息负荷、消息类型、消息元数据交给规则引擎实例处理,然后规则引擎就会根据规则链的定义处理消息:
//定义消息元数据
metaData := types.NewMetadata()
metaData.PutValue("productType", "test01")
//定义消息负荷和消息类型
msg := types.NewMsg(0, "TELEMETRY_MSG", types.JSON, metaData, "{\"temperature\":35}")

//把消息交给规则引擎处理
ruleEngine.OnMsg(msg)

规则引擎管理API

动态更新规则链

//更新根规则链
err := ruleEngine.ReloadSelf([]byte(ruleFile))
//更新规则链下某个节点
ruleEngine.ReloadChild("rule_chain_test", nodeFile)
//获取规则链定义
ruleEngine.DSL()

规则引擎实例管理:

//加载文件夹所有规则链定义到规则引擎池
rulego.Load("/rules", rulego.WithConfig(config))
//通过ID获取已经创建的规则引擎实例
ruleEngine, ok := rulego.Get("rule01")
//删除已经创建的规则引擎实例
rulego.Del("rule01")

配置:

详见文档

//创建一个默认的配置
config := rulego.NewConfig()
//调试节点回调,节点配置必须配置debugMode:true 才会触发调用
//节点入和出信息都会调用该回调函数
config.OnDebug = func (chainId,flowType string, nodeId string, msg types.RuleMsg, relationType string, err error) {
}
//使用配置
ruleEngine, err := rulego.New("rule01", []byte(ruleFile), rulego.WithConfig(config))

更多例子

关于规则链

规则节点

规则节点 是规则链的基本组件,它是一个实现了特定业务逻辑的函数。规则节点可以对传入的消息进行过滤、转换、丰富或执行某些动作。规则节点可以通过配置参数来调整其行为和输出。 你可以把业务很方便地封装成RuleGo节点组件,然后灵活配置和复用它们,像搭积木一样实现你的业务需求。

规则链

规则链 是 RuleGo 的核心概念,它是由多个规则节点组成的有向无环图,每个规则节点都是一个组件,可以实现不同的业务逻辑,节点与节点通过关系类型(relation type)进行连接。规则链可以动态配置和修改,支持嵌套和编排,实现复杂的业务流程。

以下例子定义3个规则节点,分别是对数据进行过滤->转换->推送,规则链逻辑如下图:

规则链定义:

{
  "ruleChain": {
    "name": "测试规则链",
    "root": true
  },
  "metadata": {
    "nodes": [
      {
        "id": "s1",
        "type": "jsFilter",
        "name": "过滤",
        "debugMode": true,
        "configuration": {
          "jsScript": "return msg!='bb';"
        }
      },
      {
        "id": "s2",
        "type": "jsTransform",
        "name": "转换",
        "debugMode": true,
        "configuration": {
          "jsScript": "metadata['test']='test02';\n metadata['index']=50;\n msgType='TEST_MSG_TYPE2';\n var msg2=JSON.parse(msg);\n msg2['aa']=66;\n return {'msg':msg2,'metadata':metadata,'msgType':msgType};"
        }
      },
      {
        "id": "s3",
        "type": "restApiCall",
        "name": "推送数据",
        "debugMode": true,
        "configuration": {
          "restEndpointUrlPattern": "http://192.168.216.21:9099/api/socket/msg",
          "requestMethod": "POST",
          "maxParallelRequestsCount": 200
        }
      }
    ],
    "connections": [
      {
        "fromId": "s1",
        "toId": "s2",
        "type": "True"
      },
      {
        "fromId": "s2",
        "toId": "s3",
        "type": "Success"
      }
    ]
  }
}

其他规则链例子:

  • 异步+顺序执行:


  • 使用子规则链方式:


  • 一些复杂例子:

数据集成

RuleGo 提供Endpoint模块对异构系统进行统一的数据集成和处理。详细参考: Endpoint

性能

RuleGo 大部分工作都在启动时完成,执行规则链时几乎不会额外增加系统开销,资源占用极低,特别适合在边缘服务器运行。 另外RuleGo使用有向无环图来表示规则链,每个输入消息只需要沿着图中的路径进行处理,无需匹配所有的规则, 这大大提高了消息处理的效率和速度,也节省了资源和时间。路由算法能实现:不管规则链节点数量是多少,都不会影响节点路由性能。

性能测试用例:

机器:树莓派2(900MHz Cortex-A7*4,1GB LPDDR2)  
数据大小:260B   
规则链:JS脚本过滤->JS复杂转换->HTTP推送   
测试结果:100并发和500并发,内存占用变化不大都在19M左右

更多性能测试用例

生态

贡献

欢迎任何形式的贡献,包括提交问题、建议、文档、测试或代码。请遵循以下步骤:

  • 克隆项目仓库到本地
  • 创建一个新的分支并进行修改
  • 提交一个合并请求到主分支
  • 等待审核和反馈

交流群

QQ群号:720103251

🎈加入社区讨论

许可

RuleGo使用Apache 2.0许可证,详情请参见LICENSE文件。

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

RuleGo是一个基于Go语言的轻量级、高性能、嵌入式、可扩展的组件编排规则引擎框架。也是一个支持异构系统数据集成和数据处理的事件框架。 展开 收起
Go
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Go
1
https://gitee.com/yaoqis/rulego.git
git@gitee.com:yaoqis/rulego.git
yaoqis
rulego
rulego
main

搜索帮助