zoukankan      html  css  js  c++  java
  • 分布式系统架构之构建你的任务调度中心(转载)

    分布式系统架构之构建你的任务调度中心

    分布式系统中,我们经常会遇到定时执行任务,而这些定时任务中,多数情况都是需要执行一些http请求。
    比如:

    • 轮训支付结果(虽然第三方支付中心有支付回调,但有时候并不能有效保证你的业务系统能收到正确的结果)
    • 未支付订单超时取消,电商系统订单,用户未支付订单,超时后取消订单
    • 已支付已签收订单,超时后自动完成订单
    • 同步微信公众号用户数据做分析
    • 同步企业微信通讯录及客户信息
    • 等等
      很多业务场景都需要用到定时执行http请求的任务

    本次,我们在netcore 环境,使用 Jango.JobCenter
    来快速构建我们的任务调度中心

    Jango.JobCenter目前是基于Hangfire的 .NETStandard 2.0版本
    Demo源码,请移步https://github.com/jangocheng/Jango.JobCenter.demo

    dotnet new webapi 创建一个webapi项目

    image

    dotnet add package Jango.JobCenter --version 1.0.0.1

    image

    编辑StartUp文件

    1. 引用 Jango.JobCenter
    using Jango.JobCenter;
    
    1. 修改ConfigureServices(IServiceCollection services)
    services.AddJangoJobCenter();
    
    1. 修改Configure(IApplicationBuilder app, IWebHostEnvironment env)
    app.UseJangoJobCenter();
    

    image

    还原依赖包后,dotnet run 运行

    image

    可以看到,我们的任务调度中心已经运行起来了。

    https://localhost:5001/hangfire 查看任务中心仪表盘

    image

    https://localhost:5001/swagger/index.html 查看Jango.JobCenter 的文档

    image

    Jango.JobCenter
    第一个接口为测试接口,仅仅Console输出

    curl -X POST "https://localhost:5001/console" -H "accept: */*" -d ""
    

    或者
    postman post 调用https://localhost:5001/console
    image

    可以看到结果输出 job name console is working now.

    第二个接口为http 创建定时任务接口
    参数如下:

    {
      "name": "",//job name
      "desc": "",//job 描述
      "url": "",//定时调用url
      "method": "", //http method :get/post/put/delete
      "header": "",//http header 以kv形式,多个以,分割 比如: k1:v1,k2:v2
      "requestBody": "",//http 请求体
      "isRetry": true,//遇到错误是否重试
      "retryTimes": 0,//重试次数
      "isCircle": true,//是否周期性任务
      "cron": "*/5 * * * * ?"//cron 表达式  此项移除,则系统默认5s执行一次http请求
    }
    

    我们来测试下:
    参数输入:

    {
      "name": "demo", //demo
      "desc": "定时调用百度",//
      "url": "https://www.baidu.com",
      "method": "get",//get方式
      "header": "",
      "requestBody": "",
      "isRetry": false,// 错误后不重试
      "retryTimes": 0,
      "isCircle": true//周期性任务 
      //无Cron参数,系统默认每隔5s执行一次任务
    }
    

    postman执行:
    返回结果:
    image
    来看下console输出:

    image

    黄色背景的内容,即为定时执行的结果。
    我们来看下任务中心面板
    image

    我们来看下任务中心仪表盘
    image
    可以看到已经开始执行任务了

    至此,你的任务调度中心,已经构建完成。可以任意创建相关任务了。

    原文链接:https://www.cnblogs.com/Sages/p/13322317.html

  • 相关阅读:
    springboot设置请求超时
    Dockerfile 中 ENTRYPOINT 与 CMD 的区别
    iptables
    git commit statistics
    query spring mongo limit or skip test
    创建证书
    Linux基本网络配置
    k8s
    iis express添加网站,并启动
    用cmd的方式执行exe程序
  • 原文地址:https://www.cnblogs.com/cmd-/p/13322666.html
Copyright © 2011-2022 走看看