{"id":167766,"date":"2020-01-06T13:15:59","date_gmt":"2020-01-06T05:15:59","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=167766"},"modified":"2019-12-27T11:18:09","modified_gmt":"2019-12-27T03:18:09","slug":"protobuf-data-switch","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/protobuf-data-switch.html","title":{"rendered":"\u6559\u4f60\u5982\u4f55\u4f7f\u7528 Protobuf \u505a\u6570\u636e\u4ea4\u6362"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u5728\u4ee5\u4e0d\u540c\u8bed\u8a00\u7f16\u5199\u5e76\u5728\u4e0d\u540c\u5e73\u53f0\u4e0a\u8fd0\u884c\u7684\u5e94\u7528\u7a0b\u5e8f\u4e4b\u95f4\u4ea4\u6362\u6570\u636e\u65f6\uff0cProtobuf \u7f16\u7801\u53ef\u63d0\u9ad8\u6548\u7387\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

Protocol Buffers\uff08Protobufs\uff09\u50cf XML \u548c JSON \u4e00\u6837\uff0c\u53ef\u4ee5\u8ba9\u7528\u4e0d\u540c\u8bed\u8a00\u7f16\u5199\u5e76\u5728\u4e0d\u540c\u5e73\u53f0\u4e0a\u8fd0\u884c\u7684\u5e94\u7528\u7a0b\u5e8f\u4ea4\u6362\u6570\u636e\u3002\u4f8b\u5982\uff0c\u7528 Go \u7f16\u5199\u7684\u53d1\u9001\u7a0b\u5e8f\u53ef\u4ee5\u5728 Protobuf \u4e2d\u5bf9\u4ee5 Go \u8868\u793a\u7684\u9500\u552e\u8ba2\u5355\u6570\u636e\u8fdb\u884c\u7f16\u7801\uff0c\u7136\u540e\u7528 Java \u7f16\u5199\u7684\u63a5\u6536\u65b9\u53ef\u4ee5\u5bf9\u5b83\u8fdb\u884c\u89e3\u7801\uff0c\u4ee5\u83b7\u53d6\u6240\u63a5\u6536\u8ba2\u5355\u6570\u636e\u7684 Java \u8868\u793a\u65b9\u5f0f\u3002\u8fd9\u662f\u5728\u7f51\u7edc\u8fde\u63a5\u4e0a\u7684\u7ed3\u6784\u793a\u610f\u56fe\uff1a
\nGo \u9500\u552e\u8ba2\u5355 \u2014> Pbuf \u7f16\u7801 \u2014> \u7f51\u7edc \u2014> Pbuf \u754c\u9762 \u2014> Java \u9500\u552e\u8ba2\u5355
\n\u4e0e XML \u548c JSON \u76f8\u6bd4\uff0cProtobuf \u7f16\u7801\u662f\u4e8c\u8fdb\u5236\u800c\u4e0d\u662f\u6587\u672c\uff0c\u8fd9\u4f1a\u4f7f\u8c03\u8bd5\u590d\u6742\u5316\u3002\u4f46\u662f\uff0c\u6b63\u5982\u672c\u6587\u4e2d\u7684\u4ee3\u7801\u793a\u4f8b\u6240\u786e\u8ba4\u7684\u90a3\u6837\uff0cProtobuf \u7f16\u7801\u5728\u5927\u5c0f\u4e0a\u6bd4 XML \u6216 JSON \u7f16\u7801\u8981\u6709\u6548\u5f97\u591a\u3002<\/p>\n

Protobuf \u4ee5\u53e6\u4e00\u79cd\u65b9\u5f0f\u63d0\u4f9b\u4e86\u8fd9\u79cd\u6709\u6548\u6027\u3002\u5728\u5b9e\u73b0\u7ea7\u522b\uff0cProtobuf \u548c\u5176\u4ed6\u7f16\u7801\u7cfb\u7edf\u5bf9\u7ed3\u6784\u5316\u6570\u636e\u8fdb\u884c\u5e8f\u5217\u5316serialize\u548c\u53cd\u5e8f\u5217\u5316deserialize\u3002\u5e8f\u5217\u5316\u5c06\u7279\u5b9a\u8bed\u8a00\u7684\u6570\u636e\u7ed3\u6784\u8f6c\u6362\u4e3a\u5b57\u8282\u6d41\uff0c\u53cd\u5e8f\u5217\u5316\u662f\u5c06\u5b57\u8282\u6d41\u8f6c\u6362\u56de\u7279\u5b9a\u8bed\u8a00\u7684\u6570\u636e\u7ed3\u6784\u7684\u9006\u8fd0\u7b97\u3002\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u53ef\u80fd\u6210\u4e3a\u6570\u636e\u4ea4\u6362\u7684\u74f6\u9888\uff0c\u56e0\u4e3a\u8fd9\u4e9b\u64cd\u4f5c\u4f1a\u5360\u7528\u5927\u91cf CPU\u3002\u9ad8\u6548\u7684\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u662f Protobuf \u7684\u53e6\u4e00\u4e2a\u8bbe\u8ba1\u76ee\u6807\u3002<\/p>\n

\u6700\u8fd1\u7684\u7f16\u7801\u6280\u672f\uff0c\u4f8b\u5982 Protobuf \u548c FlatBuffers\uff0c\u6e90\u81ea 1990 \u5e74\u4ee3\u521d\u671f\u7684 DCE\/RPC\uff08\u5206\u5e03\u5f0f\u8ba1\u7b97\u73af\u5883\/\u8fdc\u7a0b\u8fc7\u7a0b\u8c03\u7528Distributed Computing Environment\/Remote Procedure Call\uff09\u8ba1\u5212\u3002\u4e0e DCE\/RPC \u4e00\u6837\uff0cProtobuf \u5728\u6570\u636e\u4ea4\u6362\u4e2d\u4e3a IDL\uff08\u63a5\u53e3\u5b9a\u4e49\u8bed\u8a00\uff09\u548c\u7f16\u7801\u5c42\u505a\u51fa\u4e86\u8d21\u732e\u3002<\/p>\n

\u672c\u6587\u5c06\u7740\u773c\u4e8e\u8fd9\u4e24\u5c42\uff0c\u7136\u540e\u63d0\u4f9b Go \u548c Java \u4e2d\u7684\u4ee3\u7801\u793a\u4f8b\u4ee5\u5145\u5b9e Protobuf \u7684\u7ec6\u8282\uff0c\u5e76\u8868\u660e Protobuf \u662f\u6613\u4e8e\u4f7f\u7528\u7684\u3002<\/p>\n

Protobuf \u4f5c\u4e3a\u4e00\u4e2a IDL \u548c\u7f16\u7801\u5c42<\/strong><\/div>\n

\u50cf Protobuf \u4e00\u6837\uff0cDCE\/RPC \u88ab\u8bbe\u8ba1\u4e3a\u4e0e\u8bed\u8a00\u548c\u5e73\u53f0\u65e0\u5173\u3002\u9002\u5f53\u7684\u5e93\u548c\u5b9e\u7528\u7a0b\u5e8f\u5141\u8bb8\u4efb\u4f55\u8bed\u8a00\u548c\u5e73\u53f0\u7528\u4e8e DCE\/RPC \u9886\u57df\u3002\u6b64\u5916\uff0cDCE\/RPC \u4f53\u7cfb\u7ed3\u6784\u975e\u5e38\u4f18\u96c5\u3002IDL \u6587\u6863\u662f\u4e00\u4fa7\u7684\u8fdc\u7a0b\u8fc7\u7a0b\u4e0e\u53e6\u4e00\u4fa7\u7684\u8c03\u7528\u8005\u4e4b\u95f4\u7684\u534f\u5b9a\u3002Protobuf \u4e5f\u662f\u4ee5 IDL \u6587\u6863\u4e3a\u4e2d\u5fc3\u7684\u3002<\/p>\n

IDL \u6587\u6863\u662f\u6587\u672c\uff0c\u5728 DCE\/RPC \u4e2d\uff0c\u4f7f\u7528\u57fa\u672c C \u8bed\u6cd5\u4ee5\u53ca\u5143\u6570\u636e\u7684\u8bed\u6cd5\u6269\u5c55\uff08\u65b9\u62ec\u53f7\uff09\u548c\u4e00\u4e9b\u65b0\u7684\u5173\u952e\u5b57\uff0c\u4f8b\u5982 interface\u3002\u8fd9\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n

[uuid (2d6ead46-05e3-11ca-7dd1-426909beabcd), version(1.0)]\r\ninterface echo {\r\n   const long int ECHO_SIZE = 512;\r\n   void echo(\r\n      [in]          handle_t h,\r\n      [in, string]  idl_char from_client[ ],\r\n      [out, string] idl_char from_service[ECHO_SIZE]\r\n   );\r\n}<\/pre>\n

\u8be5 IDL \u6587\u6863\u58f0\u660e\u4e86\u4e00\u4e2a\u540d\u4e3a echo \u7684\u8fc7\u7a0b\uff0c\u8be5\u8fc7\u7a0b\u5e26\u6709\u4e09\u4e2a\u53c2\u6570\uff1a\u7c7b\u578b\u4e3a handle_t\uff08\u5b9e\u73b0\u6307\u9488\uff09\u548c idl_char\uff08ASCII \u5b57\u7b26\u6570\u7ec4\uff09\u7684 [in] \u53c2\u6570\u88ab\u4f20\u9012\u7ed9\u8fdc\u7a0b\u8fc7\u7a0b\uff0c\u800c [out] \u53c2\u6570\uff08\u4e5f\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\uff09\u4ece\u8be5\u8fc7\u7a0b\u4e2d\u4f20\u56de\u3002\u5728\u6b64\u793a\u4f8b\u4e2d\uff0cecho \u8fc7\u7a0b\u4e0d\u4f1a\u663e\u5f0f\u8fd4\u56de\u503c\uff08echo \u5de6\u4fa7\u7684 void\uff09\uff0c\u4f46\u4e5f\u53ef\u4ee5\u8fd4\u56de\u503c\u3002\u8fd4\u56de\u503c\uff0c\u4ee5\u53ca\u4e00\u4e2a\u6216\u591a\u4e2a [out] \u53c2\u6570\uff0c\u5141\u8bb8\u8fdc\u7a0b\u8fc7\u7a0b\u4efb\u610f\u8fd4\u56de\u8bb8\u591a\u503c\u3002\u4e0b\u4e00\u8282\u5c06\u4ecb\u7ecd Protobuf IDL\uff0c\u5b83\u7684\u8bed\u6cd5\u4e0d\u540c\uff0c\u4f46\u540c\u6837\u7528\u4f5c\u6570\u636e\u4ea4\u6362\u4e2d\u7684\u534f\u5b9a\u3002<\/p>\n

DCE\/RPC \u548c Protobuf \u4e2d\u7684 IDL \u6587\u6863\u662f\u521b\u5efa\u7528\u4e8e\u4ea4\u6362\u6570\u636e\u7684\u57fa\u7840\u7ed3\u6784\u4ee3\u7801\u7684\u5b9e\u7528\u7a0b\u5e8f\u7684\u8f93\u5165\uff1a
\nIDL \u6587\u6863 \u2014> DCE\/PRC \u6216 Protobuf \u5b9e\u7528\u7a0b\u5e8f \u2014> \u6570\u636e\u4ea4\u6362\u7684\u652f\u6301\u4ee3\u7801
\n\u4f5c\u4e3a\u76f8\u5bf9\u7b80\u5355\u7684\u6587\u672c\uff0cIDL \u662f\u540c\u6837\u4fbf\u4e8e\u4eba\u7c7b\u9605\u8bfb\u7684\u5173\u4e8e\u6570\u636e\u4ea4\u6362\u7ec6\u8282\u7684\u6587\u6863\uff08\u7279\u522b\u662f\u4ea4\u6362\u7684\u6570\u636e\u9879\u7684\u6570\u91cf\u548c\u6bcf\u4e2a\u9879\u7684\u6570\u636e\u7c7b\u578b\uff09\u3002<\/p>\n

Protobuf \u53ef\u7528\u4e8e\u73b0\u4ee3 RPC \u7cfb\u7edf\uff0c\u4f8b\u5982 gRPC\uff1b\u4f46\u662f Protobuf \u672c\u8eab\u4ec5\u63d0\u4f9b IDL \u5c42\u548c\u7f16\u7801\u5c42\uff0c\u7528\u4e8e\u4ece\u53d1\u9001\u8005\u4f20\u9012\u5230\u63a5\u6536\u8005\u7684\u6d88\u606f\u3002\u4e0e\u539f\u672c\u7684 DCE\/RPC \u4e00\u6837\uff0cProtobuf \u7f16\u7801\u662f\u4e8c\u8fdb\u5236\u7684\uff0c\u4f46\u6548\u7387\u66f4\u9ad8\u3002<\/p>\n

\u76ee\u524d\uff0cXML \u548c JSON \u7f16\u7801\u4ecd\u5728\u901a\u8fc7 Web \u670d\u52a1\u7b49\u6280\u672f\u8fdb\u884c\u7684\u6570\u636e\u4ea4\u6362\u4e2d\u5360\u4e3b\u5bfc\u5730\u4f4d\uff0c\u8fd9\u4e9b\u6280\u672f\u5229\u7528 Web \u670d\u52a1\u5668\u3001\u4f20\u8f93\u534f\u8bae\uff08\u4f8b\u5982 TCP\u3001HTTP\uff09\u4ee5\u53ca\u6807\u51c6\u5e93\u548c\u5b9e\u7528\u7a0b\u5e8f\u7b49\u539f\u6709\u7684\u57fa\u7840\u8bbe\u65bd\u6765\u5904\u7406 XML \u548c JSON \u6587\u6863\u3002 \u6b64\u5916\uff0c\u5404\u79cd\u7c7b\u578b\u7684\u6570\u636e\u5e93\u7cfb\u7edf\u53ef\u4ee5\u5b58\u50a8 XML \u548c JSON \u6587\u6863\uff0c\u751a\u81f3\u65e7\u5f0f\u5173\u7cfb\u578b\u7cfb\u7edf\u4e5f\u53ef\u4ee5\u8f7b\u677e\u751f\u6210\u67e5\u8be2\u7ed3\u679c\u7684 XML \u7f16\u7801\u3002\u73b0\u5728\uff0c\u6bcf\u79cd\u901a\u7528\u7f16\u7a0b\u8bed\u8a00\u90fd\u5177\u6709\u652f\u6301 XML \u548c JSON \u7684\u5e93\u3002\u90a3\u4e48\uff0c\u662f\u4ec0\u4e48\u8ba9\u6211\u4eec\u56de\u5230 Protobuf \u4e4b\u7c7b\u7684\u4e8c\u8fdb\u5236\u7f16\u7801\u7cfb\u7edf\u5462\uff1f<\/p>\n

\u8ba9\u6211\u4eec\u770b\u4e00\u4e0b\u8d1f\u5341\u8fdb\u5236\u503c -128\u3002\u4ee5 2 \u7684\u8865\u7801\u4e8c\u8fdb\u5236\u8868\u793a\u5f62\u5f0f\uff08\u5728\u7cfb\u7edf\u548c\u8bed\u8a00\u4e2d\u5360\u4e3b\u5bfc\u5730\u4f4d\uff09\u4e2d\uff0c\u6b64\u503c\u53ef\u4ee5\u5b58\u50a8\u5728\u5355\u4e2a 8 \u4f4d\u5b57\u8282\u4e2d\uff1a10000000\u3002\u6b64\u6574\u6570\u503c\u5728 XML \u6216 JSON \u4e2d\u7684\u6587\u672c\u7f16\u7801\u9700\u8981\u591a\u4e2a\u5b57\u8282\u3002\u4f8b\u5982\uff0cUTF-8 \u7f16\u7801\u9700\u8981\u56db\u4e2a\u5b57\u8282\u7684\u5b57\u7b26\u4e32\uff0c\u5373 -128\uff0c\u5373\u6bcf\u4e2a\u5b57\u7b26\u4e00\u4e2a\u5b57\u8282\uff08\u5341\u516d\u8fdb\u5236\uff0c\u503c\u4e3a 0x2d\u30010x31\u30010x32 \u548c 0x38\uff09\u3002XML \u548c JSON \u8fd8\u6dfb\u52a0\u4e86\u6807\u8bb0\u5b57\u7b26\uff0c\u4f8b\u5982\u5c16\u62ec\u53f7\u548c\u5927\u62ec\u53f7\u3002\u6709\u5173 Protobuf \u7f16\u7801\u7684\u8be6\u7ec6\u4fe1\u606f\u4e0b\u9762\u5c31\u4f1a\u4ecb\u7ecd\uff0c\u4f46\u73b0\u5728\u7684\u5173\u6ce8\u70b9\u662f\u4e00\u4e2a\u901a\u7528\u70b9\uff1a\u6587\u672c\u7f16\u7801\u7684\u538b\u7f29\u6027\u660e\u663e\u4f4e\u4e8e\u4e8c\u8fdb\u5236\u7f16\u7801\u3002<\/p>\n

\u5728 Go \u4e2d\u4f7f\u7528 Protobuf \u7684\u793a\u4f8b<\/strong><\/div>\n

\u6211\u7684\u4ee3\u7801\u793a\u4f8b\u7740\u91cd\u4e8e Protobuf \u800c\u4e0d\u662f RPC\u3002\u4ee5\u4e0b\u662f\u7b2c\u4e00\u4e2a\u793a\u4f8b\u7684\u6982\u8ff0\uff1a<\/p>\n

\u540d\u4e3a dataitem.proto \u7684 IDL \u6587\u4ef6\u5b9a\u4e49\u4e86\u4e00\u4e2a Protobuf \u6d88\u606f\uff0c\u5b83\u5177\u6709\u516d\u4e2a\u4e0d\u540c\u7c7b\u578b\u7684\u5b57\u6bb5\uff1a\u5177\u6709\u4e0d\u540c\u8303\u56f4\u7684\u6574\u6570\u503c\u3001\u56fa\u5b9a\u5927\u5c0f\u7684\u6d6e\u70b9\u503c\u4ee5\u53ca\u4e24\u4e2a\u4e0d\u540c\u957f\u5ea6\u7684\u5b57\u7b26\u4e32\u3002
\nProtobuf \u7f16\u8bd1\u5668\u4f7f\u7528 IDL \u6587\u4ef6\u751f\u6210 Go \u7248\u672c\uff08\u4ee5\u53ca\u540e\u9762\u7684 Java \u7248\u672c\uff09\u7684 Protobuf \u6d88\u606f\u53ca\u652f\u6301\u51fd\u6570\u3002
\nGo \u5e94\u7528\u7a0b\u5e8f\u4f7f\u7528\u968f\u673a\u751f\u6210\u7684\u503c\u586b\u5145\u539f\u751f\u7684 Go \u6570\u636e\u7ed3\u6784\uff0c\u7136\u540e\u5c06\u7ed3\u679c\u5e8f\u5217\u5316\u4e3a\u672c\u5730\u6587\u4ef6\u3002\u4e3a\u4e86\u8fdb\u884c\u6bd4\u8f83\uff0c XML \u548c JSON \u7f16\u7801\u4e5f\u88ab\u5e8f\u5217\u5316\u4e3a\u672c\u5730\u6587\u4ef6\u3002
\n\u4f5c\u4e3a\u6d4b\u8bd5\uff0cGo \u5e94\u7528\u7a0b\u5e8f\u901a\u8fc7\u53cd\u5e8f\u5217\u5316 Protobuf \u6587\u4ef6\u7684\u5185\u5bb9\u6765\u91cd\u5efa\u5176\u539f\u751f\u6570\u636e\u7ed3\u6784\u7684\u5b9e\u4f8b\u3002
\n\u4f5c\u4e3a\u8bed\u8a00\u4e2d\u7acb\u6027\u6d4b\u8bd5\uff0cJava \u5e94\u7528\u7a0b\u5e8f\u8fd8\u4f1a\u5bf9 Protobuf \u6587\u4ef6\u7684\u5185\u5bb9\u8fdb\u884c\u53cd\u5e8f\u5217\u5316\u4ee5\u83b7\u53d6\u539f\u751f\u6570\u636e\u7ed3\u6784\u7684\u5b9e\u4f8b\u3002
\n\u6211\u7684\u7f51\u7ad9\u4e0a\u63d0\u4f9b\u4e86\u8be5 IDL \u6587\u4ef6\u4ee5\u53ca\u4e24\u4e2a Go \u548c\u4e00\u4e2a Java \u6e90\u6587\u4ef6\uff0c\u6253\u5305\u4e3a ZIP \u6587\u4ef6\u3002<\/p>\n

\u6700\u91cd\u8981\u7684 Protobuf IDL \u6587\u6863\u5982\u4e0b\u6240\u793a\u3002\u8be5\u6587\u6863\u5b58\u50a8\u5728\u6587\u4ef6 dataitem.proto \u4e2d\uff0c\u5e76\u5177\u6709\u5e38\u89c4\u7684.proto \u6269\u5c55\u540d\u3002<\/p>\n

\u793a\u4f8b 1\u3001Protobuf IDL \u6587\u6863<\/strong><\/span><\/div>\n
syntax = \"proto3\";\r\npackage main;\r\nmessage DataItem {\r\n  int64  oddA  = 1;\r\n  int64  evenA = 2;\r\n  int32  oddB  = 3;\r\n  int32  evenB = 4;\r\n  float  small = 5;\r\n  float  big   = 6;\r\n  string short = 7;\r\n  string long  = 8;\r\n}<\/pre>\n

\u8be5 IDL \u4f7f\u7528\u5f53\u524d\u7684 proto3 \u800c\u4e0d\u662f\u8f83\u65e9\u7684 proto2 \u8bed\u6cd5\u3002\u8f6f\u4ef6\u5305\u540d\u79f0\uff08\u5728\u672c\u4f8b\u4e2d\u4e3a main\uff09\u662f\u53ef\u9009\u7684\uff0c\u4f46\u662f\u60ef\u4f8b\u4f7f\u7528\u5b83\u4ee5\u907f\u514d\u540d\u79f0\u51b2\u7a81\u3002\u8fd9\u4e2a\u7ed3\u6784\u5316\u7684\u6d88\u606f\u5305\u542b\u516b\u4e2a\u5b57\u6bb5\uff0c\u6bcf\u4e2a\u5b57\u6bb5\u90fd\u6709\u4e00\u4e2a Protobuf \u6570\u636e\u7c7b\u578b\uff08\u4f8b\u5982\uff0cint64\u3001string\uff09\u3001\u540d\u79f0\uff08\u4f8b\u5982\uff0coddA\u3001short\uff09\u548c\u4e00\u4e2a\u7b49\u53f7 = \u4e4b\u540e\u7684\u6570\u5b57\u6807\u7b7e\uff08\u5373\u952e\uff09\u3002\u6807\u7b7e\uff08\u5728\u6b64\u793a\u4f8b\u4e2d\u4e3a 1 \u5230 8\uff09\u662f\u552f\u4e00\u7684\u6574\u6570\u6807\u8bc6\u7b26\uff0c\u7528\u4e8e\u786e\u5b9a\u5b57\u6bb5\u5e8f\u5217\u5316\u7684\u987a\u5e8f\u3002<\/p>\n

Protobuf \u6d88\u606f\u53ef\u4ee5\u5d4c\u5957\u5230\u4efb\u610f\u7ea7\u522b\uff0c\u800c\u4e00\u4e2a\u6d88\u606f\u53ef\u4ee5\u662f\u53e6\u5916\u4e00\u4e2a\u6d88\u606f\u7684\u5b57\u6bb5\u7c7b\u578b\u3002\u8fd9\u662f\u4e00\u4e2a\u4f7f\u7528 DataItem \u6d88\u606f\u4f5c\u4e3a\u5b57\u6bb5\u7c7b\u578b\u7684\u793a\u4f8b\uff1a<\/p>\n

message DataItems {\r\n  repeated DataItem item = 1;\r\n}<\/pre>\n

\u5355\u4e2a DataItems \u6d88\u606f\u7531\u91cd\u590d\u7684\uff08\u96f6\u4e2a\u6216\u591a\u4e2a\uff09DataItem \u6d88\u606f\u7ec4\u6210\u3002<\/p>\n

\u4e3a\u4e86\u6e05\u6670\u8d77\u89c1\uff0cProtobuf \u8fd8\u652f\u6301\u679a\u4e3e\u7c7b\u578b\uff1a<\/p>\n

enum PartnershipStatus {\r\n  reserved \"FREE\", \"CONSTRAINED\", \"OTHER\";\r\n}<\/pre>\n

reserved \u9650\u5b9a\u7b26\u786e\u4fdd\u7528\u4e8e\u5b9e\u73b0\u8fd9\u4e09\u4e2a\u7b26\u53f7\u540d\u7684\u6570\u503c\u4e0d\u80fd\u91cd\u590d\u4f7f\u7528\u3002<\/p>\n

\u4e3a\u4e86\u751f\u6210\u4e00\u4e2a\u6216\u591a\u4e2a\u58f0\u660e Protobuf \u6d88\u606f\u7ed3\u6784\u7684\u7279\u5b9a\u4e8e\u8bed\u8a00\u7684\u7248\u672c\uff0c\u5305\u542b\u8fd9\u4e9b\u7ed3\u6784\u7684 IDL \u6587\u4ef6\u88ab\u4f20\u9012\u5230protoc \u7f16\u8bd1\u5668\uff08\u53ef\u5728 Protobuf GitHub \u5b58\u50a8\u5e93\u4e2d\u627e\u5230\uff09\u3002\u5bf9\u4e8e Go \u4ee3\u7801\uff0c\u53ef\u4ee5\u4ee5\u901a\u5e38\u7684\u65b9\u5f0f\u5b89\u88c5\u652f\u6301\u7684 Protobuf \u5e93\uff08\u8fd9\u91cc\u4ee5 \uff05 \u4f5c\u4e3a\u547d\u4ee4\u884c\u63d0\u793a\u7b26\uff09\uff1a<\/p>\n

% go get github.com\/golang\/protobuf\/proto<\/pre>\n

\u5c06 Protobuf IDL \u6587\u4ef6 dataitem.proto \u7f16\u8bd1\u4e3a Go \u6e90\u4ee3\u7801\u7684\u547d\u4ee4\u662f\uff1a<\/p>\n

% protoc --go_out=. dataitem.proto<\/pre>\n

\u6807\u5fd7 --go_out \u6307\u793a\u7f16\u8bd1\u5668\u751f\u6210 Go \u6e90\u4ee3\u7801\u3002\u5176\u4ed6\u8bed\u8a00\u4e5f\u6709\u7c7b\u4f3c\u7684\u6807\u5fd7\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u7ed3\u679c\u662f\u4e00\u4e2a\u540d\u4e3a dataitem.pb.go \u7684\u6587\u4ef6\uff0c\u8be5\u6587\u4ef6\u8db3\u591f\u5c0f\uff0c\u53ef\u4ee5\u5c06\u5176\u57fa\u672c\u5185\u5bb9\u590d\u5236\u5230 Go \u5e94\u7528\u7a0b\u5e8f\u4e2d\u3002\u4ee5\u4e0b\u662f\u751f\u6210\u7684\u4ee3\u7801\u7684\u4e3b\u8981\u90e8\u5206\uff1a<\/p>\n

var _ = proto.Marshal\r\ntype DataItem struct {\r\n   OddA  int64   `protobuf:\"varint,1,opt,name=oddA\" json:\"oddA,omitempty\"`\r\n   EvenA int64   `protobuf:\"varint,2,opt,name=evenA\" json:\"evenA,omitempty\"`\r\n   OddB  int32   `protobuf:\"varint,3,opt,name=oddB\" json:\"oddB,omitempty\"`\r\n   EvenB int32   `protobuf:\"varint,4,opt,name=evenB\" json:\"evenB,omitempty\"`\r\n   Small float32 `protobuf:\"fixed32,5,opt,name=small\" json:\"small,omitempty\"`\r\n   Big   float32 `protobuf:\"fixed32,6,opt,name=big\" json:\"big,omitempty\"`\r\n   Short string  `protobuf:\"bytes,7,opt,name=short\" json:\"short,omitempty\"`\r\n   Long  string  `protobuf:\"bytes,8,opt,name=long\" json:\"long,omitempty\"`\r\n}\r\nfunc (m *DataItem) Reset()         { *m = DataItem{} }\r\nfunc (m *DataItem) String() string { return proto.CompactTextString(m) }\r\nfunc (*DataItem) ProtoMessage()    {}\r\nfunc init() {}\r\n<\/pre>\n

\u7f16\u8bd1\u5668\u751f\u6210\u7684\u4ee3\u7801\u5177\u6709 Go \u7ed3\u6784 DataItem\uff0c\u8be5\u7ed3\u6784\u5bfc\u51fa Go \u5b57\u6bb5\uff08\u540d\u79f0\u73b0\u5df2\u5927\u5199\u5f00\u5934\uff09\uff0c\u8be5\u5b57\u6bb5\u4e0e Protobuf IDL \u4e2d\u58f0\u660e\u7684\u540d\u79f0\u5339\u914d\u3002\u8be5\u7ed3\u6784\u5b57\u6bb5\u5177\u6709\u6807\u51c6\u7684 Go \u6570\u636e\u7c7b\u578b\uff1aint32\u3001int64\u3001float32 \u548c string\u3002\u5728\u6bcf\u4e2a\u5b57\u6bb5\u884c\u7684\u672b\u5c3e\uff0c\u662f\u63cf\u8ff0 Protobuf \u7c7b\u578b\u7684\u5b57\u7b26\u4e32\uff0c\u63d0\u4f9b Protobuf IDL \u6587\u6863\u4e2d\u7684\u6570\u5b57\u6807\u7b7e\u53ca\u6709\u5173 JSON \u4fe1\u606f\u7684\u5143\u6570\u636e\uff0c\u8fd9\u5c06\u5728\u540e\u9762\u8ba8\u8bba\u3002<\/p>\n

\u6b64\u5916\u4e5f\u6709\u51fd\u6570\uff1b\u6700\u91cd\u8981\u7684\u662f Proto.Marshal\uff0c\u7528\u4e8e\u5c06 DataItem \u7ed3\u6784\u7684\u5b9e\u4f8b\u5e8f\u5217\u5316\u4e3a Protobuf \u683c\u5f0f\u3002\u8f85\u52a9\u51fd\u6570\u5305\u62ec\uff1a\u6e05\u9664 DataItem \u7ed3\u6784\u7684 Reset\uff0c\u751f\u6210 DataItem \u7684\u5355\u884c\u5b57\u7b26\u4e32\u8868\u793a\u7684 String\u3002<\/p>\n

\u63cf\u8ff0 Protobuf \u7f16\u7801\u7684\u5143\u6570\u636e\u5e94\u5728\u66f4\u8be6\u7ec6\u5730\u5206\u6790 Go \u7a0b\u5e8f\u4e4b\u524d\u8fdb\u884c\u4ed4\u7ec6\u7814\u7a76\u3002<\/p>\n

Protobuf \u7f16\u7801<\/strong><\/div>\n

Protobuf \u6d88\u606f\u7684\u7ed3\u6784\u4e3a\u952e\/\u503c\u5bf9\u7684\u96c6\u5408\uff0c\u5176\u4e2d\u6570\u5b57\u6807\u7b7e\u4e3a\u952e\uff0c\u76f8\u5e94\u7684\u5b57\u6bb5\u4e3a\u503c\u3002\u5b57\u6bb5\u540d\u79f0\uff08\u4f8b\u5982\uff0coddA \u548c small\uff09\u662f\u4f9b\u4eba\u7c7b\u9605\u8bfb\u7684\uff0c\u4f46\u662f protoc \u7f16\u8bd1\u5668\u7684\u786e\u4f7f\u7528\u4e86\u5b57\u6bb5\u540d\u79f0\u6765\u751f\u6210\u7279\u5b9a\u4e8e\u8bed\u8a00\u7684\u5bf9\u5e94\u540d\u79f0\u3002\u4f8b\u5982\uff0cProtobuf IDL \u4e2d\u7684 oddA \u548c small \u540d\u79f0\u5728 Go \u7ed3\u6784\u4e2d\u5206\u522b\u6210\u4e3a\u5b57\u6bb5 OddA \u548c Small\u3002<\/p>\n

\u952e\u548c\u5b83\u4eec\u7684\u503c\u90fd\u88ab\u7f16\u7801\uff0c\u4f46\u662f\u6709\u4e00\u4e2a\u91cd\u8981\u7684\u533a\u522b\uff1a\u4e00\u4e9b\u6570\u5b57\u503c\u5177\u6709\u56fa\u5b9a\u5927\u5c0f\u7684 32 \u6216 64 \u4f4d\u7684\u7f16\u7801\uff0c\u800c\u5176\u4ed6\u6570\u5b57\uff08\u5305\u62ec\u6d88\u606f\u6807\u7b7e\uff09\u5219\u662f varint \u7f16\u7801\u7684\uff0c\u4f4d\u6570\u53d6\u51b3\u4e8e\u6574\u6570\u7684\u7edd\u5bf9\u503c\u3002\u4f8b\u5982\uff0c\u6574\u6570\u503c 1 \u5230 15 \u9700\u8981 8 \u4f4d varint \u7f16\u7801\uff0c\u800c\u503c 16 \u5230 2047 \u9700\u8981 16 \u4f4d\u3002varint \u7f16\u7801\u5728\u672c\u8d28\u4e0a\u4e0e UTF-8 \u7f16\u7801\u7c7b\u4f3c\uff08\u4f46\u7ec6\u8282\u4e0d\u540c\uff09\uff0c\u5b83\u504f\u7231\u8f83\u5c0f\u7684\u6574\u6570\u503c\u800c\u4e0d\u662f\u8f83\u5927\u7684\u6574\u6570\u503c\u3002\uff08\u6709\u5173\u8be6\u7ec6\u5206\u6790\uff0c\u8bf7\u53c2\u89c1 Protobuf \u7f16\u7801\u6307\u5357\uff09\u7ed3\u679c\u662f\uff0cProtobuf \u6d88\u606f\u5e94\u8be5\u5728\u5b57\u6bb5\u4e2d\u5177\u6709\u8f83\u5c0f\u7684\u6574\u6570\u503c\uff08\u5982\u679c\u53ef\u80fd\uff09\uff0c\u5e76\u4e14\u952e\u6570\u5e94\u5c3d\u53ef\u80fd\u5c11\uff0c\u4f46\u6bcf\u4e2a\u5b57\u6bb5\u81f3\u5c11\u5f97\u6709\u4e00\u4e2a\u952e\u3002<\/p>\n

\u4e0b\u8868 1 \u5217\u51fa\u4e86 Protobuf \u7f16\u7801\u7684\u8981\u70b9\uff1a<\/p>\n\n\n\n\n\n\n\n
\u7f16\u7801<\/th>\n\u793a\u4f8b\u7c7b\u578b<\/th>\n\u957f\u5ea6<\/th>\n<\/tr>\n<\/thead>\n
varint<\/code><\/td>\nint32<\/code>\u3001uint32<\/code>\u3001int64<\/code><\/td>\n\u53ef\u53d8\u957f\u5ea6<\/td>\n<\/tr>\n
fixed<\/code><\/td>\nfixed32<\/code>\u3001float<\/code>\u3001double<\/code><\/td>\n\u56fa\u5b9a\u7684 32 \u4f4d\u6216 64 \u4f4d\u957f\u5ea6<\/td>\n<\/tr>\n
\u5b57\u8282\u5e8f\u5217<\/td>\nstring<\/code>\u3001bytes<\/code><\/td>\n\u5e8f\u5217\u957f\u5ea6<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u8868 1. Protobuf \u6570\u636e\u7c7b\u578b<\/p>\n

\u672a\u660e\u786e\u56fa\u5b9a\u957f\u5ea6\u7684\u6574\u6570\u7c7b\u578b\u662f varint \u7f16\u7801\u7684\uff1b\u56e0\u6b64\uff0c\u5728 varint \u7c7b\u578b\u4e2d\uff0c\u4f8b\u5982 uint32\uff08u \u4ee3\u8868\u65e0\u7b26\u53f7\uff09\uff0c\u6570\u5b57 32 \u63cf\u8ff0\u4e86\u6574\u6570\u7684\u8303\u56f4\uff08\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u4e3a 0 \u5230 232 - 1\uff09\uff0c\u800c\u4e0d\u662f\u5176\u4f4d\u7684\u5927\u5c0f\uff0c\u8be5\u4f4d\u5927\u5c0f\u53d6\u51b3\u4e8e\u503c\u3002\u76f8\u6bd4\u4e4b\u4e0b\uff0c\u5bf9\u4e8e\u56fa\u5b9a\u957f\u5ea6\u7c7b\u578b\uff08\u4f8b\u5982 fixed32 \u6216 double\uff09\uff0cProtobuf \u7f16\u7801\u5206\u522b\u9700\u8981 32 \u4f4d\u548c 64 \u4f4d\u3002Protobuf \u4e2d\u7684\u5b57\u7b26\u4e32\u662f\u5b57\u8282\u5e8f\u5217\uff1b\u56e0\u6b64\uff0c\u5b57\u6bb5\u7f16\u7801\u7684\u5927\u5c0f\u5c31\u662f\u5b57\u8282\u5e8f\u5217\u7684\u957f\u5ea6\u3002<\/p>\n

\u53e6\u4e00\u4e2a\u9ad8\u6548\u7684\u65b9\u6cd5\u503c\u5f97\u4e00\u63d0\u3002\u56de\u60f3\u4e00\u4e0b\u524d\u9762\u7684\u793a\u4f8b\uff0c\u5176\u4e2d\u7684 DataItems \u6d88\u606f\u7531\u91cd\u590d\u7684 DataItem \u5b9e\u4f8b\u7ec4\u6210\uff1a<\/p>\n

message DataItems {\r\n  repeated DataItem item = 1;\r\n}<\/pre>\n

repeated \u8868\u793a DataItem \u5b9e\u4f8b\u662f\u6253\u5305\u7684\uff1a\u96c6\u5408\u5177\u6709\u5355\u4e2a\u6807\u7b7e\uff0c\u5728\u8fd9\u91cc\u662f 1\u3002\u56e0\u6b64\uff0c\u5177\u6709\u91cd\u590d\u7684 DataItem \u5b9e\u4f8b\u7684 DataItems \u6d88\u606f\u6bd4\u5177\u6709\u591a\u4e2a\u4f46\u5355\u72ec\u7684 DataItem \u5b57\u6bb5\u3001\u6bcf\u4e2a\u5b57\u6bb5\u90fd\u9700\u8981\u81ea\u5df1\u7684\u6807\u7b7e\u7684\u6d88\u606f\u7684\u6548\u7387\u66f4\u9ad8\u3002<\/p>\n

\u4e86\u89e3\u4e86\u8fd9\u4e00\u80cc\u666f\uff0c\u8ba9\u6211\u4eec\u56de\u5230 Go \u7a0b\u5e8f\u3002<\/p>\n

dataItem \u7a0b\u5e8f\u7684\u7ec6\u8282<\/strong><\/div>\n

dataItem \u7a0b\u5e8f\u521b\u5efa\u4e00\u4e2a DataItem \u5b9e\u4f8b\uff0c\u5e76\u4f7f\u7528\u9002\u5f53\u7c7b\u578b\u7684\u968f\u673a\u751f\u6210\u7684\u503c\u586b\u5145\u5b57\u6bb5\u3002Go \u6709\u4e00\u4e2a rand \u5305\uff0c\u5e26\u6709\u7528\u4e8e\u751f\u6210\u4f2a\u968f\u673a\u6574\u6570\u548c\u6d6e\u70b9\u503c\u7684\u51fd\u6570\uff0c\u800c\u6211\u7684 randString \u51fd\u6570\u53ef\u4ee5\u4ece\u5b57\u7b26\u96c6\u4e2d\u751f\u6210\u6307\u5b9a\u957f\u5ea6\u7684\u4f2a\u968f\u673a\u5b57\u7b26\u4e32\u3002\u8bbe\u8ba1\u76ee\u6807\u662f\u8981\u6709\u4e00\u4e2a\u5177\u6709\u4e0d\u540c\u7c7b\u578b\u548c\u4f4d\u5927\u5c0f\u7684\u5b57\u6bb5\u503c\u7684 DataItem \u5b9e\u4f8b\u3002\u4f8b\u5982\uff0cOddA \u548c EvenA \u503c\u5206\u522b\u662f 64 \u4f4d\u975e\u8d1f\u6574\u6570\u503c\u7684\u5947\u6570\u548c\u5076\u6570\uff1b\u4f46\u662f OddB \u548c EvenB \u53d8\u4f53\u7684\u5927\u5c0f\u4e3a 32 \u4f4d\uff0c\u5e76\u5b58\u653e 0 \u5230 2047 \u4e4b\u95f4\u7684\u5c0f\u6574\u6570\u503c\u3002\u968f\u673a\u6d6e\u70b9\u503c\u7684\u5927\u5c0f\u4e3a 32 \u4f4d\uff0c\u5b57\u7b26\u4e32\u4e3a 16\uff08Short\uff09\u548c 32\uff08Long\uff09\u5b57\u7b26\u7684\u957f\u5ea6\u3002\u8fd9\u662f\u7528\u968f\u673a\u503c\u586b\u5145 DataItem \u7ed3\u6784\u7684\u4ee3\u7801\u6bb5\uff1a<\/p>\n

\/\/ \u53ef\u53d8\u957f\u5ea6\u6574\u6570\r\nn1 := rand.Int63()        \/\/ \u5927\u6574\u6570\r\nif (n1 & 1) == 0 { n1++ } \/\/ \u786e\u4fdd\u5176\u662f\u5947\u6570\r\n...\r\nn3 := rand.Int31() % UpperBound \/\/ \u5c0f\u6574\u6570\r\nif (n3 & 1) == 0 { n3++ }       \/\/ \u786e\u4fdd\u5176\u662f\u5947\u6570\r\n\/\/ \u56fa\u5b9a\u957f\u5ea6\u6d6e\u70b9\u6570\r\n...\r\nt1 := rand.Float32()\r\nt2 := rand.Float32()\r\n...\r\n\/\/ \u5b57\u7b26\u4e32\r\nstr1 := randString(StrShort)\r\nstr2 := randString(StrLong)\r\n\/\/ \u6d88\u606f\r\ndataItem := &DataItem {\r\n   OddA:  n1,\r\n   EvenA: n2,\r\n   OddB:  n3,\r\n   EvenB: n4,\r\n   Big:   f1,\r\n   Small: f2,\r\n   Short: str1,\r\n   Long:  str2,\r\n}<\/pre>\n

\u521b\u5efa\u5e76\u586b\u5145\u503c\u540e\uff0cDataItem \u5b9e\u4f8b\u5c06\u4ee5 XML\u3001JSON \u548c Protobuf \u8fdb\u884c\u7f16\u7801\uff0c\u6bcf\u79cd\u7f16\u7801\u5747\u5199\u5165\u672c\u5730\u6587\u4ef6\uff1a<\/p>\n

func encodeAndserialize(dataItem *DataItem) {\r\n   bytes, _ := xml.MarshalIndent(dataItem, \"\", \" \")  \/\/ Xml to dataitem.xml\r\n   ioutil.WriteFile(XmlFile, bytes, 0644)            \/\/ 0644 is file access permissions\r\n   bytes, _ = json.MarshalIndent(dataItem, \"\", \" \")  \/\/ Json to dataitem.json\r\n   ioutil.WriteFile(JsonFile, bytes, 0644)\r\n   bytes, _ = proto.Marshal(dataItem)                \/\/ Protobuf to dataitem.pbuf\r\n   ioutil.WriteFile(PbufFile, bytes, 0644)\r\n}<\/pre>\n

\u8fd9\u4e09\u4e2a\u5e8f\u5217\u5316\u51fd\u6570\u4f7f\u7528\u672f\u8bed marshal\uff0c\u5b83\u4e0e serialize \u610f\u601d\u5927\u81f4\u76f8\u540c\u3002\u5982\u4ee3\u7801\u6240\u793a\uff0c\u4e09\u4e2a Marshal \u51fd\u6570\u5747\u8fd4\u56de\u4e00\u4e2a\u5b57\u8282\u6570\u7ec4\uff0c\u7136\u540e\u5c06\u5176\u5199\u5165\u6587\u4ef6\u3002\uff08\u4e3a\u7b80\u5355\u8d77\u89c1\uff0c\u5ffd\u7565\u53ef\u80fd\u7684\u9519\u8bef\u5904\u7406\u3002\uff09\u5728\u793a\u4f8b\u8fd0\u884c\u4e2d\uff0c\u6587\u4ef6\u5927\u5c0f\u4e3a\uff1a<\/p>\n

dataitem.xml:  262 bytes\r\ndataitem.json: 212 bytes\r\ndataitem.pbuf:  88 bytes<\/pre>\n

Protobuf \u7f16\u7801\u660e\u663e\u5c0f\u4e8e\u5176\u4ed6\u4e24\u4e2a\u7f16\u7801\u65b9\u6848\u3002\u901a\u8fc7\u6d88\u9664\u7f29\u8fdb\u5b57\u7b26\uff08\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u4e3a\u7a7a\u767d\u548c\u6362\u884c\u7b26\uff09\uff0c\u53ef\u4ee5\u7a0d\u5fae\u51cf\u5c0f XML \u548c JSON \u5e8f\u5217\u5316\u7684\u5927\u5c0f\u3002<\/p>\n

\u4ee5\u4e0b\u662f dataitem.json \u6587\u4ef6\uff0c\u8be5\u6587\u4ef6\u6700\u7ec8\u662f\u7531 json.MarshalIndent \u8c03\u7528\u4ea7\u751f\u7684\uff0c\u5e76\u6dfb\u52a0\u4e86\u4ee5 ## \u5f00\u5934\u7684\u6ce8\u91ca\uff1a<\/p>\n

{\r\n \"oddA\":  4744002665212642479,                ## 64-bit >= 0\r\n \"evenA\": 2395006495604861128,                ## ditto\r\n \"oddB\":  57,                                 ## 32-bit >= 0 but < 2048\r\n \"evenB\": 468,                                ## ditto\r\n \"small\": 0.7562016,                          ## 32-bit floating-point\r\n \"big\":   0.85202795,                         ## ditto\r\n \"short\": \"ClH1oDaTtoX$HBN5\",                 ## 16 random chars\r\n \"long\":  \"xId0rD3Cri%3Wt%^QjcFLJgyXBu9^DZI\"  ## 32 random chars\r\n}<\/pre>\n

\u5c3d\u7ba1\u8fd9\u4e9b\u5e8f\u5217\u5316\u7684\u6570\u636e\u5199\u5165\u5230\u672c\u5730\u6587\u4ef6\u4e2d\uff0c\u4f46\u662f\u4e5f\u53ef\u4ee5\u4f7f\u7528\u76f8\u540c\u7684\u65b9\u6cd5\u5c06\u6570\u636e\u5199\u5165\u7f51\u7edc\u8fde\u63a5\u7684\u8f93\u51fa\u6d41\u3002<\/p>\n

\u6d4b\u8bd5\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316<\/strong><\/div>\n

Go \u7a0b\u5e8f\u63a5\u4e0b\u6765\u901a\u8fc7\u5c06\u5148\u524d\u5199\u5165 dataitem.pbuf \u6587\u4ef6\u7684\u5b57\u8282\u53cd\u5e8f\u5217\u5316\u4e3a DataItem \u5b9e\u4f8b\u6765\u8fd0\u884c\u57fa\u672c\u6d4b\u8bd5\u3002\u8fd9\u662f\u4ee3\u7801\u6bb5\uff0c\u5176\u4e2d\u53bb\u9664\u4e86\u9519\u8bef\u68c0\u67e5\u90e8\u5206\uff1a<\/p>\n

filebytes, err := ioutil.ReadFile(PbufFile) \/\/ get the bytes from the file\r\n...\r\ntestItem.Reset()                            \/\/ clear the DataItem structure\r\nerr = proto.Unmarshal(filebytes, testItem)  \/\/ deserialize into a DataItem instance<\/pre>\n

\u7528\u4e8e Protbuf \u53cd\u5e8f\u5217\u5316\u7684 proto.Unmarshal \u51fd\u6570\u4e0e proto.Marshal \u51fd\u6570\u76f8\u53cd\u3002\u539f\u59cb\u7684 DataItem \u548c\u53cd\u5e8f\u5217\u5316\u7684\u526f\u672c\u5c06\u88ab\u6253\u5370\u51fa\u6765\u4ee5\u786e\u8ba4\u5b8c\u5168\u5339\u914d\uff1a<\/p>\n

Original:\r\n2041519981506242154 3041486079683013705 1192 1879\r\n0.572123 0.326855\r\nboPb#T0O8Xd&Ps5EnSZqDg4Qztvo7IIs 9vH66AiGSQgCDxk&\r\nDeserialized:\r\n2041519981506242154 3041486079683013705 1192 1879\r\n0.572123 0.326855\r\nboPb#T0O8Xd&Ps5EnSZqDg4Qztvo7IIs 9vH66AiGSQgCDxk&<\/pre>\n
\u4e00\u4e2a Java Protobuf \u5ba2\u6237\u7aef<\/strong><\/div>\n

\u7528 Java \u5199\u7684\u793a\u4f8b\u662f\u4e3a\u4e86\u786e\u8ba4 Protobuf \u7684\u8bed\u8a00\u4e2d\u7acb\u6027\u3002\u539f\u59cb IDL \u6587\u4ef6\u53ef\u7528\u4e8e\u751f\u6210 Java \u652f\u6301\u4ee3\u7801\uff0c\u5176\u4e2d\u6d89\u53ca\u5d4c\u5957\u7c7b\u3002\u4f46\u662f\uff0c\u4e3a\u4e86\u6291\u5236\u8b66\u544a\u4fe1\u606f\uff0c\u53ef\u4ee5\u8fdb\u884c\u4e00\u4e9b\u8865\u5145\u3002\u8fd9\u662f\u4fee\u8ba2\u7248\uff0c\u5b83\u6307\u5b9a\u4e86\u4e00\u4e2a DataMsg \u4f5c\u4e3a\u5916\u90e8\u7c7b\u7684\u540d\u79f0\uff0c\u5185\u90e8\u7c7b\u5728\u8be5 Protobuf \u6d88\u606f\u540e\u9762\u81ea\u52a8\u547d\u540d\u4e3a DataItem\uff1a<\/p>\n

syntax = \"proto3\";\r\npackage main;\r\noption java_outer_classname = \"DataMsg\";\r\nmessage DataItem {\r\n...<\/pre>\n

\u8fdb\u884c\u6b64\u66f4\u6539\u540e\uff0cprotoc \u7f16\u8bd1\u4e0e\u4ee5\u524d\u76f8\u540c\uff0c\u53ea\u662f\u6240\u671f\u671b\u7684\u8f93\u51fa\u73b0\u5728\u662f Java \u800c\u4e0d\u662f Go\uff1a<\/p>\n

% protoc --java_out=. dataitem.proto<\/pre>\n

\u751f\u6210\u7684\u6e90\u6587\u4ef6\uff08\u5728\u540d\u4e3a main \u7684\u5b50\u76ee\u5f55\u4e2d\uff09\u4e3a DataMsg.java\uff0c\u957f\u5ea6\u7ea6\u4e3a 1,120 \u884c\uff1aJava \u5e76\u4e0d\u7b80\u6d01\u3002\u7f16\u8bd1\u7136\u540e\u8fd0\u884c Java \u4ee3\u7801\u9700\u8981\u5177\u6709 Protobuf \u5e93\u652f\u6301\u7684 JAR \u6587\u4ef6\u3002\u8be5\u6587\u4ef6\u4f4d\u4e8e Maven \u5b58\u50a8\u5e93\u4e2d\u3002<\/p>\n

\u653e\u7f6e\u597d\u8fd9\u4e9b\u7247\u6bb5\u540e\uff0c\u6211\u7684\u6d4b\u8bd5\u4ee3\u7801\u76f8\u5bf9\u8f83\u77ed\uff08\u5e76\u4e14\u5728 ZIP \u6587\u4ef6\u4e2d\u4ee5 Main.java \u5f62\u5f0f\u63d0\u4f9b\uff09\uff1a<\/p>\n

package main;\r\nimport java.io.FileInputStream;\r\npublic class Main {\r\n   public static void main(String[] args) {\r\n      String path = \"dataitem.pbuf\";  \/\/ from the Go program's serialization\r\n      try {\r\n         DataMsg.DataItem deserial =\r\n           DataMsg.DataItem.newBuilder().mergeFrom(new FileInputStream(path)).build();\r\n         System.out.println(deserial.getOddA()); \/\/ 64-bit odd\r\n         System.out.println(deserial.getLong()); \/\/ 32-character string\r\n      }\r\n      catch(Exception e) { System.err.println(e); }\r\n    }\r\n}<\/pre>\n

\u5f53\u7136\uff0c\u751f\u4ea7\u7ea7\u7684\u6d4b\u8bd5\u5c06\u66f4\u52a0\u5f7b\u5e95\uff0c\u4f46\u662f\u5373\u4f7f\u662f\u8be5\u521d\u6b65\u6d4b\u8bd5\u4e5f\u53ef\u4ee5\u8bc1\u660e Protobuf \u7684\u8bed\u8a00\u4e2d\u7acb\u6027\uff1adataitem.pbuf \u6587\u4ef6\u662f Go \u7a0b\u5e8f\u5bf9 Go \u8bed\u8a00\u7248\u7684 DataItem \u8fdb\u884c\u5e8f\u5217\u5316\u7684\u7ed3\u679c\uff0c\u5e76\u4e14\u8be5\u6587\u4ef6\u4e2d\u7684\u5b57\u8282\u88ab\u53cd\u5e8f\u5217\u5316\u4ee5\u4ea7\u751f\u4e00\u4e2a Java \u8bed\u8a00\u7684 DataItem \u5b9e\u4f8b\u3002Java \u6d4b\u8bd5\u7684\u8f93\u51fa\u4e0e Go \u6d4b\u8bd5\u7684\u8f93\u51fa\u76f8\u540c\u3002<\/p>\n

\u7528 numPairs \u7a0b\u5e8f\u6765\u7ed3\u675f<\/strong><\/div>\n

\u8ba9\u6211\u4eec\u4ee5\u4e00\u4e2a\u793a\u4f8b\u4f5c\u4e3a\u7ed3\u5c3e\uff0c\u6765\u7a81\u51fa Protobuf \u6548\u7387\uff0c\u4f46\u53c8\u5f3a\u8c03\u5728\u4efb\u4f55\u7f16\u7801\u6280\u672f\u4e2d\u90fd\u4f1a\u6d89\u53ca\u5230\u7684\u6210\u672c\u3002\u8003\u8651\u4ee5\u4e0b Protobuf IDL \u6587\u4ef6\uff1a<\/p>\n

syntax = \"proto3\";\r\npackage main;\r\nmessage NumPairs {\r\n  repeated NumPair pair = 1;\r\n}\r\nmessage NumPair {\r\n  int32 odd = 1;\r\n  int32 even = 2;\r\n}<\/pre>\n

NumPair \u6d88\u606f\u7531\u4e24\u4e2a int32 \u503c\u4ee5\u53ca\u6bcf\u4e2a\u5b57\u6bb5\u7684\u6574\u6570\u6807\u7b7e\u7ec4\u6210\u3002NumPairs \u6d88\u606f\u662f\u5d4c\u5165\u7684 NumPair \u6d88\u606f\u7684\u5e8f\u5217\u3002<\/p>\n

Go \u8bed\u8a00\u7684 numPairs \u7a0b\u5e8f\uff08\u5982\u4e0b\uff09\u521b\u5efa\u4e86 200 \u4e07\u4e2a NumPair \u5b9e\u4f8b\uff0c\u6bcf\u4e2a\u5b9e\u4f8b\u90fd\u9644\u52a0\u5230 NumPairs \u6d88\u606f\u4e2d\u3002\u8be5\u6d88\u606f\u53ef\u4ee5\u6309\u5e38\u89c4\u65b9\u5f0f\u8fdb\u884c\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u3002<\/p>\n

\u793a\u4f8b 2\u3001numPairs \u7a0b\u5e8f<\/strong><\/span><\/div>\n
package main\r\nimport (\r\n   \"math\/rand\"\r\n   \"time\"\r\n   \"encoding\/xml\"\r\n   \"encoding\/json\"\r\n   \"io\/ioutil\"\r\n   \"github.com\/golang\/protobuf\/proto\"\r\n)\r\n\/\/ protoc-generated code: start\r\nvar _ = proto.Marshal\r\ntype NumPairs struct {\r\n   Pair []*NumPair `protobuf:\"bytes,1,rep,name=pair\" json:\"pair,omitempty\"`\r\n}\r\nfunc (m *NumPairs) Reset()         { *m = NumPairs{} }\r\nfunc (m *NumPairs) String() string { return proto.CompactTextString(m) }\r\nfunc (*NumPairs) ProtoMessage()    {}\r\nfunc (m *NumPairs) GetPair() []*NumPair {\r\n   if m != nil { return m.Pair }\r\n   return nil\r\n}\r\ntype NumPair struct {\r\n   Odd  int32 `protobuf:\"varint,1,opt,name=odd\" json:\"odd,omitempty\"`\r\n   Even int32 `protobuf:\"varint,2,opt,name=even\" json:\"even,omitempty\"`\r\n}\r\nfunc (m *NumPair) Reset()         { *m = NumPair{} }\r\nfunc (m *NumPair) String() string { return proto.CompactTextString(m) }\r\nfunc (*NumPair) ProtoMessage()    {}\r\nfunc init() {}\r\n\/\/ protoc-generated code: finish\r\nvar numPairsStruct NumPairs\r\nvar numPairs = &numPairsStruct\r\nfunc encodeAndserialize() {\r\n   \/\/ XML encoding\r\n   filename := \".\/pairs.xml\"\r\n   bytes, _ := xml.MarshalIndent(numPairs, \"\", \" \")\r\n   ioutil.WriteFile(filename, bytes, 0644)\r\n   \/\/ JSON encoding\r\n   filename = \".\/pairs.json\"\r\n   bytes, _ = json.MarshalIndent(numPairs, \"\", \" \")\r\n   ioutil.WriteFile(filename, bytes, 0644)\r\n   \/\/ ProtoBuf encoding\r\n   filename = \".\/pairs.pbuf\"\r\n   bytes, _ = proto.Marshal(numPairs)\r\n   ioutil.WriteFile(filename, bytes, 0644)\r\n}\r\nconst HowMany = 200 * 100  * 100 \/\/ two million\r\nfunc main() {\r\n   rand.Seed(time.Now().UnixNano())\r\n   \/\/ uncomment the modulus operations to get the more efficient version\r\n   for i := 0; i < HowMany; i++ {\r\n      n1 := rand.Int31() \/\/ % 2047\r\n      if (n1 & 1) == 0 { n1++ } \/\/ ensure it's odd\r\n      n2 := rand.Int31() \/\/ % 2047\r\n      if (n2 & 1) == 1 { n2++ } \/\/ ensure it's even\r\n      next := &NumPair {\r\n                 Odd:  n1,\r\n                 Even: n2,\r\n              }\r\n      numPairs.Pair = append(numPairs.Pair, next)\r\n   }\r\n   encodeAndserialize()<\/pre>\n

\u6bcf\u4e2a NumPair \u4e2d\u968f\u673a\u751f\u6210\u7684\u5947\u6570\u548c\u5076\u6570\u503c\u7684\u8303\u56f4\u5728 0 \u5230 20 \u4ebf\u4e4b\u95f4\u53d8\u5316\u3002\u5c31\u539f\u59cb\u6570\u636e\uff08\u800c\u975e\u7f16\u7801\u6570\u636e\uff09\u800c\u8a00\uff0cGo \u7a0b\u5e8f\u4e2d\u751f\u6210\u7684\u6574\u6570\u603b\u5171\u4e3a 16MB\uff1a\u6bcf\u4e2a NumPair \u4e3a\u4e24\u4e2a\u6574\u6570\uff0c\u603b\u8ba1\u4e3a 400 \u4e07\u4e2a\u6574\u6570\uff0c\u6bcf\u4e2a\u503c\u7684\u5927\u5c0f\u4e3a\u56db\u4e2a\u5b57\u8282\u3002<\/p>\n

\u4e3a\u4e86\u8fdb\u884c\u6bd4\u8f83\uff0c\u4e0b\u8868\u5217\u51fa\u4e86 XML\u3001JSON \u548c Protobuf \u7f16\u7801\u7684\u793a\u4f8b NumsPairs \u6d88\u606f\u7684 200 \u4e07\u4e2a NumPair \u5b9e\u4f8b\u3002\u539f\u59cb\u6570\u636e\u4e5f\u5305\u62ec\u5728\u5185\u3002\u7531\u4e8e numPairs \u7a0b\u5e8f\u751f\u6210\u968f\u673a\u503c\uff0c\u56e0\u6b64\u6837\u672c\u8fd0\u884c\u7684\u8f93\u51fa\u6709\u6240\u4e0d\u540c\uff0c\u4f46\u63a5\u8fd1\u8868\u4e2d\u663e\u793a\u7684\u5927\u5c0f\u3002<\/p>\n\n\n\n\n\n\n\n\n
\u7f16\u7801<\/th>\n\u6587\u4ef6<\/th>\n\u5b57\u8282\u5927\u5c0f<\/th>\nPbuf\/\u5176\u5b83 \u6bd4\u4f8b<\/th>\n<\/tr>\n<\/thead>\n
\u65e0<\/td>\npairs.raw<\/td>\n16MB<\/td>\n169%<\/td>\n<\/tr>\n
Protobuf<\/td>\npairs.pbuf<\/td>\n27MB<\/td>\n\u2014<\/td>\n<\/tr>\n
JSON<\/td>\npairs.json<\/td>\n100MB<\/td>\n27%<\/td>\n<\/tr>\n
XML<\/td>\npairs.xml<\/td>\n126MB<\/td>\n21%<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u8868 2. 16MB \u6574\u6570\u7684\u7f16\u7801\u5f00\u9500<\/p>\n

\u4e0d\u51fa\u6240\u6599\uff0cProtobuf \u548c\u4e4b\u540e\u7684 XML \u548c JSON \u5dee\u522b\u660e\u663e\u3002Protobuf \u7f16\u7801\u5927\u7ea6\u662f JSON \u7684\u56db\u5206\u4e4b\u4e00\uff0c\u662f XML \u7684\u4e94\u5206\u4e4b\u4e00\u3002\u4f46\u662f\u539f\u59cb\u6570\u636e\u6e05\u695a\u5730\u8868\u660e Protobuf \u4e5f\u4f1a\u4ea7\u751f\u7f16\u7801\u5f00\u9500\uff1a\u5e8f\u5217\u5316\u7684 Protobuf \u6d88\u606f\u6bd4\u539f\u59cb\u6570\u636e\u5927 11MB\u3002\u5305\u62ec Protobuf \u5728\u5185\u7684\u4efb\u4f55\u7f16\u7801\u90fd\u6d89\u53ca\u7ed3\u6784\u5316\u6570\u636e\uff0c\u8fd9\u4e0d\u53ef\u907f\u514d\u5730\u4f1a\u589e\u52a0\u5b57\u8282\u3002<\/p>\n

\u5e8f\u5217\u5316\u7684 200 \u4e07\u4e2a NumPair \u5b9e\u4f8b\u4e2d\u7684\u6bcf\u4e2a\u5b9e\u4f8b\u90fd\u5305\u542b\u56db\u4e2a\u6574\u6570\u503c\uff1aGo \u7ed3\u6784\u4e2d\u7684 Even \u548c Odd \u5b57\u6bb5\u5206\u522b\u4e00\u4e2a\uff0c\u800c Protobuf \u7f16\u7801\u4e2d\u7684\u6bcf\u4e2a\u5b57\u6bb5\u3001\u6bcf\u4e2a\u6807\u7b7e\u4e00\u4e2a\u3002\u5bf9\u4e8e\u539f\u59cb\u6570\u636e\uff08\u800c\u4e0d\u662f\u7f16\u7801\u6570\u636e\uff09\uff0c\u6bcf\u4e2a\u5b9e\u4f8b\u5c06\u8fbe\u5230 16 \u4e2a\u5b57\u8282\uff0c\u6837\u672c NumPairs \u6d88\u606f\u4e2d\u6709 200 \u4e07\u4e2a\u5b9e\u4f8b\u3002\u4f46\u662f Protobuf \u6807\u8bb0\uff08\u5982 NumPair \u5b57\u6bb5\u4e2d\u7684 int32 \u503c\uff09\u4f7f\u7528 varint \u7f16\u7801\uff0c\u56e0\u6b64\u5b57\u8282\u957f\u5ea6\u6709\u6240\u4e0d\u540c\u3002\u7279\u522b\u662f\uff0c\u5c0f\u7684\u6574\u6570\u503c\uff08\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5305\u62ec\u6807\u7b7e\u5728\u5185\uff09\u9700\u8981\u4e0d\u5230\u56db\u4e2a\u5b57\u8282\u8fdb\u884c\u7f16\u7801\u3002<\/p>\n

\u5982\u679c\u5bf9 numPairs \u7a0b\u5e8f\u8fdb\u884c\u4e86\u4fee\u6539\uff0c\u4ee5\u4f7f\u4e24\u4e2a NumPair \u5b57\u6bb5\u7684\u503c\u5c0f\u4e8e 2048\uff0c\u4e14\u5176\u7f16\u7801\u4e3a\u4e00\u6216\u4e24\u4e2a\u5b57\u8282\uff0c\u5219 Protobuf \u7f16\u7801\u5c06\u4ece 27MB \u4e0b\u964d\u5230 16MB\uff0c\u8fd9\u6b63\u662f\u539f\u59cb\u6570\u636e\u7684\u5927\u5c0f\u3002\u4e0b\u8868\u603b\u7ed3\u4e86\u6837\u672c\u8fd0\u884c\u4e2d\u7684\u65b0\u7f16\u7801\u5927\u5c0f\u3002<\/p>\n\n\n\n\n\n\n\n\n
\u7f16\u7801<\/th>\n\u6587\u4ef6<\/th>\n\u5b57\u8282\u5927\u5c0f<\/th>\nPbuf\/\u5176\u5b83 \u6bd4\u4f8b<\/th>\n<\/tr>\n<\/thead>\n
None<\/td>\npairs.raw<\/td>\n16MB<\/td>\n100%<\/td>\n<\/tr>\n
Protobuf<\/td>\npairs.pbuf<\/td>\n16MB<\/td>\n\u2014<\/td>\n<\/tr>\n
JSON<\/td>\npairs.json<\/td>\n77MB<\/td>\n21%<\/td>\n<\/tr>\n
XML<\/td>\npairs.xml<\/td>\n103MB<\/td>\n15%<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u8868 3. \u7f16\u7801 16MB \u7684\u5c0f\u4e8e 2048 \u7684\u6574\u6570<\/p>\n

\u603b\u4e4b\uff0c\u4fee\u6539\u540e\u7684 numPairs \u7a0b\u5e8f\u7684\u5b57\u6bb5\u503c\u5c0f\u4e8e 2048\uff0c\u53ef\u51cf\u5c11\u539f\u59cb\u6570\u636e\u4e2d\u6bcf\u4e2a\u56db\u5b57\u8282\u6574\u6570\u503c\u7684\u5927\u5c0f\u3002\u4f46\u662f Protobuf \u7f16\u7801\u4ecd\u7136\u9700\u8981\u6807\u7b7e\uff0c\u8fd9\u4e9b\u6807\u7b7e\u4f1a\u5728 Protobuf \u6d88\u606f\u4e2d\u6dfb\u52a0\u5b57\u8282\u3002Protobuf \u7f16\u7801\u786e\u5b9e\u4f1a\u589e\u52a0\u6d88\u606f\u5927\u5c0f\uff0c\u4f46\u662f\u5982\u679c\u8981\u7f16\u7801\u76f8\u5bf9\u8f83\u5c0f\u7684\u6574\u6570\u503c\uff08\u65e0\u8bba\u662f\u5b57\u6bb5\u8fd8\u662f\u952e\uff09\uff0c\u5219\u53ef\u4ee5\u901a\u8fc7 varint \u56e0\u5b50\u6765\u51cf\u5c11\u6b64\u5f00\u9500\u3002<\/p>\n

\u5bf9\u4e8e\u5305\u542b\u6df7\u5408\u7c7b\u578b\u7684\u7ed3\u6784\u5316\u6570\u636e\uff08\u4e14\u6574\u6570\u503c\u76f8\u5bf9\u8f83\u5c0f\uff09\u7684\u4e2d\u7b49\u5927\u5c0f\u7684\u6d88\u606f\uff0cProtobuf \u660e\u663e\u4f18\u4e8e XML \u548c JSON \u7b49\u9009\u9879\u3002\u5728\u5176\u4ed6\u60c5\u51b5\u4e0b\uff0c\u6570\u636e\u53ef\u80fd\u4e0d\u9002\u5408 Protobuf \u7f16\u7801\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4e24\u4e2a\u5e94\u7528\u7a0b\u5e8f\u9700\u8981\u5171\u4eab\u5927\u91cf\u6587\u672c\u8bb0\u5f55\u6216\u5927\u6574\u6570\u503c\uff0c\u5219\u53ef\u4ee5\u91c7\u7528\u538b\u7f29\u800c\u4e0d\u662f\u7f16\u7801\u6280\u672f\u3002<\/p>\n

via: https:\/\/opensource.com\/article\/19\/10\/protobuf-data-interchange<\/p>\n

\u4f5c\u8005\uff1aMarty Kalin \u9009\u9898\uff1alujun9972 \u8bd1\u8005\uff1awxy \u6821\u5bf9\uff1awxy<\/p>\n

\u672c\u6587\u7531 LCTT \u539f\u521b\u7f16\u8bd1\uff0cLinux\u4e2d\u56fd \u8363\u8a89\u63a8\u51fa<\/p>\n","protected":false},"excerpt":{"rendered":"

Protocol Buffers\uff08Protobufs\uff09\u50cf XML \u548c JSON \u4e00\u6837\uff0c\u53ef\u4ee5\u8ba9\u7528\u4e0d\u540c\u8bed\u8a00\u7f16\u5199\u5e76\u5728 […]<\/p>\n","protected":false},"author":1920,"featured_media":167772,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-167766","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/167766","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1920"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=167766"}],"version-history":[{"count":5,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/167766\/revisions"}],"predecessor-version":[{"id":168195,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/167766\/revisions\/168195"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/167772"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=167766"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=167766"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=167766"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}