LiteFlow LiteFlow
💒首页
  • v2.13.2(当前版本)
  • What's New

    • 🌈What' s New In LiteFlow v2.13.2?
  • 历史版本

    • v2.12.X
    • v2.11.X
    • v2.10.X
    • v2.9.X
    • v2.8.X
  • 升级指南

    • 🌈2.13.0升级指南
    • 2.12.4升级指南
    • 2.12.0升级指南
    • 升级到2.9.3说明
    • 升级到2.9.X说明
    • 升级到2.8.X说明
    • 升级到2.7.X说明
👑LF CLUB社区
  • 常见问题
  • 专题解释

    • 如何理解上下文这个概念?
    • Slot是一个什么样的概念,在框架中起到什么样的作用?
💖赞助
🧩插件
🔥PPT
  • 项目介绍
  • 项目成员
  • 更新记录
  • 参与开发
👥加入群聊
🧤谁在使用
  • Gitee (opens new window)
  • GitCode (opens new window)
  • Github (opens new window)

广告采用随机轮播方式显示 ❤️成为赞助商
💒首页
  • v2.13.2(当前版本)
  • What's New

    • 🌈What' s New In LiteFlow v2.13.2?
  • 历史版本

    • v2.12.X
    • v2.11.X
    • v2.10.X
    • v2.9.X
    • v2.8.X
  • 升级指南

    • 🌈2.13.0升级指南
    • 2.12.4升级指南
    • 2.12.0升级指南
    • 升级到2.9.3说明
    • 升级到2.9.X说明
    • 升级到2.8.X说明
    • 升级到2.7.X说明
👑LF CLUB社区
  • 常见问题
  • 专题解释

    • 如何理解上下文这个概念?
    • Slot是一个什么样的概念,在框架中起到什么样的作用?
💖赞助
🧩插件
🔥PPT
  • 项目介绍
  • 项目成员
  • 更新记录
  • 参与开发
👥加入群聊
🧤谁在使用
  • Gitee (opens new window)
  • GitCode (opens new window)
  • Github (opens new window)
  • 🍤LiteFlow简介
  • 🍓项目特性
  • 🧁环境支持

  • 🍟快速开始(Hello world)

  • 🍢配置项

  • 🔗组件

  • 🧩EL规则

  • 🌮上下文

  • 🛩执行器

  • 🍋脚本组件

  • 🗂规则配置源

    • 📕本地规则文件配置
    • 📘SQL数据库配置源
      • 依赖
      • 配置
      • 配置说明
      • 使用你项目中的dataSource来进行连接
      • 轮询自动刷新
      • 自定义规则/脚本表过滤SQL
      • 支持决策路由
      • 支持多数据源框架
      • 小例子
    • 📗ZK规则文件配置源
    • 📋Nacos配置源
    • 🗄Etcd配置源
    • 📜Apollo配置源
    • 📑Redis配置源

    • 📙自定义配置源
  • 🍼元数据管理

  • 🌌异步中的线程池

  • 🎲动态构造

  • 🧮决策路由

  • 😸生命周期

  • 🎨高级特性

  • ⛱测试用例以及示例

  • 🪂性能表现
  • v2.13.X文档
  • 🗂规则配置源
铂赛东
2022-10-06
目录

📘SQL数据库配置源

版本支持:v2.9.0+

LiteFlow原生支持了标准关系型结构化数据库的配置源,只要你的数据库兼容标准SQL语法,都可以支持。

⭐️SQL插件也是官网推荐的外置配置源插件 ⭐️

# 依赖

如果使用数据库作为规则配置源,你需要添加以下额外插件依赖:

<dependency>
    <groupId>com.yomahub</groupId>
    <artifactId>liteflow-rule-sql</artifactId>
    <version>2.13.2</version>
</dependency>

# 配置

依赖了插件包之后,你无需再配置liteflow.ruleSource路径。

