SpringBoot 2.6.2 + Flowable 6.7.2 整合实战:手把手教你搞定在线流程设计器(Modeler)

张开发
2026/4/21 13:01:53 15 分钟阅读

分享文章

SpringBoot 2.6.2 + Flowable 6.7.2 整合实战:手把手教你搞定在线流程设计器(Modeler)
SpringBoot 2.6.2 Flowable 6.7.2 深度整合实战从零构建企业级流程设计平台在企业级应用开发中工作流引擎的选择往往决定了业务流程管理的灵活性和效率。本文将带您深入探索如何基于SpringBoot 2.6.2与Flowable 6.7.2构建一个功能完备的流程设计平台重点解决实际开发中的关键问题与性能优化点。1. 环境准备与基础配置1.1 版本选型与依赖管理选择SpringBoot 2.6.2与Flowable 6.7.2的组合主要基于以下考虑官方兼容性验证长期支持版本稳定性新特性支持如增强的REST API核心POM依赖配置properties flowable.version6.7.2/flowable.version /properties dependencies !-- Flowable核心依赖 -- dependency groupIdorg.flowable/groupId artifactIdflowable-spring-boot-starter/artifactId version${flowable.version}/version /dependency !-- Modeler前端依赖 -- dependency groupIdorg.flowable/groupId artifactIdflowable-ui-modeler-rest/artifactId version${flowable.version}/version /dependency !-- 数据库相关 -- dependency groupIdmysql/groupId artifactIdmysql-connector-java/artifactId version8.0.28/version /dependency /dependencies1.2 数据库配置优化针对MySQL 8.0的特定配置建议spring: datasource: url: jdbc:mysql://localhost:3306/flowable_db?useSSLfalseserverTimezoneUTCcharacterEncodingUTF-8 username: root password: yourpassword driver-class-name: com.mysql.cj.jdbc.Driver flowable: database-schema-update: true async-executor-activate: false注意生产环境务必设置database-schema-update为false避免意外修改表结构2. Modeler集成实战2.1 前端资源部署从Flowable官方GitHub获取前端资源下载flowable-engine 6.7.2解压后定位到/modules/flowable-ui/flowable-ui-modeler-frontend/src/main/resources/static将static目录复制到项目的resources/public目录下目录结构示例src/main/resources/ └── public/ └── modeler/ ├── css/ ├── fonts/ ├── images/ └── scripts/2.2 授权绕过方案默认的IDM授权会阻碍开发效率通过自定义Security配置实现免登录Configuration Order(SecurityProperties.BASIC_AUTH_ORDER - 2) public class ModelerSecurityConfig extends WebSecurityConfigurerAdapter { Override protected void configure(HttpSecurity http) throws Exception { http .csrf().disable() .authorizeRequests() .antMatchers(/modeler/**).permitAll() .anyRequest().authenticated() .and() .formLogin().disable(); } }2.3 用户信息定制重写RemoteAccountResource解决500错误RestController RequestMapping(/app) public class CustomAccountResource { GetMapping(/rest/account) public UserRepresentation getCurrentUser() { UserRepresentation user new UserRepresentation(); user.setId(admin); user.setFullName(系统管理员); user.setPrivileges(Arrays.asList( DefaultPrivileges.ACCESS_MODELER, DefaultPrivileges.ACCESS_ADMIN )); return user; } }3. 深度配置优化3.1 静态资源缓存策略通过WebMvcConfigurer优化前端加载性能Configuration public class WebConfig implements WebMvcConfigurer { Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler(/modeler/**) .addResourceLocations(classpath:/public/modeler/) .setCacheControl(CacheControl.maxAge(365, TimeUnit.DAYS)); } }3.2 多数据源冲突解决当同时使用MyBatis时需明确指定主数据源Configuration MapperScan(basePackages com.yourpackage.mapper, sqlSessionFactoryRef businessSqlSessionFactory) public class BusinessDataSourceConfig { Primary Bean ConfigurationProperties(spring.datasource.business) public DataSource businessDataSource() { return DataSourceBuilder.create().build(); } Primary Bean public SqlSessionFactory businessSqlSessionFactory( Qualifier(businessDataSource) DataSource dataSource) throws Exception { SqlSessionFactoryBean bean new SqlSessionFactoryBean(); bean.setDataSource(dataSource); return bean.getObject(); } }4. 企业级流程设计实践4.1 通用审批流程模板设计一个可复用的审批流程模板process idCommonApproval name通用审批流程 isExecutabletrue startEvent idstartEvent / userTask idapprovalTask name审批节点 flowable:assignee${approver} extensionElements flowable:taskListener eventcreate classcom.yourpackage.listener.ApprovalTaskListener / /extensionElements /userTask exclusiveGateway iddecisionGateway / endEvent idendEvent / sequenceFlow sourceRefstartEvent targetRefapprovalTask / sequenceFlow sourceRefapprovalTask targetRefdecisionGateway / sequenceFlow sourceRefdecisionGateway targetRefendEvent conditionExpression xsi:typetFormalExpression ${approvalResult approved} /conditionExpression /sequenceFlow /process4.2 动态审批人配置通过RuntimeService动态设置审批人public class ProcessService { Autowired private RuntimeService runtimeService; public String startProcess(String businessKey, String initiator) { MapString, Object variables new HashMap(); variables.put(initiator, initiator); variables.put(approver, determineApprover(initiator)); ProcessInstance instance runtimeService.startProcessInstanceByKey( CommonApproval, businessKey, variables); return instance.getId(); } private String determineApprover(String initiator) { // 实现你的审批人逻辑 return manager; } }5. 性能优化与监控5.1 异步执行器配置flowable: async-executor: activate: true core-pool-size: 5 max-pool-size: 10 queue-size: 1005.2 流程实例监控集成Spring Boot Actuator获取运行指标Endpoint(id flowable-metrics) public class FlowableMetricsEndpoint { ReadOperation public MapString, Object metrics() { MapString, Object metrics new HashMap(); metrics.put(activeProcessInstances, runtimeService.createProcessInstanceQuery().count()); // 添加更多指标... return metrics; } }6. 安全加固方案6.1 XSS防护配置Bean public FilterRegistrationBeanXssFilter xssFilter() { FilterRegistrationBeanXssFilter registration new FilterRegistrationBean(); registration.setFilter(new XssFilter()); registration.addUrlPatterns(/modeler/*); registration.setOrder(Ordered.HIGHEST_PRECEDENCE); return registration; }6.2 敏感操作审计实现ExecutionListener记录关键操作public class AuditListener implements ExecutionListener { Override public void notify(DelegateExecution execution) { String processId execution.getProcessInstanceId(); String eventName execution.getEventName(); // 记录审计日志 auditService.logOperation(processId, eventName); } }7. 容器化部署方案7.1 Dockerfile示例FROM openjdk:11-jre-slim VOLUME /tmp COPY target/flowable-app.jar app.jar ENTRYPOINT [java,-Djava.security.egdfile:/dev/./urandom,-jar,/app.jar]7.2 Kubernetes部署配置apiVersion: apps/v1 kind: Deployment metadata: name: flowable-modeler spec: replicas: 2 selector: matchLabels: app: flowable template: metadata: labels: app: flowable spec: containers: - name: flowable image: your-registry/flowable-modeler:6.7.2 ports: - containerPort: 8080 resources: limits: memory: 1Gi cpu: 500m8. 常见问题解决方案8.1 中文乱码问题配置字体解决流程图生成乱码Configuration public class FlowableConfig implements EngineConfigurationConfigurerSpringProcessEngineConfiguration { Override public void configure(SpringProcessEngineConfiguration config) { config.setActivityFontName(SimSun); config.setLabelFontName(SimSun); config.setAnnotationFontName(SimSun); } }8.2 高并发场景优化# 增加连接池大小 spring.datasource.hikari.maximum-pool-size20 # 优化事务超时 spring.transaction.default-timeout30s9. 扩展功能实现9.1 自定义表单设计器集成formio实现动态表单// 在modeler/index.html中添加 Formio.createForm(document.getElementById(formio), { components: [ { type: textfield, key: firstName, label: First Name } ] });9.2 消息通知集成通过TaskListener发送审批通知public class NotificationTaskListener implements TaskListener { Override public void notify(DelegateTask task) { String assignee task.getAssignee(); String message 您有新的审批任务: task.getName(); notificationService.send(assignee, message); } }10. 最佳实践总结在实际项目部署中我们发现以下配置组合效果最佳性能关键参数参数名推荐值说明flowable.async-executor.core-pool-sizeCPU核心数×2异步任务线程池spring.datasource.hikari.maximum-pool-size50数据库连接池flowable.process-definition-cache-limit1000流程定义缓存开发建议始终在测试环境验证流程变更使用版本控制管理BPMN文件为关键业务节点添加监听器记录审计日志定期清理历史数据设置flowable.history-level通过本文的深度整合方案我们成功构建了一个日均处理10万流程实例的生产级系统。核心在于合理配置线程池、优化数据库访问以及实现精细化的权限控制。

更多文章