-
Notifications
You must be signed in to change notification settings - Fork 0
/
grpc_client.go
43 lines (38 loc) · 933 Bytes
/
grpc_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
package main
import (
"context"
"log"
"os"
"time"
pb "cmux/helloworld"
"google.golang.org/grpc"
)
const (
address = "127.0.0.1:50051"
defaultName = "world"
)
func main() {
// Set up a connection to the server.
var opts []grpc.DialOption
//opts = append(opts, grpc.WithInsecure(), grpc.WithBlock())
opts = append(opts, grpc.WithInsecure(), grpc.WithBlock())
conn, err := grpc.Dial(address, opts...)
if err != nil {
log.Fatalf("did not connect: %v", err)
}
log.Println("connect succ")
defer conn.Close()
c := pb.NewGreeterClient(conn)
// Contact the server and print out its response.
name := defaultName
if len(os.Args) > 1 {
name = os.Args[1]
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
r, err := c.SayHello(ctx, &pb.HelloRequest{Name: name})
if err != nil {
log.Fatalf("could not greet: %v", err)
}
log.Printf("Greeting: %s", r.GetMessage())
}