找回密码
 立即注册
首页 业界区 业界 Solon Flow:轻量级流程编排引擎,让业务逻辑更优雅 ...

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

赶塑坠 2025-6-25 15:09:38
在当今快速迭代的软件开发环境中,如何高效地管理和执行业务流程成为了开发者面临的重要挑战。Solon Flow作为Solon生态中的流程编排引擎,以其轻量级、高灵活性和强大的表达能力,为开发者提供了一种全新的解决方案。
为什么选择Solon Flow?

Solon Flow是一款基于YAML/JSON配置的流程编排引擎,它完美融合了"配置即代码"的理念,具有以下核心优势:

  • 极简配置:采用YAML/JSON格式,配置简洁直观,支持自动推断和简化模式
  • 多场景支持:无缝支持业务规则编排、计算任务编排、审批流程等多种场景
  • 强大脚本能力:内置完整Java语法支持,可与多种脚本引擎集成
  • 事件驱动架构:基于DamiBus实现的事件总线,实现组件间解耦
  • 双模式引擎:同时支持无状态和有状态流程,满足不同业务需求
快速入门体验

让我们通过一个简单的Hello World示例,感受Solon Flow的魅力:
1. 添加依赖
  1. <dependency>
  2.     <groupId>org.noear</groupId>
  3.     solon-flow</artifactId>
  4. </dependency>
复制代码
2. 配置流程(demo1.chain.yml)
  1. id: "c1"
  2. layout:
  3.   - { id: "n1", type: "start", link: "n2"}
  4.   - { id: "n2", type: "activity", link: "n3", task: "System.out.println("Hello Solon Flow!");"}
  5.   - { id: "n3", type: "end"}
复制代码
3. 执行流程
  1. @Component
  2. public class DemoApp implements LifecycleBean {
  3.     @Inject
  4.     private FlowEngine flowEngine;
  5.    
  6.     @Override
  7.     public void start() {
  8.         flowEngine.eval("c1"); // 输出:Hello Solon Flow!
  9.     }
  10. }
复制代码
核心功能解析

1. 灵活的流程配置

Solon Flow支持完整的流程图概念,包括链(Chain)、节点(Node)和连接(Link)。节点类型丰富:

  • 开始节点(start):流程入口,每个链必须有且只有一个
  • 活动节点(activity):执行具体任务,支持条件和脚本
  • 网关节点(inclusive/exclusive/parallel):控制流程分支
  • 结束节点(end):流程终点
  1. # 审批流程示例
  2. id: "leave-approval"
  3. title: "请假审批"
  4. layout:
  5.   - { id: "start", type: "start", title: "发起申请", meta: {form: "leave"}, link: "tl-review"}
  6.   - { id: "tl-review", type: "activity", title: "主管审批", meta: {role: "team-leader"}, link: "gate-3days"}
  7.   - { id: "gate-3days", type: "exclusive", link: [
  8.       {nextId: "dm-review", title: "超过3天", condition: "days > 3"},
  9.       {nextId: "end", title: "3天内"}
  10.     ]}
  11.   - { id: "dm-review", type: "activity", title: "部门经理审批", meta: {role: "dept-manager"}, link: "end"}
  12.   - { id: "end", type: "end"}
复制代码
2. 强大的脚本与表达式

Solon Flow默认支持完整的Java语法脚本,同时可通过定制驱动器集成Aviator、Beetl等脚本引擎:
  1. # 业务规则示例
  2. id: "risk-control"
  3. layout:
  4.   - { type: "start"}
  5.   - { when: "score < 60", task: "context.result = '高风险'; actions.add('人工审核')"}
  6.   - { when: "score >= 60 && score < 80", task: "context.result = '中风险'"}
  7.   - { when: "score >= 80", task: "context.result = '低风险'; actions.add('自动通过')"}
  8.   - { type: "end"}
复制代码
3. 组件化开发模式

通过实现TaskComponent接口,可以将业务逻辑封装为可复用的组件:
  1. @Component("scoreCalc")
  2. public class ScoreCalculator implements TaskComponent {
  3.     @Override
  4.     public void run(FlowContext context, Node node) {
  5.         Order order = context.get("order");
  6.         // 计算逻辑...
  7.         order.setScore(calculateScore(order));
  8.     }
  9.    
  10.     private int calculateScore(Order order) {
  11.         // 评分算法实现
  12.     }
  13. }
复制代码
在流程中引用组件:
  1. id: "order-process"
  2. layout:
  3.   - { type: "start"}
  4.   - { task: "@scoreCalc"}  # 使用评分组件
  5.   - { task: "@riskCheck"}  # 使用风控组件
  6.   - { type: "end"}
复制代码
4. 事件驱动架构

Solon Flow内置基于DamiBus的事件总线,实现组件间解耦:
  1. # 事件发送示例
  2. id: "event-demo"
  3. layout:
  4.   - task: |
  5.       // 发送事件
  6.       context.eventBus().send("order.created", order);
  7.   - task: |
  8.       // 发送并等待响应
  9.       String result = context.<String,String>eventBus()
  10.           .sendAndRequest("risk.check", order);
