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 小时前
01-基于springboot框架调用ollama下的模型完成基本功能
spring boot·后端·ollama·通义千问模型qwen
烧饼Fighting2 小时前
Jenkins自动化编译部署Spring Boot项目
spring boot·自动化·jenkins
闪电悠米2 小时前
黑马点评-Redis 消息队列-01_why_redis_mq
java·数据库·spring boot·redis·缓存·junit·消息队列
我登哥MVP3 小时前
Spring Boot 从“会用”到“精通”:内容协商原理
java·spring boot·后端·spring·java-ee·maven·lua
Flittly3 小时前
【AgentScope Java新手村系列】(1)框架简介与环境搭建
java·spring boot·笔记·spring·ai
星辰徐哥12 小时前
Spring Boot 微服务架构设计与实现
spring boot·后端·微服务
星辰徐哥12 小时前
Spring Boot 数据导入导出与报表生成
spring boot·后端·ui
明夜之约12 小时前
Spring Boot 自动装配源码
java·spring boot·后端
Leaton Lee12 小时前
Spring Boot分层架构详解:从Controller到Service再到Mapper的完整流程
java·spring boot·后端·架构
Micro麦可乐12 小时前
Spring Boot 实战:从零设计一个短链系统(含完整代码与数据库设计)
数据库·spring boot·后端·哈希算法·雪花算法·短链系统