# rulego
**Repository Path**: developer_hj/rulego
## Basic Information
- **Project Name**: rulego
- **Description**: RuleGo是一个基于Go语言的轻量级、高性能、嵌入式、可扩展的组件编排规则引擎框架。
- **Primary Language**: Go
- **License**: Apache-2.0
- **Default Branch**: main
- **Homepage**: https://rulego.cc
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 122
- **Created**: 2024-07-05
- **Last Updated**: 2024-07-05
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# RuleGo
[](https://pkg.go.dev/github.com/rulego/rulego)
[](https://goreportcard.com/report/github.com/rulego/rulego)
[](https://codecov.io/gh/rulego/rulego)
[](https://github.com/rulego/rulego/actions/workflows/test.yml)
[](https://github.com/rulego/rulego/actions/workflows/build.yml)
[](https://qm.qq.com/q/8RDaYcOry8)
[English](README.md)| 中文
`RuleGo`是一个基于`Go`语言的轻量级、高性能、嵌入式、可编排组件式的规则引擎。支持异构系统数据集成,可以对输入消息进行聚合、分发、过滤、转换、丰富和执行各种动作。
RuleGo架构图
## 规则链运行示例图
[更多运行模式](http://8.134.32.225:9090/ui/)
## 安装
使用`go get`命令安装`RuleGo`:
```bash
go get github.com/rulego/rulego
```
或者使用gitee/gitcode源
```bash
go get gitee.com/rulego/rulego
#or
go get gitcode.com/rulego/rulego
如果遇到无法下载,在go.mod文件增加以下指令:
replace github.com/rulego/rulego => gitee.com/rulego/rulego version
replace gitee.com/rulego/rulego => github.com/rulego/rulego version
#or
replace github.com/rulego/rulego => gitcode.com/rulego/rulego version
replace gitcode.com/rulego/rulego => github.com/rulego/rulego version
```
## 使用
RuleGo 使用极其简单和轻量级。只需以下3步:
1. 使用JSON定义规则链:
[规则链DSL](https://rulego.cc/pages/10e1c0/)
2. 导入`RuleGo`包,并使用规则链定义,创建一个规则引擎实例:
```go
import "github.com/rulego/rulego"
//使用规则链定义,创建一个规则引擎实例
ruleEngine, err := rulego.New("rule01", []byte(ruleFile))
```
3. 把消息负荷、消息类型、消息元数据交给规则引擎实例处理,然后规则引擎就会根据规则链的定义处理消息:
```go
//定义消息元数据
metaData := types.NewMetadata()
metaData.PutValue("productType", "test01")
//定义消息负荷和消息类型
msg := types.NewMsg(0, "TELEMETRY_MSG", types.JSON, metaData, "{\"temperature\":35}")
//把消息交给规则引擎处理
ruleEngine.OnMsg(msg)
```
### 规则引擎管理API
动态更新规则链
```go
//更新根规则链
err := ruleEngine.ReloadSelf([]byte(ruleFile))
//更新规则链下某个节点
ruleEngine.ReloadChild("rule_chain_test", nodeFile)
//获取规则链定义
ruleEngine.DSL()
```
规则引擎实例管理:
```go
//加载文件夹所有规则链定义到规则引擎池
rulego.Load("/rules", rulego.WithConfig(config))
//通过ID获取已经创建的规则引擎实例
ruleEngine, ok := rulego.Get("rule01")
//删除已经创建的规则引擎实例
rulego.Del("rule01")
```
配置:
详见[文档](https://rulego.cc/pages/d59341/)
```go
//创建一个默认的配置
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))
```
### 规则链定义DSL
[规则链定义DSL](https://rulego.cc/pages/10e1c0/)
### 节点组件
- [标准组件](https://rulego.cc/pages/88fc3c/)
- [rulego-components](https://github.com/rulego/rulego-components) [文档](https://rulego.cc/pages/d7fc43/)
- [rulego-components-ai](https://github.com/rulego/rulego-components-ai)
- [rulego-components-ci](https://github.com/rulego/rulego-components-ci)
- [rulego-components-iot](https://github.com/rulego/rulego-components-iot)
- [自定义节点组件示例](examples/custom_component) [文档](https://rulego.cc/pages/caed1b/)
## 数据集成
`RuleGo` 提供`Endpoint`模块对异构系统进行统一的数据集成和处理。详细参考: [Endpoint](endpoint/README_ZH.md)
### Endpoint组件
- [Endpoint组件](https://rulego.cc/pages/691dd3/)
- [Endpoint DSL](https://rulego.cc/pages/390ad7/)
## 性能
`RuleGo` 大部分工作都在启动时完成,执行规则链时几乎不会额外增加系统开销,资源占用极低,特别适合在边缘服务器运行。
另外RuleGo使用有向无环图来表示规则链,每个输入消息只需要沿着图中的路径进行处理,无需匹配所有的规则,
这大大提高了消息处理的效率和速度,也节省了资源和时间。路由算法能实现:不管规则链节点数量是多少,都不会影响节点路由性能。
性能测试用例:
```
机器:树莓派2(900MHz Cortex-A7*4,1GB LPDDR2)
数据大小:260B
规则链:JS脚本过滤->JS复杂转换->HTTP推送
测试结果:100并发和500并发,内存占用变化不大都在19M左右
```
[更多性能测试用例](https://rulego.cc/pages/f60381/)
## 生态
- [RuleGo-Editor](https://editor.rulego.cc/) :规则链可视化编辑器
- [RuleGo-CI](http://8.134.32.225:9090/ui/) :CI/CD应用
- [rulego-components](https://gitee.com/rulego/rulego-components) :扩展组件库
- [examples/server](examples/server) :独立运行的示例工程
- [examples](examples) : 更多示例
## 贡献
欢迎任何形式的贡献,包括提交问题、建议、文档、测试或代码。请遵循以下步骤:
* 克隆项目仓库到本地
* 创建一个新的分支并进行修改
* 提交一个合并请求到主分支
* 等待审核和反馈
## 交流群
QQ群号:**720103251**
[🎈加入社区讨论](https://rulego.cc/pages/257c28/)
## 许可
`RuleGo`使用Apache 2.0许可证,详情请参见[LICENSE](LICENSE)文件。