复制代码
事件监听处理:
  1. public class RiskListener {
  2.     @Inject
  3.     private FlowEngine engine;
  4.    
  5.     public void init() {
  6.         FlowContext context = new FlowContext();
  7.         context.<String,String>eventBus().listen("risk.check", event -> {
  8.             Order order = event.getContent();
  9.             event.reply(checkRisk(order));
  10.         });
  11.         
  12.         engine.eval("event-demo", context);
  13.     }
  14. }
复制代码
企业级特性

1. 有状态流程支持

对于审批类场景,Solon Flow提供了StatefulFlowEngine:
  1. // 配置有状态引擎
  2. @Bean
  3. public StatefulFlowEngine flowEngine() {
  4.     return StatefulFlowEngine.newInstance(
  5.         StatefulSimpleFlowDriver.builder()
  6.             .stateController(new RoleBasedStateController())
  7.             .stateRepository(new RedisStateRepository())
  8.             .build()
  9.     );
  10. }
  11. // 审批处理示例
  12. public void approve(String instanceId, String nodeId, String userId) {
  13.     FlowContext context = new FlowContext(instanceId).put("user", userId);
  14.     flowEngine.postActivityState(context, nodeId, StateType.COMPLETED);
  15. }
复制代码
2. 拦截器机制

通过ChainInterceptor可以实现流程监控、日志记录等横切关注点:
  1. @Component
  2. public class MetricsInterceptor implements ChainInterceptor {
  3.     @Override
  4.     public void doIntercept(ChainInvocation inv) throws Throwable {
  5.         long start = System.currentTimeMillis();
  6.         try {
  7.             inv.invoke();
  8.         } finally {
  9.             long cost = System.currentTimeMillis() - start;
  10.             Metrics.record(inv.getChain().id(), cost);
  11.         }
  12.     }
  13. }
复制代码
3. 多环境支持

Solon Flow可以轻松集成到各种环境:
  1. // Spring集成示例
  2. @Configuration
  3. public class SpringConfig {
  4.     @Bean
  5.     public FlowEngine flowEngine(ApplicationContext ctx) {
  6.         FlowEngine engine = FlowEngine.newInstance();
  7.         engine.register(new SimpleFlowDriver(new SpringAdapter(ctx)));
  8.         engine.load("classpath:flows/**/*.yml");
  9.         return engine;
  10.     }
  11. }
  12. // 原生Java环境
  13. public class NativeApp {
  14.     public static void main(String[] args) {
  15.         FlowEngine engine = FlowEngine.newInstance();
  16.         engine.load("file:conf/flows/*.json");
  17.         engine.eval("main-flow");
  18.     }
  19. }
复制代码
典型应用场景

1. 业务规则引擎

替代Drools等规则引擎,配置更简单:
  1. id: "discount-rule"
  2. layout:
  3.   - { type: "start"}
  4.   - { when: "user.level == 'VIP' && cart.total > 1000", task: "cart.discount = 0.2"}
  5.   - { when: "user.level == 'VIP'", task: "cart.discount = 0.1"}
  6.   - { when: "cart.total > 500", task: "cart.discount = 0.05"}
  7.   - { type: "end"}
复制代码
2. 计算任务编排

类似LiteFlow的编排能力,但风格很不同:
  1. id: "data-pipeline"
  2. layout:
  3.   - { type: "start"}
  4.   - { task: "@dataExtract"}
  5.   - { task: "@dataTransform"}
  6.   - { task: "@dataLoad"}
  7.   - { type: "end"}
复制代码
3. 审批流程管理

类似 Flowable 的效果。支持会签、或签等审批模式:
  1. id: "contract-approval"
  2. layout:
  3.   - { type: "start", title: "发起合同", link: "finance"}
  4.   - { id: "finance", type: "parallel", title: "财务会签", link: ["f1", "f2"]}
  5.   - { id: "f1", type: "activity", title: "财务经理审批", meta: {role: "finance-mgr"}, link: "join"}
  6.   - { id: "f2", type: "activity", title: "财务总监审批", meta: {role: "finance-dir"}, link: "join"}
  7.   - { id: "join", type: "parallel", link: "legal"}
  8.   - { id: "legal", type: "activity", title: "法务审核", meta: {role: "legal"}, link: "end"}
  9.   - { type: "end"}
复制代码
为什么Solon Flow值得尝试?


  • 学习成本低:基于熟悉的YAML/JSON配置,半小时即可上手
  • 无缝集成:轻松融入Spring、Solon等各种Java生态
  • 性能优异:轻量级设计,单线程每秒可执行上万次简单流程
  • 灵活扩展:支持驱动器定制,满足各种特殊需求
  • 生产验证:已在多家企业生产环境稳定运行
Solon Flow重新定义了流程编排的方式,让开发者能够以更声明式的方式表达业务逻辑,大幅提升开发效率的同时,保证了系统的可维护性和扩展性。无论是简单的业务规则,还是复杂的审批流程,Solon Flow都能优雅应对。

来源:豆瓜网用户自行投稿发布,如果侵权,请联系站长删除

相关推荐

您需要登录后才可以回帖 登录 | 立即注册