LiteFlow 是一个轻量级、高性能、可编排的 组件式规则引擎/流程编排框架,专为复杂业务逻辑的解耦与灵活编排而设计。它通过声明式的规则定义(如 XML、YAML 或 EL 表达式)将业务逻辑分解为独立的组件,并通过规则文件动态编排执行顺序,从而实现业务流程的高效管理与动态调整。
组件化设计
灵活的规则编排
示例规则(EL 表达式):
THEN(checkOrder, processPayment, sendConfirmation); IF(orderValid, THEN(applyDiscount), THEN(cancelOrder));
GitHub: https://github.com/yomahub/liteflow
XML<dependency>
<groupId>com.yomahub</groupId>
<artifactId>liteflow-spring-boot-starter</artifactId>
<version>2.13.2</version>
</dependency>
在依赖了以上jar包后,你需要定义并实现一些组件,确保SpringBoot会扫描到这些组件并注册进上下文。
java@LiteflowComponent("a")
public class ACmp extends NodeComponent {
@Override
public void process() {
//do your business
}
}
以此类推再分别定义b,c组件:
java@LiteflowComponent("b")
public class BCmp extends NodeComponent {
@Override
public void process() {
//do your business
}
}
java@LiteflowComponent("c")
public class CCmp extends NodeComponent {
@Override
public void process() {
//do your business
}
}
然后,在你的SpringBoot的application.properties或者application.yml里添加配置(这里以properties为例,yaml也是一样的)
更多配置项请参考配置项章节。
ymlliteflow.rule-source=config/flow.xml
同时,你得在resources下的config/flow.xml中定义规则:
xml<?xml version="1.0" encoding="UTF-8"?>
<flow>
<chain name="chain1">
THEN(a, b, c);
</chain>
</flow>
SpringBoot在启动时会自动装载规则文件。
java@SpringBootApplication
//把你定义的组件扫入Spring上下文中
@ComponentScan({"com.xxx.xxx.cmp"})
public class LiteflowExampleApplication {
public static void main(String[] args) {
SpringApplication.run(LiteflowExampleApplication.class, args);
}
}
获取结果
java@Component
public class YourClass{
@Resource
private FlowExecutor flowExecutor;
public void testConfig(){
LiteflowResponse response = flowExecutor.execute2Resp("chain1", "arg", DefaultContext.class);
}
}
本文作者:Weee
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!