spring boot整合flowable(分库)
资源地址
github : https://github.com/flowable/flowable-engine
flowable是什么
flowable是流程管理开源解决方案,从Activiti中分离出来的;由原 Activiti 的核心开发团队创建,旨在提供一个更加轻量级、更专注于核心业务流程管理的解决方案。flowable仅仅关注流程怎么走,不关心流程状态变化,更像一个代码编排工具,数据的可见性,状态流转,操作权限等还是由业务实现。
整合
由于flowable自己的表有70多张,不适合与业务使用同一个库,这里使用分库部署
- pom.xml
<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.suozq</groupId><artifactId>workflow</artifactId><version>0.0.1-SNAPSHOT</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.7.18</version><relativePath/><!-- lookup parent from repository --></parent><properties><java.version>8</java.version><flowable.version>6.8.1</flowable.version><!-- 可选:锁定 MySQL 驱动版本 --><mysql.version>8.0.33</mysql.version></properties><dependencies><!-- Web 支持 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- Flowable --><dependency><groupId>org.flowable</groupId><artifactId>flowable-spring-boot-starter</artifactId><version>${flowable.version}</version></dependency><!-- MySQL 驱动 --><dependency><groupId>com.mysql</groupId><artifactId>mysql-connector-j</artifactId><scope>runtime</scope></dependency><!-- (可选)Actuator 监控 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><!-- (可选)Lombok 简化代码 --><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><!-- 测试 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><excludes><exclude><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></exclude></excludes></configuration></plugin></plugins></build></project>- application.yml
spring:datasource:url: jdbc:mysql://localhost:3306/biz_db?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&nullCatalogMeansCurrent=trueusername: root password: root driver-class-name: com.mysql.cj.jdbc.Driver flowable:database-schema-update:truedatasource:jdbc-url: jdbc:mysql://localhost:3306/flowable_db?useSSL=false&nullCatalogMeansCurrent=true&serverTimezone=Asia/Shanghai&characterEncoding=utf8username: root password: root driver-class-name: com.mysql.cj.jdbc.Driver activity-font-name:"宋体"annotation-font-name:"宋体"label-font-name:"宋体"database-schema-update :true : 启动时检测是否有flowable相关表,没有创建,有就校验;false: 启动时不创建表,生产环境;create-drop:启动时创建,关闭时删除,测试环境;drop-create:启动时先删除再创建,测试环境;- FlowableConfig.java
packagecom.suozq.workflow.config;importjavax.sql.DataSource;importorg.flowable.app.spring.SpringAppEngineConfiguration;importorg.flowable.spring.boot.EngineConfigurationConfigurer;importorg.flowable.spring.boot.ProcessEngineAutoConfiguration;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.boot.autoconfigure.condition.ConditionalOnProperty;importorg.springframework.boot.context.properties.ConfigurationProperties;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importorg.springframework.context.annotation.Import;importorg.springframework.jdbc.datasource.DataSourceTransactionManager;importorg.springframework.lang.NonNull;importorg.springframework.transaction.PlatformTransactionManager;importcom.zaxxer.hikari.HikariDataSource;@ConfigurationpublicclassFlowableConfigimplementsEngineConfigurationConfigurer<SpringAppEngineConfiguration>{@Bean@ConfigurationProperties(prefix ="flowable.datasource")@NonNullpublicDataSourceflowableDataSource(){DataSource dataSource =newHikariDataSource();return dataSource;}/** * 配置Flowable专用事务管理器 * * @param flowableDataSource Flowable数据源 * @return Flowable事务管理器 */@BeanpublicPlatformTransactionManagerflowableTransactionManager(){returnnewDataSourceTransactionManager(flowableDataSource());}@Overridepublicvoidconfigure(SpringAppEngineConfiguration engineConfiguration){ engineConfiguration.setDataSource(flowableDataSource()); engineConfiguration.setTransactionManager(flowableTransactionManager());}}- FlowableServiceExample.java
@ServicepublicclassFlowableServiceExample{@AutowiredprivateRepositoryService repositoryService;// 流程定义管理@AutowiredprivateRuntimeService runtimeService;// 运行时流程实例@AutowiredprivateTaskService taskService;// 任务管理@AutowiredprivateHistoryService historyService;// 历史数据@AutowiredprivateManagementService managementService;// 引擎管理@AutowiredprivateDynamicBpmnService dynamicBpmnService;// 动态修改// 1. 部署流程定义publicDeploymentdeployProcess(){return repositoryService.createDeployment().addClasspathResource("processes/leave.bpmn20.xml").name("请假流程").deploy();}// 2. 启动流程实例publicProcessInstancestartProcess(String processKey,Map<String,Object> variables){return runtimeService.startProcessInstanceByKey( processKey, variables);}// 3. 查询publicList<Task>getUserTasks(String userId){return taskService.createTaskQuery().taskAssignee(userId).active().list();}//完成任务publicvoidcompleteTask(String taskId,Map<String,Object> variables){ taskService.complete(taskId, variables);}// 4. 历史数据查询publicList<HistoricProcessInstance>getProcessHistory(){return historyService.createHistoricProcessInstanceQuery().finished().orderByProcessInstanceEndTime().desc().list();}}前端
从github下载整个压缩包后,里面有flowable-ui.war, 在同级目录下,添加application.properties配置文件,配置数据库为上面的flowable_db,
server.port=8081 spring.datasource.url=jdbc:mysql://127.0.0.1:3306/flowable_db?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai spring.datasource.username=root spring.datasource.password=root spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver flowable.database-schema-update=false
使用java -jar flowable-ui.war 命令启动,即可通过页面配置流程;配置好后,创建应用,关联流程,发布,即可在程序中使用该流程了;
坑点
- 启动报错
no flowable tables in db与Table 'xxxx.act_ge_property' doesn't exist
这种情况多主要原因就是flowable判断表是否存在的语句有漏洞,没有明确指定库名!相同物理库下,一旦也有flowable的表,就会认为表已经创建!
解决方案(仅限MySQL): 数据库url 添加参数&nullCatalogMeansCurrent=true
该参数代表如果不指定库名,则自动使用当前库名
flowable-ui 官方包中没有mysql驱动
这真挺坑的,没有mysql驱动,打个包放这儿干啥?直接往里放驱动,还会因为压缩程序问题,导致整个包都不能用! 反正我尝试了很多方式,都没有成功把驱动加入,要么就是配置外置包,要么就通过源码编译,我这里使用源码编译
flowable-ui.war
账密:admin/test