flowable java_使用Flowable Process Engine在Java中进行Saga流程编排

flowable java

by Felix Kuestahler

费利克斯·库斯塔勒(Felix Kuestahler)

使用Flowable Process Engine在Java中进行Saga流程编排 (Saga Process Orchestration in Java Using the Flowable Process Engine)

使用Camel Task在Flowable中定义Saga Process Controller,以进行系统集成 (Define a Saga Process Controller in Flowable by using the Camel Task for system integration)

介绍 (Introduction)

In this tutorial, I did an assessment of the capability of Flowable to act as a Saga Process Controller.

在本教程中,我对Flowable充当Saga Process Controller的能力进行了评估。

Flowable provides a core set of open source business process engines that are compact and highly efficient. They provide a workflow and Business Process Management (BPM) platform for developers, system admins and business users. At its heart is a lightning fast, tried and tested dynamic BPMN process engine, with accompanying DMN decision tables and CMMN case management engines, all written in Java. They are Apache 2.0 licensed open source, with a committed community. (Link)

Flowable提供了一组紧凑而高效的开源业务流程引擎核心。 它们为开发人员,系统管理员和业务用户提供了工作流和业务流程管理(BPM)平台。 它的核心是一个闪电般的,经过实践检验的动态BPMN流程引擎,以及随附的DMN决策表和CMMN案例管理引擎,所有这些都是用Java编写的。 它们是Apache 2.0许可的开放源代码,拥有一个致力于的社区。 ( 链接 )

As one can see, Flowable is positioned as a Workflow and Business Process Management Platform. What we want to understand is the feasibility of Flowable in context of low-level service orchestration, defined as Saga Orchestration.

可以看到,Flowable被定位为工作流和业务流程管理平台。 我们想了解的是在定义为Saga Orchestration的低级服务编排中Flowable的可行性。

Refer to the following excellent article by Bernd Rücker in order to get an overview of how to tackle “Business Transactions without two-phase commit

请参阅以下BerndRücker的出色文章,以概述如何处理“ 无需两阶段提交的业务交易

The Saga pattern describes how to solve distributed (business) transactions without two-phase-commit as this does not scale in distributed systems. The basic idea is to break the overall transaction into multiple steps or activities. Only the steps internally can be performed in atomic transactions but the overall consistency is taken care of by the Saga. The Saga has the responsibility to either get the overall business transaction completed or to leave the system in a known termination state. (Link)

Saga模式描述了如何在没有两阶段提交的情况下解决分布式(业务)事务,因为这在分布式系统中无法扩展。 基本思想是将整个交易分为多个步骤或活动。 Saga只能在原子事务中执行内部步骤,但总体一致性是得到照顾的。 Saga负责完成整体业务交易或使系统处于已知的终止状态。 ( 链接 )

In this article, I don’t address compensating transaction. I will concentrate on the basic skeleton necessary to execute Saga process orchestration. It’s mainly a combination of the workflow capability provided by Flowable combined with the power of Apache Camel (a Java integration framework based on the EIP pattern). Apache Camel is a first-citizen in Flowable via the so-called “Camel Task”.

在本文中,我不讨论补偿交易。 我将专注于执行Saga流程编排所需的基本框架。 它主要是Flowable提供的工作流功能与Apache Camel(基于EIP模式的Java集成框架)的强大功能的结合。 Apache Camel通过所谓的“ Camel Task”成为Flowable的第一个公民。

Spring Boot应用程序框架 (Spring Boot Application Framework)

Flowable in its nature is a Spring Boot based application. It benefits from all the features provided by Spring Boot.

本质上是可流动的是基于Spring Boot的应用程序。 它受益于Spring Boot提供的所有功能。

Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can "just run".
通过Spring Boot,可以轻松地创建独立的,基于生产级别的基于Spring的应用程序,并且可以“运行”它们。

We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need very little Spring configuration.(Link)

我们对Spring平台和第三方库持固执己见的观点,因此您可以以最小的麻烦开始使用。 大多数Spring Boot应用程序只需要很少的Spring配置。( Link )

Spring Boot is all about convention over configuration. Our build.gradle looks as follows:

Spring Boot完全是关于配置的约定。 我们的build.gradle如下所示:

The gradle build file is quite compact and consists of the following dependencies:

gradle构建文件非常紧凑,并且包含以下依赖项:

  • Spring Boot framework dependencies

    Spring Boot框架依赖
  • Flowable Spring Boot integrated framework dependencies

    Flowable Spring Boot集成框架依赖关系
  • Camel Boot integrated framework dependencies

    Camel Boot集成框架依赖性
  • As well as H2, an in-memory database required for the Flowable stateful processing engine

    与H2一样,Flowable状态处理引擎所需的内存数据库

