Solon Flow:轻量级流程编排引擎,让业务逻辑更优雅
在当今快速迭代的软件开发环境中,如何高效地管理和执行业务流程成为了开发者面临的重要挑战。Solon Flow作为Solon生态中的流程编排引擎,以其轻量级、高灵活性和强大的表达能力,为开发者提供了一种全新的解决方案。
为什么选择Solon Flow?
Solon Flow是一款基于YAML/JSON配置的流程编排引擎,它完美融合了"配置即代码"的理念,具有以下核心优势:
- 极简配置:采用YAML/JSON格式,配置简洁直观,支持自动推断和简化模式
- 多场景支持:无缝支持业务规则编排、计算任务编排、审批流程等多种场景
- 强大脚本能力:内置完整Java语法支持,可与多种脚本引擎集成
- 事件驱动架构:基于DamiBus实现的事件总线,实现组件间解耦
- 双模式引擎:同时支持无状态和有状态流程,满足不同业务需求
快速入门体验
让我们通过一个简单的Hello World示例,感受Solon Flow的魅力:
1. 添加依赖
<dependency> <groupId>org.noear</groupId> <artifactId>solon-flow</artifactId> </dependency>
2. 配置流程(demo1.chain.yml)
id: "c1" layout: - { id: "n1", type: "start", link: "n2"} - { id: "n2", type: "activity", link: "n3", task: "System.out.println(\"Hello Solon Flow!\");"} - { id: "n3", type: "end"}
3. 执行流程
@Component public class DemoApp implements LifecycleBean { @Inject private FlowEngine flowEngine; @Override public void start() { flowEngine.eval("c1"); // 输出:Hello Solon Flow! } }
核心功能解析
1. 灵活的流程配置
Solon Flow支持完整的流程图概念,包括链(Chain)、节点(Node)和连接(Link)。节点类型丰富:
- 开始节点(start):流程入口,每个链必须有且只有一个
- 活动节点(activity):执行具体任务,支持条件和脚本
- 网关节点(inclusive/exclusive/parallel):控制流程分支
- 结束节点(end):流程终点
# 审批流程示例 id: "leave-approval" title: "请假审批" layout: - { id: "start", type: "start", title: "发起申请", meta: {form: "leave"}, link: "tl-review"} - { id: "tl-review", type: "activity", title: "主管审批", meta: {role: "team-leader"}, link: "gate-3days"} - { id: "gate-3days", type: "exclusive", link: [ {nextId: "dm-review", title: "超过3天", condition: "days > 3"}, {nextId: "end", title: "3天内"} ]} - { id: "dm-review", type: "activity", title: "部门经理审批", meta: {role: "dept-manager"}, link: "end"} - { id: "end", type: "end"}
2. 强大的脚本与表达式
Solon Flow默认支持完整的Java语法脚本,同时可通过定制驱动器集成Aviator、Beetl等脚本引擎:
# 业务规则示例 id: "risk-control" layout: - { type: "start"} - { when: "score < 60", task: "context.result = '高风险'; actions.add('人工审核')"} - { when: "score >= 60 && score < 80", task: "context.result = '中风险'"} - { when: "score >= 80", task: "context.result = '低风险'; actions.add('自动通过')"} - { type: "end"}
3. 组件化开发模式
通过实现TaskComponent接口,可以将业务逻辑封装为可复用的组件:
@Component("scoreCalc") public class ScoreCalculator implements TaskComponent { @Override public void run(FlowContext context, Node node) { Order order = context.get("order"); // 计算逻辑... order.setScore(calculateScore(order)); } private int calculateScore(Order order) { // 评分算法实现 } }
在流程中引用组件:
id: "order-process" layout: - { type: "start"} - { task: "@scoreCalc"} # 使用评分组件 - { task: "@riskCheck"} # 使用风控组件 - { type: "end"}
4. 事件驱动架构
Solon Flow内置基于DamiBus的事件总线,实现组件间解耦:
# 事件发送示例 id: "event-demo" layout: - task: | // 发送事件 context.eventBus().send("order.created", order); - task: | // 发送并等待响应 String result = context.<String,String>eventBus() .sendAndRequest("risk.check", order);
事件监听处理:
public class RiskListener { @Inject private FlowEngine engine; public void init() { FlowContext context = new FlowContext(); context.<String,String>eventBus().listen("risk.check", event -> { Order order = event.getContent(); event.reply(checkRisk(order)); }); engine.eval("event-demo", context); } }
企业级特性
1. 有状态流程支持
对于审批类场景,Solon Flow提供了StatefulFlowEngine:
// 配置有状态引擎 @Bean public StatefulFlowEngine flowEngine() { return StatefulFlowEngine.newInstance( StatefulSimpleFlowDriver.builder() .stateController(new RoleBasedStateController()) .stateRepository(new RedisStateRepository()) .build() ); } // 审批处理示例 public void approve(String instanceId, String nodeId, String userId) { FlowContext context = new FlowContext(instanceId).put("user", userId); flowEngine.postActivityState(context, nodeId, StateType.COMPLETED); }
2. 拦截器机制
通过ChainInterceptor可以实现流程监控、日志记录等横切关注点:
@Component public class MetricsInterceptor implements ChainInterceptor { @Override public void doIntercept(ChainInvocation inv) throws Throwable { long start = System.currentTimeMillis(); try { inv.invoke(); } finally { long cost = System.currentTimeMillis() - start; Metrics.record(inv.getChain().id(), cost); } } }
3. 多环境支持
Solon Flow可以轻松集成到各种环境:
// Spring集成示例 @Configuration public class SpringConfig { @Bean public FlowEngine flowEngine(ApplicationContext ctx) { FlowEngine engine = FlowEngine.newInstance(); engine.register(new SimpleFlowDriver(new SpringAdapter(ctx))); engine.load("classpath:flows/**/*.yml"); return engine; } } // 原生Java环境 public class NativeApp { public static void main(String[] args) { FlowEngine engine = FlowEngine.newInstance(); engine.load("file:conf/flows/*.json"); engine.eval("main-flow"); } }
典型应用场景
1. 业务规则引擎
替代Drools等规则引擎,配置更简单:
id: "discount-rule" layout: - { type: "start"} - { when: "user.level == 'VIP' && cart.total > 1000", task: "cart.discount = 0.2"} - { when: "user.level == 'VIP'", task: "cart.discount = 0.1"} - { when: "cart.total > 500", task: "cart.discount = 0.05"} - { type: "end"}
2. 计算任务编排
类似LiteFlow的编排能力,但风格很不同:
id: "data-pipeline" layout: - { type: "start"} - { task: "@dataExtract"} - { task: "@dataTransform"} - { task: "@dataLoad"} - { type: "end"}
3. 审批流程管理
类似 Flowable 的效果。支持会签、或签等审批模式:
id: "contract-approval" layout: - { type: "start", title: "发起合同", link: "finance"} - { id: "finance", type: "parallel", title: "财务会签", link: ["f1", "f2"]} - { id: "f1", type: "activity", title: "财务经理审批", meta: {role: "finance-mgr"}, link: "join"} - { id: "f2", type: "activity", title: "财务总监审批", meta: {role: "finance-dir"}, link: "join"} - { id: "join", type: "parallel", link: "legal"} - { id: "legal", type: "activity", title: "法务审核", meta: {role: "legal"}, link: "end"} - { type: "end"}
为什么Solon Flow值得尝试?
- 学习成本低:基于熟悉的YAML/JSON配置,半小时即可上手
- 无缝集成:轻松融入Spring、Solon等各种Java生态
- 性能优异:轻量级设计,单线程每秒可执行上万次简单流程
- 灵活扩展:支持驱动器定制,满足各种特殊需求
- 生产验证:已在多家企业生产环境稳定运行
Solon Flow重新定义了流程编排的方式,让开发者能够以更声明式的方式表达业务逻辑,大幅提升开发效率的同时,保证了系统的可维护性和扩展性。无论是简单的业务规则,还是复杂的审批流程,Solon Flow都能优雅应对。

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
Anthropic 未经许可使用书籍训练 AI 模型属于“合理使用”
美国旧金山联邦法官威廉・阿尔苏普(William Alsup)裁定,Anthropic 在未经作者许可的情况下使用已出版书籍训练其 AI 模型属于“合理使用 (fair use)”。 这标志着法院首次认可 AI 公司的主张,即当 AI 公司使用受版权保护的材料训练大型语言模型(LLM)时,合理使用原则可使其免于承担责任。 法官指出,AI 模型对作品的训练类似于读者阅读并从中汲取灵感以创作新内容,而非复制或取代原作。然而,判决也指出,Anthropic 在 2021 年至 2022 年期间从 Books3、Library Genesis 和 Pirate Library Mirror 等来源下载的超过 700 万本盗版电子书不属于合理使用,这部分内容将面临陪审团审判。Anthropic 曾花费数百万美元购买并扫描大量印刷书籍,将其转换为数字格式用于内部研究。 这一裁决被认为是 AI 行业在版权合理使用方面的一个重要里程碑。同时对作者、艺术家和出版商是一个打击,他们已对 OpenAI、Meta、Midjourney、Google 等公司提起数十起诉讼。尽管这一裁决并不能保证其他法官会效仿阿...
- 下一篇
大促数据库压力激增,如何一眼定位 SQL 执行来源?
你是否曾经遇到过这样的情况:在大促活动期间,用户访问量骤增,数据库的压力陡然加大,导致响应变慢甚至服务中断?更让人头疼的是,当你试图快速定位问题所在时,却发现难以确定究竟是哪个业务逻辑中的 SQL 语句成为了性能瓶颈。面对这样的困境,本篇文章提出了对 SQL 进行 “染色” 的方法来帮助大家一眼定位问题 SQL,而无需再在多处逻辑中辗转腾挪。本文的思路主要受之前郭忠强老师发布的 如何一眼定位SQL的代码来源:一款SQL染色标记的简易MyBatis插件 文章启发,我在这个基础上对逻辑进行了简化,去除了一些无关的逻辑和工具类,并只对查询 SQL 进行染色,使这个插件“更轻”。此外,本文除了提供 Mybatis 拦截器的实现以外,还提供了针对 ibatis 框架实现拦截的方法,用于切入相对比较老的应用,希望对大家有所启发~ 在文章开展之前,我们先来了解一下什么是 SQL 染色:染色的含义是在 SQL 执行前,在 SQL 上进行注释打标,标记内容为这条 SQL 对应的是 Mapper 文件中的哪条 SQL 以及相关的方法执行堆栈,如下为在 SGM 的 SQL 执行监控端能直接看到 SQL 染色...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- CentOS关闭SELinux安全模块
- SpringBoot2更换Tomcat为Jetty,小型站点的福音
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果
- SpringBoot2初体验,简单认识spring boot2并且搭建基础工程
- CentOS7,8上快速安装Gitea,搭建Git服务器
- SpringBoot2配置默认Tomcat设置,开启更多高级功能
- Docker快速安装Oracle11G,搭建oracle11g学习环境
- SpringBoot2整合Redis,开启缓存,提高访问速度
- CentOS8安装MyCat,轻松搞定数据库的读写分离、垂直分库、水平分库