您的位置:首页 > 手游攻略 > .NET Core 分布式任务调度ScheduleMaster完整指南

.NET Core 分布式任务调度ScheduleMaster完整指南

作者:互联网  时间: 2026-09-01 16:10:01  

平时做技术实践时,很多问题不是概念不会,而是细节没串起来。拿“.NET Core 分布式任务调度ScheduleMaster”来说,它看着像小点,放到项目里常会牵出环境、配置、兼容性和维护成本。下面按实际采用顺序,把思路、关键写法和容易踩坑的地方讲清楚,便于大家直接对照操作。

1.什么是ScheduleMaster

在这个场景下,ScheduleMaster是分布式任务调度系统,是国内的一位开发者写的。简称:集中任务调度系统,最轻松的理解ScheduleMaster,就是对不同的系统里面的调度任务做统一管理的框架。

实际处理时,比如我们现在有多个系统,每个系统针对自己处理不同的业务场景。衍生出自己的调度任务,想象一下,如果每个系统人为去维护,那随着调度任务越来越多,人是崩溃的吧,可见维护和技术成本是巨大的,这时我们需选择分布式任务系统框架做统一的管理

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

2.采用ScheduleMaster

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

2.下载开源的ScheduleMaster,同时且采用ScheduleMaster调度我们的微服务接口

- 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项目的发布目录,dotnet Hos.ScheduleMaster.Web.dll启动项目,此时会生成数据库

登录账号:admin

密码:111111

4.进入Hos.ScheduleMaster.QuartzHost项目的发布目录,执行命令,启动项目

dotnet Hos.ScheduleMaster.QuartzHost.dll --urls http://*:30003

1.设置Http调度任务

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

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

2.设置程序集调度任务

1.新建一个类库,安装ScheduleMaster库, 新建一个类继承TaskBase,实现抽象方法,随后编译程序集

namespace TaskExcuteService
{
    class AssemblyTask : TaskBase
    {
        public override void Run(TaskContext context)
        {
            context.WriteLog("程序集任务");
        }
    }
}

2.找到debug目录,去掉Base程序集,随后添加为压缩文件

3.在web界面找到任务设置,选择程序集进行基本设置,设置元数据,输入程序集名称,然后输入类,同时将程序集上传,保存就能够运行了

3.采用Api接入任务

从实现思路看,为便于便于业务系统更好的接入调度系统,ScheduleMaster新建任务不仅能够在控制台中实现,系统也提供了WebAPI供业务系统采用代码接入,这种方式对延时任务来说尤其重要。

1.API Server 对接流程

  • 在控制台中新建好专用的API对接用户账号。
  • 理解这一步时,采用对接账号的用户名设置为http header中的ms_auth_user值。
  • 结合项目来看,采用经过哈希运算过的秘钥设置为http header中的ms_auth_secret值,计算规则:按{用户名}{hash(密码)}{用户名}的格式拼接得到字符串str,随后再对str做一次hash运算即得到最后秘钥,hash函数是小写的32位MD5算法。
  • 理解这一步时,采用form格式发起http调用,如果非法用户会得到401-Unauthorized。
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Add("ms_auth_user", "admin");
client.DefaultRequestHeaders.Add("ms_auth_secret", SecurityHelper.MD5($"admin{SecurityHelper.MD5("111111")}}admin"));

所有接口采用统一的得到格式,字段如下所示:

参数名称参数类型说明
Successbool是否成功
Statusint实际处理时,结果状态,0-请求失败 1-请求成功 2-登录失败 3-参数异常 4-数据异常
Messagestring得到的消息
Dataobject得到的数据

2.新建程序集任务

从实现思路看,采用API新建任务的方式不兼容上传程序包,所以在任务需启动时要确保程序包已借助其他方式上传,否则会启动失败。

  • 接口地址:(链接已移除)
  • 请求类型:POST
  • 参数格式:application/x-www-form-urlencoded
  • 得到结果:新建成功得到任务id
  • 参数列表:
