0
点赞
收藏
分享

微信扫一扫

Canal的学习

Greatiga 2024-01-01 阅读 60

一.工作流介绍

1. 什么是工作流

工作流(Workflow),是对工作流程及其各操作步骤之间业务规则的抽象、概括描述。

工作流将一套大的业务逻辑分解成业务逻辑段, 并统一控制这些业务逻辑段的执行条件,执行顺序以及相互通信,实现业务逻辑的分解和解耦。

做饭:

贷款审批:

2. 为什么要使用工作流

在企业日常的管理中,经常会有出差申请、加班申请、请假申请等流程。

如果用人工的方式管理这些流程,效率低,管理成本高。

因此为了提高效率,我们需要使用到工作流开发。

3. 使用工作流有什么好处

在工作流开发中,我们可以通过每一个流程定义清楚的看到业务的所有步骤,以及每一个流程实例运行到什么位置,参与者是谁;

能够节省更多的时间成本,提升企业信息化水平。

二. 工作流引擎

1. Camunda介绍

ActivitiActiviti 由 Alfresco 公司开发,目前最高版本为 Activiti cloud 7.1.0。由于团队分歧 ,在Activiti6版本衍生出了Flowable。 
FlowableFlowable是基于Activiti6衍生出来的版本,开发团队也是Activiti中分裂出来的,修复了Activiti6中的bug,并再此基础上实现的DMN、BPEL支持。 
CamundaCamunda是基于Activiti5的,最新版本是Camunda7.17,开发团队也是从Activiti中分裂出来的,发展轨迹和Flowable相似。通过压力测试验证Camunda BPMN引擎性能和稳定性更好,功能完善;除了BPMN,Camunda还支持CMMN(案例管理)和DMN(决策自动化)。Camunda不仅带有引擎,还有很多强大的工具,用于建模、任务管理、监控和用户管理等。 

Camunda BPM(业务流程管理)平台,用来管理,部署的流程定义、执行任务、策略等等。

下载安装一个Camunda平台,成功解压 Camunda 平台的发行版后,执行名为start.bat(对于 Windows 用户)或start.sh(对于 Unix 用户)的脚本。此脚本将启动应用程序服务器。

打开您的 Web 浏览器并导航到http://localhost:8080/以访问欢迎页面,Camunda的管理平台。

Camunda Modeler(用于编辑流程图及其他模型)平台,用来定义流程图,简单说就是一个画图工具。

下载 Modeler 后,只需将下载文件解压缩到您选择的文件夹中。

成功解压缩 zip 后,运行camunda-modeler.exe(对于 Windows 用户)、camunda-modeler.app(对于 Mac 用户)或camunda-modeler.sh(对于 Linux 用户)。  

2. Camunda常用Api

RepositoryService

RuntimeService

TaskService

FormService

HistoryService

三、学习步骤:

1、Camunda 官方快速入门教程中文版: https://zhuanlan.zhihu.com/p/375908620 或者 https://blog.csdn.net/zoollcar/article/details/117351192

   Camunda  Rest API 文档:  https://docs.camunda.org/rest/camunda-bpm-platform/7.19/

   Camunda 官网:   https://camunda.com/

2、使用Nuget包 Camunda.Worker GitHub 地址: https://github.com/AMalininHere/camunda-worker-dotnet 找里面的samples文件夹下的使用Demo代码

3、在VS2022上写一个Demo

3.1、创建一个.net6 web空模板服务,然后安装Nuget包 Camunda.Worker

1

<PackageReference Include="Camunda.Worker" Version="0.13.5" />

3.2、Program.cs改成如下

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

using System.Collections.Generic;

using System.Linq;

using System.Threading.Tasks;

namespace CamundaDemoWeb

{

    public class Program

    {

        public static void Main(string[] args)

        {

            CreateHostBuilder(args).Build().Run();

        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>

            Host.CreateDefaultBuilder(args)

                .ConfigureWebHostDefaults(webBuilder =>

                {

                    webBuilder.UseStartup<Startup>();

                });

    }

}

3.3、Startup.cs 注入Camunda 

using Camunda.Worker;
using Camunda.Worker.Client;
using CamundaDemoWeb.Handlers;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace CamundaDemoWeb
{
    public class Startup
    {

        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddExternalTaskClient(client =>
            {
                //client.BaseAddress = new Uri("http://localhost:8080/engine-rest");
                client.BaseAddress = new Uri("http://192.168.1.153:8080/engine-rest");
            });

            services.AddCamundaWorker("sampleWorker")
                .AddHandler<SayHelloHandler>()
                //.AddHandler<SayHelloGuestHandler>()
                .ConfigurePipeline(pipeline =>
                {
                    pipeline.Use(next => async context =>
                    {
                        var logger = context.ServiceProvider.GetRequiredService<ILogger<Startup>>();
                        logger.LogInformation("Started processing of task {Id}", context.Task.Id);
                        await next(context);
                        logger.LogInformation("Finished processing of task {Id}", context.Task.Id);
                    });
                });

            services.AddHealthChecks();
        }
      
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseRouting();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapGet("/", async context =>
                {
                    await context.Response.WriteAsync("Hello World!");
                });
            });
        }
    }
}

3.4、写一个SayHelloHandler

using Camunda.Worker;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace CamundaDemoWeb.Handlers
{
    //[HandlerTopics("sayHello", LockDuration = 10_000)]
    //[HandlerVariables("USERNAME")]
    [HandlerTopics("charge-card", LockDuration = 5_000)]
    [HandlerVariables("amount")]
    public class SayHelloHandler : IExternalTaskHandler
    {
        public async Task<IExecutionResult> HandleAsync(ExternalTask externalTask, CancellationToken cancellationToken)
        {
            await Task.CompletedTask;
            //throw new System.NotImplementedException();
            var username = externalTask.Variables["amount"].Value;

            //await Task.Delay(1000);

            return new CompleteResult
            {
                Variables = new Dictionary<string, Variable>
                {
                    ["MESSAGE"] = Variable.String("Hello, Guest!")
                }
            };


            
        }
    }
}

举报

相关推荐

canal===docker部署canal

canal

Canal

Canal 安装

canal配置

Canal简介

0 条评论