forked from smallnest/rpcx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_failover.go
47 lines (37 loc) · 1003 Bytes
/
client_failover.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 (
"fmt"
"time"
"github.com/smallnest/rpcx"
"github.com/smallnest/rpcx/clientselector"
)
type Args struct {
A int
B int
}
type Reply struct {
C int
}
func main() {
server1 := &clientselector.ServerPeer{Network: "tcp", Address: "127.0.0.1:8972"}
server2 := &clientselector.ServerPeer{Network: "tcp", Address: "127.0.0.1:8973"}
server3 := &clientselector.ServerPeer{Network: "tcp", Address: "127.0.0.1:8974"}
servers := []*clientselector.ServerPeer{server1, server2, server3}
s := clientselector.NewMultiClientSelector(servers, rpcx.RoundRobin, 10*time.Second)
for i := 0; i < 10; i++ {
callServer(s)
}
}
func callServer(s rpcx.ClientSelector) {
client := rpcx.NewClient(s)
client.FailMode = rpcx.Failover
args := &Args{7, 8}
var reply Reply
err := client.Call("Arith.Mul", args, &reply)
if err != nil {
log.Infof("error for Arith: %d*%d, %v", args.A, args.B, err)
} else {
log.Infof("Arith: %d*%d=%d", args.A, args.B, reply.C)
}
client.Close()
}