Comparison of Protobuf and Interface

This article compares the differences between the two IDLs, Protobuf and Interface, to help Dubbo protocol developers understand Protobuf, and pave the way for the subsequent transfer to Triple protocol and Grpc protocol.

1. Data type

1.1. Basic types

ptoto typejava type
doubledouble
floatfloat
int32int
int64long
uint32int[Note]
uint64long[Note]
sint32int
sint64long
fixed32int[Note]
fixed64long[Note]
sfixed32int
sfixed64long
boolboolean
stringString
bytesByteString

[Note] In Java, unsigned 32-bit and 64-bit integers are represented using their signed logarithms, with the top bit only stored in the sign bit.

2. Composite types

2.1. Enumeration

  • Original pb code
enum TrafficLightColor {
    TRAFFIC_LIGHT_COLOR_INVALID = 0;
    TRAFFIC_LIGHT_COLOR_UNSET = 1;
    TRAFFIC_LIGHT_COLOR_GREEN = 2;
    TRAFFIC_LIGHT_COLOR_YELLOW = 3;
    TRAFFIC_LIGHT_COLOR_RED = 4;
}
  • Generated java code

image

Enumerations are constants, so use uppercase

2.2. Arrays

  • Original pb code
message VipIDToRidReq {
    repeated uint32 vipID = 1;
}
  • Generated java code

image

The bottom layer is actually an ArrayList

2.3. Collections

PB does not support unordered and non-repeating collections, and can only borrow arrays to implement, and needs to remove duplicates by itself.

2.4. Dictionaries

  • Original pb code
message BatchOnlineRes {
    map<uint32, uint32> onlineMap = 1;//online status
}
  • Generated java code

image

2.5. Nesting

  • Original pb code
message BatchAnchorInfoRes {
    map<uint32, AnchorInfo> list = 1; //user information map list
}
/*
* The function of the corresponding interface: get user information in batches or individually
*/
message AnchorInfo {
    uint32 ownerUid = 1 [json_name="uid"]; //user id
    string nickName = 2 [json_name="nn"]; //User nickname
    string smallAvatar = 3 [json_name="savt"]; //full path of user avatar - small
    string middleAvatar = 4 [json_name="mavt"]; //Full path of user avatar - middle
    string bigAvatar = 5 [json_name="bavt"]; //Full path of user avatar - big
    string avatar = 6 [json_name="avt"]; //User avatar
}
  • Generated java code

image

3. Field default value

  • For strings, the default is the empty string.
  • For bytes, the default is the null byte.
  • For bools, the default is false.
  • For numeric types, the default value is zero.
  • For enums, the default is the first defined enum value, which must be 0.
  • For the message field, the field is not set. Its exact value is language dependent. See the generated code guide for details.

4. Overall structure

FeatureJava InterfaceProtobufRemarks
Method Overloading×
Generic/templated×
method inheritance×
Nested definitionPartial supportPB only supports message and enum nesting
import file
field is null×
Multiple input parameters×PB only supports single input parameters
0 input parameters×PB must have input parameters
0 output parameters×PB must have output parameters
The input/output parameters are abstract classes×The input/output parameters of PB must be concrete classes
The input/output parameters are interfaces×The input/output parameters of PB must be concrete
The input parameter/output parameter is the basic type×The input parameter/output parameter of PB must be a structure

5. Community profile


Last modified January 2, 2023: Enhance en docs (#1798) (95a9f4f6c1)