Go SDK integration
This topic describes how to connect a Go application to SchedulerX using its Go SDK.
Console configuration
-
To create a standard application, see Create an application. Then, see Step 2 to obtain the configuration information.
In the Application Management list, find the target application, such as
xxl-job. In the Operation column, click connection parameters.Select Go as the language. The connection parameters are as follows:
Endpoint: acm.aliyun.com, Namespace: 433d8b23-xxx-a-90d4d1b9a4af, GroupId: xxl-job-test, AppKey: b0iXMY3Bxxx+Gew== -
To create a Go job, see job management.
In the basic configuration step, enter
hellogofor Job Name, select the Application ID, set Job Type to golang, set Task Name toHelloWorld, set execution mode to standalone execution, and set Priority to Medium.
Client integration
-
Run the following command to pull the latest tagged version of the SchedulerX SDK for Go.
go get github.com/alibaba/schedulerx-worker-go@{latest_tag}Alternatively, run the following command to pull a specific branch.
go get github.com/alibaba/schedulerx-worker-go@{branch_name} -
Write the business code to implement the
Processorinterface.type Processor interface { Process(ctx *processor.JobContext) (*ProcessResult, error) }Example:
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 } -
Register the client and the job. The task name must match the name configured in the console.
package main import ( "github.com/alibaba/schedulerx-worker-go" ) func main() { // This is an example. You must obtain the actual configuration from the console. 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{} // Give your task a name and register it with the client. The name must match the one in the console. client.RegisterTask("HelloWorld", task) select {} }
Client parameters
|
Parameter |
API |
Description |
|
Custom port |
config.WithGrpcPort |
For non-standalone jobs, workers must communicate with each other. You can specify a communication port. If you do not specify one, a random idle port is used. |
|
Custom NIC |
config.WithIface |
If a machine has multiple NICs, you can specify the NIC whose IP address to use. |
|
Custom label |
config.WithLabel |
You can add labels to clients and then target jobs to clients with specific labels. This is often used for canary releases and testing. |
Example:
func main() {
// This is an example. You must obtain the actual configuration from the console.
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 match the name configured in the console.
task := &TestMapReduceJob{
mapjob.NewMapReduceJobProcessor(), // FIXME how define user behavior
}
client.RegisterTask("TestMapReduceJob", task)
select {}
}
Verify the result
-
After you connect the client, deploy the application to Alibaba Cloud.
-
Log on to the SchedulerX console.
-
In the top navigation bar, select a region.
-
In the left-side navigation pane, click Application Management.
-
On the Application Management page, view the Total number of instances.
-
If the total number of instances is 0, the connection failed. Check your application configuration.
-
If the total number of instances is greater than 0, the connection is successful. This value indicates the number of connected instances.
-
What to do next
After the application is connected to SchedulerX, you can create jobs in the SchedulerX console. For more information, see the "Create a job" section of the Job management topic.