ThinkSSL🔒 一键申购 5分钟快速签发 30天无理由退款 购买更放心 广告
初次见面GRPC === ### PRC原理 ![](https://box.kancloud.cn/ce6199430a130ebd38948fc2e2906771_1216x594.png) ![](https://box.kancloud.cn/780cd3882c67219993811613ff332ec6_1191x601.png) Gopher和你打一个招呼Hello GRPC 课程代码:[https://github.com/dollarkillerx/GRPC-Study](https://github.com/dollarkillerx/GRPC-Study) ### 课程环境 - go version > 1.6 课程是go1.12.5 - os: ubuntu18.4 - 包管理:vgo ##### 安装grpc ` vgo get -u -v google.golang.org/grpc` ##### 安装Protocol Buffers v3 [https://github.com/google/protobuf/releases](https://github.com/google/protobuf/releases)并配置到path 我们这里下载的是[protoc-3.8.0-osx-x86\_32.zip](https://github.com/protocolbuffers/protobuf/releases/download/v3.8.0/protoc-3.8.0-osx-x86_32.zip) ### 接下来,为Go安装protoc插件 `go get -u github.com/golang/protobuf/protoc-gen-go ` ### 目录结构 ``` . ├── client ├── routeguide │   ├── service.pb.go │   └── service.proto └── server ``` ### 生成客户端和服务器代码 `protoc -I routeguide/ routeguide/service.proto --go_out=plugins=grpc:routeguide` ~~~ // gRPC服务在.proto文件中定义,用于生成相应的.pb.go文件。该.pb.go文件是由编译生成的.proto用协议的编译器文件:protoc。 syntax = "proto3"; package proto;// 定义生成.pg.go包的内容 message Request { int64 a = 1; // type name = size int64 b = 2; } message Response { int64 result = 1; } service AddService { rpc Add(Request) returns (Response); rpc Multiply(Request) returns (Response); } // protoc -I routeguide/ routeguide/service.proto --go_out=plugins=grpc:routeguide 生成文件 ~~~