本文介紹如何將Golang應用通過SDK快速接入SchedulerX。
控制台配置
用戶端接入
執行以下命令,使用最新的tag拉取Go版本的SchedulerX SDK。
go get github.com/alibaba/schedulerx-worker-go@{最新的tag}或執行以下命令,拉取某個分支。
go get github.com/alibaba/schedulerx-worker-go@{分支名}編寫業務代碼,實現
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 }註冊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,可以自訂網卡名稱。 |
自訂標籤 | config.WithLabel | 可以給用戶端打上標籤,配置任務指定標籤調度,常用來做灰階發布及測試。 |
樣本:
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")))),
config.WithLabel("test")
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 {}
}結果驗證
用戶端接入完成,將該應用發布到阿里雲。
- 登入分布式任務調度平台。
- 在頂部功能表列選擇地區。
在左側導覽列,單擊應用管理。
在應用管理頁面查看執行個體總數。
如果執行個體總數為0,則說明應用接入失敗。請檢查、修改本地應用。
如果執行個體總數不為0,顯示接入的執行個體個數,則說明應用接入成功。在操作列單擊查看執行個體,即可在串連執行個體對話方塊中查看執行個體列表。
後續步驟
應用接入SchedulerX完成後,即可在分布式任務調度平台建立調度任務。更多資訊,請參見建立調度任務。