That’s it, rather straightforward.

就是这样,很简单。

Spring Boot和Flowable (Spring Boot and Flowable)

By introducing the above Flowable dependencies (refer to the Flowable Spring Boot App Documentation) and using the @SpringBootAplication annotation, a lot has happened behind the scenes:

通过引入上述Flowable依赖关系(请参阅Flowable Spring Boot应用程序文档 )并使用@SpringBootAplication批注,幕后发生了很多事情:

  • An in-memory datasource is created automatically (because the H2 driver is on the classpath). It is passed to the Flowable process engine configuration

    将自动创建内存中的数据源(因为H2驱动程序在类路径中)。 它被传递到Flowable流程引擎配置
  • A Flowable ProcessEngine, CMMNEngine, DMNEngine, FormEngine, ContentEngine, and IdmEngine beans have been created and exposed

    已创建并公开了Flowable ProcessEngine,CMMNEngine,DMNEngine,FormEngine,ContentEngine和IdmEngine Bean
  • All the Flowable services are exposed as Spring beans

    所有Flowable服务都作为Spring bean公开
  • The Spring Job Executor is created

    Spring Job Executor已创建

You may fine-tune the Flowable App — i.e. in case you only need a subset of the provided engines, refer to the following Flowable Starter List.

您可以对Flowable App进行微调,即,如果仅需要所提供引擎的子集,请参阅以下Flowable Starter List

SpringBoot和骆驼 (SpringBoot and Camel)

The Apache Camel Integration Framework is included via the following two dependencies:

通过以下两个依赖项包括了Apache Camel集成框架:

implementation 'org.apache.camel:camel-spring-boot-starter:2.23.0'implementation 'org.flowable:flowable-camel:6.4.1'
  • The first dependency ties Camel into the Spring Boot App framework,

    第一个依赖关系将Camel与Spring Boot App框架相关联,
  • The second dependency ties Camel into the Flowable Process Engine, via the so-called Camel Task, which enables the delegation of any third party system integration into a Flowable Process Model to a Camel Route (more on that later)

    第二个依赖关系通过所谓的Camel Task将Camel绑定到Flowable Process Engine中,该任务使任何第三方系统集成到Flowable Process Model的委托都可以称为Camel Route。

引导流程引导应用程序 (Bootstrapping Flowable Boot App)

To get a barebones Flowable Spring App running, follow these steps:

要运行准系统Flowable Spring App,请执行以下步骤:

Introduce an application.properties with the following properties.

介绍具有以下属性的application.properties。

management.endpoints.web.exposure.include=*management.endpoint.health.show-details=when_authorizedflowable.idm.password-encoder=spring_delegating

Provide a SecurityConfiguration class.

提供一个SecurityConfiguration类。

And load three test users at startup.

并在启动时加载三个测试用户。

Finally, we provide a REST controller, to do an initial check of our core Flowable Spring Boot App.

最后,我们提供一个REST控制器,以对我们的核心Flowable Spring Boot应用程序进行初始检查。

Start the Flowable App, via the command

通过命令启动Flowable App

gradle bootApp

And open the browser with http://localhost:8080. You will be prompted for a user and password. Provide flowfest and password test. You should now be logged in and the greeting message should be displayed.

并使用http:// localhost:8080打开浏览器。 系统将提示您输入用户名和密码。 提供flowfest和密码test 。 现在,您应该已经登录,并且应该显示问候消息。

Ok well done, our powerful Spring App is ready and we can move on.

好的,我们的功能强大的Spring App已准备就绪,我们可以继续。

流动建模器 (Flowable Modeller)

Our Saga Process will be modeled in the Flowable Modeler by using the BPM model feature. A quick introduction is given in this Flowable Tutorial.

我们的Saga流程将使用BPM模型功能在Flowable Modeler中建模。 此Flowable Tutorial中提供了快速介绍。

Via the Flowable Home Page, download the latest version (actual Flowable version v4.6.1) which is a zip file containing a war directory with 5 WAR files.

通过Flowable主页下载最新版本(实际Flowable版本v4.6.1),这是一个zip文件,其中包含带有5个WAR文件的war目录。

Deploy these 5 war files for example on a Tomcat test server and run:

例如,将这5个war文件部署在Tomcat测试服务器上并运行:

http://localhost:8080/flowable-modeler/ you will be asked to log in, provide admin with password test

http:// localhost:8080 / flowable-modeler /将要求您登录,向admin提供密码test

After a successful login, you will be directed to the Business Process Models Dashboard (which would be empty initially).

成功登录后,您将被定向到“业务流程模型”仪表盘(该仪表盘最初为空)。

