grpc和protoc的安装
protoc的安装:
1. 安装grpc的相关依赖,执行以下命令:
yum install -y gcc-c++ autoconf libtool
yum groupinstall -y "Development Tools"
2. 下载源码:
是用wget下载protobuf包
wget https://github.com/protocolbuffers/protobuf/releases/download/v3.10.0/protobuf-all-3.10.0.tar.gz
tar zvxf protobuf-all-3.10.0.tar.gz
cd protobuf-3.10.0/
./autogen.sh
./configure -prefix=/usr/local/
make
make install
ldconfig
3. protoc-gen-go插件安装
go get -u github.com/golang/protobuf/protoc-gen-go
cd /home/gopath/pkg/mod/github.com/golang/protobuf@v1.5.2/protoc-gen-go
go build
cp protoc-gen-go /usr/local/bin/
至此protoc已经安装完成。
安装grpc
1.下载grpc及依赖库
git clone https://github.com/grpc/grpc-go.git $GOPATH/src/google.golang.org/grpc
git clone https://github.com/golang/net.git $GOPATH/src/golang.org/x/net
git clone https://github.com/golang/text.git $GOPATH/src/golang.org/x/text
git clone https://github.com/golang/sys.git $GOPATH/src/golang.org/x/sys
go get -u github.com/golang/protobuf/{proto,protoc-gen-go} //安装不了的话可以skip,等后面报错了再说
git clone https://github.com/protocolbuffers/protobuf-go.git $GOPATH/src/google.golang.org/protobuf
git clone https://github.com/google/go-genproto.git $GOPATH/src/google.golang.org/genproto
2.安装grpc
cd $GOPATH/src/
go install google.golang.org/grpc
基本上go install没有提示错误,grpc就算安装好了,我们现在拿grpc里的example来测试下
3.验证:
[root@localhost golang]# cd ../../google.golang.org/grpc/examples/helloworld/greeter_server/
[root@localhost greeter_server]# go run main.go
2022/02/22 02:14:14 server listening at [::]:50051
2022/02/22 02:14:55 Received: world
[root@localhost ~]# cd /home/gopath/src/google.golang.org/grpc/examples/helloworld/greeter_client/
[root@localhost greeter_client]# go run main.go
2022/02/22 02:14:55 Greeting: Hello world