nmid的使用包含client和worker
Client
普通golang代码中运行
const SERVERHOST = "127.0.0.1"
const SERVERPORT = "6808"
func main() {
var client *cli.Client
var err error
serverAddr := SERVERHOST + ":" + SERVERPORT
client, err = cli.NewClient("tcp", serverAddr).Start()
if nil == client || err != nil {
log.Println(err)
return
}
defer client.Close()
client.ErrHandler = func(e error) {
if model.RESTIMEOUT == e {
log.Println("time out here")
} else {
log.Println(e)
}
fmt.Println("client err here")
}
respHandler := func(resp *cli.Response) {
if resp.DataType == model.PDT_S_RETURN_DATA && resp.RetLen != 0 {
if resp.RetLen == 0 {
log.Println("ret empty")
return
}
var retStruct model.RetStruct
err := msgpack.Unmarshal(resp.Ret, &retStruct)
if nil != err {
log.Fatalln(err)
return
}
if retStruct.Code != 0 {
log.Println(retStruct.Msg)
return
}
fmt.Println(string(retStruct.Data))
}
}
paramsName1 := make(map[string]interface{})
paramsName1["name"] = "nmid"
params1, err := msgpack.Marshal(¶msName1)
if err != nil {
log.Fatalln("params msgpack error:", err)
os.Exit(1)
}
err = client.Do("ToUpper", params1, respHandler)
if nil != err {
fmt.Println(err)
}
}
Worker
普通golang代码中运行
const NMIDSERVERHOST = "127.0.0.1"
const NMIDSERVERPORT = "6808"
func ToUpper(job wor.Job) ([]byte, error) {
resp := job.GetResponse()
if nil == resp {
return []byte(``), fmt.Errorf("response data error")
}
if len(resp.ParamsMap) > 0 {
name := resp.ParamsMap["name"].(string)
retStruct := model.GetRetStruct()
retStruct.Msg = "ok"
retStruct.Data = []byte(strings.ToUpper(name))
ret, err := msgpack.Marshal(retStruct)
if nil != err {
return []byte(``), err
}
resp.RetLen = uint32(len(ret))
resp.Ret = ret
return ret, nil
}
return nil, fmt.Errorf("response data error")
}
func main() {
wname := "Worker1"
var worker *wor.Worker
var err error
serverAddr := NMIDSERVERHOST + ":" + NMIDSERVERPORT
worker = wor.NewWorker().SetWorkerName(wname)
err = worker.AddServer("tcp", serverAddr)
if err != nil {
log.Fatalln(err)
worker.WorkerClose()
return
}
worker.AddFunction("ToUpper", ToUpper)
//register to discovery server
worker.Register(wor.EtcdConfig{Addrs: discoverys, Username: disUsername, Password: disPassword})
if err = worker.WorkerReady(); err != nil {
log.Fatalln(err)
worker.WorkerClose()
return
}
go worker.WorkerDo()
quits := make(chan os.Signal, 1)
signal.Notify(quits, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT /*syscall.SIGUSR1*/)
switch <-quits {
case syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT:
worker.WorkerClose()
}
}