Flowable7工作流引擎在Spring Boot3中的集成与实践
发布时间:2026/10/14 0:19:46 锦皓数字建站

1. 项目概述最近在重构公司的工作流引擎决定将老旧的Activiti5升级到Flowable7。作为一款轻量级业务流程引擎Flowable7在Spring Boot3.x环境下展现出强大的兼容性和灵活性。今天我就来分享下从零开始集成Flowable7的完整过程包括环境搭建、流程设计、部署运行等核心环节。这个方案特别适合需要快速构建审批流、任务流转的中小型系统。相比ActivitiFlowable7提供了更简洁的API、更完善的REST支持和更活跃的社区。通过Spring Boot Starter方式集成30分钟内就能让第一个流程跑起来。2. 环境准备与基础集成2.1 依赖配置首先在pom.xml中添加关键依赖dependency groupIdorg.flowable/groupId artifactIdflowable-spring-boot-starter/artifactId version7.0.0/version /dependency dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-jdbc/artifactId /dependency注意Flowable7.x默认使用Spring Boot3.x的自动配置如果项目还在用Spring Boot2.x需要特别注意版本兼容性问题。2.2 数据库配置Flowable默认会创建23张表来存储流程定义、实例、任务等数据。在application.yml中配置spring: datasource: url: jdbc:mysql://localhost:3306/flowable-demo?useSSLfalse username: root password: 123456 driver-class-name: com.mysql.cj.jdbc.Driver flowable: database-schema-update: true关键参数说明database-schema-update: true表示自动创建或更新表结构生产环境建议改为false并通过Flyway管理2.3 启动验证启动应用后检查日志看到以下输出说明集成成功o.f.s.SpringProcessEngineConfiguration : ProcessEngine default created o.f.s.SpringProcessEngineConfiguration : Flowable BPMN Process Engine configured3. 流程设计与部署3.1 创建BPMN流程图在resources/processes下新建leave-request.bpmn20.xmldefinitions xmlnshttp://www.omg.org/spec/BPMN/20100524/MODEL process idleaveRequest name请假流程 startEvent idstart/ userTask idleaderAudit name主管审批/ exclusiveGateway iddecision/ sequenceFlow sourceRefstart targetRefleaderAudit/ sequenceFlow sourceRefleaderAudit targetRefdecision/ sequenceFlow sourceRefdecision targetRefhrAudit conditionExpression xsi:typetFormalExpression ![CDATA[${days 3}]] /conditionExpression /sequenceFlow userTask idhrAudit nameHR审批/ endEvent idend/ sequenceFlow sourceRefhrAudit targetRefend/ /process /definitions这个流程定义了员工发起请假申请主管审批请假天数3天时需要HR二次审批3.2 部署流程定义通过RepositoryService部署流程RestController public class ProcessController { Autowired private RepositoryService repositoryService; PostMapping(/deploy) public String deploy() { Deployment deployment repositoryService.createDeployment() .addClasspathResource(processes/leave-request.bpmn20.xml) .name(请假流程部署) .deploy(); return 部署成功ID deployment.getId(); } }部署成功后可以在ACT_RE_PROCDEF表中看到流程定义信息。4. 流程实例管理4.1 启动流程实例Autowired private RuntimeService runtimeService; public String startProcess(String employee, int days) { MapString, Object variables new HashMap(); variables.put(employee, employee); variables.put(days, days); ProcessInstance instance runtimeService.startProcessInstanceByKey( leaveRequest, variables); return instance.getId(); }4.2 查询与完成任务Autowired private TaskService taskService; // 查询用户任务 public ListTask getTasks(String assignee) { return taskService.createTaskQuery() .taskAssignee(assignee) .list(); } // 完成任务 public void completeTask(String taskId) { taskService.complete(taskId); }5. 实战技巧与避坑指南5.1 性能优化建议异步执行器配置flowable: async-executor-activate: true async-executor-core-pool-size: 10 async-executor-max-pool-size: 50历史数据清理historyService.createHistoricProcessInstanceQuery() .finished() .list() .forEach(instance - historyService.deleteHistoricProcessInstance(instance.getId()));5.2 常见问题解决问题1启动时报表不存在错误检查数据库用户是否有建表权限确认flowable.database-schema-update配置为true问题2流程定义修改后不生效每次修改BPMN文件需要重新部署生成新版本使用repositoryService.createDeployment().enableDuplicateFiltering(true)问题3任务查询慢为ACT_RU_TASK表的PROC_INST_ID_字段添加索引避免使用通配符查询6. 扩展功能实现6.1 动态分配任务处理人在BPMN中使用表达式动态指定处理人userTask idleaderAudit name主管审批 flowable:assignee${employee.department.leader}/6.2 邮件通知集成配置邮件服务器后可以在流程中添加邮件任务serviceTask idsendMail flowable:typemail extensionElements flowable:field nameto flowable:string${employee.email}/flowable:string /flowable:field flowable:field namesubject flowable:string您的请假申请已提交/flowable:string /flowable:field /extensionElements /serviceTask7. 监控与管理7.1 流程实例监控// 查询运行中的流程实例 runtimeService.createProcessInstanceQuery() .processDefinitionKey(leaveRequest) .list(); // 获取流程变量 runtimeService.getVariables(processInstanceId);7.2 使用Flowable UI添加依赖后访问/flowable-uidependency groupIdorg.flowable/groupId artifactIdflowable-ui-modeler-rest/artifactId version7.0.0/version /dependency8. 生产环境建议数据库连接池建议使用HikariCP并合理配置大小日志分离将Flowable日志单独输出到文件集群部署需要配置分布式锁和共享数据库版本控制使用Git管理BPMN文件变更历史实际项目中我们发现当并发量超过100TPS时需要特别注意异步执行器的线程池配置和数据库连接池大小。另外建议对历史数据设置自动归档策略避免主表数据膨胀影响查询性能。
锦
锦皓数字建站
深耕本土企业品牌数字化升级,专注原创端正雅致商务官网,从视觉设计到稳定运维全程保驾护航。