Skip to content

Latest commit

 

History

History
117 lines (88 loc) · 2.42 KB

README.md

File metadata and controls

117 lines (88 loc) · 2.42 KB

gRPC Usage

protoc (Protocol buffer compiler) 安装

Install protoc from protobuf

protoc --version  # Ensure compiler version is 3+

Install Go plugins for the protoc

# Install the protocol compiler plugins for Go using the following commands:
go install google.golang.org/protobuf/cmd/[email protected]
go install google.golang.org/grpc/cmd/[email protected]

# Update your PATH so that the protoc compiler can find the plugins:
export PATH="$PATH:$(go env GOPATH)/bin"

构建 gRPC 服务

定义 .proto 文件,本文以 pixiu.proto 为例

syntax="proto3";

option go_package = "go-learning/practise/grpc-practise/pixiu/pixiu";

package pixiu;

service Pixiu {
  rpc GetPixiu (PixiuRequest) returns (PixiuReply) {}
  // 其他接口
}

// The request message.
message PixiuRequest {
  int64  id = 1;
  string name = 2;
}

// The response message.
message PixiuReply {
  string message = 1;
}

生成 gRPC 代码

protoc --go_out=. --go_opt=paths=source_relative \
  --go-grpc_out=. --go-grpc_opt=paths=source_relative \
  pixiu/pixiu.proto

执行命令之后,会在 pixiu 目录生成 pixiu.pb.gopixiu_grpc.pb.go 代码文件

  • pixiu.pb.go 结构体
  • pixiu_grpc.pb.go: 客户端和服务端代码

实现 gRPC 服务端

...
type server struct {
	pd.UnimplementedPixiuServer
}

func (s *server) GetPixiu(ctx context.Context, in *pd.PixiuRequest) (*pd.PixiuReply, error) {
	log.Printf("Received %s %d", in.Name, in.Id)
	return &pd.PixiuReply{Message: fmt.Sprintf("%s %d", in.GetName(), in.GetId())}, nil
}

func main() {
	l, _ := net.Listen("tcp", ":30000")

	s := grpc.NewServer()
	pd.RegisterPixiuServer(s, &server{})

	if err = s.Serve(l); err != nil {
		...
	}
}

实现 gRPC 客户端

func main() {
	conn, err := grpc.Dial(addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("failed to connect rpc server %v", err)
	}
	defer conn.Close()

	c := pd.NewPixiuClient(conn)

	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
	defer cancel()

	r, err := c.GetPixiu(ctx, &pd.PixiuRequest{Id: 12345, Name: "caoyingjun"})
	...
}

执行

启动 gRPC server

go run server/server.go

执行 gRPC client

go run client/client.go

# 回显
2022/03/20 19:43:13 say hello caoyingjun 12345