|
| 1 | +# grpc |
| 2 | + |
| 3 | +## 目录 |
| 4 | + |
| 5 | +* [grpc helloworld例子](#helloworld) |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +## helloworld |
| 10 | + |
| 11 | +目录结构: |
| 12 | +``` |
| 13 | +├── client.go |
| 14 | +├── helloworld |
| 15 | +│ ├── build.sh |
| 16 | +│ ├── helloworld.pb.go |
| 17 | +│ └── helloworld.proto |
| 18 | +└── server.go |
| 19 | +``` |
| 20 | + |
| 21 | +proto 定义 |
| 22 | +``` |
| 23 | +syntax = "proto3"; |
| 24 | +
|
| 25 | +package helloworld; |
| 26 | +
|
| 27 | +service Greeter { |
| 28 | + rpc SayHello (Helloreq) returns (Hellorsp) {} |
| 29 | +} |
| 30 | +
|
| 31 | +message Helloreq { |
| 32 | + string message = 1; |
| 33 | +} |
| 34 | +
|
| 35 | +message Hellorsp { |
| 36 | + string message = 1; |
| 37 | +} |
| 38 | +``` |
| 39 | + |
| 40 | +构建 |
| 41 | +``` |
| 42 | +protoc -I . --go_out=plugins=grpc:. ./helloworld.proto |
| 43 | +``` |
| 44 | + |
| 45 | +sever.go |
| 46 | +``` |
| 47 | +package main |
| 48 | +
|
| 49 | +import ( |
| 50 | + "context" |
| 51 | + "log" |
| 52 | + "net" |
| 53 | +
|
| 54 | + pb "./helloworld" |
| 55 | + "google.golang.org/grpc" |
| 56 | +) |
| 57 | +
|
| 58 | +const ( |
| 59 | + port = ":50051" |
| 60 | +) |
| 61 | +
|
| 62 | +// server is used to implement helloworld.GreeterServer. |
| 63 | +type server struct { |
| 64 | + pb.UnimplementedGreeterServer |
| 65 | +} |
| 66 | +
|
| 67 | +// SayHello implements helloworld.GreeterServer |
| 68 | +func (s *server) SayHello(ctx context.Context, in *pb.Helloreq) (*pb.Hellorsp, error) { |
| 69 | + log.Printf("Received: %v", in.GetMessage()) |
| 70 | + return &pb.Hellorsp{Message: "Hello " + in.GetMessage()}, nil |
| 71 | +} |
| 72 | +
|
| 73 | +func main() { |
| 74 | + lis, err := net.Listen("tcp", port) |
| 75 | + if err != nil { |
| 76 | + log.Fatalf("failed to listen: %v", err) |
| 77 | + } |
| 78 | + s := grpc.NewServer() |
| 79 | + pb.RegisterGreeterServer(s, &server{}) |
| 80 | + if err := s.Serve(lis); err != nil { |
| 81 | + log.Fatalf("failed to serve: %v", err) |
| 82 | + } |
| 83 | +} |
| 84 | +
|
| 85 | +``` |
| 86 | + |
| 87 | +client.go |
| 88 | +``` |
| 89 | +package main |
| 90 | +
|
| 91 | +import ( |
| 92 | + "context" |
| 93 | + "log" |
| 94 | + "os" |
| 95 | + "time" |
| 96 | +
|
| 97 | + pb "./helloworld" |
| 98 | + "google.golang.org/grpc" |
| 99 | +) |
| 100 | +
|
| 101 | +const ( |
| 102 | + address = "localhost:50051" |
| 103 | + defaultName = "world" |
| 104 | +) |
| 105 | +
|
| 106 | +func main() { |
| 107 | + // Set up a connection to the server. |
| 108 | + conn, err := grpc.Dial(address, grpc.WithInsecure(), grpc.WithBlock()) |
| 109 | + if err != nil { |
| 110 | + log.Fatalf("did not connect: %v", err) |
| 111 | + } |
| 112 | + defer conn.Close() |
| 113 | + c := pb.NewGreeterClient(conn) |
| 114 | +
|
| 115 | + // Contact the server and print out its response. |
| 116 | + message := defaultName |
| 117 | + if len(os.Args) > 1 { |
| 118 | + message = os.Args[1] |
| 119 | + } |
| 120 | + ctx, cancel := context.WithTimeout(context.Background(), time.Second) |
| 121 | + defer cancel() |
| 122 | + r, err := c.SayHello(ctx, &pb.Helloreq{Message: message}) |
| 123 | + if err != nil { |
| 124 | + log.Fatalf("could not greet: %v", err) |
| 125 | + } |
| 126 | + log.Printf("Greeting: %s", r.GetMessage()) |
| 127 | +} |
| 128 | +``` |
| 129 | + |
| 130 | +``` |
| 131 | +go run server.go |
| 132 | +go run client.go |
| 133 | +``` |
0 commit comments