0. 先决必要条件:golang 1.9+,这里略过。
1. quick install
来自这里 https://github.com/grpc/homebrew-grpc
brew tap grpc/grpc
brew install --with-plugins grpc
2. 从代码编译安装
2.1 protobuf install
brew install autoconf automake libtool curl make g++ unzip
下载[protobuf](https://github.com/protocolbuffers/protobuf/releases/latest)
tar -zxvf protobuf-x.x.x
cd protobuf-x.x.x
./configure --prefix=/usr
make
make check
sudo make install
protoc --version
以上是基于release版本变异安装的过程。如果直接git clone master分支,可能还需要先安装xcode,之后:
sudo xcode-select --install
更详细步骤参考github项目protobuf
2.2 grpc-go install
官方安装命令:go get google.golang.org/grpc
然而一般都会失败报错:
https fetch failed: Get https://google.golang.org/grpc?go-get=1: net/http: TLS handshake timeout
原因你懂的。
其实是这个代码已经转移到github上面了,但是代码里面的包依赖还是没有修改,还是google.golang.org这种,
所以:
git clone https://github.com/grpc/grpc-go.git GOPATH/src/google.golang.org/grpc
git clone https://github.com/golang/net.gitGOPATH/src/golang.org/x/net
git clone https://github.com/golang/text.git GOPATH/src/golang.org/x/text
go get -u -v github.com/golang/protobuf/{proto,protoc-gen-go}
cdGOPATH/src/github.com/golang/protobuf
make
git clone https://github.com/google/go-genproto.git GOPATH/src/google.golang.org/genproto
cdGOPATH/src/
go install google.golang.org/grpc
这一部分参考,实测可行:https://www.jianshu.com/p/dba4c7a6d608
2.3 grpc-gateway
这个东西可以把grpc接口以HTTP API暴露出来,即自动生成一个兼容http协议的网关,官网repo
安装参考官方README文档:
go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway
go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger
go get -u github.com/golang/protobuf/protoc-gen-go
使用示例(参考官方文档):
syntax = "proto3";
package echo;
import "google/api/annotations.proto";
message StringMessage {
string value = 1;
}
service EchoService {
rpc Echo(StringMessage) returns (StringMessage) {
option (google.api.http) = {
post: "v1/echo"
body: "*"
};
}
}
protoc -I/usr/local/include -I.
-IGOPATH/src -IGOPATH/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis
--go_out=plugins=grpc:.
path/to/echo_service.proto
protoc -I/usr/local/include -I.
-IGOPATH/src -IGOPATH/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis
--grpc-gateway_out=logtostderr=true:.
path/to/echo_service.proto
protoc -I/usr/local/include -I.
-IGOPATH/src -IGOPATH/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis
--swagger_out=logtostderr=true:.
path/to/echo_service.proto
3. 总结
至此,grpc-go mac 开发环境ready!后续将继续分享grpc使用笔记,下篇计划:
grpc-go使用入门级示例。