Remote Procedure Calls with RPC
This lesson covers how Go lets two different machines (a server machine and a client machine) communicate with another using remote calls.
We'll cover the following...
Go programs can communicate with each other through the net/rpc-package
; so this is another application of the client-server paradigm. It provides a convenient means of making function calls over a network connection. Of course, this is only useful when the programs run on different machines. The rpc
package builds on the package gob
to turn its encode/decode automation into transport for method calls across the network.
A server registers an object, making it visible as a service with the type-name of the object. This provides access to the exported methods of that object across a network or other I/O connection for remote clients. It is all about exposing methods on types over a network. The package uses the Http and TCP protocol, and the gob
package for data transport. A server may register multiple objects (services) of different types, but it is an error to register multiple objects of the same type.
Here we discuss a simple example:
package main import ( "net/http" "log" "net" "net/rpc" "time" "rpcobjects" ) func main() { calc := new(rpcobjects.Args) rpc.Register(calc) rpc.HandleHTTP() listener, e := net.Listen("tcp", "0.0.0.0:3001") if e != nil { log.Fatal("Starting RPC-server -listen error:", e) } go http.Serve(listener, nil) time.Sleep(1000e9) }
Remark: If you’re running locally, ...