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***d88527 分钟前
SpringBoot 集成 Activiti 7 工作流引擎
java·spring boot·后端
计算机毕设VX:Fegn089531 分钟前
计算机毕业设计|基于springboot + vue小型房屋租赁系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
better_liang1 小时前
每日Java面试场景题知识点之-XXL-JOB分布式任务调度实践
java·spring boot·xxl-job·分布式任务调度·企业级开发
q***o3761 小时前
Spring Boot环境配置
java·spring boot·后端
hhzz1 小时前
Springboot项目中使用POI操作Excel(详细教程系列3/3)
spring boot·后端·excel·poi·easypoi
刀法如飞2 小时前
开箱即用的 DDD(领域驱动设计)工程脚手架,基于 Spring Boot 4.0.1 和 Java 21
java·spring boot·mysql·spring·设计模式·intellij-idea
小飞Coding3 小时前
Spring Boot 框架级扩展点全景图:从启动到关闭,打造你的 Starter 基石
spring boot
yrldjsbk4 小时前
第 3 章 实战项目 1:通用用户管理后端(接单高频需求)
spring boot·maven·mybatis
v***59834 小时前
springBoot连接远程Redis连接失败(已解决)
spring boot·redis·后端
Coder_Boy_6 小时前
基于SpringAI的在线考试系统-企业级软件研发工程应用规范实现细节
大数据·开发语言·人工智能·spring boot