文档

Golang SDK接入

更新时间:

本文介绍如何将Golang应用通过SDK快速接入SchedulerX。

控制台配置

  1. 请参见创建应用,创建一个普通应用,并参见步骤2,生成配置信息。

    image.png

    image.png

  2. 请参见任务管理,创建一个Golang类型任务。

    image.png

客户端接入

说明

SchedulerX提供的Golang SDK暂不适用于Windows环境。

  1. 执行以下命令,使用最新的tag拉取Go版本的SchedulerX SDK。

    go get github.com/alibaba/schedulerx-worker-go@{最新的tag}

    或执行以下命令,拉取某个分支。

    go get github.com/alibaba/schedulerx-worker-go@{分支名}
  2. 编写业务代码,实现Processor接口。

    type Processor interface {
        Process(ctx *processor.JobContext) (*ProcessResult, error)
    }

    示例:

    package main
    
    import (
    	"fmt"
    	"github.com/alibaba/schedulerx-worker-go/processor"
    	"github.com/alibaba/schedulerx-worker-go/processor/jobcontext"
    	"time"
    )
    
    var _ processor.Processor = &HelloWorld{}
    
    type HelloWorld struct{}
    
    func (h *HelloWorld) Process(ctx *jobcontext.JobContext) (*processor.ProcessResult, error) {
    	fmt.Println("[Process] Start process my task: Hello world!")
    	// mock execute task
    	time.Sleep(3 * time.Second)
    	ret := new(processor.ProcessResult)
    	ret.SetStatus(processor.InstanceStatusSucceed)
    	fmt.Println("[Process] End process my task: Hello world!")
    	return ret, nil
    }
    
  3. 注册Client和Job,任务名称与控制台保持一致。

    package main
    
    import (
    	"github.com/alibaba/schedulerx-worker-go"
    )
    
    func main() {
    	// This is just an example, the real configuration needs to be obtained from the platform
    	cfg := &schedulerx.Config{
    		Endpoint:  "acm.aliyun.com",
    		Namespace: "433d8b23-xxx-xxx-xxx-90d4d1b9a4af",
    		GroupId:   "xueren_sub",
    		AppKey:    "xxxxxx",
    	}
    	client, err := schedulerx.GetClient(cfg)
    	if err != nil {
    		panic(err)
    	}
    	task := &HelloWorld{}
    
    	// 给你的任务取一个名字,并注册到client中,要和控制台保持一致
    	client.RegisterTask("HelloWorld", task)
    	select {}
    }
    

Client配置参数

配置项

接口

说明

自定义端口

config.WithGrpcPort

非单机任务,worker之间需要互联,可以指定端口,不配置的话随机选择空闲端口。

自定义网卡

config.WithIface

如果机器上有多个网卡,想要使用指定网卡的ip,可以自定义网卡名称。

示例:

func main() {
    // This is just an example, the real configuration needs to be obtained from the platform
    cfg := &schedulerx.Config{
        Endpoint:  "acm.aliyun.com",
        Namespace: "fa6ed99e-xxxxxx-a2bf1659d039",
        GroupId:   "xueren_test_sub",
        AppKey:    "myV5K5Xaf1kxxxxxxxx",
    }
    client, err := schedulerx.GetClient(cfg, schedulerx.WithWorkerConfig(config.NewWorkerConfig(
        config.WithGrpcPort(8001),
        config.WithIface("eth0"))))
    if err != nil {
        panic(err)
    }

    // The name TestMapReduceJob registered here must be consistent with the configured on the platform
    task := &TestMapReduceJob{
        mapjob.NewMapReduceJobProcessor(), // FIXME how define user behavior
    }
    client.RegisterTask("TestMapReduceJob", task)
    select {}
}

结果验证

  1. 客户端接入完成,将该应用发布到阿里云。

  2. 登录分布式任务调度平台
  3. 在顶部菜单栏选择地域。
  4. 在左侧导航栏,单击应用管理

  5. 应用管理页面查看实例总数

    • 如果实例总数为0,则说明应用接入失败。请检查、修改本地应用。

    • 如果实例总数不为0,显示接入的实例个数,则说明应用接入成功。在操作列单击查看实例,即可在连接实例对话框中查看实例列表。

后续步骤

应用接入SchedulerX完成后,即可在分布式任务调度平台创建调度任务。更多信息,请参见创建调度任务

相关文档

Golang任务

  • 本页导读 (1)
文档反馈