service/vendor/github.com/xxl-job/xxl-job-executor-go
lwl0608 34fa94bf1f consul 2024-05-16 19:59:10 +08:00
..
.gitignore by Robin at 20240117; xxl-job 2024-01-17 19:52:54 +08:00
LICENSE by Robin at 20240117; xxl-job 2024-01-17 19:52:54 +08:00
README.md by Robin at 20240117; xxl-job 2024-01-17 19:52:54 +08:00
constants.go by Robin at 20240117; xxl-job 2024-01-17 19:52:54 +08:00
dto.go by Robin at 20240117; xxl-job 2024-01-17 19:52:54 +08:00
executor.go consul 2024-05-16 19:59:10 +08:00
log.go by Robin at 20240117; xxl-job 2024-01-17 19:52:54 +08:00
log_handler.go by Robin at 20240117; xxl-job 2024-01-17 19:52:54 +08:00
middleware.go by Robin at 20240117; xxl-job 2024-01-17 19:52:54 +08:00
optinos.go by Robin at 20240117; xxl-job 2024-01-17 19:52:54 +08:00
task.go by Robin at 20240117; xxl-job 2024-01-17 19:52:54 +08:00
task_list.go by Robin at 20240117; xxl-job 2024-01-17 19:52:54 +08:00
util.go by Robin at 20240117; xxl-job 2024-01-17 19:52:54 +08:00

README.md

xxl-job-executor-go

很多公司java与go开发共存java中有xxl-job做为任务调度引擎为此也出现了go执行器(客户端),使用起来比较简单:

支持

1.执行器注册
2.耗时任务取消
3.任务注册像写http.Handler一样方便
4.任务panic处理
5.阻塞策略处理
6.任务完成支持返回执行备注
7.任务超时取消 (单位0为不限制)
8.失败重试次数(在参数param中目前由任务自行处理)
9.可自定义日志
10.自定义日志查看handler
11.支持外部路由可与gin集成
12.支持自定义中间件

Example

package main

import (
	"context"
	"fmt"
	xxl "github.com/xxl-job/xxl-job-executor-go"
	"github.com/xxl-job/xxl-job-executor-go/example/task"
	"log"
)

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("9999"),       //默认9999非必填
		xxl.RegistryKey("golang-jobs"), //执行器名称
		xxl.SetLogger(&logger{}),       //自定义日志
	)
	exec.Init()
	exec.Use(customMiddleware)
	//设置日志查看handler
	exec.LogHandler(customLogHandle)
	//注册任务handler
	exec.RegTask("task.test", task.Test)
	exec.RegTask("task.test2", task.Test2)
	exec.RegTask("task.panic", task.Panic)
	log.Fatal(exec.Run())
}

// 自定义日志处理器
func customLogHandle(req *xxl.LogReq) *xxl.LogRes {
	return &xxl.LogRes{Code: xxl.SuccessCode, Msg: "", Content: xxl.LogResContent{
		FromLineNum: req.FromLineNum,
		ToLineNum:   2,
		LogContent:  "这个是自定义日志handler",
		IsEnd:       true,
	}}
}

// xxl.Logger接口实现
type logger struct{}

func (l *logger) Info(format string, a ...interface{}) {
	fmt.Println(fmt.Sprintf("自定义日志 - "+format, a...))
}

func (l *logger) Error(format string, a ...interface{}) {
	log.Println(fmt.Sprintf("自定义日志 - "+format, a...))
}

// 自定义中间件
func customMiddleware(tf xxl.TaskFunc) xxl.TaskFunc {
	return func(cxt context.Context, param *xxl.RunReq) string {
		log.Println("I am a middleware start")
		res := tf(cxt, param)
		log.Println("I am a middleware end")
		return res
	}
}

示例项目

github.com/xxl-job/xxl-job-executor-go/example/

与gin框架集成

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

xxl-job-admin配置

添加执行器

执行器管理->新增执行器,执行器列表如下:

AppName		名称		注册方式	OnLine 		机器地址 		操作
golang-jobs	golang执行器	自动注册 		查看 ( 1    

查看->注册节点

http://127.0.0.1:9999

添加任务

任务管理->新增(注意使用BEAN模式JobHandler与RegTask名称一致)

1	测试panic	BEANtask.panic	* 0 * * * ?	admin	STOP	
2	测试耗时任务	BEANtask.test2	* * * * * ?	admin	STOP	
3	测试golang	BEANtask.test		* * * * * ?	admin	STOP