ARTICLE DETAIL

资讯详情

深耕郑州网站建设与运营推广的一线实战洞察。

Flowable Spring Boot 集成:Starter 极速搭建完整教程

Flowable Spring Boot 集成:Starter 极速搭建完整教程 Flowable Spring Boot 集成Starter 极速搭建完整教程【免费下载链接】flowable-userguideFlowable最新中文文档,ai自动生成BPM体验地址http://flow.je4.cn/#/login项目地址: https://gitcode.com/gh_mirrors/fl/flowable-userguideFlowable Spring Boot 集成一直是 Java 开发者最关心的入门话题。作为一款轻量级的业务流程引擎BPMFlowable 提供了官方flowable-spring-boot-starter让你只需添加一个依赖、写一个主类就能在几分钟内跑起完整的流程引擎。本教程基于官方中文文档整理面向零基础新手带你一步步完成 Flowable Spring Boot 极速搭建从依赖配置、自动部署到数据库切换与监控一篇文章全搞定。 本教程内容参考官方中文文档 ch05a-Spring-Boot.adoc并可在仓库中随时查阅更多细节。为什么选择 Flowable Spring Boot 集成️Spring Boot 的核心思想是约定大于配置而 Flowable 与 Spring Boot 的深度集成正是这一理念的最佳体现。你不需要手动编写繁琐的 XML 配置不需要关心引擎的初始化顺序flowable-spring-boot-starter会为你处理好一切✅ 自动创建流程引擎所需的数据库表✅ 自动暴露ProcessEngine、CmmnEngine、DmnEngine等多个引擎 Bean✅ 所有 Flowable 服务如RuntimeService、TaskService自动注册为 Spring Bean✅ 自动启动 Spring Job Executor支持定时任务与异步任务极速上手添加 flowable-spring-boot-starter 依赖 Flowable Spring Boot 集成第一步就是在pom.xml中添加核心 Starter 依赖。如果不需要引入所有引擎也可以选择只含流程引擎的 Starter本文先从全家桶开始dependency groupIdorg.flowable/groupId artifactIdflowable-spring-boot-starter/artifactId version${flowable.version}/version /dependency接下来编写一个最简单的 Spring Boot 主类就这么简单import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; SpringBootApplication public class MyApplication { public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } }添加 H2 数据库驱动零配置跑通第一个应用 Flowable 需要数据库存储数据。如果此时直接运行会提示缺少数据库驱动。对于本地体验推荐先引入 H2 内存数据库做到真正的零配置启动dependency groupIdcom.h2database/groupId artifactIdh2/artifactId version1.4.197/version /dependency再次启动应用你会发现控制台自动打印出 H2 表创建日志各引擎IDM、DMN、Form、Content、CMMN、BPMN全部初始化完成Tomcat 在 8080 端口启动。仅仅添加依赖并加上SpringBootApplication注解Flowable Spring Boot 集成就在幕后替你完成了建库、建表、引擎初始化等全部工作。自动部署processes 目录放上流程定义即生效 Flowable Spring Boot 集成有一个非常实用的特性——自动部署。只要在classpath下创建processes目录放入 BPMN 2.0 流程定义文件如one-task-process.bpmn20.xml应用启动时就会自动部署。同理cases目录下的 CMMN 事例、forms目录下的表单定义也会被自动部署。BPMN 2.0 流程定义的核心是从 XML 文件经 Flowable 引擎解析生成包含图形信息的流程定义对象。一个最简单的单任务流程只需寥寥数行 XMLdefinitions xmlnshttp://www.omg.org/spec/BPMN/20100524/MODEL xmlns:flowablehttp://flowable.org/bpmn targetNamespaceExamples process idoneTaskProcess nameThe One Task Process startEvent idtheStart / sequenceFlow idflow1 sourceReftheStart targetReftheTask / userTask idtheTask namemy task / sequenceFlow idflow2 sourceReftheTask targetReftheEnd / endEvent idtheEnd / /process /definitions为了验证部署是否生效可以添加一个CommandLineRunnerBean在应用启动时查询流程定义和任务数量Bean public CommandLineRunner init(final RepositoryService repositoryService, final RuntimeService runtimeService, final TaskService taskService) { return strings - { System.out.println(Number of process definitions : repositoryService.createProcessDefinitionQuery().count()); System.out.println(Number of tasks : taskService.createTaskQuery().count()); runtimeService.startProcessInstanceByKey(oneTaskProcess); System.out.println(Number of tasks after process start: taskService.createTaskQuery().count()); }; }运行后你会看到输出流程定义数量为 1启动流程后任务数从 0 变为 1说明 Flowable Spring Boot 自动部署与流程启动完全正常。更换数据源切换到 MySQL 数据库的最快配置方法 ️默认情况下 H2 内存数据库在应用关闭后数据会丢失。生产环境通常需要切换到 MySQL。Flowable Spring Boot 集成更换数据源非常简单只要在application.properties中提供数据库连接信息并添加对应驱动依赖即可spring.datasource.urljdbc:mysql://127.0.0.1:3306/flowable-spring-boot?characterEncodingUTF-8 spring.datasource.usernameflowable spring.datasource.passwordflowabledependency groupIdmysql/groupId artifactIdmysql-connector-java/artifactId version5.1.45/version /dependency移除 H2 依赖后重启应用控制台会打印flowable.mysql.create.engine.sql等建表日志说明引擎已连接 MySQL 并使用 HikariCP 连接池。多次重启后任务数量不再丢失这就是持久化数据库带来的好处。调试技巧如何查看 Flowable 运行时数据 在开发阶段使用 H2 内存数据库调试单元测试时如何直观地查看 Flowable 数据官方文档给出了一套非常好用的调试方法在测试代码中打上断点以 Debug 模式运行 JUnit 测试然后在 IDE 的 Display 窗口中执行org.h2.tools.Server.createWebServer(-web).start()随后浏览器访问http://localhost:8082填入内存数据库 JDBC URL默认jdbc:h2:mem:flowable即可连接。此时你可以直接查询ACT_RU_TASK等引擎表实时观察任务的创建情况帮助理解流程执行的每一步。这一技巧对新手排查流程为什么没走到预期节点这类问题非常有效官方文档 ch04-API.adoc 的调试单元测试章节有完整截图说明。暴露 REST API在 Spring Boot 中快速封装接口 Flowable Spring Boot 集成的另一个亮点是 REST 支持。添加spring-boot-starter-web依赖后你只需注入RuntimeService和TaskService就能轻松封装业务接口Service public class MyService { Autowired private RuntimeService runtimeService; Autowired private TaskService taskService; Transactional public void startProcess() { runtimeService.startProcessInstanceByKey(oneTaskProcess); } Transactional public ListTask getTasks(String assignee) { return taskService.createTaskQuery().taskAssignee(assignee).list(); } }再用RestController暴露端点用 curl 即可调用curl -X POST http://localhost:8080/process启动流程curl http://localhost:8080/tasks?assigneekermit查询某人的待办任务列表。监控与运维Actuator 端点查看流程运行状态 Flowable 提供了 Spring Boot Actuator 端点默认映射到/actuator/flowable。Spring Boot 默认只开启info与health需要在application.properties中显式启用management.endpoint.flowable.enabledtrue启用后调用curl http://localhost:8080/actuator/flowable就能拿到完整统计信息包括已部署的流程定义、运行中的流程实例数、已完成任务数等非常便于线上监控{ deployedProcessDefinitions: [oneTaskProcess (v1)], processDefinitionCount: 1, runningProcessInstanceCount: {oneTaskProcess (v1): 0}, completedTaskCount: 2, openTaskCount: 0 }常用配置参数速查表 Flowable Spring Boot 集成支持通过application.properties灵活调整行为以下是几个高频参数配置项作用flowable.check-process-definitions是否自动部署流程定义默认trueflowable.database-schema-update数据库 schema 更新策略flowable.history-level历史级别如auditflowable.process.async-executor-activate是否启用异步执行器flowable.process.servlet.pathBPMN REST API 路径默认/process-apiflowable.cmmn.enabled是否启用 CMMN 引擎完整的参数列表含邮件、HTTP、LDAP、Actuator 等可查阅 ch05a-Spring-Boot.adoc 的配置 Flowable 应用章节。进阶整合多个 Starter 与自定义异步执行器 如果只需部分引擎可以按需组合依赖例如同时使用流程、CMMN、Form 引擎并集成 LDAPdependency groupIdorg.flowable/groupId artifactIdflowable-spring-boot-starter-process/artifactId version${flowable.version}/version /dependency dependency groupIdorg.flowable/groupId artifactIdflowable-spring-boot-starter-cmmn/artifactId version${flowable.version}/version /dependency异步执行器方面流程与 CMMN 引擎默认共享同一个 SpringTaskExecutor如需区分可用Process与Cmmn注解分别定义专门的执行器 Bean实现更精细的资源控制。写在最后 ✨本文带你走完了 Flowable Spring Boot 集成的全流程从添加flowable-spring-boot-starter依赖、零配置启动、自动部署流程定义到切换 MySQL、调试数据、暴露 REST API 与 Actuator 监控。可以看到得益于 Starter 的约定大于配置整个搭建过程几乎没有繁琐的 XML 配置非常适合新手快速上手。如果本文对你有帮助欢迎收藏后续还可以继续深入 BPMN 建模、网关编排与流程引擎高级调优探索更多 Flowable 的能力边界。【免费下载链接】flowable-userguideFlowable最新中文文档,ai自动生成BPM体验地址http://flow.je4.cn/#/login项目地址: https://gitcode.com/gh_mirrors/fl/flowable-userguide创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表