protobuf协议java版本使用说明

张宝
2023-12-01

1. 初始话协议字段

xxx .proto

syntax = "proto3"; //protobuf版本

option java_package = "com.xxx.protobuf"; //生成java代码的数据包路径
option java_outer_classname = "XxxProtocol"; //java类名

message Xxx { //具体协议

    string id = 1; 

}

2. 代码生成

protoc.exe下载地址

windows下借助protoc.exe的生成java代码

protoc --proto_path=./ --java_out=d:\xxx\src\main\java xxx .proto

3. 字段类型

.proto Type Notes   C++ Type    Java Type   Python Type[2]  Go Type Ruby Type   C# Type PHP Type
double      double  double  float   float64 Float   double  float
float       float   float   float   float32 Float   float   float
int32   Uses variable-length encoding. Inefficient for encoding negative numbers – if your field is likely to have negative values, use sint32 instead. int32   int int int32   Fixnum or Bignum (as required)  int integer
int64   Uses variable-length encoding. Inefficient for encoding negative numbers – if your field is likely to have negative values, use sint64 instead. int64   long    int/long[3] int64   Bignum  long    integer/string[5]
uint32  Uses variable-length encoding.  uint32  int[1]  int/long[3] uint32  Fixnum or Bignum (as required)  uint    integer
uint64  Uses variable-length encoding.  uint64  long[1] int/long[3] uint64  Bignum  ulong   integer/string[5]
sint32  Uses variable-length encoding. Signed int value. These more efficiently encode negative numbers than regular int32s.    int32   int int int32   Fixnum or Bignum (as required)  int integer
sint64  Uses variable-length encoding. Signed int value. These more efficiently encode negative numbers than regular int64s.    int64   long    int/long[3] int64   Bignum  long    integer/string[5]
fixed32 Always four bytes. More efficient than uint32 if values are often greater than 228. uint32  int[1]  int/long[3] uint32  Fixnum or Bignum (as required)  uint    integer
fixed64 Always eight bytes. More efficient than uint64 if values are often greater than 256.    uint64  long[1] int/long[3] uint64  Bignum  ulong   integer/string[5]
sfixed32    Always four bytes.  int32   int int int32   Fixnum or Bignum (as required)  int integer
sfixed64    Always eight bytes. int64   long    int/long[3] int64   Bignum  long    integer/string[5]
bool        bool    boolean bool    bool    TrueClass/FalseClass    bool    boolean
string  A string must always contain UTF-8 encoded or 7-bit ASCII text. string  String  str/unicode[4]  string  String (UTF-8)  string  string
bytes   May contain any arbitrary sequence of bytes.    string  ByteString  str []byte  String (ASCII-8BIT) ByteString  string

4. 使用pom依赖

 <dependency>
   <groupId>com.google.protobuf</groupId>
     <artifactId>protobuf-java</artifactId>
     <version>3.2.0</version>
 </dependency>
        <!-- https://mvnrepository.com/artifact/com.googlecode.protobuf-java-format/protobuf-java-format -->
 <dependency>
       <groupId>com.googlecode.protobuf-java-format</groupId>
       <artifactId>protobuf-java-format</artifactId>
       <version>1.4</version>
 </dependency>
 类似资料: