一、Win安装protobuf
1、下载编译器
protobuf下载地址:https://github.com/protocolbuffers/protobuf/releases
选择自己需要的版本下载,这里下载的是 protoc-3.19.1-win64.zip,下载之后进行解压即可。

2、配置环境变量
path 系统变量中添加 protoc环境变量(指向解压文件位置下的bin)。

3、检查是否配置成功
cmd 之后,输入:protoc --version
shell
# 查看版本
D:\TempFiles>protoc --version
libprotoc 3.19.1
D:\TempFiles>protoc --help
这样我们就可以在命令行中根据 *.proto文件生成 proto 对象。
二、IDEA中使用 protobuf插件
1、安装插件
在idea中找到File--->setting--->Plugins,输入protobuf,并安装如下两个插件:

重启idea并设置全局protobuf:Tools--->Configure GenProtobuf

设置 protobuf的信息。

- protoc path :我们下载的 protobuf 编辑器的位置,在 bin 目录下有一个 .exe 文件。
- quick gen : 对应的语言 Java。
2、使用插件
(1)编写一个简单的 User.proto文件
如果 .proto文件在 IDEA中打开有些字段信息报红,不要认为自己有问题,这是语法高亮罢了。
java
syntax = "proto3";
message User {
int32 sex = 1;
string name = 2;
}
(2)右击 User.proto文件,选择生成 quick gen protobuf here

IDEA使用工具生成其实也是运行的 proto命令。和你在 CMD窗口输入 proto命令是一样的。
-- 求知若饥,虚心若愚。