forked from printfcoder/stack-rpc-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
39 lines (34 loc) · 865 Bytes
/
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
package main
import (
"context"
"os"
"time"
pb "github.com/micro-in-cn/tutorials/examples/grpc/proto/go/pure-grpc"
log "github.com/micro/go-micro/v2/logger"
"google.golang.org/grpc"
)
const (
address = "localhost:9090"
defaultName = "我是来自grpc风格的客户端请求!"
)
func main() {
// Set up a connection to the server.
conn, err := grpc.Dial(address, grpc.WithInsecure())
if err != nil {
log.Fatalf("did not connect: %v", err)
}
defer conn.Close()
c := pb.NewSayClient(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)
defer cancel()
r, err := c.Hello(ctx, &pb.Request{Name: name})
if err != nil {
log.Fatalf("could not greet: %v", err)
}
log.Infof("Greeting: %s", r.Msg)
}