2 | go应用之Grpc : 安装

环境要求

gRPC 需要 Go 1.6+版本

查看

$ go version

golang 安装参考:http://www.xmoot.com/kf3.html

安装 Protocol Buffers v3

下载地址:https://github.com/protocolbuffers/protobuf/releases

1,选择一个对应平台的zip包下载:

$wget https://github.com/protocolbuffers/protobuf/releases/download/v3.6.1/protoc-3.6.1-linux-x86_64.zip

2, 安装

$mkdir protoc
$unzip protoc-3.6.1-linux-x86_64.zip -d ./protoc
$mv protoc /usr/local

3,设置环境变量

 $vim ~/.bashrc

加上:

export PROTOCROOT=/usr/local/protoc
export PATH=$PATH:$PROTOCROOT/bin

生效

$source ~/.bashrc

4, 检查

$protoc --version
libprotoc 3.6.1

5, 安装 Protobuf Golang 插件

$go get -u -v github.com/golang/protobuf/protoc-gen-go

查看:

$which protoc-gen-go
/root/gocode/bin/protoc-gen-go#在$GOPATH 目录的bin下。

安装成功。

2,安装grpc

1,官方安装方式

按照官网的安装方法:

$go get -u -v google.golang.org/grpc

是不是很简单,但是。。。就哈哈了。
如果你安装过程中遇到这个下面的错误就放弃这种方式吧:

package google.golang.org/grpc: unrecognized import path "google.golang.org/grpc" (https fetch: Get https://google.golang.org/grpc?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)

老老实实跟着我下面的步骤安装吧:

注:centos 的git 安装:yum install git

2,手动安装方式

1, 建立相关文件夹

$mkdir -p $GOPATH/src/google.golang.org/
$cd $GOPATH/src/google.golang.org
$git clone https://github.com/grpc/grpc-go.git #下载grpc源码
$mv grpc-go/ grpc
#下载go-genproto
$git clone https://github.com/google/go-genproto.git
$mv go-genproto/ genproto

2,下载依赖包

好在google把依赖包移到了github上,https://github.com/golang
grpc需要一下依赖:crypto net oauth2 sys text tools,我们分别下载就行,

$mkdir -p $GOPATH/src/golang.org/x #grpc规则中的目录
$cd $GOPATH/src/golang.org/x
$git clone https://github.com/golang/net.git
$git clone https://github.com/golang/sys.git
$git clone https://github.com/golang/text.git
$git clone https://github.com/golang/crypto.git
$git clone https://github.com/golang/oauth2.git

3, 安装仓库

$cd $GOPATH/src/
$go install google.golang.org/grpc

到这里grpc-go的安装就结束了, 接下来就按照官方的例子测试一下就行了,不在多说。https://grpc.io/docs/quickstart/go.html

遇到的错误

错误1

ackage go/scanner: unrecognized import path "go/scanner" (import path does not begin with hostname)
package go/token: unrecognized import path "go/token" (import path does not begin with hostname)
package go/ast: unrecognized import path "go/ast" (import path does not begin with hostname)
package go/build: unrecognized import path "go/build" (import path does not begin with hostname)
package go/parser: unrecognized import path "go/parser" (import path does not begin with hostname)
package go/printer: unrecognized import path "go/printer" (import path does not begin with hostname)

解决:

$GOROOT 不设置对或golang没安装对。看参考:http://www.xmoot.com/kf3.html

错误2

package google.golang.org/grpc: unrecognized import path "google.golang.org/grpc" (https fetch: Get https://google.golang.org/grpc?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)

参考:上面grpc 安装

错误3

grpc/internal/transport/controlbuf.go:27:2: cannot find package "golang.org/x/net/http2" in any of:
    /home/dc2-user/go/src/golang.org/x/net/http2 (from $GOROOT)
    /home/dc2-user/gopath/src/golang.org/x/net/http2 (from $GOPATH)
grpc/internal/transport/controlbuf.go:28:2: cannot find package "golang.org/x/net/http2/hpack" in any of:
    /home/dc2-user/go/src/golang.org/x/net/http2/hpack (from $GOROOT)
    /home/dc2-user/gopath/src/golang.org/x/net/http2/hpack (from $GOPATH)
grpc/server.go:36:2: cannot find package "golang.org/x/net/trace" in any of:
    /home/dc2-user/go/src/golang.org/x/net/trace (from $GOROOT)
    /home/dc2-user/gopath/src/golang.org/x/net/trace (from $GOPATH)
grpc/internal/channelz/types_linux.go:26:2: cannot find package "golang.org/x/sys/unix" in any of:
    /home/dc2-user/go/src/golang.org/x/sys/unix (from $GOROOT)
    /home/dc2-user/gopath/src/golang.org/x/sys/unix (from $GOPATH)
grpc/status/status.go:37:2: cannot find package "google.golang.org/genproto/googleapis/rpc/status" in any of:
    /home/dc2-user/go/src/google.golang.org/genproto/googleapis/rpc/status (from $GOROOT)
    /home/dc2-user/gopath/src/google.golang.org/genproto/googleapis/rpc/status (from $GOPATH)

说明依赖包没安装:参考上面文档中的 2,下载依赖包


ph.w
ph.w 11个月前

已被 0 人挖起

回应
登录 后发表评论
  • 消灭零回应