以东哥家的京东为例,我现在进了一批只需998的好家伙.每次618要进行大促销668,双十一888,双十二180.
看到需求大喊一句,这么简单看不起我?于是一顿操作猛如虎.写道:
if ("618".equals(day)){
System.out.println("668");
}else if ("11.11".equals(day)){
System.out.println("888");
}else if ("12.12".equals(day)){
System.out.println("180");
}else{
System.out.println("998");
}
image.png
好家伙,你这是ifelse一哥呀 于是乎上来一套策略
public interface NodeComponent {
void process() ;
}
@Component
public class AComponent extends NodeComponent {
@Override
public void process() {
/**
* 这里逻辑省略
*
*/
System.out.println("618");
}
}
@Component
public class BComponent extends NodeComponent {
@Override
public void process() {
/**
* 这里逻辑省略
*
*/
System.out.println("11.11");
}
}
//下面省略12.12
//主流程
@Component
public class TestFlow {
@Resource
private Map<String, NodeComponent> map;
public void run(String name) throws Exception {
for (Map.Entry<String, NodeComponent> entry : map.entrySet()) {
String key = entry.getKey();
if (StringUtils.equals(name,key)){
entry.getValue().process();
}
}
}
}
我这还有一个系统需求如下:undefined 需要进行查询前的校验,先进行A校验->B校验->>C校验->>D校验,这时候怎么办?..责任链?undefined 那如果流程改了undefined B校验->A校验->>C校验->>D校验undefined 或者undefined B校验->D校验->>A校验->>Cundefined 思考一下?你是不是想打一顿产品?别急,这时候新玩意来了.....往下看>>>>>>>
image.png
LiteFlow就是为解耦复杂逻辑而生,如果你要对复杂业务逻辑进行新写或者重构,用liteflow最合适不过。它是一个轻量,快速的组件式流程引擎框架,组件编排,帮助解耦业务代码,让每一个业务片段都是一个组件,并支持热加载规则配置,实现即时修改。
使用Liteflow,你需要去把复杂的业务逻辑按代码片段拆分成一个个小组件,并定义一个规则流程配置。这样,所有的组件,就能按照你的规则配置去进行复杂的流转。
image.png
Liteflow适用于拥有复杂逻辑的业务,比如说价格引擎,下单流程,规则校验等,这些业务往往都拥有很多步骤,这些步骤完全可以按照业务粒度拆分成一个个独立的组件,进行装配复用变更。使用Liteflow,你会得到一个灵活度高,扩展性很强的系统。因为组件之间相互独立,也也可以避免改一处而动全身的这样的风险。
Liteflow的Gitee仓库:https://gitee.com/dromara/liteFlow
Liteflow的Github仓库:https://github.com/dromara/liteflow
<dependency>
<groupId>com.yomahub</groupId>
<artifactId>liteflow-spring-boot-starter</artifactId>
<version>${project.parent.version}</version>
</dependency>
@Component("a")
public class AComponent extends NodeComponent {
@Override
public void process() {
String str = this.getSlot().getRequestData();
System.out.println(str);
System.out.println("Acomponent executed!");
this.getSlot().setOutput(this.getNodeId(), "A component output");
}
}
@Component("b")
public class BComponent extends NodeComponent {
@Override
public void process() {
try {
Thread.sleep(400L);
String[] temp = new String[1000];
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Bcomponent executed!");
}
}
@Component
public class TestFlow{
@Resource
private FlowExecutor flowExecutor;
@Override
public void run(String... args) throws Exception {
Slot slot = flowExecutor.execute("chain3", "it's a request");
System.out.println(slot);
}
}
image.png
<?xml version="1.0" encoding="UTF-8"?>
<flow>
<chain name="chain1">
<then value="a,cond(b|d)"/> <!-- cond是条件节点,根据cond里的逻辑决定路由到b节点还是d节点,可以配置多个 -->
<then value="e,f,g"/>
</chain>
<chain name="chain2">
<then value="a,c"/> <!-- then表示串行 -->
<when value="b,d"/> <!-- when表示串行 -->
<then value="e,f,g"/>
</chain>
</flow>
liteflow.rule-source=config/flow.xml
#------以下非必须-------
#slot的数量,默认值为1024
liteflow.slot-size=2048
#异步线程最长的等待时间秒(只用于when),默认值为15
liteflow.when-max-wait-second=20
#是否开启监控log打印,默认值为false
liteflow.monitor.enable-log=true
#监控队列存储大小,默认值为200
liteflow.monitor.queue-limit=300
#监控一开始延迟多少执行,默认值为300000毫秒,也就是5分钟
liteflow.monitor.delay=10000
#监控日志打印每过多少时间执行一次,默认值为300000毫秒,也就是5分钟
liteflow.monitor.period=10000
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。