您现在的位置是:首页 > 文章详情

Solon Flow:轻量级流程编排引擎,让业务逻辑更优雅

日期:2025-06-25点击:18

在当今快速迭代的软件开发环境中,如何高效地管理和执行业务流程成为了开发者面临的重要挑战。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都能优雅应对。

原文链接:https://www.oschina.net/news/357211
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章