go语言的grpc安装记录

grpc安装

是一种rpc服务,包含服务端和客户端,能够更容易地创建分布式应用和服务。

默认使用protocol buffers这种结构序列化机制(当然也可以使用json)。

安装grpc

protoc是protocol buffer的编译器,根据.proto来生成创建应用所需的特定客户端和服务端的代码(PHP仅支持创建客户端代码)。生成的代码同时包括客户端的存根和服务端要实现的抽象接口,均包含Greeter所定义的方法。

1.安装grpc-go

  export GOPROXY="https://goproxy.cn,direct" //设置goproxy方便下载
  go get -u google.golang.org/grpc

如果还是下载不了,可以从github上下载源码到src目录下,用go install 安装

  git clone https://github.com/grpc/grpc-go.git $GOPATH/src/google.golang.org/grpc //这里的$GOPATH,是自己go env gopath里的目录

2.安装Protocol Buffers V3

请根据自己的操作系统下载相应的包,下载地址:https://github.com/google/protobuf/releases

  • 1.解压文件

    curl -OL https://github.com/protocolbuffers/protobuf/releases/download/v3.10.1/protoc-3.10.1-linux-x86_64.zip
    unzip protoc-3.10.1-linux-x86_64.zip -d /usr/local/bin //这里可以解压到自己想放到目录下,这里我放到了/usr/local/bin,默认是在PATH里了;如果自己的目录无法在全局下调用,需要设置加到PATH里 export PATH="$PATH:自己的目录"
    
  • 2.安装protoc的go插件:

    go get -u github.com/golang/protobuf/protoc-gen-go //把插件工具安装到gopath目录里的bin里
    
  • 4.加入到环境变量:

     export PATH=$PATH:$GOPATH/bin //把gopath/bin目录加到全局,执行demo的protoc时会找这个插件
    

运行demo

客户端和服务端都是go,demo地址是https://github.com/grpc/grpc-go/tree/master/examples

工作区的目录如下:

- bin
- pkg
- src
	- github.com
		- golang
			- protobuf  
	- google.golang.org
		- grpc  

在grpc/examples目录下,有greeter_server和greeter_client目录,go run 运行即可看到效果。

go run greeter_server/main.go # 加 & 放在后台运行

go run greeter_client/main.go # 如果顺利的话,会在服务端那边看到打印的world 

参考地址:

https://grpc.io/docs/quickstart/go/

https://github.com/grpc/grpc-go/tree/master/examples

posted @ 2019-10-15 19:56  yeevan  阅读(877)  评论(0编辑  收藏  举报