ScheduleMaster分布式任务调度中心基本使用和原理
文章目录
- 一、ScheduleMaster 核心概念
- 二、ScheduleMaster 应用场景
- 三、ScheduleMaster 项目落地
- 四、ScheduleMaster 运行原理
- 五、ScheduleMaster 程序集任务
- 六、ScheduleMaster API接口任务(使用代码自定义创建任务)
- 七、ScheduleMaster 集群和集群原理
一、ScheduleMaster 核心概念
- 概念
统一执多个系统的任务【回收超时订单,清理垃圾信息 】,如图:
二、ScheduleMaster 应用场景
- 场景
主要应用在微服务系统中。
如图:
三、ScheduleMaster 项目落地
- 工具
- ScheduleMaster
网盘下载地址:
链接:https://pan.baidu.com/s/1LcCHS__zRRJv_HHwsza3cg
提取码:eyup - Demo 项目
- ScheduleMaster
- 步骤
运行ScheduleMaster
启动命令
#进入Hos.ScheduleMaster.Web\bin\Release\netcoreapp3.1\publish目录中启动 #备注:默认数据库为sqlserver,如果其他数据库需要在appsettings.json中修改数据库类型和数据库连接地址dotnet Hos.ScheduleMaster.Web.dll #进入Hos.ScheduleMaster.QuartzHost\bin\Release\netcoreapp3.1\publish 目录中启动dotnet Hos.ScheduleMaster.QuartzHost.dll --urls http://*:30001
运行结果如图:
浏览器运行结果:http://localhost:30000,用户名:admin 密码:111111
如图:
Demo项目
- 新建一个订单回收API接口
private readonly ILogger<HomeController> logger;public HomeController(ILogger<HomeController> _logger) {logger = _logger;} /// <summary>/// 超时订单回收接口/// </summary>/// <returns></returns>[HttpPost]public IActionResult OrderCancel() {logger.LogInformation("回收订单任务");return Ok("回收订单任务");}
- 新建一个订单回收API接口
新建任务
点击任务列表
如图:
点击创建任务按钮->选择 “基础信息配置”
如图:
选择“元数据配置”,在点击“保存”即可,如图:
运行结果(每隔5秒进行调用订单回收接口),如图:
四、ScheduleMaster 运行原理
- 原理
Master 概念
主节点:协调 Hos.ScheduleMaster.WebNode 概念
工作节点:执行业务 Hos.ScheduleMaster.QuartzHost数据库
用来存储任务信息。全局架构
如图:
执行过程
客户端---->Hos.ScheduleMaster.Web(master节点)---->Hos.ScheduleMaster.QuartzHost(工作节点)---->订单回收接口- master节点的核心
- 选择工作节点
- 指定工作节点,执行任务
- 工作节点的核心
- 取出任务配置信息
- 使用Quartz根据配置运行任务
- 使用HttpClient 调用接口
- 使用反射调用程序集方法
- master节点的核心
五、ScheduleMaster 程序集任务
- 工具
- 控制台Demo项目
- ScheduleMaster
- 步骤
新建一个Console项目
//项目安装 ScheduleMaster
新建OrderServer类继承
public class OrderServer:TaskBase{ public override void Run(TaskContext context){//超时订单逻辑context.WriteLog("订单开始回收.......成功");}}
- 控制台项目打包
项目编译后,进入 bin文件件,将Hos.ScheduleMaster.Base.dll除外的所有的文件打包成压缩文件,如图:
- 控制台项目打包
ScheduleMaster 配置
- 点击“任务列表”目录,点击“创建任务”按钮,任务类型选择“程序集任务”,如图:
- 点击“任务列表”目录,点击“创建任务”按钮,任务类型选择“程序集任务”,如图:
六、ScheduleMaster API接口任务(使用代码自定义创建任务)
实现代码如下:
/// <summary>/// 通过代码创建任务/// </summary>/// <returns></returns>[HttpPost("CreateTask")]public async Task<IActionResult> CreateTask(){HttpClient client = new HttpClient();//登录 设置用户名和密码client.DefaultRequestHeaders.Add("ms_auth_user", "admin");client.DefaultRequestHeaders.Add("ms_auth_secret", MD5($"admin{MD5("111111")}admin"));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", "0/5 * * * * ?"));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", "order_cancel_http_api"));args.Add(new KeyValuePair<string, string>("HttpRequestUrl", "http://localhost:5000/Order"));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();logger.LogInformation(content); return Ok("创建成功"); } /// <summary>/// MD5加密/// </summary>/// <param name="prestr"></param>/// <returns></returns>public static string MD5(string prestr){StringBuilder sb = new StringBuilder(32);MD5 md5 = new MD5CryptoServiceProvider();byte[] t = md5.ComputeHash(Encoding.GetEncoding("UTF-8").GetBytes(prestr));for (int i = 0; i < t.Length; i++){sb.Append(t[i].ToString("x").PadLeft(2, '0'));}return sb.ToString();}
官方API
- 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"));
签名验证这块设计的比较简单,具体源码逻辑可以参考
Hos.ScheduleMaster.Web.Filters.AccessControlFilter
。API返回格式
所有接口采用统一的返回格式,字段如下:参数名称 参数类型 说明 Success bool 是否成功 Status int 结果状态,0-请求失败 1-请求成功 2-登录失败 3-参数异常 4-数据异常 Message string 返回的消息 Data object 返回的数据 创建程序集任务
- 接口地址:
http://yourip:30000/api/task/create
- 请求类型:
POST
- 参数格式:
application/x-www-form-urlencoded
- 返回结果:创建成功返回任务id
- 参数列表:
参数名称 参数类型 是否必填 说明 MetaType int 是 任务类型,这里固定是1 Title string 是 任务名称 RunLoop bool 是 是否按周期执行 CronExpression string 否 cron表达式,如果RunLoop为true则必填 AssemblyName string 是 程序集名称 ClassName string 是 执行类名称,包含完整命名空间 StartDate DateTime 是 任务开始时间 EndDate DateTime 否 任务停止时间,为空表示不限停止时间 Remark string 否 任务描述说明 Keepers List<int> 否 监护人id Nexts List<guid> 否 子级任务id Executors List<string> 否 执行节点名称 RunNow bool 否 创建成功是否立即启动 Params List<ScheduleParam> 否 自定义参数列表,也可以通过CustomParamsJson字段直接传json格式字符串 ScheduleParam:
参数名称 参数类型 是否必填 说明 ParamKey string 是 参数名称 ParamValue string 是 参数值 ParamRemark string 否 参数说明 代码示例:
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);
要提一下的是,使用API创建任务的方式不支持上传程序包,所以在任务需要启动时要确保程序包已通过其他方式上传,否则会启动失败。
- 接口地址:
创建HTTP任务
- 接口地址:
http://yourip:30000/api/task/create
- 请求类型:
POST
- 参数格式:
application/x-www-form-urlencoded
- 返回结果:创建成功返回任务id
- 参数列表:
参数名称 参数类型 是否必填 说明 MetaType int 是 任务类型,这里固定是2 Title string 是 任务名称 RunLoop bool 是 是否按周期执行 CronExpression string 否 cron表达式,如果RunLoop为true则必填 StartDate DateTime 是 任务开始时间 EndDate DateTime 否 任务停止时间,为空表示不限停止时间 Remark string 否 任务描述说明 HttpRequestUrl string 是 请求地址 HttpMethod string 是 请求方式,仅支持GET\POST\PUT\DELETE HttpContentType string 是 参数格式,仅支持application/json和application/x-www-form-urlencoded HttpHeaders string 否 自定义请求头,ScheduleParam列表的json字符串 HttpBody string 是 如果是json格式参数,则是对应参数的json字符串;如果是form格式参数,则是对应ScheduleParam列表的json字符串。 Keepers List<int> 否 监护人id Nexts List<guid> 否 子级任务id Executors List<string> 否 执行节点名称 RunNow bool 否 创建成功是否立即启动 代码示例:
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);
- 接口地址:
创建延时任务
- 接口地址:
http://yourip:30000/api/delaytask/create
- 请求类型:
POST
- 参数格式:
application/x-www-form-urlencoded
- 返回结果:创建成功返回任务id
- 参数列表:
参数名称 参数类型 是否必填 说明 SourceApp string 是 来源 Topic string 是 主题 ContentKey string 是 业务关键字 DelayTimeSpan int 是 延迟相对时间 DelayAbsoluteTime DateTime 是 延迟绝对时间 NotifyUrl string 是 回调地址 NotifyDataType string 是 回调参数格式,仅支持application/json和application/x-www-form-urlencoded NotifyBody string 是 回调参数,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);}
- 接口地址:
- API Server 对接流程
七、ScheduleMaster 集群和集群原理
主要针对工作节点使用集群
- 步骤
- 进入工作节点项目根目录下【ScheduleMasterCore\Hos.ScheduleMaster.QuartzHost\bin\Release\netcoreapp3.1\publish】,更改配置文件【appsettings.json】, 因为要启动多个节点,只需要更改节点名称和端口号即可【切记:节点名称不能够重复】,如下:
"NodeSetting": {"IdentityName": "worker1", //节点名称"Role": "worker","Protocol": "http","IP": "localhost","Port": 30001, //端口号"Priority": 1,"MaxConcurrency": 20}
- 启动
#进入Hos.ScheduleMaster.QuartzHost\bin\Release\netcoreapp3.1\publish 目录中启动dotnet Hos.ScheduleMaster.QuartzHost.dll --urls http://*:30002
- 运行结果,如图:
- 进入工作节点项目根目录下【ScheduleMasterCore\Hos.ScheduleMaster.QuartzHost\bin\Release\netcoreapp3.1\publish】,更改配置文件【appsettings.json】, 因为要启动多个节点,只需要更改节点名称和端口号即可【切记:节点名称不能够重复】,如下:
- 步骤
场景
如果当前的工作节点宕机了,会不会转移到其他的工作节点上?
需要更改任务的执行节点,选择多个执行节点,如图:
有两个工作节点,30001和30002,其中把30001宕机了[有个心跳检测的API接口,定时任务不断的检测当前的节点是否可用],会直接转移到其他节点执行任务,运行结果如下:
ScheduleMaster分布式任务调度中心基本使用和原理相关推荐
- 分布式任务调度中心xxl-job
xxl-job 分布式任务调度中心优点 xxl-job简介 部署调度中心 执行器的配置 任务配置 分布式任务调度中心优点 1.多台机器集群部署保证定时任务不被重复执行 2.动态的调整定时任务的执行时间 ...
- hutool的定时任务不支持依赖注入怎么办_分布式任务调度平台xxljob的内部原理,及在转转的落地实践...
让世界因流转更美好 值此教师节来临之际,衷心祝愿所有的老师教师节快乐,身体健康,幸福平安,工作顺利,桃李满天下.您们辛苦了! 作者简介 · 杜云杰,架构师,转转架构部负责人,负责服务治理.MQ.云平台 ...
- 第二十章 分布式任务调度中心DolphinScheduler架构设计
1.调度系统概述 1.1.调度系统介绍 含义:在指定时间协调器通过分布式执行器并行执行任务. (1)目标 分布式环境下处理任务调度,在基于给定的时间点,给定的时间间隔或者给定执行次数自动的执行任务 ...
- xxl-job分布式任务调度中心部署实践(1)
文章目录 前言 环境说明 一.xxl-job是什么? 二.使用步骤 1.下载源码 2.工程介绍 3.xxl-job-admin配置 3.1 初始化数据库 3.2 修改xxl-job-admin配置 3 ...
- SpringBoot集成XxlJob分布式任务调度中心(超详细之手把手教学)
一.前言 废话就不多说了,介绍Xxl-Job的网上已经有很多,本文就不多加复制粘贴了,直接步入第二步.(PS:本文包括Xxl-Job分布式定时任务调度中心的搭建,以及SpringBoot集成XxlJo ...
- 『分布式任务调度利器』掌握ScheduleMaster分布式任务调度平台的实践指南
- 分布式任务调度平台一站式讲解
文章目录 一.传统的定时任务 1. 传统的定时任务存在那些缺点 2. 定时任务集群幂等性问题 二.传统定时任务的实现方案 2.1. 多线程 2.2. TimeTask 2.3. 线程池 2.4. Sp ...
- xxl-job(许雪里开发),分布式任务调度平台+定时任务
xxl-Job(分布式任务调度中心(许雪里开发) 面试题:你的项目是分布式的,项目的定时任务是怎么做的? 分布式任务调度如果让你来实现,有什么思路,怎么实现? 有没有了解过一些开源的方案来解决这一块问 ...
- Quartz分布式任务调度原理
什么是分布式定时任务调度 定时任务调度 在很多应用场景下我们需要定时执行一些任务,比如订单系统的超时状态判断.缓存数据的定时更新等等,最简单粗暴的方式是用while(true)+sleep的组合来空转 ...
最新文章
- sqlconnection,sqlcommand,SqlDataAdapter ,ExecuteNonQuery,ExecuteScalar
- Android动态加载插件APK
- 软件设计过程经验谈 之 如何做好领域模型设计
- python字典与json转换_python字典与json转换的方法总结
- 利用SoapHeader验证web service调用的合法性(dwonmoon)
- 实验楼 linux内核原理与分析,《Linux内核原理与分析》第一周作业 20189210
- mysql sql 备份数据_mysql怎么进行数据库备份和还原,以及自动备份
- php 结构和函数 区别吗,php的语言结构和函数的区别
- 2.4配置自定义拦截器
- python中for循环遍历文件_Python中的用for,while循环遍历文件实例
- JS高级程序设计——阅读笔记六
- 清华大学计算机杜瑜皓,我在清华等你来|2015国际信息学奥赛全球第四名杜瑜皓:人生不搏枉少年...
- 产品管理 OKR:最佳实践和示例
- ASP计算周开始和一年有多少周及某年第一周开始日期
- wstmart商城系统研究日志一
- 电子传真文档怎样加盖印章
- OneDrive容量缩水,微软安抚用户:Office 365免费用一年
- 九方财富更新招股书:上半年营收9亿 冲刺港股一年未果
- 访问Servlet的几种方式
- 机器视觉 边缘检测算子