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

一、protobuf Ubantu 20.04 LTS

1、安装依赖相关

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

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文件即完成安装及正常使用。

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

姚二龙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值