定义rpc调用的方法
package library
type Watcher intfunc (w *Watcher) GetInfo(arg int,result *int) error{ *result = 1 return nil}
服务器端代码
package mainimport( "fmt" "net" "net/rpc" "net/http" "library")func main(){ watcher := new(library.Watcher) rpc.Register(watcher) rpc.HandleHTTP() l,err := net.Listen("tcp", ":1234") if err != nil{ fmt.Println("监听失败,端口可能已经被占用") } fmt.Println("正在监听1234端口") http.Serve(l,nil)}
客户端代码
package mainimport ( "fmt" "net/rpc")func main(){ client,err := rpc.DialHTTP("tcp", "127.0.0.1:1234") if err !=nil{ fmt.Println("链接rpc服务器失败:",err) } var reply int err = client.Call("Watcher.GetInfo", 1, &reply) if err!=nil{ fmt.Println("调用远程服务失败",err) } fmt.Println("远程服务返回结果:", reply)}
客户端调用后,如果正常会返回 1;