-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
39 lines (31 loc) · 943 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"
"fmt"
pb "github.com/Danny5487401/go_package_example/08_grpc/01_grpc_helloworld/proto"
"log"
_ "github.com/mbobakov/grpc-consul-resolver" // It's important consul实现了下面的两个接口
"google.golang.org/grpc"
)
func main() {
conn, err := grpc.DialContext(
context.Background(),
// whoami: 是名称
"consul://tencent.danny.games:8500/helloworld",
grpc.WithInsecure(),
//grpc.WithBlock(),
//grpc.WithBalancerName() 已经弃用的方法
//关于serverConfig https://github.com/grpc/grpc/blob/master/doc/service_config.md
grpc.WithDefaultServiceConfig(`{"loadBalancingPolicy": "round_robin"}`), //json格式
)
if err != nil {
log.Fatal(err)
}
defer conn.Close()
c := pb.NewGreeterClient(conn)
r, err := c.SayHello(context.Background(), &pb.HelloRequest{Name: "danny"})
if err != nil {
log.Fatalf("could not greet: %v", err)
}
fmt.Sprintln(r.GetMessage())
}