首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >LiteFlow 引擎框架

LiteFlow 引擎框架

原创
作者头像
花落花相惜
发布2021-12-15 14:39:00
2.4K0
发布2021-12-15 14:39:00
举报

初级开发

      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");
        }

中级开发

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();
            }
        }
    }
}

LiteFlow简介

LiteFlow就是为解耦复杂逻辑而生,如果你要对复杂业务逻辑进行新写或者重构,用liteflow最合适不过。它是一个轻量,快速的组件式流程引擎框架,组件编排,帮助解耦业务代码,让每一个业务片段都是一个组件,并支持热加载规则配置,实现即时修改。

使用Liteflow,你需要去把复杂的业务逻辑按代码片段拆分成一个个小组件,并定义一个规则流程配置。这样,所有的组件,就能按照你的规则配置去进行复杂的流转。

优点:

  • 编排利器
  • 规则轻量
  • 优雅稳定
  • 扩展灵活

特性:

  • 复杂业务的解耦利器,为所有组件提供统一的实现协议
  • 基于规则文件来编排流程,并可进行热编排
  • 框架中支持zookeeper流程配置,即时推送修改内容
  • 能自由扩展配置持久化源,提供扩展接口
  • 支持springboot的自动装配,也支持spring的配置和非spring的项目
  • 提供串行和并行2种模式,提供常见常见的表达式语句
  • 提供无级嵌套子流程模式
  • 数据槽高并发隔离机制
  • 自带简单的监控,能够知道每个组件的运行耗时排行

架构设计

image.png

Liteflow适用于哪些场景

Liteflow适用于拥有复杂逻辑的业务,比如说价格引擎,下单流程,规则校验等,这些业务往往都拥有很多步骤,这些步骤完全可以按照业务粒度拆分成一个个独立的组件,进行装配复用变更。使用Liteflow,你会得到一个灵活度高,扩展性很强的系统。因为组件之间相互独立,也也可以避免改一处而动全身的这样的风险。

Liteflow的Gitee仓库:https://gitee.com/dromara/liteFlow

Liteflow的Github仓库:https://github.com/dromara/liteflow

使用文档地址

springboot中集成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);
    }
}
  • 创建flow.xml文件 src/main/resources/config/flow.xml

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>
  • application.properties
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

总结

1) 引入依赖
2) 定义流程节点继承NodeComponent 
3) 注入:   
      @Resource 
      private FlowExecutor flowExecutor;
4)application.properties:
      liteflow.rule-source=config/flow.xml

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
作者已关闭评论
0 条评论
热度
最新
推荐阅读
目录
  • 初级开发
  • 中级开发
  • LiteFlow简介
  • 优点:
  • 特性:
  • 架构设计
  • Liteflow适用于哪些场景
  • springboot中集成liteflow
  • 总结
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档