使用LiteFlow,你需要去把復雜的業務邏輯按代碼片段拆分成一個個小組件,並定義一個規則流程配置。這樣,所有的組件,就能按照你的規則配置去進行復雜的流轉。
依賴
LiteFlow提供了liteflow-spring-boot-starter依賴包,提供自動裝配功能
<dependency> <groupId>com.yomahub</groupId> <artifactId>liteflow-spring-boot-starter</artifactId> <version>2.6.3</version> </dependency>
組件的定義
在依賴了以上jar包后。 需要定義並實現一些組件,確保SpringBoot會掃描到這些組件並注冊進上下文
import com.yomahub.liteflow.core.NodeComponent; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; @Slf4j @Component("a") public class ACmp extends NodeComponent { @Override public void process() { //do your business log.info("ACmp process"); } }
SpringBoot配置文件
liteflow: rule-source: config/flow.xml #-----------------以下非必須----------------- enable: true #liteflow是否開啟,默認為true monitor: delay: 10000 #監控一開始延遲多少執行,默認值為300000毫秒,也就是5分鍾 enable-log: true #是否開啟監控log打印,默認值為false period: 10000 #監控日志打印每過多少時間執行一次,默認值為300000毫秒,也就是5分鍾 queue-limit: 300 #監控隊列存儲大小,默認值為200 parse-on-start: true #是否在啟動的時候就解析規則,默認為true retry-count: 0 #全局重試次數,默認為0 slot-size: 1024 #slot的數量,默認值為1024 when-max-wait-second: 15 #異步線程最長的等待時間秒(只用於when),默認值為15 when-max-workers: 4 #異步線程池最大線程數,默認為4 when-queue-limit: 512 #異步線程池等待隊列數,默認為512 zk-node: /lite-flow/flow #zkNode的節點,只有使用zk作為配置源的時候才起作用
規則文件的定義
同時,在resources下的config/flow.xml中定義規則:
<?xml version="1.0" encoding="UTF-8"?>
<flow>
<chain name="chain1">
<then value="a,b,c"/>
<when value="d,e"/>
</chain>
</flow>
官方文檔:
https://yomahub.com/liteflow/docs/
