xxl-job-executor的gin中間件

xxl-job-executor的gin中間件

背景

xxl-job-executor-go是xxl-job的golang執(zhí)行器,可以獨立運行,有時候我們要與項目或者框架(如:gin框架)集成起來合并為一個服務(wù),本項目因此而生。

本項目地址

https://github.com/gin-middleware/xxl-job-executor

執(zhí)行器項目地址

https://github.com/xxl-job/xxl-job-executor-go

與gin集成示例

package main

import (
    "github.com/gin-gonic/gin"
    "github.com/gin-middleware/xxl-job-executor"
    "github.com/xxl-job/xxl-job-executor-go"
    "github.com/xxl-job/xxl-job-executor-go/example/task"
    "log"
)

const Port = "9999"

func main() {
    //初始化執(zhí)行器
    exec := xxl.NewExecutor(
        xxl.ServerAddr("http://127.0.0.1/xxl-job-admin"),
        xxl.AccessToken(""),            //請求令牌(默認(rèn)為空)
        xxl.ExecutorIp("127.0.0.1"),    //可自動獲取
        xxl.ExecutorPort(Port),         //默認(rèn)9999(此處要與gin服務(wù)啟動port必需一至)
        xxl.RegistryKey("golang-jobs"), //執(zhí)行器名稱
    )
    exec.Init()
    //添加到gin路由
    r := gin.Default()
    xxl_job_executor_gin.XxlJobMux(r, exec)

    //注冊gin的handler
    r.GET("ping", func(cxt *gin.Context) {
        cxt.JSON(200, "pong")
    })

    //注冊任務(wù)handler
    exec.RegTask("task.test", task.Test)
    exec.RegTask("task.test2", task.Test2)
    exec.RegTask("task.panic", task.Panic)

    log.Fatal(r.Run(":" + Port))
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容