Golang微服务入门到精通之路-4-protobuf安装及使用

一、protobuf Ubantu 20.04 LTS

1、安装依赖相关

Golang微服务入门到精通之路-4-protobuf安装及使用

安装时报了个错:把它去掉就好 (以下是我已安装好的样子)

E: Unable to locate package cur1
root@DESKTOP-AKFISP1:~# apt-get install autoconf automake libtool cur1 make g++ unzip libffi-dev -y
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package cur1
root@DESKTOP-AKFISP1:~# apt-get install autoconf automake libtool make g++ unzip libffi-dev -y
Reading package lists... Done
Building dependency tree
Reading state information... Done
autoconf is already the newest version (2.69-11.1).
automake is already the newest version (1:1.16.1-4ubuntu6).
g++ is already the newest version (4:9.3.0-1ubuntu2).
libffi-dev is already the newest version (3.3-4).
libtool is already the newest version (2.4.6-14).
make is already the newest version (4.2.1-1.2).
unzip is already the newest version (6.0-25ubuntu1).
0 upgraded, 0 newly installed, 0 to remove and 94 not upgraded.
root@DESKTOP-AKFISP1:~#

2、apt-get 直接安装protobuf

apt-get install protobuf-compiler
root@DESKTOP-AKFISP1:~# apt-get install protobuf-compiler
Reading package lists... Done
Building dependency tree
Reading state information... Done
protobuf-compiler is already the newest version (3.6.1.3-2ubuntu5).
0 upgraded, 0 newly installed, 0 to remove and 94 not upgraded.
root@DESKTOP-AKFISP1:~#

3、下载grpc相关包及protobuf插件

grpc相关包:默认会安装到GOPATH

go get google.golang.org/grpc
go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.26.0
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.1.0

下载完成

root@DESKTOP-AKFISP1:~# go get google.golang.org/grpc
go: downloading golang.org/x/net v0.0.0-20200822124328-c89045814202
go: downloading github.com/golang/protobuf v1.4.3
go: downloading golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd
go: downloading google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013
go: downloading golang.org/x/text v0.3.0
root@DESKTOP-AKFISP1:~# go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.26.0
root@DESKTOP-AKFISP1:~# go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.1.0
go: downloading google.golang.org/protobuf v1.23.0
root@DESKTOP-AKFISP1:~#

4、验证版本:protoc --version

root@DESKTOP-AKFISP1:~# protoc --version
libprotoc 3.6.1
root@DESKTOP-AKFISP1:~#

二、编写protobuf

1、创建一个文件夹:grpcDemo

2、创建一个文件:vim Hello.proto

粘贴一下内容

syntax = "proto3";
option go_package="./;golang";

package hello;

message Req {
    string msg = 1;
}

message Res {
    string msg = 2;
}

service HelloGRPC {
    rpc Say(Req) returns (Res);
}

运行命令,如下:

protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative ./Hello.proto
root@DESKTOP-AKFISP1:~# mkdir grpcDemo
root@DESKTOP-AKFISP1:~# cd grpcDemo/
root@DESKTOP-AKFISP1:~/grpcDemo# vim Hello.proto
root@DESKTOP-AKFISP1:~/grpcDemo#
root@DESKTOP-AKFISP1:~/grpcDemo#
root@DESKTOP-AKFISP1:~/grpcDemo# cat Hello.proto
syntax = "proto3";
option go_package="./;Hello";

package hello;

message Req {
    string msg = 1;
}

message Res {
    string msg = 2;
}

service HelloGRPC {
    rpc Say(Req) returns (Res);
}
root@DESKTOP-AKFISP1:~/grpcDemo# protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative ./Hello.proto
root@DESKTOP-AKFISP1:~/grpcDemo# ll
total 12
drwxr-xr-x 1 root root  512 Dec  5 11:23 ./
drwx------ 1 root root  512 Dec  5 11:20 ../
-rw-r--r-- 1 root root 5639 Dec  5 11:23 Hello.pb.go
-rw-r--r-- 1 root root  194 Dec  5 11:20 Hello.proto
-rw-r--r-- 1 root root 3289 Dec  5 11:23 Hello_grpc.pb.go
root@DESKTOP-AKFISP1:~/grpcDemo#

3、最后可以看到多了两个xxx.pb.go文件即完成安装及正常使用。

Golang微服务入门到精通之路-4-protobuf安装及使用

欢迎来大家QQ交流群一起学习:482713805

上一篇:Golang - 关于 proto 文件的一点小思考


下一篇:C#中使用protobuf-net进行序列化