RPC work flow chart
- 1. Call the client handle; performs transmission parameters
- 2. Call the local system kernel to send network messages
- 3. The message to the remote host
- 4. The server handles the message and obtain parameters obtained
- 5. Perform remote procedure
- 6. The process of execution of the results back to the server handle
- 7. The server returns the handle, call the remote system kernel
- 8. When the news reached the local host
- 9. Client receives the message handler by the kernel
- 10. The client receives the data returned handle
Go language support for the RPC: HTTP、TCP、JSPNRPC
but in the Go
middle RPC
is unique in that it uses a GoLang Gob
coding language can only support Go!
- GoLang Gob: Golang is carrying a packet data structure of a sequence encoding / decoding tool. Coding uses Encoder, decoding using Decoder. A typical application scenario is RPC (remote procedure calls).
HTTP RPC Demo
- Server-side code
main Package Import ( " errors " " FMT " " NET / HTTP " " NET / RPC " ) type Arith int FUNC rpcDemo () { arith: = new new (Arith) fmt.Println ( " arith === " , arith) RPC .Register (arith) // HandleHTTP registered RPC HTTP handlers message to the debug server // debugging programs on DEFAUTUPCPATH and debug debugging path. // still need to call http.Services (), usually in the GO statement. rpc.HandleHTTP () ERR: = http.ListenAndServe ( " : 1234" , Nil) IF ERR! = Nil { fmt.Println ( " ERR ================ " , err.Error ()) } } type the Args struct { A, B int } type Quotient struct { Quo, Rem int } / / function must be derived (initials) // must have two types of parameters derived, // the first parameter is the received parameter, the second parameter is returned to the client parameter, a second parameter must be pointer type // function also has a return error FUNC (Arith T *) the Multiply (* args the args, Reply * int ) {error * Reply * = args.A args.B fmt.Println (" This method of implementation of the Multiply ah --- Hey --- " , Reply) return nil } FUNC (T * Arith) Divide (* args the Args, Quo * Quotient) {error IF args.B == 0 { return errors. new ( " Divide by ZERO " ) } quo.Quo = args.A / args.B quo.Rem = args.A% args.B fmt.Println ( " this method of performing ah Hey --- --- Divide quo == " , Quo) return nil } FUNC main () { rpcDemo () }
The server is running: go run server.go
- The client code
package main import ( "flag" "fmt" "log" "net/rpc" "strconv" ) type ArgsTwo struct { A, B int } type QuotientTwo struct { Quo, Rem int } type Conf struct { serverAddress string i1 string i2 string } var conf = Conf{} func SetConfiguration() { flag.StringVar(&conf.serverAddress, "address", "127.0.0.1:1234", "The address of the rpc") flag.StringVar(&conf.i1, "i1", "100", "100") flag.StringVar(&conf.i2, "i2", "2", "2") } func main() { SetConfiguration() flag.Parse() fmt.Println ( "severAddress = " , Conf.serverAddress) // DelayHTTP at a specified network address to connect to HTTP RPC server // the default listener on HTTP RPC path. Client, ERR: = rpc.DialHTTP ( " tcp " , conf.serverAddress) IF ERR =! {nil log.Fatal ( " error occurs in the place where DialHTTP " , ERR) } i1_, _: = strconv.Atoi (conf.i1) i2_, _: = strconv.Atoi (conf.i2) args: = ArgsTwo { A: i1_, B: i2_} var Reply int // . named functions call call, wait for it to complete, it returns an error status and the Client.call = ERR ( "Arith.Multiply " , args, & Reply) IF ! ERR = nil { log.Fatal ( " Call the Multiply error error oh arith: " , ERR) } fmt.Printf ( " Arith multiplication:% d *% d =% D \ n- " , args.A, args.B, Reply) var quot QuotientTwo // call the function call name, wait for it to complete, and returns to its error state. ERR = the Client.call ( " Arith.Divide " , args, & quot) IF ! ERR = nil { log.Fatal ( " arith error: " " } Fmt.Printf (Arith taking the integer division:% d /% d =% d remainder D% \ n- " , args.A, args.B, quot.Quo, quot.Rem) }
Client compiler: go build client.go
client runs: -
[the root @ wangjq RPC] # ./ Client severAddress = 127.0 . 0.1 : 1234 Arith multiplication: 100 * 2 = 200 is Arith division rounded: 100 / 2 = 50 remainder 0 [the root @ wangjq RPC] # [the root @ wangjq RPC] #. / Client --address 127.0 . 0.1 : 1234 -i1 200 -I2 5 severAddress = 127.0 . 0.1 : 1234 Arith multiplication: 200 * 5 =1000 Arith taking the integer division: 200 / 5 = 40 remainder 0