Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
rushuinet committed Dec 23, 2020
1 parent ea87871 commit 4bbb522
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

go.sum
48 changes: 46 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,46 @@
# xxl-job-executor
与xxl-job-executor的集成
# xxl-job-executor的gin中间件
## 背景
xxl-job-executor-go是xxl-job的golang执行器,可以独立运行,有时候我们要与项目或者框架(如:gin框架)集成起来合并为一个服务,本项目因此而生。
## 执行器项目地址
https://github.com/xxl-job/xxl-job-executor-go
## 与gin集成示例
```go
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() {
//初始化执行器
exec := xxl.NewExecutor(
xxl.ServerAddr("http://127.0.0.1/xxl-job-admin"),
xxl.AccessToken(""), //请求令牌(默认为空)
xxl.ExecutorIp("127.0.0.1"), //可自动获取
xxl.ExecutorPort(Port), //默认9999(此处要与gin服务启动port必需一至)
xxl.RegistryKey("golang-jobs"), //执行器名称
)
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")
})

//注册任务handler
exec.RegTask("task.test", task.Test)
exec.RegTask("task.test2", task.Test2)
exec.RegTask("task.panic", task.Panic)

log.Fatal(r.Run(":" + Port))
}
```
38 changes: 38 additions & 0 deletions example/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
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() {
//初始化执行器
exec := xxl.NewExecutor(
xxl.ServerAddr("http://127.0.0.1/xxl-job-admin"),
xxl.AccessToken(""), //请求令牌(默认为空)
xxl.ExecutorIp("127.0.0.1"), //可自动获取
xxl.ExecutorPort(Port), //默认9999(此处要与gin服务启动port必需一至)
xxl.RegistryKey("golang-jobs"), //执行器名称
)
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")
})

//注册任务handler
exec.RegTask("task.test", task.Test)
exec.RegTask("task.test2", task.Test2)
exec.RegTask("task.panic", task.Panic)

log.Fatal(r.Run(":" + Port))
}
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module github.com/gin-middleware/xxl-job-executor

go 1.14

require (
github.com/gin-gonic/gin v1.6.3
github.com/xxl-job/xxl-job-executor-go v0.4.0
)
13 changes: 13 additions & 0 deletions middleware.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package xxl_job_executor_gin

import (
"github.com/gin-gonic/gin"
"github.com/xxl-job/xxl-job-executor-go"
)

func XxlJobMux(e *gin.Engine, exec xxl.Executor) {
//注册的gin的路由
e.POST("run", gin.WrapF(exec.RunTask))
e.POST("kill", gin.WrapF(exec.KillTask))
e.POST("log", gin.WrapF(exec.TaskLog))
}

0 comments on commit 4bbb522

Please sign in to comment.