-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
executable file
·79 lines (68 loc) · 1.88 KB
/
main.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// GNU GPL v3 License
// Copyright (c) 2017 github.com:iTrellis
package main
import (
"context"
"flag"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"time"
"github.com/iTrellis/etcdnaming"
"github.com/iTrellis/etcdnaming/go-server/proto"
"google.golang.org/grpc"
"google.golang.org/grpc/balancer/roundrobin"
)
var (
name = flag.String("name", "hello", "service name")
ver = flag.String("ver", "v0", "server's version")
cli = flag.String("client", "1", "clinet name")
reg = flag.String("reg", "http://127.0.0.1:2379", "register etcd address")
)
func init() {
if err := os.Setenv("GRPC_GO_LOG_SEVERITY_LEVEL", "info"); err != nil {
panic(err)
}
}
func main() {
flag.Parse()
fmt.Println("connect to:", *name, *ver)
etcdnaming.NewBuilder(etcdnaming.BuilderOptions{
Server: *name,
Version: *ver,
Endpoint: *reg,
LooperTime: 5 * time.Second,
})
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
c, err := etcdnaming.DialContext(ctx, grpc.WithInsecure(),
grpc.WithDefaultServiceConfig(fmt.Sprintf(`{"LoadBalancingPolicy": "%s"}`, roundrobin.Name)))
if err != nil {
log.Printf("grpc dial: %s", err)
return
}
client := proto.NewHelloClient(c)
ticker := time.NewTicker(1000 * time.Millisecond)
for t := range ticker.C {
resp, err := client.SayWorld(context.Background(), &proto.ReqSayWorld{Name: *cli + ": haha"})
if err != nil {
log.Println("aa:", err)
time.Sleep(time.Second)
continue
}
fmt.Printf("%v: Reply is %s\n", t, resp.Message)
}
// getting stop message and revoke etcd
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGTERM, syscall.SIGINT, syscall.SIGHUP, syscall.SIGQUIT)
message := <-ch
log.Printf("receive signal '%v'", message)
if err := c.Close(); err != nil {
log.Printf("failure to close etcd client: '%v'", err)
} else {
log.Printf("close etcd ok")
}
time.Sleep(time.Second)
}