﻿{"id":450,"date":"2011-05-06T10:27:11","date_gmt":"2011-05-06T02:27:11","guid":{"rendered":"http:\/\/nick.workao.org\/?p=450"},"modified":"2011-05-06T12:44:41","modified_gmt":"2011-05-06T04:44:41","slug":"%e8%bd%acprotocol-buffers%e7%9a%84%e5%ba%94%e7%94%a8%e4%b8%8e%e5%88%86%e6%9e%90","status":"publish","type":"post","link":"https:\/\/nick.txtcc.com\/index.php\/linux\/450","title":{"rendered":"\u8f6cProtocol Buffers\u7684\u5e94\u7528\u4e0e\u5206\u6790"},"content":{"rendered":"<div>\n<h2>1\u00a0 Protocol Buffers\u7684\u4ecb\u7ecd<\/h2>\nProtocol Buffers\u662f\u4e00\u79cd\u7528\u4e8e\u5e8f\u5217\u5316\u7ed3\u6784\u5316\u6570\u636e\u7684\u673a\u5236\uff0c\u5b83\u5177\u6709\u7075\u6d3b\u3001\u9ad8\u6548\u3001\u81ea\u52a8\u5316\u7684\u7279\u70b9\u3002\u7c7b\u4f3c\u4e8eXML\uff0c\u4f46\u662f\u6bd4XML\u66f4\u5c0f\u5de7\u3001\u5feb\u6377\u3001\u7b80\u5355\u3002\u5728Google\u00a0\u51e0\u4e4e\u6240\u6709\u5b83\u5185\u90e8\u7684RPC\u534f\u8bae\u548c\u6587\u4ef6\u683c\u5f0f\u90fd\u662f\u91c7\u7528PB\u3002\nPB\u5177\u6709\u4ee5\u4e0b\u7279\u70b9\uff1a\n<ol>\n    <li>\u5e73\u53f0\u65e0\u5173\u3001\u8bed\u8a00\u65e0\u5173<\/li>\n    <li>\u9ad8\u6027\u80fd \u6bd4XML\u575720-100\u500d<\/li>\n    <li>\u4f53\u79ef\u5c0f \u6bd4XML\u5c0f3-10\u500d<\/li>\n    <li>\u4f7f\u7528\u7b80\u5355<\/li>\n    <li>\u517c\u5bb9\u6027\u597d<\/li>\n<\/ol>\n\u5728\u8fd9\u91cc\uff0c\u6211\u505a\u4e86\u4e2a\u5c0f\u5b9e\u9a8c\uff0c\u5c06\u4e00\u4e2a29230KB\u7684\u81ea\u5b9a\u4e49\u683c\u5f0f\u7684\u6587\u672c\u6570\u636e\u8f6c\u6362\u6210PB\u548cXML\uff1a\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"0\" width=\"520\">\n<tbody>\n<tr>\n<td width=\"160\" valign=\"top\"><\/td>\n<td width=\"180\" valign=\"top\">PB<\/td>\n<td width=\"180\" valign=\"top\">XML<\/td>\n<\/tr>\n<tr>\n<td width=\"160\" valign=\"top\">\u8f6c\u6362\u540e\u7684\u5927\u5c0f<\/td>\n<td width=\"180\" valign=\"top\">21011KB<\/td>\n<td width=\"180\" valign=\"top\">43202KB<\/td>\n<\/tr>\n<tr>\n<td width=\"160\" valign=\"top\">\u89e3\u6790\u65f6\u95f4\uff08100\u6b21\u5faa\u73af\uff09<\/td>\n<td width=\"180\" valign=\"top\">18610ms<\/td>\n<td width=\"180\" valign=\"top\">169251ms<\/td>\n<\/tr>\n<tr>\n<td width=\"160\" valign=\"top\">\u5b8c\u6210\u89e3\u6790\u6240\u5199\u4ee3\u7801\u884c\u6570<\/td>\n<td width=\"180\" valign=\"top\">1\u884c<\/td>\n<td width=\"180\" valign=\"top\">50\u884c<\/td>\n<\/tr>\n<tr>\n<td colspan=\"3\" width=\"540\" valign=\"top\">\u4e0e\u5b98\u65b9\u8bf4\u6cd5\u7684\u5dee\u8ddd\uff0c\u4e3b\u8981\u53ef\u80fd\u662f\u56e0\u4e3a\u5e94\u7528\u573a\u666f\u4e0d\u540c\uff0c\u6211\u7684\u6d4b\u8bd5\u6570\u636e\u4e2d\u5b57\u6bb5\u6bd4\u8f83\u957f<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n\u88681\uff1aPB\u4e0eXML\u7684\u5b9e\u9a8c\u6bd4\u8f83\n\n\u53ef\u89c1\uff0cPB\u4f5c\u4e3a\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u6570\u636e\u534f\u8bae\uff0c\u5728\u65f6\u95f4\u3001\u7a7a\u95f4\u4e0a\u90fd\u6709\u4e00\u5b9a\u7684\u4f18\u52bf\u3002\n<h2>2\u00a0 Protocol Buffers\u7684\u7b80\u5355\u5e94\u7528<\/h2>\n<h3>2.1\u00a0 \u521b\u5efa\u6d41\u7a0b<\/h3>\n<h4>2.1.1\u00a0 \u5b9a\u4e49\u4e00\u4e2a.proto\u6587\u4ef6<\/h4>\n\u65b0\u5efa\u4e00\u4e2a\u6587\u4ef6\uff0c\u547d\u540d\u4e3aaddressbook.proto\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a\n<div>\n<pre name=\"code\">package tutorial;\/\/\u547d\u540d\u7a7a\u95f4\n\noption java_package = \"com.example.tutorial\";\/\/\u751f\u6210\u6587\u4ef6\u7684\u5305\u540d\noption java_outer_classname = \"AddressBookProtos\";\/\/\u7c7b\u540d\n\nmessage Person { \/\/\u8981\u63cf\u8ff0\u7684\u7ed3\u6784\u5316\u6570\u636e\n\n    required string name = 1;\/\/required\u8868\u793a\u8fd9\u4e2a\u5b57\u6bb5\u4e0d\u80fd\u4e3a\u7a7a\n    required int32 id = 2;\/\/\u7b49\u53f7\u540e\u9762\u7684\u5185\u5bb9\u4e3a\u6570\u5b57\u522b\u540d\n    optional string email = 3;\/\/optional\u8868\u793a\u53ef\u4ee5\u4e3a\u7a7a\n\n    PhoneNumber {\/\/\u5185\u90e8message\n        required string number = 1;\n        optional int32 type = 2;\n    }\n\n    repeated PhoneNumber phone = 4\n}\n\nmessage AddressBook {\n    repeated Person person = 1;\/\/\u662f\u4e2a\u96c6\u5408\n}<\/pre>\n<\/div>\n\u5bf9\u4ee5\u4e0a\u5185\u5bb9\u7684\u4e00\u70b9\u89e3\u91ca\uff1a\n<ul>\n    <li>PB\u6240\u652f\u6301\u7684\u5143\u7c7b\u578b\u6570\u636e\u8bf7\u53c2\u8003\uff1a<a title=\"http:\/\/code.google.com\/intl\/zh-CN\/apis\/protocolbuffers\/docs\/proto.html\" href=\"http:\/\/code.google.com\/intl\/zh-CN\/apis\/protocolbuffers\/docs\/proto.html\">PB\u5143\u7c7b\u578b\u6570\u636e<\/a><\/li>\n    <li>\u4fee\u9970\u7b26required\uff1a\u8fd9\u4e2a\u4fee\u9970\u7b26\u5e94\u8be5\u8c28\u614e\u4f7f\u7528\uff0c\u6ee5\u7528\u4f1a\u5bfc\u81f4\u540e\u7eed\u7684\u4fee\u6539\u5bb9\u6613\u51fa\u73b0\u517c\u5bb9\u6027\u95ee\u9898\uff1b<\/li>\n    <li>\u4fee\u9970\u7b26optional\uff1a\u5bf9\u4e8e\u5e38\u51fa\u73b0\u7684\u5c5e\u6027\uff0c\u4e3a\u8282\u7701\u7a7a\u95f4\u5e94\u8be5\u53d61-16\u7684\u522b\u540d\uff1b<\/li>\n    <li>PB\u662f\u4ee5key-value\u7684\u5f62\u5f0f\u6765\u5c06\u7ed3\u6784\u5316\u6570\u636e\u5e8f\u5217\u5316\u7684\u3002\u5b83\u91c7\u7528\u4e86\u5c06\u7b49\u53f7\u540e\u7684\u6570\u5b57\u522b\u540d\u4ee5\u53ca\u5c5e\u6027\u7684\u7c7b\u578b\u7528varints\u7f16\u7801\u6210\u4e00\u4e2a\u6570\u5b57\uff0c\u6765\u4f5c\u4e3akey\u3002<\/li>\n<\/ul>\n<h4>2.1.2\u00a0 \u4f7f\u7528PB\u7f16\u8bd1\u5668<\/h4>\n\u8f93\u5165\uff1aprotoc\u00a0\u00a0\u00a0\u00a0\u00a0 -I=$SRC_DIR \u2013java_out=$DST_DIR $SRC_DIR\/addressbook.proto\n\u5176\u4e2d\u00a0\u00a0\u00a0 -I\u6307\u5b9a.proto\u6587\u4ef6\u6240\u5728\u76ee\u5f55\n\u2013java_out\u6307\u5b9a\u751f\u6210java\u6587\u4ef6\u6240\u5728\u7684\u76ee\u5f55\n<h4>2.1.3\u00a0 \u4f7f\u7528PB\u7684API\u6765\u5199\u5165\u548c\u8bfb\u53d6messages<\/h4>\n\u7ecf\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u4f1a\u5728\u6307\u5b9a\u7684$DST_DIR\u76ee\u5f55\u4e0b\u751f\u6210\u4e00\u4e2aAddressBookProtos.java\u7684\u7c7b\u3002\u5728maven\u4e2d\u5f15\u5165protobuf-java\u8fd9\u4e2a\u4f9d\u8d56\u540e\uff0c\u5229\u7528\u8fd9\u4e2a\u7c7b\uff0c\u4fbf\u80fd\u5e8f\u5217\u5316\/\u53cd\u5e8f\u5217\u5316\u6570\u636e\u4e86\u3002\n\u751f\u6210\u7684\u4ee3\u7801\u7ed3\u6784\u5982\u4e0b\uff1a\n<div>\n<pre name=\"code\" class=\"c-sharp\">class AddressBookProtos{\n    class Person{\n        class PhoneNumber{class Builder{} }\n        class Builder{}\n    }\n    class AddressBook{class Builder{} }\n}<\/pre>\n<\/div>\n\u53ef\u4ee5\u770b\u5230Person\u3001PhoneNumber\u3001AddressBook\u8fd9\u4e9b\u5185\u90e8\u7c7b\u5219\u5bf9\u5e94\u4e86\u6240\u5b9a\u4e49\u7684\u90a3\u4e9bmessage\u3002\n<h3>2.2\u00a0 \u5e8f\u5217\u5316\u6570\u636e\u53ca\u5206\u6790<\/h3>\n\u901a\u8fc7\u9605\u8bfb\u4ee3\u7801\u53ef\u4ee5\u770b\u5230\uff0c\u4ee5\u4e0a\u4e09\u4e2a\u7c7b\u7684\u6210\u5458\u53d8\u91cf\u90fd\u662fprivate\u7c7b\u578b\u7684\uff0c\u5e76\u4e14\uff0c\u53ea\u63d0\u4f9b\u4e86getter\u65b9\u6cd5\uff0c\u800c\u6ca1\u6709\u63d0\u4f9bsetter\u65b9\u6cd5\u53bb\u4e3a\u6570\u636e\u53d8\u91cf\u8d4b\u503c\u3002\nPB\u5229\u7528\u4e86\u5185\u90e8\u7c7b\u53ef\u4ee5\u8bbf\u95ee\u5230\u5916\u90e8\u7c7b\u4e2d\u79c1\u6709\u6210\u5458\u53d8\u91cf\u7684\u7279\u6027\u3002\u5bf9\u5916\u90e8\u7c7b\u7684\u4efb\u4f55\u8d4b\u503c\u64cd\u4f5c\u90fd\u9700\u8981\u901a\u8fc7Builder\u5185\u90e8\u7c7b\u6765\u8fdb\u884c\u3002Builder\u4e2d\u6709\u4e00\u4e2a\u6307\u5411\u5916\u90e8 \u7c7b\u7684\u5f15\u7528\uff08\u540d\u4e3aresult\uff09\uff0c\u5f53\u8d4b\u503c\u5b8c\u6210\uff0c\u8c03\u7528Builder\u7684build()\u65b9\u6cd5\u65f6\uff0c\u4f1a\u628a\u8fd9\u4e2a\u5bf9\u8c61\u8fd4\u56de\uff0c\u540c\u65f6\u4f7fresult\u6307\u5411null\u3002\nPB\u901a\u8fc7\u8fd9\u6837\u4e00\u79cd\u65b9\u5f0f\u4fdd\u8bc1\u4e86\u6570\u636e\u5b89\u5168\u6027\uff0c\u4e00\u65e6\u6570\u636e\u6784\u5efa\u5b8c\u6bd5\uff0c\u5c06\u65e0\u6cd5\u518d\u5bf9\u5176\u8fdb\u884c\u4fee\u6539\u3002\n\u62ffPhoneNumber\u8fd9\u4e2a\u7c7b\u6765\u8bf4\uff0c\u5bf9\u6210\u5458\u53d8\u91cfnumber\u3001type\u8d4b\u503c\uff0c\u9700\u8981\u4ee5\u5982\u4e0b\u65b9\u5f0f\u6765\u8fdb\u884c\uff1a\n<div>\n<pre>PhoneNumber.Builder builder = PhoneNumber.newBuilder();\n\n\/\/\u8c03\u7528setter\u8d4b\u503c\uff0csetter\u8fd4\u56de\u4e86this\uff0c\u6240\u4ee5\u53ef\u4ee5\u94fe\u5f0f\u8868\u8ff0\nbuilder.setNumber(\"111\").setType(1);\n\n\/\/\u8d4b\u503c\u5b8c\u6210\u540e\uff0c\u8c03\u7528Builder\u7684build\u65b9\u6cd5\uff0c\u5c06\u8fd4\u56dePhoneNumber\u5bf9\u8c61\nPhoneNumber phoneNumber = builder.build();<\/pre>\n<\/div>\n\u6784\u5efa\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u8c03\u7528writeTo\u65b9\u6cd5\uff0c\u5c06\u6570\u636e\u5199\u5165\u6570\u636e\u6d41\u4e2d\u3002\n<h4>2.3\u00a0 \u53cd\u5e8f\u5217\u5316\u53ca\u5206\u6790<\/h4>\n\u4e00\u884c\u4ee3\u7801\u4fbf\u80fd\u5b8c\u6210\u53cd\u5e8f\u5217\u5316\uff1a\n<div>\n<pre>AddressBook \u00a0list = AddressBook .parseFrom(inputStream\u6216buffer);<\/pre>\n<\/div>\n\u80cc\u540ePB\u505a\u4e86\u5f88\u591a\u4e8b\u60c5\uff1a\n<ol>\n    <li>\u6839\u636einputStream\u6216\u8005buffer\u53bb\u6784\u9020\u4e00\u4e2aCodedInputStream\uff1b<\/li>\n    <li>\u7136\u540e\u4f7f\u7528\u751f\u6210\u4ee3\u7801\u4e2d\u7684mergeFrom\u65b9\u6cd5\uff0c\u53bb\u89e3\u6790\u4e8c\u8fdb\u5236\u6570\u636e\uff1a\n\u9996\u5148\u8c03\u7528CodedInputStream\u7684readTag\uff0c\u4e5f\u5c31\u662f\u4ece\u4e2d\u53d6\u5f97key\u503c\uff08int\u7c7b\u578b\uff09\uff0c\u7136\u540e\u901a\u8fc7swtich\u5757\u6765\u5f80\u5bf9\u8c61\u4e2d\u8d4b\u503c\uff08PB\u91c7\u7528\u4e86Base 128 Varints\u7684\u65b9\u5f0f\u6765\u7f16\u7801\u8fd9\u4e2a\u6570\u5b57\uff0c\u540e\u9762\u4f1a\u4ecb\u7ecd\u8fd9\u79cd\u65b9\u5f0f\u7684\uff09\u3002<\/li>\n    <li>\u5c06\u6570\u636e\u89e3\u6790\u5b8c\u6210\u540e\uff0c\u4f1a\u8c03\u7528build()\u65b9\u6cd5\uff0c\u5c06\u6784\u5efa\u597d\u7684\u5bf9\u8c61\u8fd4\u56de\u3002<\/li>\n<\/ol>\n<h2>3\u00a0 message\u7684\u7f16\u7801\u7279\u70b9<\/h2>\nPB\u4e4b\u6240\u4ee5\u89e3\u6790\u901f\u5ea6\u5feb\u3001\u6240\u5360\u4f53\u79ef\u5c0f\uff0c\u5f88\u5927\u7a0b\u5ea6\u4e0a\u662f\u7531\u5b83\u5e8f\u5217\u5316\u7684\u7f16\u7801\u7279\u70b9\u6765\u51b3\u5b9a\u7684\u3002\n<h3>3.1 Base 128 Varints<\/h3>\nPB\u91c7\u7528\u4e86Base 128 Varints\u6765\u53d8\u957f\u7f16\u7801\u6574\u6570\uff1a\n<ol>\n    <li>\u53d8\u957f\u7f16\u7801\u7684\u6574\u6570\uff0c\u5b83\u53ef\u80fd\u5305\u542b\u591a\u4e2abyte\uff0c\u5bf9\u4e8e\u6bcf\u4e2abyte\u76848\u4f4d\uff0c\u5176\u4e2d\u540e7\u4f4d\u8868\u793a\u6570\u503c\uff0c\u6700\u9ad8\u7684\u4e00\u4f4d\u8868\u793a\u662f\u5426\u8fd8\u6709\u8fd8\u6709\u53e6\u4e00\u4e2abyte\uff0c0\u8868\u793a\u6ca1\u6709\uff0c1\u8868\u793a\u6709\uff1b<\/li>\n    <li>\u8d8a\u524d\u9762\u7684byte\u8868\u793a\u6570\u503c\u7684\u4f4e\u4f4d\uff0c\u8d8a\u540e\u9762\u7684byte\u8868\u793a\u6570\u503c\u7684\u9ad8\u4f4d\uff1b<\/li>\n<\/ol>\n\u4f8b\u5b50\uff1a\n300 \u00a0\u00a0varints\u00a0 \u7f16\u7801\u4e3a\uff1a1010 1100 0000 0010\n\u89e3\u91ca\u5982\u4e0b\uff1a\n300\u76842\u8fdb\u5236\u7f16\u7801\u4e3a\uff1a0001 0010 1100\n\u6309\u7167\u521a\u624d\u7684\u89c4\u5219\uff0c\u9ad8\u4f4e\u4f4d\u98a0\u5012\uff0c\u622a\u53d6\u6700\u540e\u76847\u4e3a\u653e\u5728\u7b2c\u4e00\u4e2abyte\uff0c\u5219\u7b2c\u4e00byte\u4e3a1010 1100\uff08\u5176\u4e2d\u6700\u9ad8\u4f4d1\u8868\u793a\uff0c\u540e\u7eed\u8fd8\u6709byte\uff09\uff1b\u63a5\u7740\u5269\u4e0b\u7684\u5185\u5bb9\u653e\u5230\u7b2c\u4e8c\u4e2abyte\uff0c\u4e3a0000 0010\uff08\u5176\u4e2d\u6700\u9ad8\u4f4d0\u8868\u793a\uff0c\u540e\u7eed\u65e0byte\uff0c\u8fd9\u4e2a\u6570\u5230\u8fd9\u91cc\u622a\u6b62\u4e86\uff09\u3002\n\u4e8e\u662f\uff0c\u5408\u5728\u4e00\u8d77\u4e3a 1010 1100 0000 0010\uff1b\n<h3>3.2 Key-Value<\/h3>\n\u5982\u524d\u6240\u8ff0\uff0cPB\u7684message\u662f\u4e00\u7cfb\u5217\u7684key-value\u5bf9\uff0c\u5728\u4e8c\u8fdb\u5236\u6570\u636e\u4e2d\uff0c\u4f7f\u7528varints\u6570\u5b57\uff08\u5305\u542b\u4e86\u522b\u540d\u4ee5\u53ca\u5c5e\u6027\u7c7b\u578b\u4fe1\u606f\uff09\u6765\u4f5c\u4e3akey\uff0c\u8fdb\u800c\u901a\u8fc7\u7531PB\u7f16\u8bd1\u5668\u751f\u6210\u7684\u4ee3\u7801\u6765\u6784\u9020\u4ee5\u53ca\u89e3\u6790\u6570\u636e\u3002\nPB\u5c06 key\u7f16\u7801\u6210\u4e0b\u9762\u7684\u7ed3\u6784\uff1a\nX YYYY ZZZ\n\u5176\u4e2d\uff1a\u6700\u9ad8\u4f4dX\u8868\u793a\u662f\u5426\u8fd8\u6709\u540e\u7eed\u7684byte\u6765\u7f16\u7801\u6570\u5b57\u522b\u540d\uff1bYYYY\u7528\u4e8e\u7f16\u7801\u522b\u540d\uff0c\u5b9a\u4e49\u4e86\u591a\u4f5916\u4e2a\u5c5e\u6027\uff0c\u5219\u9700\u8981\u7528\u5230\u989d\u5916\u7684byte\uff0c\u6240\u4ee5\u51fa\u73b0\u9891\u7387\u9ad8\u7684\u5b57\u6bb5\u5e94\u5f53\u53d61-16\u7684\u522b\u540d\uff09\uff1bZZZ\u8868\u793a\u8fd9\u4e2a\u5b57\u6bb5\u7684\u7c7b\u578b\uff0cPB\u652f\u6301\u7684\u5c5e\u6027\u7684\u5bf9\u5e94\u89c4\u5219\u5982\u4e0b\u8868\uff1a\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td width=\"54\" valign=\"top\"><strong>Type<\/strong><\/td>\n<td width=\"132\" valign=\"top\"><strong>Meaning<\/strong><\/td>\n<td width=\"287\" valign=\"top\"><strong>Used For<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"54\" valign=\"top\">0<\/td>\n<td width=\"132\" valign=\"top\">Varint<\/td>\n<td width=\"287\" valign=\"top\">int32, int64,   uint32, uint64, sint32,sint64, bool,   enum<\/td>\n<\/tr>\n<tr>\n<td width=\"54\" valign=\"top\">1<\/td>\n<td width=\"132\" valign=\"top\">64-bit<\/td>\n<td width=\"287\" valign=\"top\">fixed64,   sfixed64, double<\/td>\n<\/tr>\n<tr>\n<td width=\"54\" valign=\"top\">2<\/td>\n<td width=\"132\" valign=\"top\">Length-delimited<\/td>\n<td width=\"287\" valign=\"top\">string, bytes,   embedded messages,packed repeated   fields<\/td>\n<\/tr>\n<tr>\n<td width=\"54\" valign=\"top\">3<\/td>\n<td width=\"132\" valign=\"top\">Start group<\/td>\n<td width=\"287\" valign=\"top\">groups   (deprecated)<\/td>\n<\/tr>\n<tr>\n<td width=\"54\" valign=\"top\">4<\/td>\n<td width=\"132\" valign=\"top\">End group<\/td>\n<td width=\"287\" valign=\"top\">groups   (deprecated)<\/td>\n<\/tr>\n<tr>\n<td width=\"54\" valign=\"top\">5<\/td>\n<td width=\"132\" valign=\"top\">32-bit<\/td>\n<td width=\"287\" valign=\"top\">fixed32,   sfixed32, floa<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n\u88682\uff1aPB \u5c5e\u6027\u5bf9\u5e94\u89c4\u5219\n\u4f8b\u5b50\uff1a\nrequired int32 a=1;\u00a0 \u5728\u5e94\u7528\u4e2d\u7ed9a\u8d4b\u503c150\u00a0\u00a0 \uff0c\u5e8f\u5217\u5316\u540e08 96 01\n<ul>\n    <li>08\u4ee3\u8868\u7684\u662fkey 0 0001 000\uff0c \u6700\u9ad8\u4f4d\u4e3a0\uff0c\u8868\u793a\u8fd9\u4e2akey\u4e3a\u4e00\u4e2abyte\uff0c\u4e2d\u95f4\u56db\u4f4d\u8868\u793aa\u7684\u6570\u5b57\u522b\u540d\uff0c\u6700\u540e\u4e09\u4f4d\u8868\u793aa\u7684\u5c5e\u6027\u7c7b\u578b\uff1b<\/li>\n    <li>96 01\u4ee3\u8868\u7684\u662fvalue\uff0c\u4e8c\u8fdb\u5236\u4e3a\uff1a1001 0110 0000 0001\n\u2192 001 0110\u00a0\u00a0\u00a0 000 0001\uff08\u53bb\u6389\u6700\u9ad8\u4f4d\uff09\n\u2192 22\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 +\u00a0 1*2^7 = 150<\/li>\n<\/ul>\n<h3>3.3 Zig-Zag<\/h3>\n\u91c7\u7528varints\u7684\u65b9\u5f0f\u7f16\u7801\u6709\u7b26\u53f7\u7684\u6574\u6570\uff0c\u6548\u7387\u6bd4\u8f83\u5dee\uff0c\u56e0\u4e3a\u8d1f\u6570\u7684\u6700\u9ad8\u4f4d\u662f1\uff0c\u8fd9\u6837\u5c31\u5bfc\u81f4\u4e86\u60c5\u51b5\u7c7b\u4f3c\u4e8e\u7f16\u7801\u4e00\u4e2a\u5f88\u5927\u7684\u6570\u3002\n\n\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0cProtocol Buffers\u5b9a\u4e49\u4e86sint32\/sint64\u5c5e\u6027\uff0c\u4ed6\u4eec\u91c7\u7528\u4e86\u201c\u4e4b\u5b57\u5f62\u201d\uff08ZigZag\uff09\u7f16\u7801\u7684\u65b9\u5f0f\uff0c\u5c06\u8d1f\u6570\u7f16\u7801\u6210\u6b63\u6570\uff0c\u4ea4\u66ff\u8fdb\u884c\u3002\u770b\u4e86\u4e0b\u8868\u5c31\u5f88\u597d\u7406\u89e3\u4e86\uff1a\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td width=\"187\" valign=\"top\"><strong>Signed Original<\/strong><\/td>\n<td width=\"198\" valign=\"top\"><strong>Encoded As<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"187\" valign=\"top\">0<\/td>\n<td width=\"198\" valign=\"top\">0<\/td>\n<\/tr>\n<tr>\n<td width=\"187\" valign=\"top\">-1<\/td>\n<td width=\"198\" valign=\"top\">1<\/td>\n<\/tr>\n<tr>\n<td width=\"187\" valign=\"top\">1<\/td>\n<td width=\"198\" valign=\"top\">2<\/td>\n<\/tr>\n<tr>\n<td width=\"187\" valign=\"top\">-2<\/td>\n<td width=\"198\" valign=\"top\">3<\/td>\n<\/tr>\n<tr>\n<td width=\"187\" valign=\"top\">2147483647<\/td>\n<td width=\"198\" valign=\"top\">4294967294<\/td>\n<\/tr>\n<tr>\n<td width=\"187\" valign=\"top\">2147483648<\/td>\n<td width=\"198\" valign=\"top\">4294967295<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n\u88683\uff1aZig-Zag\u7f16\u7801\u89c4\u5219\n\u5229\u7528\u8fd9\u4e2a\u65b9\u5f0f\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u8282\u7701\u5b58\u50a8\u7a7a\u95f4\uff0c\u4e5f\u80fd\u63d0\u9ad8\u89e3\u6790\u6548\u7387\u3002\n\n\u4e86\u89e3\u4e86\u4ee5\u4e0a\u5185\u5bb9\uff0c\u5bf9\u4e8e\u5176\u4ed6\u6570\u636e\u7c7b\u578b\u7684\u7f16\u7801\uff0c\u4e5f\u662f\u5f88\u597d\u7406\u89e3\u7684\uff0c\u5927\u5bb6\u53ef\u4ee5\u53c2\u8003\u5b98\u65b9\u6587\u6863\uff0c\u8fd9\u91cc\u4e0d\u505a\u8be6\u8ff0\u3002\n<h2>4 \u5176\u4ed6<\/h2>\n\u5b98\u65b9\u6587\u6863\u4e2d\uff0c\u6709\u63d0\u5230PB\u63d0\u4f9b\u4e86RPC\u7684\u63a5\u53e3\uff0c\u4f46\u662f\u6ca1\u6709\u63d0\u4f9b\u5177\u4f53\u5b9e\u73b0\u3002\u5f53\u5728\u7684.proto\u6587\u4ef6\u4e2d\uff0c\u52a0\u5165\u5982\u4e0b\u5b9a\u4e49\uff1a\n<div>\n<pre>service XXX {\n    rpc MMM(request) returns(response);\n}<\/pre>\n<\/div>\nPB\u4fbf\u4f1a\u4e3a\u4f60\u751f\u6210\u4e00\u4e2a\u4ee3\u8868\u8fd9\u4e2a\u670d\u52a1\u7684XXX\u865a\u7c7b\uff0c\u901a\u8fc7\u5b9e\u73b0\u8fd9\u4e2a\u7c7b\u4e2d\u7684abstract MMM\u65b9\u6cd5\uff0c\u4ee5\u53ca\u63d0\u4f9bRpcChannel\u7684\u5b9e\u73b0\uff0c\u4f60\u4fbf\u53ef\u4ee5\u5229\u7528Protocol Buffers\u5b9e\u73b0\u4f60\u7684RPC\u4e86\u3002\n\n\u7b2c\u4e09\u65b9\u7684RPC\u5b9e\u73b0\u5927\u5bb6\u53ef\u4ee5\u53c2\u8003<a href=\"http:\/\/code.google.com\/p\/protobuf\/wiki\/ThirdPartyAddOns\">ThirdPartyRPC<\/a>\n\n\u5728\u8fd9\u91cc\uff0c\u6211\u5229\u7528\u4e86\u7b2c\u4e09\u65b9\u5b9e\u73b0<a href=\"http:\/\/code.google.com\/p\/protobuf-socket-rpc\/\">protobuf-socket-rpc<\/a>\uff0c\u5199\u4e86\u4e00\u4e2a\u5c0f\u4f8b\u5b50\uff0c\u6709\u5174\u8da3\u7684\u53ef\u4ee5\u770b\u770b\u3002\u5982\u4e0b\uff1a<a title=\"Protocol buffer\u7684rpc\u4f8b\u5b50\" href=\"http:\/\/www.searchtb.com\/wp-content\/uploads\/2010\/11\/rpc0.rar\">Protocol buffer\u7684rpc\u4f8b\u5b50<\/a>\n<h2>5 \u5c0f\u7ed3<\/h2>\nPB\u5177\u6709\u8de8\u5e73\u53f0\u3001\u89e3\u6790\u901f\u5ea6\u5feb\u3001\u5e8f\u5217\u5316\u6570\u636e\u4f53\u79ef\u5c0f\u3001\u6269\u5c55\u6027\u9ad8\u3001\u4f7f\u7528\u7b80\u5355\u7684\u7279\u70b9\u3002\u4f46\u662f\u6211\u4eec\u4e5f\u53ef\u4ee5\u770b\u5230\uff0c\u76f8\u6bd4\u4e8eXML\uff0cPB\u7684\u6570\u636e\uff0c\u5e76\u4e0d\u662f\u81ea\u7136\u53ef\u8bfb\u7684\uff1b\u540c\u65f6\u5b83\u751f\u6210\u7684\u4ee3\u7801\u4e0d\u662f\u7eafpojo\uff0c\u5bf9\u4e8e\u4ee3\u7801\u6709\u4e00\u5b9a\u7684\u4fb5\u5165\u6027\u3002\u5728\u4f60\u7684\u9879\u76ee\u4e2d\uff0c\u5982\u679c\u5bf9\u4e8e\u4ee5\u4e0a\u7f3a\u70b9\u8981\u6c42\u5e76\u4e0d\u9ad8\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u7740\u4f7f\u7528PB\u3002\n\n<\/div>","protected":false},"excerpt":{"rendered":"<p>1\u00a0 Protocol Buffers\u7684\u4ecb\u7ecd Protocol Buffers\u662f\u4e00\u79cd\u7528\u4e8e\u5e8f\u5217\u5316\u7ed3\u6784\u5316\u6570\u636e\u7684\u673a\u5236\uff0c\u5b83\u5177\u6709\u7075\u6d3b\u3001\u9ad8\u6548\u3001\u81ea\u52a8\u5316\u7684\u7279\u70b9\u3002\u7c7b\u4f3c\u4e8eXML\uff0c\u4f46\u662f\u6bd4XML\u66f4\u5c0f\u5de7\u3001\u5feb\u6377\u3001\u7b80\u5355\u3002\u5728Goog&#46;&#46;&#46;<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[272,273],"class_list":["post-450","post","type-post","status-publish","format-standard","hentry","category-linux","tag-pb","tag-protocol-buffers"],"_links":{"self":[{"href":"https:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts\/450","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/comments?post=450"}],"version-history":[{"count":3,"href":"https:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts\/450\/revisions"}],"predecessor-version":[{"id":452,"href":"https:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts\/450\/revisions\/452"}],"wp:attachment":[{"href":"https:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/media?parent=450"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/categories?post=450"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/tags?post=450"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}