参数名称参数类型是否必填说明
MetaTypeint任务类型,这里固定是1
Titlestring任务名称
RunLoopbool是否按周期执行
CronExpressionstringcron表达式,如果RunLoop为true则必填
AssemblyNamestring程序集名称
ClassNamestring执行类名称,包含完整命名空间
StartDateDateTime任务开始时间
EndDateDateTime任务停止时间,为空表示不限停止时间
Remarkstring任务描述说明
KeepersList<int>监护人id
NextsList<guid>子级任务id
ExecutorsList<string>执行节点名称
RunNowbool新建成功是否立即启动
ParamsList<ScheduleParam>从实现思路看,自定义参数列表,也能够借助CustomParamsJson字段直接传json格式字符串

ScheduleParam:

参数名称参数类型是否必填说明
ParamKeystring参数名称
ParamValuestring参数值
ParamRemarkstring参数说明
HttpClient client = new HttpClient();
List<KeyValuePair<string, string>> args = new List<KeyValuePair<string, string>>();
args.Add(new KeyValuePair<string, string>("MetaType", "1"));
args.Add(new KeyValuePair<string, string>("RunLoop", "true"));
args.Add(new KeyValuePair<string, string>("CronExpression", "33 0/8 * * * ?"));
args.Add(new KeyValuePair<string, string>("Remark", "By Xunit Tester Created"));
args.Add(new KeyValuePair<string, string>("StartDate", DateTime.Today.ToString("yyyy-MM-dd HH:mm:ss")));
args.Add(new KeyValuePair<string, string>("Title", "程序集接口测试任务"));
args.Add(new KeyValuePair<string, string>("AssemblyName", "Hos.ScheduleMaster.Demo"));
args.Add(new KeyValuePair<string, string>("ClassName", "Hos.ScheduleMaster.Demo.Simple"));
args.Add(new KeyValuePair<string, string>("CustomParamsJson", "[{"ParamKey":"k1","ParamValue":"1111","ParamRemark":"r1"},{"ParamKey":"k2","ParamValue":"2222","ParamRemark":"r2"}]"));
args.Add(new KeyValuePair<string, string>("Keepers", "1"));
args.Add(new KeyValuePair<string, string>("Keepers", "2"));
//args.Add(new KeyValuePair<string, string>("Nexts", ""));
//args.Add(new KeyValuePair<string, string>("Executors", ""));
HttpContent reqContent = new FormUrlEncodedContent(args);
var response = await client.PostAsync("http://localhost:30000/api/Task/Create", reqContent);
var content = await response.Content.ReadAsStringAsync();
Debug.WriteLine(content);

3.新建HTTP任务

  • 接口地址:(链接已移除)
  • 请求类型:POST
  • 参数格式:application/x-www-form-urlencoded
  • 得到结果:新建成功得到任务id
  • 参数列表:
参数名称参数类型是否必填说明
MetaTypeint任务类型,这里固定是2
Titlestring任务名称
RunLoopbool是否按周期执行
CronExpressionstringcron表达式,如果RunLoop为true则必填
StartDateDateTime任务开始时间
EndDateDateTime任务停止时间,为空表示不限停止时间
Remarkstring任务描述说明
HttpRequestUrlstring请求地址
HttpMethodstring请求方式,仅兼容GETPOSTPUTDELETE
HttpContentTypestring理解这一步时,参数格式,仅兼容application/json和application/x-www-form-urlencoded
HttpHeadersstring自定义请求头,ScheduleParam列表的json字符串
HttpBodystring从实现思路看,若是json格式参数,则是对应参数的json字符串;如果是form格式参数,则是对应ScheduleParam列表的json字符串。
KeepersList<int>监护人id
NextsList<guid>子级任务id
ExecutorsList<string>执行节点名称
RunNowbool新建成功是否立即启动
HttpClient client = new HttpClient();
List<KeyValuePair<string, string>> args = new List<KeyValuePair<string, string>>();
args.Add(new KeyValuePair<string, string>("MetaType", "2"));
args.Add(new KeyValuePair<string, string>("RunLoop", "true"));
args.Add(new KeyValuePair<string, string>("CronExpression", "22 0/8 * * * ?"));
args.Add(new KeyValuePair<string, string>("Remark", "By Xunit Tester Created"));
args.Add(new KeyValuePair<string, string>("StartDate", DateTime.Today.ToString("yyyy-MM-dd HH:mm:ss")));
args.Add(new KeyValuePair<string, string>("Title", "Http接口测试任务"));
args.Add(new KeyValuePair<string, string>("HttpRequestUrl", "http://localhost:56655/api/1.0/value/jsonpost"));
args.Add(new KeyValuePair<string, string>("HttpMethod", "POST"));
args.Add(new KeyValuePair<string, string>("HttpContentType", "application/json"));
args.Add(new KeyValuePair<string, string>("HttpHeaders", "[]"));
args.Add(new KeyValuePair<string, string>("HttpBody", "{ "Posts": [{ "PostId": 666, "Title": "tester", "Content":"testtesttest" }], "BlogId": 111, "Url":"qweqrrttryrtyrtrtrt" }"));
HttpContent reqContent = new FormUrlEncodedContent(args);
var response = await client.PostAsync("http://localhost:30000/api/Task/Create", reqContent);
var content = await response.Content.ReadAsStringAsync();
Debug.WriteLine(content);

