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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人