/
client.go
47 lines (34 loc) · 935 Bytes
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package main
import (
"context"
"flag"
"log"
"time"
"github.com/smallnest/rpcx/share"
example "github.com/rpcxio/rpcx-examples"
"github.com/smallnest/rpcx/client"
)
var (
addr = flag.String("addr", "localhost:8972", "server address")
)
func main() {
flag.Parse()
d := client.NewPeer2PeerDiscovery("tcp@"+*addr, "")
option := client.DefaultOption
option.ReadTimeout = 10 * time.Second
xclient := client.NewXClient("Arith", client.Failtry, client.RandomSelect, d, option)
defer xclient.Close()
//xclient.Auth("bearer tGzv3JOkF0XG5Qx2TlKWIA")
xclient.Auth("bearer abcdefg1234567")
args := &example.Args{
A: 10,
B: 20,
}
reply := &example.Reply{}
ctx := context.WithValue(context.Background(), share.ReqMetaDataKey, make(map[string]string))
err := xclient.Call(ctx, "Mul", args, reply)
if err != nil {
log.Fatalf("failed to call: %v", err)
}
log.Printf("%d * %d = %d", args.A, args.B, reply.C)
}