package main import ( "github.com/streadway/amqp" "log" "net" "net/rpc" "net/rpc/jsonrpc" "os" "time" ) type MPMsg struct { } func (mpMsg *MPMsg) Handle(args *map[string]interface{},reply *map[string]interface{}) error { *reply = map[string]interface{}{} return nil } func main() { lf,_ := os.OpenFile("/var/log/"+time.Now().Format("2006-01-02T15:04:05")+".log",os.O_APPEND|os.O_CREATE|os.O_RDWR,0666) defer lf.Close() log.SetOutput(lf) mpMsg := new(MPMsg) rpc.Register(mpMsg) addr,_ := net.ResolveTCPAddr("tcp",":135") ln,e := net.ListenTCP("tcp",addr) if e != nil { panic(e) } for { conn,e := ln.Accept() if e != nil { continue } go jsonrpc.ServeConn(conn) } }
// RPC calls. rpcClient,e := jsonrpc.Dial("tcp","2d1c07f4-8d2d-11e5-bf81-54ee755d5aba:135") if e != nil { log.Println("Error dail rpc server:",e) http.Error(w,e.Error(),http.StatusInternalServerError) return } var reply map[string]interface{} e = rpcClient.Call("MPMsg.Handle",msg,&reply) if e != nil { log.Println("Error call rpc method:",http.StatusInternalServerError) return }原文链接:https://www.f2er.com/go/190087.html