4.新建延时任务

  • 接口地址:(链接已移除)
  • 请求类型:POST
  • 参数格式:application/x-www-form-urlencoded
  • 得到结果:新建成功得到任务id
  • 参数列表:
参数名称参数类型是否必填说明
SourceAppstring来源
Topicstring主题
ContentKeystring业务关键字
DelayTimeSpanint延迟相对时间
DelayAbsoluteTimeDateTime延迟绝对时间
NotifyUrlstring回调地址
NotifyDataTypestring结合项目来看,回调参数格式,仅兼容application/json和application/x-www-form-urlencoded
NotifyBodystring回调参数,json格式字符串
for (int i = 0; i < 5; i++)
{
    int rndNum = new Random().Next(20, 500);
    List<KeyValuePair<string, string>> args = new List<KeyValuePair<string, string>>();
    args.Add(new KeyValuePair<string, string>("SourceApp", "TestApp"));
    args.Add(new KeyValuePair<string, string>("Topic", "TestApp.Trade.TimeoutCancel"));
    args.Add(new KeyValuePair<string, string>("ContentKey", i.ToString()));
    args.Add(new KeyValuePair<string, string>("DelayTimeSpan", rndNum.ToString()));
    args.Add(new KeyValuePair<string, string>("DelayAbsoluteTime", DateTime.Now.AddSeconds(rndNum).ToString("yyyy-MM-dd HH:mm:ss")));
    args.Add(new KeyValuePair<string, string>("NotifyUrl", "http://localhost:56655/api/1.0/value/delaypost"));
    args.Add(new KeyValuePair<string, string>("NotifyDataType", "application/json"));
    args.Add(new KeyValuePair<string, string>("NotifyBody", "{ "Posts": [{ "PostId": 666, "Title": "tester", "Content":"testtesttest" }], "BlogId": 111, "Url":"qweqrrttryrtyrtrtrt" }"));
    HttpContent reqContent = new FormUrlEncodedContent(args);
    var response = await client.PostAsync("http://localhost:30000/api/DelayTask/Create", reqContent);
    var content = await response.Content.ReadAsStringAsync();
    Debug.WriteLine(content);
}

4.框架轻松分析

1.全局设计

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

2.Master和Work分析

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

理解这一步时,当然master和work除了主要承载了整个管理系统的UI可视化、后台业务操作、任务执行之外,如果从细节以及实现方法来说主要做了以下事情:

Master

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

Work

  • 1.取出任务设置信息
  • 2.采用Quartz根据设置运行任务
  • 3.采用反射调用程序集
  • 4.采用httpclient调用http 接口

到此这篇关于分布式任务调度ScheduleMaster的文章就介绍到这了,更多相关分布式任务调度ScheduleMaster内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多兼容脚本之家!

您可能感兴趣的文章:
  • .net任务调度框架Hangfire简介
  • Abp集成HangFire开源.NET任务调度框架
  • 详解.NET Core采用Quartz执行调度任务进阶
  • 详解免费开源的DotNet任务调度组件Quartz.NET(.NET组件介绍之五)
  • .net任务调度框架FluentScheduler简介

最新游戏

更多

Copyright©2010-2019. All rights reserved | 波波三国游戏官网|[email protected]

备案编号:湘ICP备2022015115号-4