Solon 集成 LiteFlow:轻量级工作流引擎的极简实践指南
在复杂的业务场景中,工作流引擎是解耦业务逻辑、提升可维护性的核心组件。传统的BPM引擎(如 Activiti、Flowable)虽功能强大,但学习曲线陡峭且资源消耗较大。LiteFlow 作为一款国产轻量级规则引擎/流程引擎,以其零学习成本、高可扩展性和极致性能成为微服务架构下的理想选择。本文将详细讲解 Solon 集成 LiteFlow 的全过程,助你轻松驾驭轻量级流程编排。
一、LiteFlow核心优势
- 轻量嵌入:仅需2个核心JAR包,无数据库依赖
- 规则驱动:基于EL表达式的链式规则配置,变更实时生效
- 组件化设计:业务逻辑封装为可复用组件,支持热插拔
- 高性能:无反射执行,单线程每秒可处理万级任务
- 多类型支持:顺序流、条件分支、循环、嵌套、异步并行
二、Solon 集成实战
准备:添加依赖包
<!-- pom.xml 依赖 -->
<dependencies>
<dependency>
<groupId>com.yomahub</groupId>
<artifactId>liteflow-solon-plugin</artifactId>
<version>最新版</version>
</dependency>
</dependencies>
步骤1:定义流程组件
import com.yomahub.liteflow.core.NodeComponent;
import org.noear.solon.annotation.Managed;
// 普通组件示例
@Managed("paymentAction")
public class PaymentAction extends NodeComponent {
@Override
public void process() {
PaymentContext context = this.getContextBean(PaymentContext.class);
// 执行支付逻辑
System.out.println("处理支付, 订单:" + context.getOrderId());
}
}
// 条件组件示例(用于分支判断)
@Managed("userCheck")
public class UserCheck extends NodeComponent {
@Override
public void process() {
UserContext context = this.getContextBean(UserContext.class);
if(context.isVip()) {
this.setIsEnd(true); // 终止流程
}
}
}
步骤2:配置流程规则
resources/flow.yml 配置EL表达式规则:
liteflow:
rule-source: config/flow.el.xml
resources/config/flow.el.xml:
<?xml version="1.0" encoding="UTF-8"?>
<flow>
<chain name="orderProcess">
THEN(
initOrder,
WHEN(
checkInventory,
checkUserCredit
),
SWITCH(choosePayWay).TO(
CASE(aliPay).DO(aliPayAction),
CASE(wechatPay).DO(wechatPayAction)
),
AFTER(paymentAction).WHEN(userCheck)
);
</chain>
</flow>
步骤3:初始化上下文并执行流程
import com.yomahub.liteflow.core.FlowExecutor;
import com.yomahub.liteflow.flow.LiteflowResponse;
import org.noear.solon.annotation.*;
@Controller
public class OrderController {
@Inject
private FlowExecutor flowExecutor;
@Post
@Mapping("/submitOrder")
public String submitOrder(@Body OrderDTO order) {
OrderContext context = new OrderContext();
context.setOrderId(order.getId());
context.setAmount(order.getAmount());
LiteflowResponse response = flowExecutor.execute2Resp(
"orderProcess",
context,
OrderContext.class
);
return response.isSuccess() ? "订单成功" : "流程失败";
}
}
三、高级特性应用
异步并行执行
<!-- 配置并行节点 -->
<chain name="parallelChain">
THEN(
a,
WHEN(b, c, d), <!-- b,c,d并行执行 -->
e
);
</chain>
嵌套子流程
<chain name="mainFlow">
THEN(prepare, SUB(orderProcess), notify);
</chain>
组件降级处理
import com.yomahub.liteflow.core.NodeComponent;
import org.noear.solon.annotation.Managed;
@Managed("paymentAction")
public class PaymentAction extends NodeComponent {
@Inject
FallbackService fallbackService;
@Override
public void process() {...}
@Override
public void onError() {
// 支付失败时执行补偿逻辑
fallbackService.compensate();
}
}
规则热更新
import com.yomahub.liteflow.flow.FlowBus;
import org.noear.solon.annotation.Managed;
import org.noear.solon.core.bean.LifecycleBean;
// 动态添加规则
FlowBus.addChain("newChain", "THEN(a,b,c)");
// 监听规则变化
@Managed
public class FlowConfig implements LifecycleBean {
@Override
public void postStart() throws Throwable {
FileWatcher.watch(Paths.get("config/flow"),
() -> FlowBus.reloadRule());
}
}
四、监控与调试
流程跟踪
LiteflowResponse response = flowExecutor.execute2Resp(
"orderProcess",
context,
OrderContext.class,
// 开启执行链路跟踪
SlotCallbackBuilder.builder().build()
);
System.out.println(response.getExecuteStepStr());
输出示例:initOrder[✓] => checkInventory[✓] => checkUserCredit[✓] => ...
可视化监控(需企业版)
liteflow:
monitor:
enable-log: true
queue-limit: 200
delay: 30
period: 120
五、最佳实践建议
上下文设计原则
- 使用独立Context对象传递流程数据
- 避免在组件中操作数据库事务(应在Service层控制)
组件规范
- 单个组件代码不超过200行
- 组件命名采用"业务域+操作"格式(如:stockDeduct)
异常处理
- 业务异常通过 throw BusinessException 中断流程
- 系统异常自动触发 onError 回调
规则管理进阶
// 从数据库加载规则
@Managed
public class DBRuleLoader implements RuleSource {
@Override
public String loadRules() {
return ruleMapper.selectByApp("order-service");
}
}
结语
通过 Solon 集成 LiteFlow,我们实现了:
- 业务可视化编排:复杂流程通过EL表达式清晰定义
- 组件热插拔:新增业务节点无需停服
- 极致性能:单机万级TPS满足高并发场景
- 灵活扩展:支持自定义节点、拦截器、上下文
在微服务架构下,LiteFlow 的轻量级特性使其成为业务流程编排的理想选择。其简洁的API设计让开发者能快速上手,而强大的异步并行、嵌套流程等特性又能支撑复杂业务场景。
Solon 集成 LiteFlow:轻量级工作流引擎的极简实践指南的更多相关文章
- Mongodb极简实践
MongoDB 极简实践入门 1. 为什么用MongoDB? 传统的计算机应用大多使用关系型数据库来存储数据,比如大家可能熟悉的MySql, Sqlite等等,它的特点是数据以表格(table)的形式 ...
- MongoDB 极简实践入门
原作者StevenSLXie; 原链接(https://github.com/StevenSLXie/Tutorials-for-Web-Developers/blob/master/MongoDB% ...
- 黑科技抢先尝(续2) - Windows terminal中Powershell Tab的极简美化指南
目录 安装python 安装git 安装powerline字体 主题定制 安装oh-my-posh 查看策略组的执行权限 使用choco 安装终端模拟器 - ConEmu 优化 PowerShell ...
- 黑科技抢先尝(续) - Windows terminal中WSL Linux 终端的极简美化指南
目录 修改默认源,为apt-get安装提速 安装python 和 python pip 安装 zsh 安装powerline-font中的特定字体 安装powerline-shell 修改~目录下的配 ...
- 极简SpringBoot指南-Chapter05-SpringBoot中的AOP面向切面编程简介
仓库地址 w4ngzhen/springboot-simple-guide: This is a project that guides SpringBoot users to get started ...
- 极简SpringBoot指南-Chapter04-基于SpringBoot的书籍管理Web服务
仓库地址 w4ngzhen/springboot-simple-guide: This is a project that guides SpringBoot users to get started ...
- 极简SpringBoot指南-Chapter03-基于SpringBoot的Web服务
仓库地址 w4ngzhen/springboot-simple-guide: This is a project that guides SpringBoot users to get started ...
- 极简SpringBoot指南-Chapter02-Spring依赖注入的方式
仓库地址 w4ngzhen/springboot-simple-guide: This is a project that guides SpringBoot users to get started ...
- 极简SpringBoot指南-Chapter01-如何用Spring框架声明Bean
仓库地址 w4ngzhen/springboot-simple-guide: This is a project that guides SpringBoot users to get started ...
- 极简SpringBoot指南-Chapter00-学习SpringBoot前的基本知识
仓库地址 w4ngzhen/springboot-simple-guide: This is a project that guides SpringBoot users to get started ...
随机推荐
- vue3 基础-事件绑定 & 修饰符
无非就是 js 的一些 事件, 按键, 鼠标 等的一些绑定在 vue 的实现而已, 很好理解. 先来看一个基础例子. 事件初体验 <!DOCTYPE html> <html lang ...
- 操作系统:设备I/O -- 如何表示设备类型与设备驱动?
计算机的结构 计算机结构示意图: 主板上的各种芯片并非独立存在,而是以总线为基础连接在一起的,各自完成自己的工作,又能相互打配合,共同实现用户要求的功能. 如何管理设备 前面的学习中宏,实现了管理内存 ...
- Gin?有这一篇就够了!
Gin Gin是Golang的一个后端框架,封装比较优雅,API友好. go get -u github.com/gin-gonic/gin 1.hello word package main imp ...
- 运维排查 | SaltStack 远程命令执行中文乱码问题
哈喽大家好,我是咸鱼. 问题 我在一台服务器上写了一个简单的 Python 脚本 haha.py,内容如下: [root@localhost ~]# cat haha.py print("你 ...
- es聚合查询自动补0----java代码
ES语句 GET /event_log_hulianwang_v3/_search { "size": 0, "query": { "bool&quo ...
- 如何用看板系统打造中小企业的AI时代敏捷工作流?
一.敏捷与看板:AI浪潮下企业协同的"底层重构" 在AI工具和数字平台井喷式发展的今天,敏捷已不再是IT行业专属概念,而成为企业组织效率提升的共识.看板系统作为敏捷核心机制之一,正 ...
- JuiceFS介绍
简单介绍 JuiceFS 是一款面向云原生设计的高性能分布式文件系统,在 Apache 2.0 开源协议下发布.提供完备的 POSIX 兼容性,可将几乎所有对象存储接入本地作为海量本地磁盘使用,亦可同 ...
- PHP框架中用户认证和授权的实现方法与示例
本文由 ChatMoney团队出品 在Web开发中,用户认证(Authentication)和授权(Authorization)是构建安全应用程序的核心组件.用户认证是验证用户身份的过程,确保用户是他 ...
- 微软开源 Azure Functions MCP Extension
Azure Functions MCP Extension 是微软推出的开源扩展库,旨在将 Azure Functions 与模型上下文协议(Model Context Protocol, MCP) ...
- Edge 突然显示网站“不安全”的一个处理办法。
Edge浏览器升级后,访问以前可以正常访问的网站,地址栏前红色提示"不安全",网页无法访问,一会弹出来提示说"证书可能未被验证". 在我们确认该网站是正常的情况 ...