1、官方推荐(需科学上网)
go get -u google.golang.org/grpc
2、通过github.com
进入到第一个$GOPATH目录(因为go get
会默认安装在第一个下)下,新建google.golang.org
目录,拉取golang
在github
上的镜像库:
cd /usr/local/go/path/src
mkdir google.golang.org
cd google.golang.org/
git clone https://github.com/grpc/grpc-go
mv grpc-go/ grpc/
目录结构:
google.golang.org/
└── grpc
...
而在grpc
下有许多常用的包,例如:
grpc
所支持的元数据结构,包中方法可以对MD
进行获取和处理grpc
所支持的各种认证凭据,封装了客户端对服务端进行身份验证所需要的所有状态,并做出各种断言grpc
使用的标准错误码,可通用编译器插件
go get -u github.com/golang/protobuf/protoc-gen-go
将Protoc Plugin
的可执行文件从$GOPATH中移动到$GOBIN下
mv /usr/local/go/path/bin/protoc-gen-go /usr/local/go/bin/
建议可以阅读下官方文档的介绍
wget https://github.com/google/protobuf/releases/download/v3.5.1/protobuf-all-3.5.1.zip
unzip protobuf-all-3.5.1.zip
cd protobuf-3.5.1/
./configure
make
make install
检查是否安装成功
protoc --version
如果出现报错
protoc: error while loading shared libraries: libprotobuf.so.15: cannot open shared object file: No such file or directory
则执行ldconfig
后,再次运行即可成功
ldconfig
我们通过控制台输出的信息可以知道,Protocol Buffers Libraries
的默认安装路径在/usr/local/lib
Libraries have been installed in:
/usr/local/lib
If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
- add LIBDIR to the `LD_LIBRARY_PATH' environment variable
during execution
- add LIBDIR to the `LD_RUN_PATH' environment variable
during linking
- use the `-Wl,-rpath -Wl,LIBDIR' linker flag
- have your system administrator add LIBDIR to `/etc/ld.so.conf'
See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
而我们安装了一个新的动态链接库,ldconfig
一般在系统启动时运行,所以现在会找不到这个lib
,因此我们要手动执行ldconfig
,让动态链接库为系统所共享,它是一个动态链接库管理命令,这就是ldconfig
命令的作用
我们按照惯例执行protoc --help
(查看帮助文档),我们抽出几个常用的命令进行讲解
1、-IPATH, --proto_path=PATH
:指定import
搜索的目录,可指定多个,如果不指定则默认当前工作目录
2、--go_out
:生成golang
源文件
若要将额外的参数传递给插件,可使用从输出目录中分离出来的逗号分隔的参数列表:
protoc --go_out=plugins=grpc,import_path=mypackage:. *.proto
import_prefix=xxx
:将指定前缀添加到所有import
路径的开头import_path=foo/bar
:如果文件没有声明go_package
,则用作包。如果它包含斜杠,那么最右边的斜杠将被忽略。plugins=plugin1+plugin2
:指定要加载的子插件列表(我们所下载的repo中唯一的插件是grpc)Mfoo/bar.proto=quux/shme
: M
参数,指定.proto
文件编译后的包名(foo/bar.proto
编译后为包名为quux/shme
)如果proto
文件指定了RPC
服务,protoc-gen-go
可以生成与grpc
相兼容的代码,我们仅需要将plugins=grpc
参数传递给--go_out
,就可以达到这个目的
protoc --go_out=plugins=grpc:. *.proto
grpc-gateway是protoc的一个插件。它读取gRPC服务定义,并生成一个反向代理服务器,将RESTful JSON API转换为gRPC。此服务器是根据gRPC定义中的自定义选项生成的。
go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway
如果出现以下报错,我们分析错误提示可得知是连接超时(大概是被墙了)
package google.golang.org/genproto/googleapis/api/annotations: unrecognized import path "google.golang.org/genproto/googleapis/api/annotations" (https fetch: Get https://google.golang.org/genproto/googleapis/api/annotations?go-get=1: dial tcp 216.239.37.1:443: getsockopt: connection timed out)
有两种解决方法,
1、科学上网
2、通过github.com
进入到第一个$GOTPATH目录的google.golang.org
目录下,拉取genproto
在github
上的go-genproto
镜像库:
cd /usr/local/go/path/src/google.golang.org
git clone https://github.com/google/go-genproto.git
mv go-genproto/ genproto/
在安装完毕后,我们将grpc-gateway
的可执行文件从$GOPATH中移动到$GOBIN
mv /usr/local/go/path/bin/protoc-gen-grpc-gateway /usr/local/go/bin/