TheRiver | blog

You have reached the world's edge, none but devils play past here

0%

grpc hello

用golang+grpc生成一个简单的hello world程序。代码地址:https://github.com/RiverFerry/grpc

步骤

准备工作

brew install protobuf
brew install golang
brew install grpc

go get -u github.com/golang/protobuf/{proto,protoc-gen-go}
go get -u google.golang.org/grpc

proto文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
syntax = "proto3";

package hello;


message HelloRequest {
string name = 1;
}

message HelloReply {
string message = 1;
}

service Greeter {
rpc SayHello (HelloRequest) returns (HelloReply) {}
}


server

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"
"log"
"net"

pb "grpc/hello"

"google.golang.org/grpc"
)

const (
port = ":10000"
)

// server is used to implement helloworld.GreeterServer.
type server struct {
pb.UnimplementedGreeterServer
}

// SayHello implements helloworld.GreeterServer
func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
log.Printf("Received: %v", in.GetName())
return &pb.HelloReply{Message: "Hello " + in.GetName()}, nil
}

func main() {
lis, err := net.Listen("tcp", port)
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
s := grpc.NewServer()
pb.RegisterGreeterServer(s, &server{})
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}

client

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
package main

import (
"context"
"log"
"os"
"time"

pb "grpc/hello"

"google.golang.org/grpc"
)

const (
address = "localhost:10000"
defaultName = "23333333"
)

func main() {
// Set up a connection to the server.
conn, err := grpc.Dial(address, grpc.WithInsecure(), grpc.WithBlock())
if err != nil {
log.Fatalf("did not connect: %v", err)
}
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)
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())
}

其他

protoc -I=../grpc/hello --go_out=plugins=grpc:./hello/ hello.proto --experimental_allow_proto3_optional
  • -I是–proto_path的简写,表示proto文件所在路径
  • –go_out后面./hello表示生成文件所在路径
  • experimental_allow_proto3_optional是proto3版本要带的参数
  • 把项目代码放在GOPATH/src下面,import的时候pb "grpc/hello"路径到hello.pb.go文件所在的文件夹就行了
  • proto文件的message添加optional后go文件中的struct的name/message就变成string*,这里需要注意下

总结

main的代码基本是拷贝官方的example,这里主要是编译和设置的问题比较多。其他细节的东西后面其他文章再研究,然后补充。

参考

gRPC 官方文档中文版V1.0

protoc-gen-go: program not found or is not executable

Command not found go — on Mac after installing Go

Could not make proto path relative and No such file or directory

Correct format of protoc go_package?

用Golang构建gRPC服务

----------- ending -----------