springboot整合規(guī)則引擎(liteflow)使用方式
規(guī)則引擎,定義業(yè)務(wù)執(zhí)行邏輯,把公共組件抽出來進行封裝。
作用:
- 可以將公有代碼抽為組件
- 根據(jù)不同的模式走不同的組件流程
- 參數(shù)可以在組件之間共享

1、導(dǎo)入依賴
<dependency>
<groupId>com.yomahub</groupId>
<artifactId>liteflow-spring-boot-starter</artifactId>
<version>2.13.1</version>
</dependency>2、組件定義
@LiteflowComponent("a")
public class ACmp extends NodeComponent {
// 走這個流程 通過默認(rèn)上下文方式
@Override
public void process() {
User user = this.getContextBean(User.class);
user.setRealName(user.getRealName()+"A");
System.out.println(user);
System.out.println("AAA");
//do your business
}
}@LiteflowComponent("b")
public class BCmp extends NodeComponent {
@Override
public void process() {
User user = this.getContextBean(User.class);
user.setRealName(user.getRealName()+"B");
System.out.println(user);
System.out.println("BBB");
//do your business
}
// 走這個流程 注解方式
@LiteflowMethod(value = LiteFlowMethodEnum.PROCESS,nodeType = NodeTypeEnum.COMMON, nodeId = "b")
public void processB(NodeComponent bindCmp,@LiteflowFact("user") User user) {
// User user = bindCmp.getContextBean(User.class);
user.setRealName(user.getRealName()+"B2");
System.out.println(user);
System.out.println("BBB2");
}
}@LiteflowComponent("c")
public class CCmp extends NodeComponent {
@Override
public void process() {
User user = this.getContextBean(User.class);
user.setRealName(user.getRealName()+"C");
System.out.println(user);
System.out.println("CCC");
}
// 走這個流程 NodeComponent獲取方式
@LiteflowMethod(value = LiteFlowMethodEnum.PROCESS,nodeType = NodeTypeEnum.COMMON, nodeId = "c")
public void processB(NodeComponent bindCmp) {
User user = bindCmp.getContextBean(User.class);
user.setRealName(user.getRealName()+"C2");
System.out.println(user);
System.out.println("CCC");
}
}3、配置yml、xml
- yml
liteflow.rule-source=config/flow.xml
- xml配置鏈路組件
可以定義多條鏈路,每條鏈路執(zhí)行順序可以定義,就是把需要執(zhí)行的組件以及順序排列好,后面業(yè)務(wù)可以直接調(diào)用,同時可以根據(jù)需求定義。目前是最簡單的一條完整執(zhí)行鏈路。
<?xml version="1.0" encoding="UTF-8"?>
<flow>
<chain name="chain1">
THEN(a, c, b);
</chain>
</flow>4、執(zhí)行調(diào)用
啟動類加掃描
@SpringBootApplication
//把你定義的組件掃入Spring上下文中
@ComponentScan({"com.xxx.xxx.cmp"})
public class LiteflowExampleApplication {
public static void main(String[] args) {
SpringApplication.run(LiteflowExampleApplication.class, args);
}
}業(yè)務(wù)類調(diào)用
@Component
public class FlowService{
@Resource
private FlowExecutor flowExecutor;
public void testConfig(){
System.out.println("start------------------");
User user = new User();
user.setRealName("張三");
user.setAddIp("1.2.3.4");
user.setAddress("中國四川成都");
LiteflowResponse response = flowExecutor.execute2Resp("chain1", "arg111111", user);
System.out.println("end-----------------------");
}
}5、測試結(jié)果
控制臺打印參數(shù)順序:
user的RealName為:AC2B2 ,流程是先a 再c 再b
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot與velocity的結(jié)合的示例代碼
本篇文章主要介紹了SpringBoot與velocity的結(jié)合的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03
java的Jackson將json字符串轉(zhuǎn)換成泛型List
這篇文章主要介紹了java的Jackson將json字符串轉(zhuǎn)換成泛型List ,這里整理了詳細的代碼,有需要的小伙伴可以參考下。2017-02-02
Jenkins服務(wù)器更新密鑰后任務(wù)構(gòu)建不了的排查實錄與解決方案
在一次生產(chǎn)環(huán)境的 SSH 密鑰輪換中,我遇到了一個極其令人困惑的問題:在 Jenkins 服務(wù)器上手動執(zhí)行 ssh -i 命令測試新密鑰到目標(biāo)服務(wù)器,直接報 Permission denied,本文記錄了完整的排查過程,希望能幫助遇到類似故障的運維人員快速定位問題,需要的朋友可以參考下2026-05-05
SpringBoot連接Microsoft SQL Server實現(xiàn)登錄驗證
本文主要介紹了SpringBoot連接Microsoft SQL Server實現(xiàn)登錄驗證,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-05-05
使用Get方式提交數(shù)據(jù)到Tomcat服務(wù)器的方法
這篇文章將介紹向服務(wù)器發(fā)送數(shù)據(jù),并且服務(wù)器將數(shù)據(jù)的處理結(jié)果返回給客戶端,本文給大家介紹使用Get方式向服務(wù)器發(fā)送數(shù)據(jù),感興趣的朋友一起學(xué)習(xí)吧2016-04-04

