-
Notifications
You must be signed in to change notification settings - Fork 140
/
main.go
61 lines (52 loc) · 1.75 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
package main
import (
"context"
"log"
"time"
"ultimate-go/awesome-go/consul/consul-resolver"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/resolver"
pb "ultimate-go/awesome-go/grpc-go/helloworld/helloworld"
)
const (
address = "localhost:50051"
)
func clientInterceptor(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
return invoker(ctx, method, req, reply, cc, opts...)
}
func main() {
// Set up a connection to the server.
conn, err := grpc.Dial("consul:https:///greeter", grpc.WithInsecure(), grpc.WithUnaryInterceptor(clientInterceptor))
if err != nil {
log.Fatalf("did not connect: %v", err)
}
c := pb.NewGreeterClient(conn)
for i := 0; i < 10000; i++ {
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*200)
defer cancel()
ctxx := metadata.AppendToOutgoingContext(ctx, "go_client_md_1", "c1", "go_client_md_2", "c2")
r, err := c.SayHello(ctxx, &pb.HelloRequest{Name: "world"})
if err != nil {
log.Printf("could not greet: %v", err)
} else {
log.Printf("Greeting: %v, %s", i, r.Message)
}
time.Sleep(time.Second)
}
/* ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond * 3)
defer cancel()
ctxx := metadata.AppendToOutgoingContext(ctx, "go_client_md_1", "c1", "go_client_md_2", "c2")
r, err := c.SayHello(ctxx, &pb.HelloRequest{Name: "world"})
if err != nil {
log.Fatalf("could not greet: %v", err)
}
log.Printf("Greeting: %s", r.Message)*/
//conn.Close()
time.Sleep(time.Second * 20)
}
func init() {
// Register the example ResolverBuilder. This is usually done in a package's
// init() function.
resolver.Register(&consul_resolver.Builder{})
}