只需要配置插件的额外参数即可:

    liteflow:
      rule-source-ext-data-map:
        url: jdbc:mysql://localhost:3306/poseidon
        driverClassName: com.mysql.cj.jdbc.Driver
        username: root
        password: 123456
        applicationName: demo
        #是否开启SQL日志
        sqlLogEnabled: true
        #是否开启SQL数据轮询自动刷新机制 默认不开启
        pollingEnabled: true
        pollingIntervalSeconds: 60
        pollingStartSeconds: 60
        #以下是chain表的配置,这个一定得有
        chainTableName: chain
        chainApplicationNameField: application_name
        chainNameField: chain_name
        elDataField: el_data
        #以下是决策路由字段的配置,如果你没用到决策路由,可以不配置
        routeField: route
        namespaceField: namespace
        #是否启用这条规则
        chainEnableField: enable
        #规则表自定义过滤SQL
        chainCustomSql: 这里设置自定义规则表SQL
        #以下是script表的配置,如果你没使用到脚本,下面可以不配置
        scriptTableName: script
        scriptApplicationNameField: application_name
        scriptIdField: script_id
        scriptNameField: script_name
        scriptDataField: script_data
        scriptTypeField: script_type
        scriptLanguageField: script_language
        #是否启用这条脚本
        scriptEnableField: enable
        #脚本表自定义过滤SQL
        scriptCustomSql: 这里设置自定义脚本表SQL
    
    liteflow.rule-source-ext-data={\
      "url":"jdbc:mysql://localhost:3306/poseidon",\
      "driverClassName":"com.mysql.cj.jdbc.Driver",\
      "username":"root",\
      "password":"123456",\
      "applicationName": "demo",\
      "sqlLogEnabled": true,\
      "pollingEnabled": true,\
      "pollingIntervalSeconds": 60,\
      "pollingStartSeconds": 60,\
      "chainTableName": "chain",\
      "chainApplicationNameField": "application_name",\
      "chainNameField": "chain_name",\
      "elDataField": "el_data",\
      "routeField": "route",\
      "namespaceField": "namespace",\
      "chainEnableField": "enable",\
      "chainCustomSql": "这里设置自定义规则表SQL",\
      "scriptTableName": "script",\
      "scriptApplicationNameField": "application_name",\
      "scriptIdField": "script_id",\
      "scriptNameField": "script_name",\
      "scriptDataField": "script_data",\
      "scriptTypeField": "script_type",\
      "scriptLanguageField": "script_language",\
      "scriptEnableField": "enable",\
      "scriptCustomSql": "这里设置自定义脚本表SQL"
      }
    
    // Make sure to add code blocks to your code group

    # 配置说明

    LiteFlow并不约束你的表名和表结构,你只需要把表名和相关的字段名配置在参数里即可。

    对于配置项说明如下:

    配置项 说明 是否必须
    url jdbc的连接url 否,如果使用自有数据源可以不配置
    driverClassName 驱动器类名 否,如果使用自有数据源可以不配置
    username 数据库用户名 否,如果使用自有数据源可以不配置
    password 数据库密码 否,如果使用自有数据源可以不配置
    applicationName 你的应用名称 是
    sqlLogEnabled 是否开启SQL日志 默认开启 否
    pollingEnabled 是否开启SQL数据轮询自动刷新机制 默认不开启 否,不配置就是不开启
    pollingIntervalSeconds SQL数据轮询时间间隔(s) 默认为60s 否
    pollingStartSeconds 规则配置后首次轮询的起始时间(s) 默认为60s 否
    chainTableName 编排规则表的表名 是
    chainApplicationNameField 编排规则表中应用名称存储字段名 是
    chainNameField 规则名称存储的字段名 是
    elDataField EL表达式的字段(只存EL) 是
    routeField 决策路由EL字段 否
    namespaceField namespace字段 否
    chainEnableField 此chain是否生效,此字段最好在mysql中定义成tinyInt类型,1为生效,0为不生效 否,如果不配置,默认就是生效
    chainCustomSql 可以设置自定义的规则表过滤SQL 否
    scriptTableName 你的脚本存储表的表名 是
    scriptApplicationNameField 脚本表中应用名称存储字段名 是
    scriptIdField 脚本组件的Id的字段名 是
    scriptNameField 脚本组件名称的字段名 是
    scriptDataField 脚本数据的字段名 是
    scriptTypeField 脚本类型(script | switch_script | boolean_script | for_script) 是
    scriptLanguageField 脚本语言类型(groovy | qlexpress | js | python | lua | aviator | java | kotlin) 否,如果只有一种脚本类型,可以不配置
    scriptEnableField 此脚本是否生效,此字段最好在mysql中定义成tinyInt类型,1为生效,0为不生效 否,如果不配置,默认就是生效
    scriptCustomSql 可以设置自定义的脚本表过滤SQL 否

    在数据库中,你至少需要一张表来存储编排规则,这是必须的。

    如果你使用到了脚本,那么需要第二张表来存储脚本。

    在规则表中,一行数据就是一个规则。在脚本表中,一行数据就是一个脚本组件。


    举例:

    规则表:liteflow_chain

    id application_name chain_name chain_desc el_data create_time enable
    1 demo chain1 测试流程1 THEN(a, b, c, s1,s2); 2022-09-19 19:31:00 1

    脚本表:liteflow_script

    id application_name script_id script_name script_data script_type script_language create_time enable
    1 demo s1 脚本s1 import cn.hutool.core.date.DateUtil
    def date = DateUtil.parse("2022-10-17 13:31:43")
    println(date) defaultContext.setData("demoDate", date)
    class Student {
    int studentID
    String studentName
    }
    Student student = new Student() student.studentID = 100301 student.studentName = "张三" defaultContext.setData("student",student) def a=3
    def b=2
    defaultContext.setData("s1",a*b)
    script groovy 2022-09-19 19:31:00 1
    2 demo s2 脚本s2 defaultContext.setData("s2","hello") script groovy 2022-09-19 19:31:00 1

    # 使用你项目中的dataSource来进行连接v2.10.6+

    LiteFlow支持了使用项目中已存在的Datasource来进行数据库连接。如果你项目中已有链接配置,比如:

    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    spring.datasource.url=jdbc:mysql://localhost:3306/poseidon
    spring.datasource.username=root
    spring.datasource.password=123456
    

    那么你在rule-source-ext-data-map中无需再配置以下几项:

    url: jdbc:mysql://localhost:3306/poseidon
    driverClassName: com.mysql.cj.jdbc.Driver
    username: root
    password: 123456
    

    提示

    需要注意的是,如果你的系统中声明了多个数据源,那么LiteFlow会自动判断该选用哪个数据源。

    如果你的系统中使用了动态数据源,那么请确保默认数据源是含有LiteFlow链路数据的表数据的。

    # 轮询自动刷新v2.11.1+

    LiteFlow支持SQL数据源轮询模式的自动刷新机制。你可以在配置项中通过pollingEnabled: true来开启自动刷新:

      liteflow:
        rule-source-ext-data-map:
          ...
          pollingEnabled: true
          ##以下非必须,默认1分钟
          pollingIntervalSeconds: 60
          pollingStartSeconds: 60
          ...
      
      liteflow.rule-source-ext-data={\
        ...\
        "pollingEnabled": true,\
        "pollingIntervalSeconds": 60,\
        "pollingStartSeconds": 60,\
        ...
        }
      
      // Make sure to add code blocks to your code group

      轮询模式的自动刷新根据预设的时间间隔定时拉取SQL中的数据,与本地保存的数据SHA值进行对比来判断是否需要更新数据。

      定时轮询存在些微的性能消耗;受轮询间隔限制,数据更新有一定延迟性。

      # 自定义规则/脚本表过滤SQLv2.12.4+

      liteflow:
        rule-source-ext-data-map:
          ...
          chainCustomSql: 这里设置自定义规则表SQL
          scriptCustomSql: 这里设置自定义脚本表SQL
          ...
      

      以上两个属性可以用来自定义你的过滤SQL。这里的配置的是完整的SQL。

      配置了自定义过滤SQL,则会完全忽略applicationName和enable属性了,完全根据你的自定义SQL来去查询了,但是返回的字段还是要符合开发者配置的映射字段。

      这个特性比较适合想自定义查询规则/脚本表的开发者,突破框架的applicationName和enable的限制。

      # 支持决策路由v2.12.1+

      只需要配置routeField和namespaceFiled字段,并在数据库对应的映射字段存入决策路由的表达式即可开启数据库对决策路由的支持。

      决策路由的具体使用方式请看决策路由用法。

      # 支持多数据源框架v2.13.0+

      LiteFlow支持了常用的两种多数据源框架,分别是:

      Baomidou社区的dynamic-datasource和Shardingsphere社区的shardingsphere-jdbc。

      当有多数据源的时候,你现在可以指定了:

      liteflow.rule-source-ext-data={\
        ...
        "baomidouDataSource":"h2-first",\
        ...
      }
      
      liteflow.rule-source-ext-data={\
        ...
        "shardingJdbcDataSource":"ds_1",\
        ...
      }
      

      # 小例子

      为了让大家能简单上手SQL规则文件的配置和运行,这里有一个小demo,大家可以拉到本地来运行,需要你替换数据库的配置信息。

      运行项目前,先读项目里的readme.txt文件。

      https://github.com/bryan31/liteflow-ext-rule-demo

      帮助我们改善此文档 (opens new window)
      上次更新: 2025/05/07, 13:09:49
      📕本地规则文件配置
      📗ZK规则文件配置源

      ← 📕本地规则文件配置 📗ZK规则文件配置源→

      Theme by Vdoing | Copyright © 2020-2025 铂赛东 | MIT License
      沪ICP备18012955号-2
      • 跟随系统
      • 浅色模式
      • 深色模式
      • 阅读模式