The article release to www.crownblog.icu/post/rpc_01…

What is the RPC

RPC generation refers to Remote Procedure Call (RPC). == is a popular communication method between different nodes in distributed system. Its Call includes transport protocol and coding (object serial number) protocol, etc. Allows a program running on one computer to call a subroutine on another without the developer having to program for this interaction.

Actual scenario:

There are two servers, A and B. Application C on A wants to call application D on server B. Can they call directly locally?

The answer is no, but it’s very convenient to use RPC. So RPC is often described as being as simple as calling a function locally.

Common RPC Frameworks

RPC version of “Hello, World”

Basic steps:

  • The service side
  1. The registration service

Rpc. RegisterName(” service name “, object)

  1. Listen on port

listener, err := net.Listen(“tcp”, “:1234”)

  1. Establish a connection

conn, err := listener.Accept()

  1. Bind the connection to the RPC service

rpc.ServeConn(conn)

  • The client
  1. RPC connection service

client, err := rpc.Dial(“tcp”, “localhost:1234”)

  1. Calling a function remotely

Client. Call(” Service name. Method name “, incoming parameter, outgoing parameter)

Construct a HelloService type where the Hello method is used to print:

type HelloService struct {}

func (p *HelloService) Hello(request string, reply *string) error {
    *reply = "hello:" + request
    return nil
}
Copy the code

The Hello method must meet the RPC rules of the Go language:

  • Methods are derived (uppercase)
  • Methods take two arguments, both of exported type (such as struct) or built in type (such as int)
  • The second argument to the method is a pointer
  • Method has only one return value of type error interface

Register an object of type HelloService as an RPC service:

func main(a) {
    rpc.RegisterName("HelloService".new(HelloService))

    listener, err := net.Listen("tcp".": 1234")
    iferr ! =nil {
        log.Fatal("ListenTCP error:", err)
    }

    conn, err := listener.Accept()
    iferr ! =nil {
        log.Fatal("Accept error:", err)
    }

    rpc.ServeConn(conn)
}
Copy the code

The rpc.Register function call registers all object methods in the object type that meet the RPC rules as RPC functions, and all registered methods are placed under the “HelloService” service space. We then establish a unique TCP link and provide RPC services to the other party on that TCP link via the rpc.serveconn function.

Client request HelloService service:

func main(a) {
    client, err := rpc.Dial("tcp"."localhost:1234")
    iferr ! =nil {
        log.Fatal("dialing:", err)
    }

    var reply string
    err = client.Call("HelloService.Hello"."hello", &reply)
    iferr ! =nil {
        log.Fatal(err)
    }

    fmt.Println(reply)
}
Copy the code

Dial the RPC service first, and then invoke the specific RPC methods through client.Call. When calling client.Call, the first argument is the dot linked RPC service name and method name, and the second and third arguments are the two arguments we define for the RPC method.

Reference:

studygolang.com/pkgdoc

Chai2010. Gitbooks. IO/advanced – go…