• 设为首页
  • 收藏本站
  • 积分充值
  • VIP赞助
  • 手机版
  • 微博
  • 微信
    微信公众号 添加方式:
    1:搜索微信号(888888
    2:扫描左侧二维码
  • 快捷导航
    福建二哥 门户 查看主题

    .NET Core 分布式任务调度ScheduleMaster详解

    发布者: 天下网吧 | 发布时间: 2025-6-18 08:10| 查看数: 45| 评论数: 0|帖子模式

    1.什么是ScheduleMaster

    ScheduleMaster是
    1. 分布式任务调度
    复制代码
    系统,是国内的一位开发者写的。简称:集中任务调度系统,最简单的理解ScheduleMaster,就是对不同的系统里面的调度任务做统一管理的框架。
    例如我们现在有多个系统,每个系统针对自己处理不同的业务场景。衍生出自己的调度任务,想象一下,如果每个系统人为去维护,那随着调度任务越来越多,人是崩溃的吧,可见维护和技术成本是巨大的,这时我们需要选择分布式任务系统框架做统一的管理

    当然有目前有很多相对优秀分布式任务系统框架,我们主要学习 ScheduleMaster

    2.使用ScheduleMaster

    1.首先我们需要使用NET Core web Api创建几个模拟的微服务,分别为 考勤、算薪、邮件、短信

    2.下载开源的ScheduleMaster,并且使用ScheduleMaster调度我们的微服务接口
    1. - sqlserver:"Persist Security Info = False; User ID =sa; Password =123456; Initial Catalog =schedule_master; Server =."- postgresql:"Server=localhost;Port=5432;Database=schedule_master;User Id=postgres;Password=123456;Pooling=true;MaxPoolSize=20;"- mysql:"Data Source=localhost;Database=schedule_master;User ID=root;Password=123456;pooling=true;CharSet=utf8mb4;port=3306;sslmode=none;TreatTinyAsBoolean=true"
    复制代码
    修改Host的配置文件和支持的数据库,框架默认使用Mysql
    修改Web的配置文件和支持的数据库,框架默认使用Mysql

    3.进入Hos.ScheduleMaster.Web项目的发布目录,
    1. dotnet Hos.ScheduleMaster.Web.dll
    复制代码
    启动项目 ,此时会生成数据库
    登录账号:admin
    密码:111111
    4.进入Hos.ScheduleMaster.QuartzHost项目的发布目录,执行命令,启动项目
    1. dotnet Hos.ScheduleMaster.QuartzHost.dll --urls http://*:30003
    复制代码
    1.配置Http调度任务

    5.准备就绪后,使用后台查看节点管理,可以看到web主节点30000和任务调度的接口30002已经在运行

    6.使用任务列表菜单,创建定时调度任务,配置基础信息和元数据配置,然后点击保存就开始执行任务


    2.配置程序集调度任务

    1.创建一个类库,安装ScheduleMaster库, 创建一个类继承TaskBase,实现抽象方法,然后编译程序集
    1. namespace TaskExcuteService
    2. {
    3.     class AssemblyTask : TaskBase
    4.     {
    5.         public override void Run(TaskContext context)
    6.         {
    7.             context.WriteLog("程序集任务");
    8.         }
    9.     }
    10. }
    复制代码
    2.找到debug目录,去掉Base程序集,然后添加为压缩文件
    3.在web界面找到任务配置,选择程序集进行基本配置,配置元数据,输入程序集名称,然后输入类,并将程序集上传,保存就可以运行了

    3.使用Api接入任务

    为了方便业务系统更好的接入调度系统,ScheduleMaster创建任务不仅可以在控制台中实现,系统也提供了WebAPI供业务系统使用代码接入,这种方式对延时任务来说尤其重要。

    1.API Server 对接流程


    • 在控制台中创建好专用的API对接用户账号。
    • 使用对接账号的用户名设置为http header中的
      1. ms_auth_user
      复制代码
      值。
    • 使用经过哈希运算过的秘钥设置为http header中的
      1. ms_auth_secret值
      复制代码
      ,计算规则:按{用户名}{hash(密码)}{用户名}的格式拼接得到字符串str,然后再对str做一次hash运算即得到最终秘钥,hash函数是小写的32位MD5算法。
    • 使用form格式发起http调用,如果非法用户会返回401-Unauthorized。
    1. HttpClient client = new HttpClient();
    2. client.DefaultRequestHeaders.Add("ms_auth_user", "admin");
    3. client.DefaultRequestHeaders.Add("ms_auth_secret", SecurityHelper.MD5($"admin{SecurityHelper.MD5("111111")}}admin"));
    复制代码
    所有接口采用统一的返回格式,字段如下:
    参数名称参数类型说明Successbool是否成功Statusint结果状态,0-请求失败 1-请求成功 2-登录失败 3-参数异常 4-数据异常Messagestring返回的消息Dataobject返回的数据
    2.创建程序集任务

    使用API创建任务的方式不支持上传程序包,所以在任务需要启动时要确保程序包已通过其他方式上传,否则会启动失败。

    • 接口地址:
      1. http://yourip:30000/api/task/create
      复制代码
    • 请求类型:
      1. POST
      复制代码
    • 参数格式:
      1. application/x-www-form-urlencoded
      复制代码
    • 返回结果:创建成功返回任务id
    • 参数列表:
    参数名称参数类型是否必填说明MetaTypeint是任务类型,这里固定是1Titlestring是任务名称RunLoopbool是是否按周期执行CronExpressionstring否cron表达式,如果RunLoop为true则必填AssemblyNamestring是程序集名称ClassNamestring是执行类名称,包含完整命名空间StartDateDateTime是任务开始时间EndDateDateTime否任务停止时间,为空表示不限停止时间Remarkstring否任务描述说明KeepersList<int>否监护人idNextsList<guid>否子级任务idExecutorsList<string>否执行节点名称RunNowbool否创建成功是否立即启动ParamsList<ScheduleParam>否自定义参数列表,也可以通过CustomParamsJson字段直接传json格式字符串
    1. ScheduleParam:
    复制代码
    参数名称参数类型是否必填说明ParamKeystring是参数名称ParamValuestring是参数值ParamRemarkstring否参数说明
    1. HttpClient client = new HttpClient();
    2. List<KeyValuePair<string, string>> args = new List<KeyValuePair<string, string>>();
    3. args.Add(new KeyValuePair<string, string>("MetaType", "1"));
    4. args.Add(new KeyValuePair<string, string>("RunLoop", "true"));
    5. args.Add(new KeyValuePair<string, string>("CronExpression", "33 0/8 * * * ?"));
    6. args.Add(new KeyValuePair<string, string>("Remark", "By Xunit Tester Created"));
    7. args.Add(new KeyValuePair<string, string>("StartDate", DateTime.Today.ToString("yyyy-MM-dd HH:mm:ss")));
    8. args.Add(new KeyValuePair<string, string>("Title", "程序集接口测试任务"));
    9. args.Add(new KeyValuePair<string, string>("AssemblyName", "Hos.ScheduleMaster.Demo"));
    10. args.Add(new KeyValuePair<string, string>("ClassName", "Hos.ScheduleMaster.Demo.Simple"));
    11. args.Add(new KeyValuePair<string, string>("CustomParamsJson", "[{"ParamKey":"k1","ParamValue":"1111","ParamRemark":"r1"},{"ParamKey":"k2","ParamValue":"2222","ParamRemark":"r2"}]"));
    12. args.Add(new KeyValuePair<string, string>("Keepers", "1"));
    13. args.Add(new KeyValuePair<string, string>("Keepers", "2"));
    14. //args.Add(new KeyValuePair<string, string>("Nexts", ""));
    15. //args.Add(new KeyValuePair<string, string>("Executors", ""));
    16. HttpContent reqContent = new FormUrlEncodedContent(args);
    17. var response = await client.PostAsync("http://localhost:30000/api/Task/Create", reqContent);
    18. var content = await response.Content.ReadAsStringAsync();
    19. Debug.WriteLine(content);
    复制代码
    3.创建HTTP任务


    • 接口地址:
      1. http://yourip:30000/api/task/create
      复制代码
    • 请求类型:
      1. POST
      复制代码
    • 参数格式:
      1. application/x-www-form-urlencoded
      复制代码
    • 返回结果:创建成功返回任务id
    • 参数列表:
    参数名称参数类型是否必填说明MetaTypeint是任务类型,这里固定是2Titlestring是任务名称RunLoopbool是是否按周期执行CronExpressionstring否cron表达式,如果RunLoop为true则必填StartDateDateTime是任务开始时间EndDateDateTime否任务停止时间,为空表示不限停止时间Remarkstring否任务描述说明HttpRequestUrlstring是请求地址HttpMethodstring是请求方式,仅支持GET\POST\PUT\DELETEHttpContentTypestring是参数格式,仅支持application/json和application/x-www-form-urlencodedHttpHeadersstring否自定义请求头,ScheduleParam列表的json字符串HttpBodystring是如果是json格式参数,则是对应参数的json字符串;如果是form格式参数,则是对应ScheduleParam列表的json字符串。KeepersList<int>否监护人idNextsList<guid>否子级任务idExecutorsList<string>否执行节点名称RunNowbool否创建成功是否立即启动
    1. HttpClient client = new HttpClient();
    2. List<KeyValuePair<string, string>> args = new List<KeyValuePair<string, string>>();
    3. args.Add(new KeyValuePair<string, string>("MetaType", "2"));
    4. args.Add(new KeyValuePair<string, string>("RunLoop", "true"));
    5. args.Add(new KeyValuePair<string, string>("CronExpression", "22 0/8 * * * ?"));
    6. args.Add(new KeyValuePair<string, string>("Remark", "By Xunit Tester Created"));
    7. args.Add(new KeyValuePair<string, string>("StartDate", DateTime.Today.ToString("yyyy-MM-dd HH:mm:ss")));
    8. args.Add(new KeyValuePair<string, string>("Title", "Http接口测试任务"));
    9. args.Add(new KeyValuePair<string, string>("HttpRequestUrl", "http://localhost:56655/api/1.0/value/jsonpost"));
    10. args.Add(new KeyValuePair<string, string>("HttpMethod", "POST"));
    11. args.Add(new KeyValuePair<string, string>("HttpContentType", "application/json"));
    12. args.Add(new KeyValuePair<string, string>("HttpHeaders", "[]"));
    13. args.Add(new KeyValuePair<string, string>("HttpBody", "{ "Posts": [{ "PostId": 666, "Title": "tester", "Content":"testtesttest" }], "BlogId": 111, "Url":"qweqrrttryrtyrtrtrt" }"));
    14. HttpContent reqContent = new FormUrlEncodedContent(args);
    15. var response = await client.PostAsync("http://localhost:30000/api/Task/Create", reqContent);
    16. var content = await response.Content.ReadAsStringAsync();
    17. Debug.WriteLine(content);
    复制代码
    4.创建延时任务


    • 接口地址:
      1. http://yourip:30000/api/delaytask/create
      复制代码
    • 请求类型:
      1. POST
      复制代码
    • 参数格式:
      1. application/x-www-form-urlencoded
      复制代码
    • 返回结果:创建成功返回任务id
    • 参数列表:
    参数名称参数类型是否必填说明SourceAppstring是来源Topicstring是主题ContentKeystring是业务关键字DelayTimeSpanint是延迟相对时间DelayAbsoluteTimeDateTime是延迟绝对时间NotifyUrlstring是回调地址NotifyDataTypestring是回调参数格式,仅支持application/json和application/x-www-form-urlencodedNotifyBodystring是回调参数,json格式字符串
    1. for (int i = 0; i < 5; i++)
    2. {
    3.     int rndNum = new Random().Next(20, 500);
    4.     List<KeyValuePair<string, string>> args = new List<KeyValuePair<string, string>>();
    5.     args.Add(new KeyValuePair<string, string>("SourceApp", "TestApp"));
    6.     args.Add(new KeyValuePair<string, string>("Topic", "TestApp.Trade.TimeoutCancel"));
    7.     args.Add(new KeyValuePair<string, string>("ContentKey", i.ToString()));
    8.     args.Add(new KeyValuePair<string, string>("DelayTimeSpan", rndNum.ToString()));
    9.     args.Add(new KeyValuePair<string, string>("DelayAbsoluteTime", DateTime.Now.AddSeconds(rndNum).ToString("yyyy-MM-dd HH:mm:ss")));
    10.     args.Add(new KeyValuePair<string, string>("NotifyUrl", "http://localhost:56655/api/1.0/value/delaypost"));
    11.     args.Add(new KeyValuePair<string, string>("NotifyDataType", "application/json"));
    12.     args.Add(new KeyValuePair<string, string>("NotifyBody", "{ "Posts": [{ "PostId": 666, "Title": "tester", "Content":"testtesttest" }], "BlogId": 111, "Url":"qweqrrttryrtyrtrtrt" }"));
    13.     HttpContent reqContent = new FormUrlEncodedContent(args);
    14.     var response = await client.PostAsync("http://localhost:30000/api/DelayTask/Create", reqContent);
    15.     var content = await response.Content.ReadAsStringAsync();
    16.     Debug.WriteLine(content);
    17. }
    复制代码
    4.框架简单分析


    1.全局设计

    根据官网的设计图,以及操作的流程,简单总结一下任务全局流程为客户端—–>master——>work—–>执行任务。从大的架构方向的思想就是,将原有单个服务中业务和任务调度混合的方式做一些改变,使业务和调度分离,专门把任务调度部分剥离出来,作为一个独立的进程,来统一调用和管理任务,而原有服务只做业务处理。


    2.Master和Work分析

    我们主要从主节点,从节点,数据表这几个方面来简单分析,整个业务的时序流程,从本质来看并不复杂master和Work之间的关系是相互配合的,可能乍一看下面整个图有点混乱,下面来解释一下,其实Master节点将任务添加到数据中,然后work节点,去从对应的数据表中取出任务数据,然后根据任务对应的配置,生成配置信息,调用Quartz执行任务,这就是我们整个框架中最核心的业务。

    当然master和work除了主要承载了整个管理系统的UI可视化、后台业务操作、任务执行之外,如果从细节以及实现方式来说主要做了以下事情:
    1. Master
    复制代码

    • 1.分配任务执行和选择节点
    • 2.对work节点进行健康检查,对任务进行故障转移
    1. Work
    复制代码

    • 1.取出任务配置信息
    • 2.使用Quartz根据配置运行任务
    • 3.使用反射调用程序集
    • 4.使用httpclient调用http 接口
    到此这篇关于分布式任务调度ScheduleMaster的文章就介绍到这了,更多相关分布式任务调度ScheduleMaster内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

    来源:https://www.jb51.net/article/250003.htm
    免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

    本帖子中包含更多资源

    您需要 登录 才可以下载或查看,没有账号?立即注册

    ×

    最新评论

    QQ Archiver 手机版 小黑屋 福建二哥 ( 闽ICP备2022004717号|闽公网安备35052402000345号 )

    Powered by Discuz! X3.5 © 2001-2023

    快速回复 返回顶部 返回列表