grpc-go 的安装和使用

简单来讲 gRPC 是一个 基于 HTTP/2 协议设计的 RPC 框架,它采用了 Protobuf 作为 IDL (接口定义语言)

grpc-go 安装

  1. 使用 go get 安装代码库

go get -u google.golang.org/grpc
  1. 安装 protocol buffers v3 mac os 下使用 homebrew 安装

  brew install automake
  brew install libtool
  brew install protobuf
  1. 安装针对Go语言的代码生成插件

go get -u github.com/golang/protobuf/protoc-gen-go
  1. 加入到PATH路径

export PATH=$PATH:$GOPATH/bin
  1. 编写 .proto 文件

vim helloworld.proto
syntax = "proto3";

package protos;
// The greeting service definition.
service Hello {
  // Sends a greeting
  rpc SayHello (HelloRequest) returns (HelloReply) {}
}

// The request message containing the user's name.
message HelloRequest {
  string name = 1;
}

// The response message containing the greetings
message HelloReply {
  string message = 1;
}
  1. 编译 .proto 文件

官方 example 运行

server.go 建立

client 建立

Last updated

Was this helpful?