spring boot整合flowable(分库)

资源地址

github : https://github.com/flowable/flowable-engine

flowable是什么

flowable是流程管理开源解决方案,从Activiti中分离出来的;由原 Activiti 的核心开发团队创建,旨在提供一个更加轻量级、更专注于核心业务流程管理的解决方案。flowable仅仅关注流程怎么走,不关心流程状态变化,更像一个代码编排工具,数据的可见性,状态流转,操作权限等还是由业务实现。

整合

由于flowable自己的表有70多张,不适合与业务使用同一个库,这里使用分库部署

  • pom.xml
xml 复制代码
<project xmlns="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
yml 复制代码
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/biz_db?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&nullCatalogMeansCurrent=true
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver
flowable:
  database-schema-update: true 
  datasource:
     jdbc-url: jdbc:mysql://localhost:3306/flowable_db?useSSL=false&nullCatalogMeansCurrent=true&serverTimezone=Asia/Shanghai&characterEncoding=utf8
     username: 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
java 复制代码
package com.suozq.workflow.config;

import javax.sql.DataSource;

import org.flowable.app.spring.SpringAppEngineConfiguration;
import org.flowable.spring.boot.EngineConfigurationConfigurer;
import org.flowable.spring.boot.ProcessEngineAutoConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.lang.NonNull;
import org.springframework.transaction.PlatformTransactionManager;

import com.zaxxer.hikari.HikariDataSource;



@Configuration
public class FlowableConfig implements EngineConfigurationConfigurer<SpringAppEngineConfiguration>{

  
    @Bean
    @ConfigurationProperties(prefix = "flowable.datasource")
    @NonNull
    public DataSource flowableDataSource() {
        DataSource dataSource = new HikariDataSource();
        return dataSource;
    }

    /**
     * 配置Flowable专用事务管理器
     *
     * @param flowableDataSource Flowable数据源
     * @return Flowable事务管理器
     */
    @Bean
    public PlatformTransactionManager flowableTransactionManager() {
        return new DataSourceTransactionManager(flowableDataSource());
    }



    @Override
    public void configure(SpringAppEngineConfiguration engineConfiguration) {
        engineConfiguration.setDataSource(flowableDataSource());
        engineConfiguration.setTransactionManager(flowableTransactionManager());
    }

  
}
  • FlowableServiceExample.java
java 复制代码
@Service
public class FlowableServiceExample {
    
    @Autowired
    private RepositoryService repositoryService;  // 流程定义管理
    @Autowired
    private RuntimeService runtimeService;        // 运行时流程实例
    @Autowired
    private TaskService taskService;              // 任务管理
    @Autowired
    private HistoryService historyService;        // 历史数据
    @Autowired
    private ManagementService managementService;  // 引擎管理
    @Autowired
    private DynamicBpmnService dynamicBpmnService; // 动态修改
    
    // 1. 部署流程定义
    public Deployment deployProcess() {
        return repositoryService.createDeployment()
            .addClasspathResource("processes/leave.bpmn20.xml")
            .name("请假流程")
            .deploy();
    }
    
    // 2. 启动流程实例
    public ProcessInstance startProcess(String processKey, 
                                       Map<String, Object> variables) {
        return runtimeService.startProcessInstanceByKey(
            processKey, variables);
    }
    
    // 3. 查询
    public List<Task> getUserTasks(String userId) {
        return taskService.createTaskQuery()
            .taskAssignee(userId)
            .active()
            .list();
    }
    //完成任务
    public void completeTask(String taskId, 
                            Map<String, Object> variables) {
        taskService.complete(taskId, variables);
    }
    
    // 4. 历史数据查询
    public List<HistoricProcessInstance> getProcessHistory() {
        return historyService.createHistoricProcessInstanceQuery()
            .finished()
            .orderByProcessInstanceEndTime().desc()
            .list();
    }
}

前端

从github下载整个压缩包后,里面有flowable-ui.war, 在同级目录下,添加application.properties配置文件,配置数据库为上面的flowable_db,

java 复制代码
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 命令启动,即可通过页面配置流程;配置好后,创建应用,关联流程,发布,即可在程序中使用该流程了;

坑点

  1. 启动报错no flowable tables in dbTable 'xxxx.act_ge_property' doesn't exist

这种情况多主要原因就是flowable判断表是否存在的语句有漏洞,没有明确指定库名!相同物理库下,一旦也有flowable的表,就会认为表已经创建!

解决方案(仅限MySQL): 数据库url 添加参数&nullCatalogMeansCurrent=true

该参数代表如果不指定库名,则自动使用当前库名

相关推荐
码匠君2 小时前
Dante Cloud 升级 Spring Boot 4 经验分享
经验分享·spring boot·后端
大学生资源网2 小时前
基于JavaWeb的邮件收发系统的设计与实现(源码+文档)
java·开发语言·spring boot·mysql·毕业设计·源码·课程设计
IT 行者2 小时前
Spring Boot 4 升级指南:告别RestTemplate,拥抱现代HTTP客户端
spring boot·后端·http
qq_12498707532 小时前
基于微信小程序的校园资讯共享平台的设计与实现(源码+论文+部署+安装)
spring boot·后端·微信小程序·小程序·毕业设计
期待のcode2 小时前
JWT令牌
前端·javascript·spring boot·安全
计算机毕设指导63 小时前
基于微信小程序的派出所业务管理系统【源码文末联系】
java·spring boot·mysql·微信小程序·小程序·tomcat·uniapp
番茄撒旦在上3 小时前
Docker部署springboot项目
服务器·spring boot·docker·容器
用户8307196840823 小时前
Spring Boot 多数据源与事务管理深度解析:从原理到实践
java·spring boot
invicinble3 小时前
关于java集合--set篇
spring boot