Client
In the normal we register methods as services'methods. Methods must follow the below rules:
- exported method of exported type
- three arguments, the first is of context.Context, both of exported type for three arguments
- the third argument is a pointer
one return value, of type error
And rpcx can also register raw functions as services and functions must follow the below rules:- three arguments, the first is of context.Context, both of exported type for three arguments
- the third argument is a pointer
- one return value, of type error
There is an example.
Server must use to register a function and provides a service name.
```go server.gotype Args struct { A int B int}
func mul(ctx context.Context, args _Args, reply _Reply) error { reply.C = args.A * args.B return nil}
func main() { flag.Parse()
Client use the service name and function name to access:
```go client.go
d := client.NewPeer2PeerDiscovery("tcp@"+*addr, "")
xclient := client.NewXClient("a.fake.service", client.Failtry, client.RandomSelect, d, client.DefaultOption)
args := &example.Args{
B: 20,
}
err := xclient.Call(context.Background(), "mul", args, reply)
if err != nil {
log.Fatalf("failed to call: %v", err)
}
log.Printf("%d * %d = %d", args.A, args.B, reply.C)
By smallnest updated 2018-12-04 11:47:26