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

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

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

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

  • 规则链

  • 标准组件

  • 扩展组件

  • 自定义组件

  • 组件市场

  • 可视化

  • AOP

  • 触发器

  • 高级主题

  • RuleGo-Server

    • 概述与快速开始
    • 安装与部署
    • 用户认证与权限
    • 可视化编辑器
    • AI 功能
    • MCP 服务
    • 组件市场
    • 运行日志
    • 国际化
    • 二次开发
      • 项目结构
      • 模块系统
      • 服务容器(DI)
        • 核心服务键
      • 自定义认证器
      • 自定义授权器
      • 自定义存储
      • Bridge 嵌入模式
      • 热重载
      • 构建标签
      • 多租户
    • REST API 参考
    • AI 助手使用教程
    • 调试规则链
  • 问题

目录

二次开发

RuleGo-Server 采用模块化架构,提供服务容器(DI)、可替换存储、嵌入模式等扩展能力。

# 项目结构

server/
├── cmd/server/          # 入口 + 构建标签注册
├── app/                 # 公共:App 生命周期、Container、Module 接口
├── config/              # 公共:配置结构与加载器
├── services/            # 公共:服务接口定义
├── model/               # 公共:数据模型
├── store/               # 公共:存储接口
├── bridge/              # 公共:HTTP 桥接(嵌入其他框架)
├── internal/
│   ├── endpoint/        # REST + WebSocket 路由
│   ├── modules/         # 业务模块实现
│   │   ├── rule/        #   规则链模块
│   │   ├── user/        #   用户认证模块
│   │   ├── node/        #   节点池模块
│   │   ├── runlog/      #   执行日志模块
│   │   ├── locale/      #   国际化模块
│   │   ├── skill/       #   技能模块
│   │   ├── system/      #   系统配置模块
│   │   ├── marketplace/ #   市场模块
│   │   └── mcp/         #   MCP 模块
│   ├── store/           # 存储实现
│   │   ├── filestore/   #   文件存储(默认)
│   │   ├── bboltstore/  #   BBolt 存储
│   │   ├── jsonlstore/  #   JSONL 存储
│   │   └── nopstore/    #   空存储
│   └── registry/        # 内置组件注册
├── data/                # 运行时数据
└── editor/              # 前端静态资源
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

# 模块系统

所有功能以 Module 接口实现,拥有 Init → Start → Stop 生命周期:

type Module interface {
    Init(container *app.Container) error
    Start() error
    Stop()
    Priority() int  // 数字越小越先初始化
}
1
2
3
4
5
6

内置 9 个模块:

模块 优先级 说明
user 10 认证与授权
mcp 25 MCP 服务
rule 30 规则链管理
node 35 节点池管理
runlog 45 运行日志
locale 50 国际化
skill 55 技能管理
system 65 系统配置
marketplace 70 组件市场

# 服务容器(DI)

轻量级依赖注入容器,模块通过名称注册和获取服务:

// 注册服务
container.Set("module.rule.executor", executor)

// 获取服务
executor := container.GetAs[services.ChainExecutor]("module.rule.executor")
1
2
3
4
5

# 核心服务键

服务键 接口 说明
module.user.auth UserService 用户服务
module.user.authenticator Authenticator 认证器(可替换)
module.user.authorizer Authorizer 授权器(可替换)
module.rule.catalog RuleCatalog 规则链目录
module.rule.executor ChainExecutor 规则链执行器
module.rule.manager RuleManager 规则链管理
module.rule.engine_manager EngineManager 引擎池(多租户)
module.node.service NodeService 节点池服务
module.runlog.service RunLogService 运行日志服务
module.locale.service LocaleService 国际化服务
module.skill.service SkillService 技能服务
module.system.settings ConfigService 系统配置服务
module.marketplace.service MarketplaceService 市场服务
module.mcp.service McpService MCP 服务

# 自定义认证器

替换默认认证器实现 OAuth2、LDAP 等:

type Authenticator interface {
    Authenticate(r *http.Request) (*model.UserContext, error)
}
1
2
3

在模块初始化时替换:

func (m *MyModule) Init(container *app.Container) error {
    container.Set(services.KeyAuthenticator, &OAuth2Authenticator{})
    return nil
}
1
2
3
4

# 自定义授权器

实现 RBAC、ABAC 等权限控制:

type Authorizer interface {
    Authorize(user *model.UserContext, resource, action string) bool
}
1
2
3

# 自定义存储

通过 StoreProvider 接口注入数据库存储:

type StoreProvider interface {
    NewRuleStore() RuleStore
    NewUserStore() UserStore
    NewRunLogStore() RunLogStore
    NewComponentStore() ComponentStore
    // ...
}
1
2
3
4
5
6
7

默认使用文件存储(filestore),可替换为 MySQL、PostgreSQL 等。

# Bridge 嵌入模式

将 RuleGo-Server 的 API 嵌入到 Gin、Echo 等框架中:

import "github.com/rulego/rulego/server/bridge"

func main() {
    // 使用 Gin 作为主框架
    r := gin.Default()

    // 嵌入 RuleGo-Server API
    handler := bridge.NewHandler(bridge.Options{
        ConfigPath: "./config.conf",
        BasePath:   "/rulego",
    })
    r.Any("/rulego/*path", gin.WrapH(handler))

    r.Run(":8080")
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

嵌入后 API 路径变为 /rulego/api/v1/...。

# 热重载

支持运行时重新加载配置,无需重启进程:

app.Reload() // 停止所有模块 → 重载配置 → 重新初始化 → 启动
1

# 构建标签

通过 Go build tags 按需引入组件:

// cmd/server/with_ai.go
//go:build with_ai

import _ "github.com/rulego/rulego-components-ai/all"
1
2
3
4
标签 说明
with_ai AI 组件(ai/agent、LLM 工具)
with_iot IoT 组件
with_etl ETL 数据处理组件
with_ci CI/CD 组件
with_extend 扩展组件

# 多租户

EngineManager 为每个用户维护独立的规则引擎池:

  • 规则链按用户隔离
  • 组件配置按用户隔离
  • 共享节点按用户隔离
  • 系统配置按用户隔离

用户身份通过 ContextWithMCPRequestingUser 注入到执行上下文。

在 GitHub 上编辑此页 (opens new window)
上次更新: 2026/05/28, 01:50:04
国际化
REST API 参考

← 国际化 REST API 参考→

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

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