You are ready to test it out. The Flowable default configuration introduces a persistent DB. This ensures that your model artifacts will be stored and are available after rebooting the Application server.

您已准备好进行测试。 Flowable默认配置引入了持久数据库。 这样可以确保您的模型工件将被存储并在重新启动应用程序服务器后可用。

One minor manual modification was necessary on an Ubuntu Standard Tomcat Application Server in order for the loading of the Flowable Modeler to be successful. In the tomcat artifact directory ( /var/lib/tomcat8) I had to create a flowable-db directory with user/group right tomcat8:tomcat8, otherwise the application startup failed (due to the fact that tomcat8 apt packages are installed by root).

为了成功完成Flowable Modeler的加载,必须在Ubuntu标准Tomcat应用服务器上进行一次小的手动修改。 在tomcat工件目录(/ var / lib / tomcat8)中,我必须使用用户/组权限tomcat8:tomcat8创建一个flowable flowable-db目录,否则应用程序启动失败(由于tomcat8 apt软件包是由root安装的事实) 。

流动的传奇:Hello World Process (Flowable Saga: Hello World Process)

In Flowable we model our hello world process, which consists of 4 steps.

在Flowable中,我们为Hello World流程建模,该流程包括4个步骤。

  • Initialize Variables Task sets an input variable which we want to process in our Camel Task

    初始化变量任务设置一个我们要在骆驼任务中处理的输入变量

  • Async Camel Task will delegate the process to a Camel Route, which processes the input variable. We will use Camel Routes for any kind of external system integration. This task is called in an asynchronous manner, i.e. after delegating the processing to the Came Route the task is terminated and moved forward to the…

    异步骆驼任务会将进程委派给骆驼路线,该骆驼路线处理输入变量。 我们将使用骆驼路线进行任何类型的外部系统集成。 以异步方式调用此任务,即,在将处理委托给“来话路由”之后,该任务终止并前进到…

  • Receive Process Response Task is an asynchronous Receiver Task, which waits to the completion of the Camel Route processing. After event triggering the processed data will be handed over to the…

    接收流程响应任务 是异步接收器任务,它等待骆驼路由处理完成。 事件触发后,处理后的数据将移交给……

  • Save Variable Task, which extracts the process result.

    保存变量任务,以提取过程结果。

As you will see later by using the power of Camel for the Integration, we can elegantly introduce an asynchronous behavior in our Saga process flow with just one line of code.

正如您稍后将通过使用Camel的集成功能所看到的那样,我们可以仅用一行代码就在Saga流程中优雅地引入异步行为。

The Project structure is straightforward. We need three classes to get this process flow implemented, as well as the BPMN process model:

项目结构很简单。 我们需要三个类来实现该流程以及BPMN流程模型:

Taken into consideration that we may have multiple Saga process flows in a Flowable Spring App we strive for clear packaging rule:

考虑到我们在Flowable Spring应用程序中可能有多个Saga流程,因此我们努力制定明确的包装规则:

  • Each BPMN process model has a unique id, in our case the id is saga1Process. Therefore

    每个BPMN流程模型都有一个唯一的ID,在我们的例子中,该ID为saga1Process 。 因此

  • any kind of tasks referenced in the process model is within a package task.saga1

    流程模型中引用的任何类型的任务都在包task.saga1

  • any kind of Camel route referenced in the process model is within a package route.saga1

    流程模型中引用的任何一种骆驼路线都在包装route.saga1

InitVariablesTask (InitVariablesTask)

We simulate here the retrieval of some input data provided by a calling application. We set the variable name to Hello World. This variable is now part of the process context.

我们在这里模拟调用应用程序提供的一些输入数据的检索。 我们将变量name设置为Hello World 。 现在,此变量是流程上下文的一部分。

Within the model, we associate our class with our Service Task:

在模型中,我们将类与服务任务相关联:

骆驼路线 (Camel Route)

Let’s switch now to the Camel Task which is a Camel Service Task, which we configure as Asynchronous.

现在让我们切换到Camel Task,它是Camel服务任务,我们将其配置为异步。

As one can see the only thing we configure in our a Camel Task is the Service Task Id and we set the Asynchronous flag.

可以看到,我们在骆驼任务中唯一配置的是服务任务ID,并设置了异步标志。

The wiring of the Service Task to the Camel route is done within the route class, via naming conventions. The overall process id is saga1Process and the task id camelAsyncTask, so let’s look at the Camel Route class. We establish the inbound route on line 17 to our Camel Service Task

通过命名约定,在路由类内将服务任务连接到骆驼路由。 总体进程ID为saga1Process ,任务ID为camelAsyncTask ,因此让我们看一下Camel Route类。 我们在第17行建立到骆驼服务任务的入站路线

