SpringBoot 集成輕量級(jí)規(guī)則引擎 LiteFlow 實(shí)現(xiàn)規(guī)則編排藝術(shù)
一、規(guī)則引擎為何成為復(fù)雜業(yè)務(wù)的剛需?
在電商訂單處理、金融風(fēng)控、物流調(diào)度等場(chǎng)景中,業(yè)務(wù)規(guī)則常呈現(xiàn)動(dòng)態(tài)性、復(fù)雜性、高頻變更的特征。傳統(tǒng)硬編碼開(kāi)發(fā)模式面臨兩大痛點(diǎn):
- 代碼臃腫:分支邏輯嵌套導(dǎo)致代碼可讀性差,維護(hù)成本指數(shù)級(jí)增長(zhǎng);
- 變更低效:修改規(guī)則需重新發(fā)布系統(tǒng),無(wú)法實(shí)現(xiàn)熱更新,影響業(yè)務(wù)連續(xù)性。
LiteFlow 作為輕量級(jí)規(guī)則引擎,通過(guò)組件化拆分+可視化編排,支持動(dòng)態(tài)調(diào)整流程順序、并行異步執(zhí)行、熱部署等特性,成為解決上述問(wèn)題的利器。
二、LiteFlow核心機(jī)制解析
1. 組件化設(shè)計(jì):原子能力解耦
每個(gè)業(yè)務(wù)邏輯封裝為獨(dú)立組件,繼承 NodeComponent 并實(shí)現(xiàn) process() 方法:
@Component("paymentCheck")
public class PaymentCheckCmp extends NodeComponent {
@Override
public void process() {
PaymentContext context = getContextBean(PaymentContext.class);
if (!checkRisk(context.getOrderId())) {
throw new RuntimeException("風(fēng)控校驗(yàn)失敗");
}
}
}
組件類型支持普通節(jié)點(diǎn)、條件分支(NodeIfComponent)、循環(huán)控制等,覆蓋90%業(yè)務(wù)場(chǎng)景。
2. 規(guī)則編排:DSL驅(qū)動(dòng)的流程設(shè)計(jì)
通過(guò)XML/YAML定義執(zhí)行鏈路,支持串行(THEN)、并行(WHEN)、嵌套等組合模式:
<chain name="orderProcess">
THEN(
paymentCheck,
WHEN(
inventoryDeduction,
couponVerify
),
IF(orderType, premiumService, standardService)
);
</chain>
優(yōu)勢(shì):業(yè)務(wù)流程一目了然,調(diào)整無(wú)需修改代碼。
3. 動(dòng)態(tài)熱更新:實(shí)時(shí)響應(yīng)業(yè)務(wù)變化
規(guī)則文件支持從 Nacos、ZK 等配置中心加載,修改后秒級(jí)生效,避免服務(wù)重啟。
4. 數(shù)據(jù)上下文:跨組件參數(shù)傳遞
通過(guò)自定義上下文對(duì)象實(shí)現(xiàn)組件間數(shù)據(jù)共享:
// 定義上下文
public class OrderContext extends BaseContext {
private Order order;
private PaymentResult paymentResult;
}
// 組件中獲取
OrderContext context = getContextBean(OrderContext.class);
三、SpringBoot集成LiteFlow全流程實(shí)戰(zhàn)
1. 環(huán)境搭建
依賴引入:
<dependency>
<groupId>com.yomahub</groupId>
<artifactId>liteflow-spring-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
配置文件:
liteflow:
rule-source: classpath:rules/order_flow.xml
slot-size: 2048 # 上下文槽位數(shù)
when-max-workers: 32 # 并行線程數(shù)
print-execution-log: true # 打印執(zhí)行日志
2. 組件開(kāi)發(fā)示例
定義庫(kù)存扣減組件:
@Component("inventoryDeduction")
public class InventoryDeductionCmp extends NodeComponent {
@Autowired
private InventoryService inventoryService;
@Override
public void process() {
OrderContext context = getContextBean(OrderContext.class);
inventoryService.deduct(context.getOrder().getSkuId(), context.getOrder().getQuantity());
}
}
3. 規(guī)則文件設(shè)計(jì)
order_flow.xml 定義訂單處理流程:
<flow>
<chain name="orderProcessChain">
THEN(
paymentCheck,
WHEN(inventoryDeduction, couponVerify),
orderStatusUpdate,
IF(isPremiumUser, sendGift, SWITCH(region).to(sendSMS, sendEmail))
);
</chain>
</flow>
4. 流程觸發(fā)與控制層
@RestController
public class OrderController {
@Autowired
private FlowExecutor flowExecutor;
@PostMapping("/submit")
public String submitOrder(@RequestBody OrderRequest request) {
OrderContext context = new OrderContext();
context.setOrder(request.getOrder());
LiteflowResponse response = flowExecutor.execute2Resp("orderProcessChain", null, context);
return response.isSuccess() ? "成功" : "失敗: " + response.getMessage();
}
}
四、高級(jí)特性與性能優(yōu)化
1. 異步編排提升吞吐量
通過(guò) WHEN 關(guān)鍵字實(shí)現(xiàn)并行執(zhí)行,結(jié)合線程池參數(shù)優(yōu)化:
liteflow:
when-max-workers: 64 # 并行線程數(shù)
when-queue-limit: 10240 # 等待隊(duì)列長(zhǎng)度
2. 動(dòng)態(tài)規(guī)則切換
集成Nacos實(shí)現(xiàn)規(guī)則熱更新:
@Bean
public LiteFlowConfigGetter liteFlowConfigGetter() {
return new NacosLiteFlowConfigGetter();
}
3. 全鏈路監(jiān)控與調(diào)優(yōu)
開(kāi)啟執(zhí)行日志與耗時(shí)統(tǒng)計(jì):
liteflow:
print-execution-log: true
monitor:
enable-log: true
period: 300000 # 5分鐘輸出一次統(tǒng)計(jì)
五、最佳實(shí)踐與避坑指南
1. 組件設(shè)計(jì)原則
- 單一職責(zé):每個(gè)組件只處理一個(gè)業(yè)務(wù)動(dòng)作。
- 冪等設(shè)計(jì):支持重復(fù)執(zhí)行,避免臟數(shù)據(jù)。
2. 規(guī)則版本管理
- 使用Git管理規(guī)則文件變更歷史;
- 通過(guò)chainName_v2形式實(shí)現(xiàn)灰度發(fā)布。
3. 異常處理策略
- 全局異常捕獲:繼承DefaultNodeExecutor自定義異常處理邏輯;
- 重試機(jī)制:配置retry-count實(shí)現(xiàn)節(jié)點(diǎn)級(jí)重試。
六、結(jié)語(yǔ)
LiteFlow 通過(guò)規(guī)則與代碼解耦、動(dòng)態(tài)編排、高性能執(zhí)行三大特性,為復(fù)雜業(yè)務(wù)系統(tǒng)提供了優(yōu)雅的解決方案,規(guī)則變更效率將得到大幅度的提升。