from("flowable:saga1Process:camelAsyncTask")

and will put the message received into the Camel seda component (line 18), which will decouple the Camel Service Task from the processing in an asynchronous manner (as said by just one line of code)

并将接收到的消息放入Camel seda组件(第18行),这将以异步方式(仅用一行代码表示)将Camel服务任务与处理分离。

.to("seda:continueAsync")
SEDA组件的简短介绍 (A short intro to the SEDA component)

We will use the seda: Camel component. This provides asynchronous SEDA behavior. Messages are exchanged on a BlockingQueue and consumers are invoked in a separate thread from the producer.

我们将使用seda: Camel组件。 这提供了异步SEDA行为。 在BlockingQueue上交换消息,并在与生产者不同的线程中调用使用者。

The staged event-driven architecture (SEDA) refers to an approach to software architecture that decomposes a complex, event-driven application into a set of stages connected by queues. It avoids the high overhead associated with thread-based concurrency models (i.e. locking, unlocking, and polling for locks), and decouples event and thread scheduling from application logic.

分段事件驱动的体系结构 ( SEDA )是一种软件体系结构的方法,它将复杂的,事件驱动的应用程序分解为由队列连接的一组阶段。 它避免了与基于线程并发模型相关的高开销(即锁定,解锁和轮询锁定),并使事件和线程调度与应用程序逻辑脱钩。

As we can see after the retrieval of the message, we simulate some processing

正如我们在检索消息后所看到的,我们模拟了一些处理

.log(LoggingLevel.INFO, logger, "External System Processing...").transform().simple("Processed: ${property.input}, { Result: OK }");

Finally, we return the result back to our Saga process

最后,我们将结果返回到Saga流程

from("seda:continueAsync")        .to("flowable:saga1Process:receiveAsyncTask");

That’s it. So any kind of external system integration we can delegate to Camel routes, which are providing powerful integration concepts. As for the example, instead of implementing a retry mechanism as part of the Saga process, one can use an exception handler with a re-delivery policy (also known as the RedeliveryErrorHandler in Camel). This error handler will allow you to set the number of retries and also set things like the delay between retries etc.

而已。 因此,任何类型的外部系统集成都可以委托给Camel路线,这些路线提供了强大的集成概念。 对于示例,可以将异常处理程序与重新传递策略一起使用(在Camel中也称为RedeliveryErrorHandler ),而不是在Saga流程中实现重试机制。 该错误处理程序将允许您设置重试次数,还可以设置重试之间的延迟等。

public void configure()  {    // ExceptionHandler with RedliveryPolicy    errorHandler(defaultErrorHandler()            .maximumRedeliveries(3)            .backOffMultiplier(4)            .retryAttemptedLogLevel(LoggingLevel.WARN));

接收异步响应任务 (Receive Async Response Task)

This Service Receiver Task will serve as the bridge back from Camel as explained above.

如上所述,此服务接收器任务将充当从Camel返回的桥梁。

保存输出任务 (Save Output Task)

Finally, we print out the produced Camel message which was passed over.

最后,我们将产生的Camel消息打印出来并传递出去。

Spring启动应用 (Spring Boot App)

Within the SpringBootApplication we introduce a CommandLineRunner which will trigger our process model.

SpringBootApplication我们引入了一个CommandLineRunner ,它将触发我们的流程模型。

If we restart our application:

如果我们重新启动应用程序:

gradle bootApp

we can see on the console the processing output of our Saga process.

我们可以在控制台上看到Saga流程的处理输出。

Last but not least you may also run unit tests via

最后但并非最不重要的一点是,您还可以通过以下方式运行单元测试

gradle test

To wrap it up: As one can see, Flowable allows, with the concept of Camel Task, a powerful integration of third-party systems into a BPMN process flow. This approach will ensure that the orchestration flow will not be overloaded by enterprise integration logic which is offloaded to Camel Routes.

总结一下:正如人们所看到的那样,Flowable允许使用Camel Task的概念将第三方系统强大地集成到BPMN流程中。 这种方法将确保编排流程不会被卸载到骆驼路线的企业集成逻辑所超载。

You can check out the code here:

您可以在此处查看代码:

talfco/tutorial-flowable-sagaA tutorial which describes the approach how to use Flowable and Camel for a Saga Process Manager …github.com

talfco / tutorial-flowable-saga 一个教程,描述了如何将 Flowable 和Camel用于Saga Process Manager的方法… github.com

Happy coding then.

然后编码愉快。

翻译自: https://www.freecodecamp.org/news/saga-process-orchestration-in-java-using-the-flowable-process-engine-6baa9593bbcc/

flowable java

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值