# 第åç« ï¼ç¼ç ä¸æ¼å
![](img/ch4.png)
> å¯åæé
>
> ââ 以å¼æç赫æå
å©ç¹ï¼ä¸ºææå¾æå¼ï¼å
Œ
å 360 å¹´ï¼
>
-------------------
[TOC]
åºç¨ç¨åºä¸å¯é¿å
å°éæ¶é´èååãæ°äº§åçæ¨åºï¼å¯¹éæ±çæ·±å
¥ç解ï¼æè
åä¸ç¯å¢çååï¼æ»ä¼ä¼´éç **åè½ï¼featureï¼** çå¢å¢æ¹æ¹ã[第ä¸ç« ](ch1.md) ä»ç»äº **å¯æ¼åæ§ï¼evolvabilityï¼** çæ¦å¿µï¼åºè¯¥å°½åæ建è½çµæ´»éåºååçç³»ç»ï¼è¯·åé
â[å¯æ¼åæ§ï¼æ¥æ±åå](ch1.md#å¯æ¼åæ§ï¼æ¥æ±åå)âï¼ã
å¨å¤§å¤æ°æ
åµä¸ï¼ä¿®æ¹åºç¨ç¨åºçåè½ä¹æå³çéè¦æ´æ¹å
¶åå¨çæ°æ®ï¼å¯è½éè¦ä½¿ç¨æ°çå段æè®°å½ç±»åï¼æè
以æ°æ¹å¼å±ç¤ºç°ææ°æ®ã
æä»¬å¨ [第äºç« ](ch2.md) 讨论çæ°æ®æ¨¡åæä¸åçæ¹æ³æ¥åºå¯¹è¿ç§ååãå
³ç³»æ°æ®åºé常åå®æ°æ®åºä¸çæææ°æ®é½éµå¾ªä¸ä¸ªæ¨¡å¼ï¼å°½ç®¡å¯ä»¥æ´æ¹è¯¥æ¨¡å¼ï¼éè¿æ¨¡å¼è¿ç§»ï¼å³ `ALTER` è¯å¥ï¼ï¼ä½æ¯å¨ä»»ä½æ¶é´ç¹é½æä¸ä»
æä¸ä¸ªæ£ç¡®ç模å¼ãç¸æ¯ä¹ä¸ï¼**读æ¶æ¨¡å¼**ï¼schema-on-readï¼æ **æ 模å¼**ï¼å³ schemalessï¼æ°æ®åºä¸ä¼å¼ºå¶ä¸ä¸ªæ¨¡å¼ï¼å æ¤æ°æ®åºå¯ä»¥å
å«å¨ä¸åæ¶é´åå
¥çæ°èæ°æ®æ ¼å¼çæ··åï¼è¯·åé
â[æ档模åä¸ç模å¼çµæ´»æ§](ch2.md#æ档模åä¸ç模å¼çµæ´»æ§)â ï¼ã
å½æ°æ® **æ ¼å¼ï¼formatï¼** æ **模å¼ï¼schemaï¼** åçååæ¶ï¼é常éè¦å¯¹åºç¨ç¨åºä»£ç è¿è¡ç¸åºçæ´æ¹ï¼ä¾å¦ï¼ä¸ºè®°å½æ·»å æ°å段ï¼ç¶åä¿®æ¹ç¨åºå¼å§è¯»å该å段ï¼ãä½å¨å¤§ååºç¨ç¨åºä¸ï¼ä»£ç åæ´é常ä¸ä¼ç«å³å®æï¼
* å¯¹äº **æå¡ç«¯ï¼server-sideï¼** åºç¨ç¨åºï¼å¯è½éè¦æ§è¡ **æ»å¨å级 ï¼rolling upgradeï¼** ï¼ä¹ç§°ä¸º **é¶æ®µåå¸ï¼staged rolloutï¼** ï¼ï¼ä¸æ¬¡å°æ°çæ¬é¨ç½²å°å°æ°å 个èç¹ï¼æ£æ¥æ°çæ¬æ¯å¦è¿è¡æ£å¸¸ï¼ç¶åéæ¸é¨å®ææçèç¹ãè¿æ ·æ éä¸ææå¡å³å¯é¨ç½²æ°çæ¬ï¼ä¸ºé¢ç¹åå¸æä¾äºå¯è¡æ§ï¼ä»è带æ¥æ´å¥½çå¯æ¼åæ§ã
* å¯¹äº **客æ·ç«¯ï¼client-sideï¼** åºç¨ç¨åºï¼åä¸å级就è¦çç¨æ·çå¿æ
äºãç¨æ·å¯è½ç¸å½é¿ä¸æ®µæ¶é´éé½ä¸ä¼å»å级软件ã
è¿æå³çï¼æ°æ§çæ¬ç代ç ï¼ä»¥åæ°æ§æ°æ®æ ¼å¼å¯è½ä¼å¨ç³»ç»ä¸åæ¶å
±å¤ãç³»ç»æ³è¦ç»§ç»é¡ºå©è¿è¡ï¼å°±éè¦ä¿æ **ååå
¼å®¹æ§**ï¼
* ååå
¼å®¹ (backward compatibility)
æ°ä»£ç å¯ä»¥è¯»æ§æ°æ®ã
* ååå
¼å®¹ (forward compatibility)
æ§ä»£ç å¯ä»¥è¯»æ°æ°æ®ã
ååå
¼å®¹æ§é常并ä¸é¾å®ç°ï¼æ°ä»£ç çä½è
å½ç¶ç¥éç±æ§ä»£ç 使ç¨çæ°æ®æ ¼å¼ï¼å æ¤å¯ä»¥æ¾ç¤ºå°å¤çå®ï¼æç®åçåæ³æ¯ï¼ä¿çæ§ä»£ç å³å¯è¯»åæ§æ°æ®ï¼ã
ååå
¼å®¹æ§å¯è½ä¼æ´æ£æï¼å 为æ§ççç¨åºéè¦å¿½ç¥æ°çæ°æ®æ ¼å¼ä¸æ°å¢çé¨åã
æ¬ç« ä¸å°ä»ç»å ç§ç¼ç æ°æ®çæ ¼å¼ï¼å
æ¬ JSONãXMLãProtocol BuffersãThrift å Avroãå°¤å
¶å°å
³æ³¨è¿äºæ ¼å¼å¦ä½åºå¯¹æ¨¡å¼ååï¼ä»¥åå®ä»¬å¦ä½å¯¹æ°æ§ä»£ç æ°æ®éè¦å
±åçç³»ç»æä¾æ¯æãç¶åå°è®¨è®ºå¦ä½ä½¿ç¨è¿äºæ ¼å¼è¿è¡æ°æ®åå¨åéä¿¡ï¼å¨ Web æå¡ä¸ï¼**表述æ§ç¶æä¼ éï¼RESTï¼** å **è¿ç¨è¿ç¨è°ç¨ï¼RPCï¼**ï¼ä»¥å **æ¶æ¯ä¼ éç³»ç»**ï¼å¦ Actor åæ¶æ¯éåï¼ã
## ç¼ç æ°æ®çæ ¼å¼
ç¨åºé常ï¼è³å°ï¼ä½¿ç¨ä¸¤ç§å½¢å¼çæ°æ®ï¼
1. å¨å
åä¸ï¼æ°æ®ä¿åå¨å¯¹è±¡ãç»æä½ãå表ãæ°ç»ãæ£å表ãæ çä¸ã è¿äºæ°æ®ç»æé对 CPU çé«æ访é®åæä½è¿è¡äºä¼åï¼é常使ç¨æéï¼ã
2. å¦æè¦å°æ°æ®åå
¥æ件ï¼æéè¿ç½ç»åéï¼åå¿
é¡»å°å
¶ **ç¼ç ï¼encodeï¼** 为æç§èªå
å«çåèåºåï¼ä¾å¦ï¼JSON ææ¡£ï¼ã ç±äºæ¯ä¸ªè¿ç¨é½æèªå·±ç¬ç«çå°å空é´ï¼ä¸ä¸ªè¿ç¨ä¸çæé对任ä½å
¶ä»è¿ç¨é½æ²¡ææä¹ï¼æ以è¿ä¸ªåèåºå表示ä¼ä¸é常å¨å
åä¸ä½¿ç¨çæ°æ®ç»æå®å
¨ä¸å [^i]ã
[^i]: é¤ä¸äºç¹æ®æ
åµå¤ï¼ä¾å¦æäºå
åæ å°æ件æç´æ¥å¨å缩æ°æ®ä¸æä½ï¼å¦ â[åå缩](ch4.md#åå缩)â ä¸æè¿°ï¼ã
æ以ï¼éè¦å¨ä¸¤ç§è¡¨ç¤ºä¹é´è¿è¡æç§ç±»åçç¿»è¯ã ä»å
åä¸è¡¨ç¤ºå°åèåºåç转æ¢ç§°ä¸º **ç¼ç ï¼Encodingï¼** ï¼ä¹ç§°ä¸º **åºååï¼serializationï¼** æ **ç¼ç»ï¼marshallingï¼**ï¼ï¼åè¿æ¥ç§°ä¸º **解ç ï¼Decodingï¼**[^ii]ï¼**解æï¼Parsingï¼**ï¼**ååºååï¼deserializationï¼**ï¼**åç¼ç» (unmarshallingï¼**ï¼[^è¯i]ã
[^ii]: 请注æï¼**ç¼ç ï¼encodeï¼** ä¸ **å å¯ï¼encryptionï¼** æ å
³ã æ¬ä¹¦ä¸è®¨è®ºå å¯ã
[^è¯i]: Marshal ä¸ Serialization çåºå«ï¼Marshal ä¸ä»
ä¼ è¾å¯¹è±¡çç¶æï¼èä¸ä¼ä¸èµ·ä¼ è¾å¯¹è±¡çæ¹æ³ï¼ç¸å
³ä»£ç ï¼ã
> #### æ¯è¯å²çª
> ä¸å¹¸çæ¯ï¼å¨ [第ä¸ç« ](ch7.md)ï¼ **äºå¡ï¼Transactionï¼** çä¸ä¸æéï¼**åºååï¼Serializationï¼** è¿ä¸ªæ¯è¯ä¹åºç°äºï¼èä¸å
·æå®å
¨ä¸åçå«ä¹ã尽管åºååå¯è½æ¯æ´å¸¸è§çæ¯è¯ï¼ä¸ºäºé¿å
æ¯è¯éè½½ï¼æ¬ä¹¦ä¸åæä½¿ç¨ **ç¼ç ï¼Encodingï¼** 表达æ¤å«ä¹ã
è¿æ¯ä¸ä¸ªå¸¸è§çé®é¢ï¼å èæ许å¤åºåç¼ç æ ¼å¼å¯ä¾éæ©ã é¦å
让æ们æ¦è§ä¸ä¸ã
### è¯è¨ç¹å®çæ ¼å¼
许å¤ç¼ç¨è¯è¨é½å
建äºå°å
å对象ç¼ç 为åèåºåçæ¯æãä¾å¦ï¼Java æ `java.io.Serializable` ã1ãï¼Ruby æ `Marshal`ã2ãï¼Python æ `pickle`ã3ãççã许å¤ç¬¬ä¸æ¹åºä¹åå¨ï¼ä¾å¦ `Kryo for Java` ã4ãã
è¿äºç¼ç åºé常æ¹ä¾¿ï¼å¯ä»¥ç¨å¾å°çé¢å¤ä»£ç å®ç°å
å对象çä¿åä¸æ¢å¤ãä½æ¯å®ä»¬ä¹æä¸äºæ·±å±æ¬¡çé®é¢ï¼
* è¿ç±»ç¼ç é常ä¸ç¹å®çç¼ç¨è¯è¨æ·±åº¦ç»å®ï¼å
¶ä»è¯è¨å¾é¾è¯»åè¿ç§æ°æ®ãå¦æ以è¿ç±»ç¼ç åå¨æä¼ è¾æ°æ®ï¼é£ä½ å°±åè¿é¨è¯è¨ç»æ»å¨ä¸èµ·äºã并ä¸å¾é¾å°ç³»ç»ä¸å
¶ä»ç»ç»çç³»ç»ï¼å¯è½ç¨çæ¯ä¸åçè¯è¨ï¼è¿è¡éæã
* 为äºæ¢å¤ç¸å对象类åçæ°æ®ï¼è§£ç è¿ç¨éè¦ **å®ä¾åä»»æç±»** çè½åï¼è¿é常æ¯å®å
¨é®é¢çä¸ä¸ªæ¥æºã5ãï¼å¦ææ»å»è
å¯ä»¥è®©åºç¨ç¨åºè§£ç ä»»æçåèåºåï¼ä»ä»¬å°±è½å®ä¾åä»»æçç±»ï¼è¿ä¼å
许ä»ä»¬åå¯æçäºæ
ï¼å¦è¿ç¨æ§è¡ä»»æ代ç ã6,7ãã
* å¨è¿äºåºä¸ï¼æ°æ®çæ¬æ§å¶é常æ¯äºåæèèçãå 为å®ä»¬æ¨å¨å¿«éç®ä¾¿å°å¯¹æ°æ®è¿è¡ç¼ç ï¼æ以å¾å¾å¿½ç¥äºååååå
¼å®¹æ§å¸¦æ¥ç麻ç¦é®é¢ã
* æçï¼ç¼ç æ解ç æè±è´¹ç CPU æ¶é´ï¼ä»¥åç¼ç ç»æç大å°ï¼å¾å¾ä¹æ¯äºåæèèçã ä¾å¦ï¼Java çå
ç½®åºååç±äºå
¶ç³ç³çæ§è½åèè¿çç¼ç èèåæèã8ãã
å æ¤ï¼é¤é临æ¶ä½¿ç¨ï¼éç¨è¯è¨å
ç½®ç¼ç é常æ¯ä¸ä¸ªå主æã
### JSONãXMLåäºè¿å¶åä½
å½æ们è°å°å¯ä»¥è¢«å¤ç§ç¼ç¨è¯è¨è¯»åçæ åç¼ç æ¶ï¼JSON å XML æ¯ææ¾ç¼çè§éè
ãå®ä»¬å¹¿ä¸ºäººç¥ï¼å¹¿åæ¯æï¼ä¹ â广åææ¶âã XML ç»å¸¸æ¶å°æ¹è¯ï¼è¿äºåé¿ä¸ä¸è¿ä»½å¤æã9ãã JSON çæµè¡å主è¦æºäºï¼éè¿æ为 JavaScript çä¸ä¸ªåéï¼Web æµè§å¨çå
ç½®æ¯æï¼ä»¥åç¸å¯¹äº XML çç®åæ§ã CSV æ¯å¦ä¸ç§æµè¡çä¸è¯è¨æ å
³çæ ¼å¼ï¼å°½ç®¡å
¶åè½ç¸å¯¹è¾å¼±ã
JSONï¼XML å CSV å±äºææ¬æ ¼å¼ï¼å æ¤å
·æ人类å¯è¯»æ§ï¼å°½ç®¡å®ä»¬çè¯æ³æ¯ä¸ä¸ªçé¨äºè®®è¯é¢ï¼ãé¤äºè¡¨é¢çè¯æ³é®é¢ä¹å¤ï¼å®ä»¬ä¹åå¨ä¸äºå¾®å¦çé®é¢ï¼
* **æ°å¼ï¼numbersï¼** çç¼ç å¤ææ§ä¹ä¹å¤ãXML å CSV ä¸è½åºåæ°ååå符串ï¼é¤éå¼ç¨ä¸ä¸ªå¤é¨æ¨¡å¼ï¼ã JSON è½ç¶åºåå符串ä¸æ°å¼ï¼ä½ä¸åºåæ´æ°åæµ®ç¹æ°ï¼èä¸ä¸è½æå®ç²¾åº¦ã
* å½å¤çæ´å¤§çæ°å¼æ¶ï¼è¿ä¸ªé®é¢æ¾å¾å°¤ä¸ºä¸¥éãä¾å¦å¤§äº $2^{53}$ çæ´æ°æ æ³ä½¿ç¨ IEEE 754 å精度浮ç¹æ°ç²¾ç¡®è¡¨ç¤ºï¼å æ¤å¨ä½¿ç¨æµ®ç¹æ°ï¼ä¾å¦ JavaScriptï¼çè¯è¨è¿è¡åææ¶ï¼è¿äºæ°åä¼åå¾ä¸åç¡®ã Twitter æä¸ä¸ªå
³äºå¤§äº $2^{53}$ çæ°åçä¾åï¼å®ä½¿ç¨ 64 ä½æ´æ°æ¥æ è¯æ¯æ¡æ¨æã Twitter API è¿åç JSON å
å«äºä¸¤ç§æ¨ç¹ IDï¼ä¸ç§æ¯ JSON æ°å¼ï¼å¦ä¸ç§æ¯åè¿å¶å符串ï¼ä»¥é¿å
JavaScript ç¨åºæ æ³æ£ç¡®è§£ææ°åçé®é¢ã10ãã
* JSON å XML 对 Unicode å符串ï¼å³äººç±»å¯è¯»çææ¬ï¼æå¾å¥½çæ¯æï¼ä½æ¯å®ä»¬ä¸æ¯æäºè¿å¶æ°æ®ï¼å³ä¸å¸¦ **å符ç¼ç (character encoding)** çåèåºåï¼ãäºè¿å¶ä¸²æ¯å¾æç¨çåè½ï¼äººä»¬éè¿ä½¿ç¨ Base64 å°äºè¿å¶æ°æ®ç¼ç 为ææ¬æ¥ç»è¿æ¤éå¶ãå
¶ç¹æç模å¼æ è¯çè¿ä¸ªå¼åºå½è¢«è§£é为 Base64 ç¼ç çäºè¿å¶æ°æ®ãè¿ç§æ¹æ¡è½ç¶ç®¡ç¨ï¼ä½æ¯è¾ Hackyï¼å¹¶ä¸ä¼å¢å ä¸åä¹ä¸çæ°æ®å¤§å°ã
* XML ã11ãå JSON ã12ãé½æå¯éç模å¼æ¯æãè¿äºæ¨¡å¼è¯è¨ç¸å½å¼ºå¤§ï¼æ以å¦ä¹ åå®ç°èµ·æ¥é½ç¸å½å¤æã XML 模å¼ç使ç¨ç¸å½æ®éï¼ä½è®¸å¤åºäº JSON çå·¥å
·æä¸ä¼å»æè
¾æ¨¡å¼ã对æ°æ®çæ£ç¡®è§£è¯»ï¼ä¾å¦åºåæ°å¼ä¸äºè¿å¶ä¸²ï¼åå³äºæ¨¡å¼ä¸çä¿¡æ¯ï¼å æ¤ä¸ä½¿ç¨ XML/JSON 模å¼çåºç¨ç¨åºå¯è½éè¦å¯¹ç¸åºçç¼ç / 解ç é»è¾è¿è¡ç¡¬ç¼ç ã
* CSV 没æä»»ä½æ¨¡å¼ï¼å æ¤æ¯è¡åæ¯åçå«ä¹å®å
¨ç±åºç¨ç¨åºèªè¡å®ä¹ãå¦æåºç¨ç¨åºåæ´æ·»å äºæ°çè¡æåï¼é£ä¹è¿ç§åæ´å¿
é¡»éè¿æå·¥å¤çã CSV ä¹æ¯ä¸ä¸ªç¸å½æ¨¡ç³çæ ¼å¼ï¼å¦æä¸ä¸ªå¼å
å«éå·ææ¢è¡ç¬¦ï¼ä¼åçä»ä¹ï¼ï¼ã尽管å
¶è½¬ä¹è§åå·²ç»è¢«æ£å¼æå®ã13ãï¼ä½å¹¶ä¸æ¯ææç解æå¨é½æ£ç¡®çå®ç°äºæ åã
尽管åå¨è¿äºç¼ºé·ï¼ä½ JSONãXML å CSV 对å¾å¤éæ±æ¥è¯´å·²ç»è¶³å¤å¥½äºãå®ä»¬å¾å¯è½ä¼ç»§ç»æµè¡ä¸å»ï¼ç¹å«æ¯ä½ä¸ºæ°æ®äº¤æ¢æ ¼å¼æ¥è¯´ï¼å³å°æ°æ®ä»ä¸ä¸ªç»ç»åéå°å¦ä¸ä¸ªç»ç»ï¼ãå¨è¿ç§æ
åµä¸ï¼åªè¦äººä»¬å¯¹æ ¼å¼æ¯ä»ä¹æè§ä¸è´ï¼æ ¼å¼æå¤ç¾è§æè
æçæå¤é«æå°±æ æè°äºã让ä¸åçç»ç»å°±è¿äºä¸è¥¿è¾¾æä¸è´çé¾åº¦è¶
è¿äºç»å¤§å¤æ°é®é¢ã
#### äºè¿å¶ç¼ç
对äºä»
å¨ç»ç»å
é¨ä½¿ç¨çæ°æ®ï¼ä½¿ç¨æå°å
¬çº¦æ°å¼çç¼ç æ ¼å¼ååè¾å°ãä¾å¦ï¼å¯ä»¥éæ©æ´ç´§åææ´å¿«ç解ææ ¼å¼ãè½ç¶å¯¹å°æ°æ®éæ¥è¯´ï¼æ¶çå¯ä»¥å¿½ç¥ä¸è®¡ï¼ä½ä¸æ¦è¾¾å° TB 级å«ï¼æ°æ®æ ¼å¼çéåå°±ä¼äº§ç巨大çå½±åã
JSON æ¯ XML ç®æ´ï¼ä½ä¸äºè¿å¶æ ¼å¼ç¸æ¯è¿æ¯å¤ªå 空é´ãè¿ä¸äºå®å¯¼è´å¤§éäºè¿å¶ç¼ç çæ¬ JSONï¼MessagePackãBSONãBJSONãUBJSONãBISON å Smile çï¼ å XMLï¼ä¾å¦ WBXML å Fast Infosetï¼çåºç°ãè¿äºæ ¼å¼å·²ç»å¨åç§åæ ·çé¢åä¸éç¨ï¼ä½æ¯æ²¡æä¸ä¸ªè½åææ¬ç JSON å XML é£æ ·è¢«å¹¿æ³éç¨ã
è¿äºæ ¼å¼ä¸çä¸äºæ©å±äºä¸ç»æ°æ®ç±»åï¼ä¾å¦ï¼åºåæ´æ°åæµ®ç¹æ°ï¼æè
å¢å 对äºè¿å¶å符串çæ¯æï¼ï¼å¦ä¸æ¹é¢ï¼å®ä»¬æ²¡ææ¹å JSON / XML çæ°æ®æ¨¡åãç¹å«æ¯ç±äºå®ä»¬æ²¡æè§å®æ¨¡å¼ï¼æ以å®ä»¬éè¦å¨ç¼ç æ°æ®ä¸å
å«ææç对象å段å称ãä¹å°±æ¯è¯´ï¼å¨ [ä¾ 4-1]() ä¸ç JSON ææ¡£çäºè¿å¶ç¼ç ä¸ï¼éè¦å¨æå¤å
å«å符串 `userName`ï¼`favoriteNumber` å `interests`ã
**ä¾ 4-1 æ¬ç« ä¸ç¨äºå±ç¤ºäºè¿å¶ç¼ç ç示ä¾è®°å½**
```json
{
"userName": "Martin",
"favoriteNumber": 1337,
"interests": ["daydreaming", "hacking"]
}
```
æ们æ¥çä¸ä¸ª MessagePack çä¾åï¼å®æ¯ä¸ä¸ª JSON çäºè¿å¶ç¼ç ãå¾ 4-1 æ¾ç¤ºäºå¦æä½¿ç¨ MessagePack ã14ã对 [ä¾ 4-1]() ä¸ç JSON ææ¡£è¿è¡ç¼ç ï¼åå¾å°çåèåºåãåå 个åèå¦ä¸ï¼
1. 第ä¸ä¸ªåè `0x83` 表示æ¥ä¸æ¥æ¯ **3** 个å段ï¼ä½åä½ = `0x03`ï¼ç **对象 object**ï¼é«åä½ = `0x80`ï¼ã ï¼å¦ææ³ç¥éå¦æä¸ä¸ªå¯¹è±¡æ 15 个以ä¸çå段ä¼åçä»ä¹æ
åµï¼å段çæ°éå¡ä¸è¿ 4 个 bit éï¼é£ä¹å®ä¼ç¨å¦ä¸ä¸ªä¸åçç±»åæ è¯ç¬¦ï¼å段çæ°é被ç¼ç 两个æå个åèï¼ã
2. 第äºä¸ªåè `0xa8` 表示æ¥ä¸æ¥æ¯ **8** åèé¿ï¼ä½åä½ = `0x08`ï¼çå符串ï¼é«åä½ = `0x0a`ï¼ã
3. æ¥ä¸æ¥å
«ä¸ªåèæ¯ ASCII å符串形å¼çå段å称 `userName`ãç±äºä¹åå·²ç»ææé¿åº¦ï¼ä¸éè¦ä»»ä½æ è®°æ¥æ è¯å符串çç»æä½ç½®ï¼æè
ä»»ä½è½¬ä¹ï¼ã
4. æ¥ä¸æ¥çä¸ä¸ªåè对åç¼ä¸º `0xa6` çå
个åæ¯çåç¬¦ä¸²å¼ `Martin` è¿è¡ç¼ç ï¼ä¾æ¤ç±»æ¨ã
äºè¿å¶ç¼ç é¿åº¦ä¸º 66 个åèï¼ä»
ç¥å°äºææ¬ JSON ç¼ç æåç 81 个åèï¼å é¤äºç©ºç½ï¼ãææç JSON çäºè¿å¶ç¼ç å¨è¿æ¹é¢æ¯ç¸ä¼¼çã空é´èçäºä¸ä¸ç¹ï¼ä»¥å解æå éï¼æ¯å¦è½å¼¥è¡¥å¯è¯»æ§çæ失ï¼è°ä¹è¯´ä¸åã
å¨ä¸é¢çç« èä¸ï¼è½è¾¾å°æ¯è¿å¥½å¾å¤çç»æï¼åªç¨ 32 个åè对ç¸åçè®°å½è¿è¡ç¼ç ã
![](img/fig4-1.png)
**å¾ 4-1 ä½¿ç¨ MessagePack ç¼ç çè®°å½ï¼ä¾ 4-1ï¼**
### Thriftä¸Protocol Buffers
Apache Thrift ã15ãå Protocol Buffersï¼protobufï¼ã16ãæ¯åºäºç¸ååççäºè¿å¶ç¼ç åºã Protocol Buffers æåæ¯å¨ Google å¼åçï¼Thrift æåæ¯å¨ Facebook å¼åçï¼å¹¶ä¸é½æ¯å¨ 2007~2008 å¼æºçã17ãã
Thrift å Protocol Buffers é½éè¦ä¸ä¸ªæ¨¡å¼æ¥ç¼ç ä»»ä½æ°æ®ãè¦å¨ Thrift ç [ä¾ 4-1]() ä¸å¯¹æ°æ®è¿è¡ç¼ç ï¼å¯ä»¥ä½¿ç¨ Thrift **æ¥å£å®ä¹è¯è¨ï¼IDLï¼** æ¥æ述模å¼ï¼å¦ä¸æ示ï¼
```c
struct Person {
1: required string userName,
2: optional i64 favoriteNumber,
3: optional list interests
}
```
Protocol Buffers ççæ模å¼å®ä¹çèµ·æ¥é常ç¸ä¼¼ï¼
```protobuf
message Person {
required string user_name = 1;
optional int64 favorite_number = 2;
repeated string interests = 3;
}
```
Thrift å Protocol Buffers æ¯ä¸ä¸ªé½å¸¦æä¸ä¸ªä»£ç çæå·¥å
·ï¼å®éç¨äºç±»ä¼¼äºè¿éæ示ç模å¼å®ä¹ï¼å¹¶ä¸çæäºä»¥åç§ç¼ç¨è¯è¨å®ç°æ¨¡å¼çç±»ã18ããä½ çåºç¨ç¨åºä»£ç å¯ä»¥è°ç¨æ¤çæç代ç æ¥å¯¹æ¨¡å¼çè®°å½è¿è¡ç¼ç æ解ç ã
ç¨è¿ä¸ªæ¨¡å¼ç¼ç çæ°æ®æ¯ä»ä¹æ ·çï¼ä»¤äººå°æçæ¯ï¼Thrift æ两ç§ä¸åçäºè¿å¶ç¼ç æ ¼å¼ [^iii]ï¼åå«ç§°ä¸º BinaryProtocol å CompactProtocolãå
æ¥çç BinaryProtocolã使ç¨è¿ç§æ ¼å¼çç¼ç æ¥ç¼ç [ä¾ 4-1]() ä¸çæ¶æ¯åªéè¦ 59 个åèï¼å¦ [å¾ 4-2](img/fig4-2.png) æ示ã19ãã
![](img/fig4-2.png)
**å¾ 4-2 ä½¿ç¨ Thrift äºè¿å¶åè®®ç¼ç çè®°å½**
[^iii]: å®é
ä¸ï¼Thrift æä¸ç§äºè¿å¶åè®®ï¼BinaryProtocolãCompactProtocol å DenseProtocolï¼å°½ç®¡ DenseProtocol åªæ¯æ C ++ å®ç°ï¼æ以ä¸ç®ä½è·¨è¯è¨ã18ãã é¤æ¤ä¹å¤ï¼å®è¿æ两ç§ä¸åçåºäº JSON çç¼ç æ ¼å¼ã19ãã çéï¼
ä¸ [å¾ 4-1](Img/fig4-1.png) 类似ï¼æ¯ä¸ªå段é½æä¸ä¸ªç±»å注éï¼ç¨äºæ示å®æ¯ä¸ä¸ªå符串ï¼æ´æ°ï¼å表çï¼ï¼è¿å¯ä»¥æ ¹æ®éè¦æå®é¿åº¦ï¼å符串çé¿åº¦ï¼å表ä¸ç项ç®æ°ï¼ ãåºç°å¨æ°æ®ä¸çå符串 `(âMartinâ, âdaydreamingâ, âhackingâ)` ä¹è¢«ç¼ç 为 ASCIIï¼æè
说ï¼UTF-8ï¼ï¼ä¸ä¹å类似ã
ä¸ [å¾ 4-1](img/fig4-1.png) ç¸æ¯ï¼æ大çåºå«æ¯æ²¡æå段å `(userName, favoriteNumber, interests)`ãç¸åï¼ç¼ç æ°æ®å
å«å段æ ç¾ï¼å®ä»¬æ¯æ°å `(1, 2 å 3)`ãè¿äºæ¯æ¨¡å¼å®ä¹ä¸åºç°çæ°åãå段æ è®°å°±åå段çå«å - å®ä»¬æ¯è¯´æ们æ£å¨è°è®ºçå段çä¸ç§ç´§åçæ¹å¼ï¼èä¸å¿
æ¼åºå段å称ã
Thrift CompactProtocol ç¼ç å¨è¯ä¹ä¸çåäº BinaryProtocolï¼ä½æ¯å¦ [å¾ 4-3](img/fig4-3.png) æ示ï¼å®åªå°ç¸åçä¿¡æ¯æå
æåªæ 34 个åèãå®éè¿å°å段类ååæ ç¾å·æå
å°å个åèä¸ï¼å¹¶ä½¿ç¨å¯åé¿åº¦æ´æ°æ¥å®ç°ãæ°å 1337 ä¸æ¯ä½¿ç¨å
¨é¨å
«ä¸ªåèï¼èæ¯ç¨ä¸¤ä¸ªåèç¼ç ï¼æ¯ä¸ªåèçæé«ä½ç¨æ¥æ示æ¯å¦è¿ææ´å¤çåèãè¿æå³ç - 64 å° 63 ä¹é´çæ°å被ç¼ç 为ä¸ä¸ªåèï¼-8192 å 8191 ä¹é´çæ°å以两个åèç¼ç ï¼ççãè¾å¤§çæ°å使ç¨æ´å¤çåèã
![](img/fig4-3.png)
**å¾ 4-3 ä½¿ç¨ Thrift å缩åè®®ç¼ç çè®°å½**
æåï¼Protocol Buffersï¼åªæä¸ç§äºè¿å¶ç¼ç æ ¼å¼ï¼å¯¹ç¸åçæ°æ®è¿è¡ç¼ç ï¼å¦ [å¾ 4-4](img/fig4-4.png) æ示ã å®çæå
æ¹å¼ç¨æä¸åï¼ä½ä¸ Thrift ç CompactProtocol é常ç¸ä¼¼ã Protobuf å°åæ ·çè®°å½å¡è¿äº 33 个åèä¸ã
![](img/fig4-4.png)
**å¾ 4-4 ä½¿ç¨ Protobuf ç¼ç çè®°å½**
éè¦æ³¨æçä¸ä¸ªç»èï¼å¨åé¢æ示ç模å¼ä¸ï¼æ¯ä¸ªå段被æ 记为å¿
éæå¯éï¼ä½æ¯è¿å¯¹å段å¦ä½ç¼ç 没æä»»ä½å½±åï¼äºè¿å¶æ°æ®ä¸æ²¡æä»»ä½å段æ示æå段æ¯å¦å¿
é¡»ï¼ãåºå«å¨äºï¼å¦æå段设置为 `required`ï¼ä½æªè®¾ç½®è¯¥å段ï¼åæéçè¿è¡æ¶æ£æ¥å°å¤±è´¥ï¼è¿å¯¹äºæè·é误é常æç¨ã
#### å段æ ç¾å模å¼æ¼å
æ们ä¹å说è¿ï¼æ¨¡å¼ä¸å¯é¿å
å°éè¦éçæ¶é´èæ¹åãæ们称ä¹ä¸ºæ¨¡å¼æ¼åã Thrift å Protocol Buffers å¦ä½å¤ç模å¼æ´æ¹ï¼åæ¶ä¿æååå
¼å®¹æ§ï¼
ä»ç¤ºä¾ä¸å¯ä»¥çåºï¼ç¼ç çè®°å½å°±æ¯å
¶ç¼ç å段çæ¼æ¥ãæ¯ä¸ªå段ç±å
¶æ ç¾å·ç ï¼æ ·æ¬æ¨¡å¼ä¸çæ°å 1,2,3ï¼æ è¯ï¼å¹¶ç¨æ°æ®ç±»åï¼ä¾å¦å符串ææ´æ°ï¼æ³¨éãå¦æ没æ设置å段å¼ï¼åç®åå°ä»ç¼ç è®°å½ä¸çç¥ãä»ä¸å¯ä»¥çå°ï¼å段æ 记对ç¼ç æ°æ®çå«ä¹è³å
³éè¦ãä½ å¯ä»¥æ´æ¹æ¶æä¸å段çå称ï¼å 为ç¼ç çæ°æ®æ°¸è¿ä¸ä¼å¼ç¨å段å称ï¼ä½ä¸è½æ´æ¹å段çæ è®°ï¼å 为è¿ä¼ä½¿ææç°æçç¼ç æ°æ®æ æã
ä½ å¯ä»¥æ·»å æ°çå段å°æ¶æï¼åªè¦ä½ ç»æ¯ä¸ªå段ä¸ä¸ªæ°çæ ç¾å·ç ãå¦ææ§ç代ç ï¼ä¸ç¥éä½ æ·»å çæ°çæ ç¾å·ç ï¼è¯å¾è¯»åæ°ä»£ç åå
¥çæ°æ®ï¼å
æ¬ä¸ä¸ªæ°çå段ï¼å
¶æ ç¾å·ç ä¸è½è¯å«ï¼å®å¯ä»¥ç®åå°å¿½ç¥è¯¥å段ãæ°æ®ç±»å注éå
许解æå¨ç¡®å®éè¦è·³è¿çåèæ°ãè¿ä¿æäºååå
¼å®¹æ§ï¼æ§ä»£ç å¯ä»¥è¯»åç±æ°ä»£ç ç¼åçè®°å½ã
ååå
¼å®¹æ§å¢ï¼åªè¦æ¯ä¸ªå段é½æä¸ä¸ªå¯ä¸çæ ç¾å·ç ï¼æ°ç代ç æ»æ¯å¯ä»¥è¯»åæ§çæ°æ®ï¼å 为æ ç¾å·ç ä»ç¶å
·æç¸åçå«ä¹ãå¯ä¸çç»èæ¯ï¼å¦æä½ æ·»å ä¸ä¸ªæ°çå段ï¼ä½ ä¸è½è®¾ç½®ä¸ºå¿
éãå¦æä½ è¦æ·»å ä¸ä¸ªå段并å°å
¶è®¾ç½®ä¸ºå¿
éï¼é£ä¹å¦ææ°ä»£ç 读åæ§ä»£ç åå
¥çæ°æ®ï¼å该æ£æ¥å°å¤±è´¥ï¼å 为æ§ä»£ç ä¸ä¼åå
¥ä½ æ·»å çæ°å段ãå æ¤ï¼ä¸ºäºä¿æååå
¼å®¹æ§ï¼å¨æ¨¡å¼çåå§é¨ç½²ä¹å **æ·»å çæ¯ä¸ªå段å¿
é¡»æ¯å¯éçæå
·æé»è®¤å¼**ã
å é¤ä¸ä¸ªå段就åæ·»å ä¸ä¸ªå段ï¼åªæ¯è¿åè¦èèçæ¯ååå
¼å®¹æ§ãè¿æå³çä½ åªè½å é¤ä¸ä¸ªå¯éçå段ï¼å¿
éå段永è¿ä¸è½å é¤ï¼ï¼èä¸ä½ ä¸è½å次使ç¨ç¸åçæ ç¾å·ç ï¼å ä¸ºä½ å¯è½ä»ç¶ææ°æ®åå¨å
å«æ§æ ç¾å·ç çå°æ¹ï¼è该å段å¿
须被æ°ä»£ç 忽ç¥ï¼ã
#### æ°æ®ç±»åå模å¼æ¼å
å¦ä½æ¹åå段çæ°æ®ç±»åï¼è¿ä¹è®¸æ¯å¯è½ç ââ 详ç»ä¿¡æ¯è¯·æ¥é
ç¸å
³çææ¡£ ââ ä½æ¯æä¸ä¸ªé£é©ï¼å¼å°å¤±å»ç²¾åº¦æ被æªæãä¾å¦ï¼åè®¾ä½ å°ä¸ä¸ª 32 ä½çæ´æ°åæä¸ä¸ª 64 ä½çæ´æ°ãæ°ä»£ç å¯ä»¥è½»æ¾è¯»åæ§ä»£ç åå
¥çæ°æ®ï¼å 为解æå¨å¯ä»¥ç¨é¶å¡«å
ä»»ä½ç¼ºå¤±çä½ãä½æ¯ï¼å¦ææ§ä»£ç 读åç±æ°ä»£ç åå
¥çæ°æ®ï¼åæ§ä»£ç ä»ä½¿ç¨ 32 ä½åéæ¥ä¿å该å¼ãå¦æ解ç ç 64 ä½å¼ä¸éå 32 ä½ï¼åå®å°è¢«æªæã
Protobuf çä¸ä¸ªå¥æªçç»èæ¯ï¼å®æ²¡æå表ææ°ç»æ°æ®ç±»åï¼èæ¯æä¸ä¸ªå段çéå¤æ è®°ï¼`repeated`ï¼è¿æ¯é¤å¿
éåå¯éä¹å¤ç第ä¸ä¸ªé项ï¼ãå¦ [å¾ 4-4](img/fig4-4.png) æ示ï¼éå¤å段çç¼ç æ£å¦å®æ说çé£æ ·ï¼åä¸ä¸ªå段æ è®°åªæ¯ç®åå°åºç°å¨è®°å½ä¸ãè¿å
·æå¾å¥½çææï¼å¯ä»¥å°å¯éï¼åå¼ï¼å段æ´æ¹ä¸ºéå¤ï¼å¤å¼ï¼å段ã读åæ§æ°æ®çæ°ä»£ç ä¼çå°ä¸ä¸ªå
å«é¶ä¸ªæä¸ä¸ªå
ç´ çå表ï¼åå³äºè¯¥å段æ¯å¦åå¨ï¼ã读åæ°æ°æ®çæ§ä»£ç åªè½çå°å表çæåä¸ä¸ªå
ç´ ã
Thrift æä¸ä¸ªä¸ç¨çå表æ°æ®ç±»åï¼å®ä½¿ç¨å表å
ç´ çæ°æ®ç±»åè¿è¡åæ°åãè¿ä¸å
许 Protocol Buffers æåçä»åå¼å°å¤å¼çæ¼åï¼ä½æ¯å®å
·ææ¯æåµå¥å表çä¼ç¹ã
### Avro
Apache Avro ã20ãæ¯å¦ä¸ç§äºè¿å¶ç¼ç æ ¼å¼ï¼ä¸ Protocol Buffers å Thrift æçæ趣çä¸åã å®æ¯ä½ä¸º Hadoop çä¸ä¸ªå项ç®å¨ 2009 å¹´å¼å§çï¼å 为 Thrift ä¸éå Hadoop çç¨ä¾ã21ãã
Avro ä¹ä½¿ç¨æ¨¡å¼æ¥æå®æ£å¨ç¼ç çæ°æ®çç»æã å®æ两ç§æ¨¡å¼è¯è¨ï¼ä¸ç§ï¼Avro IDLï¼ç¨äºäººå·¥ç¼è¾ï¼ä¸ç§ï¼åºäº JSONï¼æ´æäºæºå¨è¯»åã
æä»¬ç¨ Avro IDL ç¼åç示ä¾æ¨¡å¼å¯è½å¦ä¸æ示ï¼
```c
record Person {
string userName;
union { null, long } favoriteNumber = null;
array interests;
}
```
çä»·ç JSON 表示ï¼
```json
{
"type": "record",
"name": "Person",
"fields": [
{"name": "userName", "type": "string"},
{"name": "favoriteNumber", "type": ["null", "long"], "default": null},
{"name": "interests", "type": {"type": "array", "items": "string"}}
]
}
```
é¦å
ï¼è¯·æ³¨æ模å¼ä¸æ²¡ææ ç¾å·ç ã å¦ææ们使ç¨è¿ä¸ªæ¨¡å¼ç¼ç æ们çä¾åè®°å½ï¼[ä¾ 4-1]()ï¼ï¼Avro äºè¿å¶ç¼ç åªæ 32 个åèé¿ï¼è¿æ¯æ们æè§è¿çææç¼ç ä¸æç´§åçã ç¼ç åèåºåçåè§£å¦ [å¾ 4-5](img/fig4-5.png) æ示ã
å¦æä½ æ£æ¥åèåºåï¼ä½ å¯ä»¥çå°æ²¡æä»ä¹å¯ä»¥è¯å«å段æå
¶æ°æ®ç±»åã ç¼ç åªæ¯ç±è¿å¨ä¸èµ·çå¼ç»æã ä¸ä¸ªå符串åªæ¯ä¸ä¸ªé¿åº¦åç¼ï¼åè· UTF-8 åèï¼ä½æ¯å¨è¢«å
å«çæ°æ®ä¸æ²¡æä»»ä½å
容åè¯ä½ å®æ¯ä¸ä¸ªå符串ã å®å¯ä»¥æ¯ä¸ä¸ªæ´æ°ï¼ä¹å¯ä»¥æ¯å
¶ä»çæ´æ°ã æ´æ°ä½¿ç¨å¯åé¿åº¦ç¼ç ï¼ä¸ Thrift ç CompactProtocol ç¸åï¼è¿è¡ç¼ç ã
![](img/fig4-5.png)
**å¾ 4-5 ä½¿ç¨ Avro ç¼ç çè®°å½**
为äºè§£æäºè¿å¶æ°æ®ï¼ä½ æç
§å®ä»¬åºç°å¨æ¨¡å¼ä¸ç顺åºéåè¿äºå段ï¼å¹¶ä½¿ç¨æ¨¡å¼æ¥åè¯ä½ æ¯ä¸ªå段çæ°æ®ç±»åãè¿æå³çå¦æ读åæ°æ®ç代ç 使ç¨ä¸åå
¥æ°æ®ç代ç å®å
¨ç¸åç模å¼ï¼æè½æ£ç¡®è§£ç äºè¿å¶æ°æ®ãReader å Writer ä¹é´ç模å¼ä¸å¹é
æå³çé误å°è§£ç æ°æ®ã
é£ä¹ï¼Avro å¦ä½æ¯æ模å¼æ¼åå¢ï¼
#### Writer模å¼ä¸Reader模å¼
æäº Avroï¼å½åºç¨ç¨åºæ³è¦ç¼ç ä¸äºæ°æ®ï¼å°å
¶åå
¥æ件ææ°æ®åºï¼éè¿ç½ç»åéçï¼æ¶ï¼å®ä½¿ç¨å®ç¥éçä»»ä½çæ¬ç模å¼ç¼ç æ°æ®ï¼ä¾å¦ï¼æ¨¡å¼å¯è½è¢«ç¼è¯å°åºç¨ç¨åºä¸ãè¿è¢«ç§°ä¸º Writer 模å¼ã
å½ä¸ä¸ªåºç¨ç¨åºæ³è¦è§£ç ä¸äºæ°æ®ï¼ä»ä¸ä¸ªæ件ææ°æ®åºè¯»åæ°æ®ï¼ä»ç½ç»æ¥æ¶æ°æ®çï¼æ¶ï¼å®å¸ææ°æ®å¨æ个模å¼ä¸ï¼è¿å°±æ¯ Reader 模å¼ãè¿æ¯åºç¨ç¨åºä»£ç æä¾èµç模å¼ï¼å¨åºç¨ç¨åºçæ建è¿ç¨ä¸ï¼ä»£ç å¯è½å·²ç»ä»è¯¥æ¨¡å¼çæã
Avro çå
³é®ææ³æ¯ Writer 模å¼å Reader 模å¼ä¸å¿
æ¯ç¸åç - ä»ä»¬åªéè¦å
¼å®¹ãå½æ°æ®è§£ç ï¼è¯»åï¼æ¶ï¼Avro åºéè¿å¹¶ææ¥ç Writer 模å¼å Reader 模å¼å¹¶å°æ°æ®ä» Writer 模å¼è½¬æ¢å° Reader 模å¼æ¥è§£å³å·®å¼ã Avro è§èã20ãç¡®åå°å®ä¹äºè¿ç§è§£æçå·¥ä½åçï¼å¦ [å¾ 4-6](img/fig4-6.png) æ示ã
ä¾å¦ï¼å¦æ Writer 模å¼å Reader 模å¼çå段顺åºä¸åï¼è¿æ¯æ²¡æé®é¢çï¼å 为模å¼è§£æéè¿å段åå¹é
å段ãå¦æ读åæ°æ®ç代ç éå°åºç°å¨ Writer 模å¼ä¸ä½ä¸å¨ Reader 模å¼ä¸çå段ï¼å忽ç¥å®ãå¦æ读åæ°æ®ç代ç éè¦æ个å段ï¼ä½æ¯ Writer 模å¼ä¸å
å«è¯¥å称çå段ï¼å使ç¨å¨ Reader 模å¼ä¸å£°æçé»è®¤å¼å¡«å
ã
![](img/fig4-6.png)
**å¾ 4-6 ä¸ä¸ª Avro Reader 解å³è¯»å模å¼çå·®å¼**
#### 模å¼æ¼åè§å
ä½¿ç¨ Avroï¼ååå
¼å®¹æ§æå³çä½ å¯ä»¥å°æ°çæ¬ç模å¼ä½ä¸º Writerï¼å¹¶å°æ§çæ¬ç模å¼ä½ä¸º Readerãç¸åï¼ååå
¼å®¹æå³çä½ å¯ä»¥æä¸ä¸ªä½ä¸º Reader çæ°çæ¬æ¨¡å¼åä½ä¸º Writer çæ§çæ¬æ¨¡å¼ã
为äºä¿æå
¼å®¹æ§ï¼ä½ åªè½æ·»å æå é¤å
·æé»è®¤å¼çå段ï¼æ们ç Avro 模å¼ä¸çå段 `favoriteNumber` çé»è®¤å¼ä¸º `null`ï¼ãä¾å¦ï¼åè®¾ä½ æ·»å äºä¸ä¸ªæé»è®¤å¼çå段ï¼è¿ä¸ªæ°çå段å°åå¨äºæ°æ¨¡å¼èä¸æ¯æ§æ¨¡å¼ä¸ãå½ä½¿ç¨æ°æ¨¡å¼ç Reader 读å使ç¨æ§æ¨¡å¼åå
¥çè®°å½æ¶ï¼å°ä¸ºç¼ºå°çå段填å
é»è®¤å¼ã
å¦æä½ è¦æ·»å ä¸ä¸ªæ²¡æé»è®¤å¼çå段ï¼æ°ç Reader å°æ æ³è¯»åæ§ Writer åçæ°æ®ï¼æä»¥ä½ ä¼ç ´åååå
¼å®¹æ§ãå¦æä½ è¦å é¤æ²¡æé»è®¤å¼çå段ï¼æ§ç Reader å°æ æ³è¯»åæ° Writer åå
¥çæ°æ®ï¼å æ¤ä½ ä¼æç ´ååå
¼å®¹æ§ãå¨ä¸äºç¼ç¨è¯è¨ä¸ï¼null æ¯ä»»ä½åéå¯ä»¥æ¥åçé»è®¤å¼ï¼ä½å¨ Avro ä¸å¹¶ä¸æ¯è¿æ ·ï¼å¦æè¦å
许ä¸ä¸ªå段为 `null`ï¼åå¿
须使ç¨èåç±»åãä¾å¦ï¼`union {null, long, string} field;` 表示 field å¯ä»¥æ¯æ°åæå符串ï¼ä¹å¯ä»¥æ¯ `null`ãå¦æè¦å° null ä½ä¸ºé»è®¤å¼ï¼åå®å¿
é¡»æ¯ union çåæ¯ä¹ä¸ [^iv]ãè¿æ ·çåæ³æ¯é»è®¤æ
åµä¸å°±å
许任ä½åéæ¯ `null` æ¾å¾æ´å åé¿ï¼ä½æ¯éè¿æç¡®ä»ä¹å¯ä»¥åä»ä¹ä¸å¯ä»¥æ¯ `null`ï¼æå©äºé²æ¢åºéã22ãã
[^iv]: ç¡®åå°è¯´ï¼é»è®¤å¼å¿
é¡»æ¯èåç第ä¸ä¸ªåæ¯çç±»åï¼å°½ç®¡è¿æ¯ Avro çç¹å®éå¶ï¼èä¸æ¯èåç±»åçä¸è¬ç¹å¾ã
å æ¤ï¼Avro 没æå Protocol Buffers å Thrift é£æ ·ç `optional` å `required` æ è®°ï¼ä½å®æèåç±»ååé»è®¤å¼ï¼ã
åªè¦ Avro å¯ä»¥æ¯æç¸åºçç±»å转æ¢ï¼å°±å¯ä»¥æ¹åå段çæ°æ®ç±»åãæ´æ¹å段çå称ä¹æ¯å¯è½çï¼ä½æç¹æ£æï¼Reader 模å¼å¯ä»¥å
å«å段å称çå«åï¼æ以å®å¯ä»¥å¹é
æ§ Writer ç模å¼å段å称ä¸å«åãè¿æå³çæ´æ¹å段å称æ¯ååå
¼å®¹çï¼ä½ä¸è½ååå
¼å®¹ãåæ ·ï¼åèåç±»åæ·»å åæ¯ä¹æ¯ååå
¼å®¹çï¼ä½ä¸è½ååå
¼å®¹ã
#### ä½Writer模å¼å°åºæ¯ä»ä¹ï¼
å°ç®å为æ¢ï¼æ们ä¸ç´è·³è¿äºä¸ä¸ªéè¦çé®é¢ï¼å¯¹äºä¸æ®µç¹å®çç¼ç æ°æ®ï¼Reader å¦ä½ç¥éå
¶ Writer 模å¼ï¼æ们ä¸è½åªå°æ´ä¸ªæ¨¡å¼å
æ¬å¨æ¯ä¸ªè®°å½ä¸ï¼å 为模å¼å¯è½æ¯ç¼ç çæ°æ®å¤§å¾å¤ï¼ä»è使äºè¿å¶ç¼ç èççææ空é´é½æ¯å¾å³çã
çæ¡åå³äº Avro 使ç¨çä¸ä¸æã举å 个ä¾åï¼
* æå¾å¤è®°å½ç大æ件
Avro çä¸ä¸ªå¸¸è§ç¨é - å°¤å
¶æ¯å¨ Hadoop ç¯å¢ä¸ - ç¨äºåå¨å
å«æ°ç¾ä¸æ¡è®°å½ç大æ件ï¼ææè®°å½é½ä½¿ç¨ç¸åç模å¼è¿è¡ç¼ç ï¼æ们å°å¨ [第åç« ](ch10.md) 讨论è¿ç§æ
åµï¼ãå¨è¿ç§æ
åµä¸ï¼è¯¥æ件çä½è
å¯ä»¥å¨æ件çå¼å¤´åªå
å«ä¸æ¬¡ Writer 模å¼ã Avro æå®äºä¸ä¸ªæä»¶æ ¼å¼ï¼å¯¹è±¡å®¹å¨æ件ï¼æ¥åå°è¿ä¸ç¹ã
* æ¯æç¬ç«åå
¥çè®°å½çæ°æ®åº
å¨ä¸ä¸ªæ°æ®åºä¸ï¼ä¸åçè®°å½å¯è½ä¼å¨ä¸åçæ¶é´ç¹ä½¿ç¨ä¸åç Writer 模å¼æ¥åå
¥ - ä½ ä¸è½åå®ææçè®°å½é½æç¸åç模å¼ãæç®åç解å³æ¹æ¡æ¯å¨æ¯ä¸ªç¼ç è®°å½çå¼å§å¤å
å«ä¸ä¸ªçæ¬å·ï¼å¹¶å¨æ°æ®åºä¸ä¿çä¸ä¸ªæ¨¡å¼çæ¬å表ãReader å¯ä»¥è·åè®°å½ï¼æåçæ¬å·ï¼ç¶åä»æ°æ®åºä¸è·å该çæ¬å·ç Writer 模å¼ã使ç¨è¯¥ Writer 模å¼ï¼å®å¯ä»¥è§£ç è®°å½çå
¶ä½é¨åï¼ä¾å¦ Espresso ã23ãå°±æ¯è¿æ ·å·¥ä½çï¼ã
* éè¿ç½ç»è¿æ¥åéè®°å½
å½ä¸¤ä¸ªè¿ç¨éè¿ååç½ç»è¿æ¥è¿è¡éä¿¡æ¶ï¼ä»ä»¬å¯ä»¥å¨è¿æ¥è®¾ç½®ä¸åå模å¼çæ¬ï¼ç¶åå¨è¿æ¥ççå½å¨æä¸ä½¿ç¨è¯¥æ¨¡å¼ã Avro RPC åè®®ï¼è¯·åé
â[æå¡ä¸çæ°æ®æµï¼REST ä¸ RPC](#æå¡ä¸çæ°æ®æµï¼RESTä¸RPC)âï¼å°±æ¯è¿æ ·å·¥ä½çã
å
·æ模å¼çæ¬çæ°æ®åºå¨ä»»ä½æ
åµä¸é½æ¯é常æç¨çï¼å 为å®å
å½ææ¡£å¹¶ä¸ºä½ æä¾äºæ£æ¥æ¨¡å¼å
¼å®¹æ§çæºä¼ã24ããä½ä¸ºçæ¬å·ï¼ä½ å¯ä»¥ä½¿ç¨ä¸ä¸ªç®åçéå¢æ´æ°ï¼æè
ä½ å¯ä»¥ä½¿ç¨æ¨¡å¼çæ£åã
#### å¨æçæç模å¼
ä¸ Protocol Buffers å Thrift ç¸æ¯ï¼Avro æ¹æ³çä¸ä¸ªä¼ç¹æ¯æ¶æä¸å
å«ä»»ä½æ ç¾å·ç ãä½ä¸ºä»ä¹è¿å¾éè¦ï¼å¨æ¨¡å¼ä¸ä¿çä¸äºæ°åæä»ä¹é®é¢ï¼
ä¸åä¹å¤å¨äº Avro 对å¨æçæç模å¼æ´ååãä¾å¦ï¼åå¦ä½ æä¸ä¸ªå
³ç³»æ°æ®åºï¼ä½ æ³è¦æå®çå
容转å¨å°ä¸ä¸ªæ件ä¸ï¼å¹¶ä¸ä½ æ³ä½¿ç¨äºè¿å¶æ ¼å¼æ¥é¿å
åé¢æå°çææ¬æ ¼å¼ï¼JSONï¼CSVï¼SQLï¼çé®é¢ãå¦æä½ ä½¿ç¨ Avroï¼ä½ å¯ä»¥å¾å®¹æå°ä»å
³ç³»æ¨¡å¼çæä¸ä¸ª Avro 模å¼ï¼å¨æ们ä¹åçå°ç JSON 表示ä¸ï¼ï¼å¹¶ä½¿ç¨è¯¥æ¨¡å¼å¯¹æ°æ®åºå
容è¿è¡ç¼ç ï¼å¹¶å°å
¶å
¨é¨è½¬å¨å° Avro 对象容å¨æ件ã25ãä¸ãä½ ä¸ºæ¯ä¸ªæ°æ®åºè¡¨çæä¸ä¸ªè®°å½æ¨¡å¼ï¼æ¯ä¸ªåæ为该记å½ä¸çä¸ä¸ªå段ãæ°æ®åºä¸çåå称æ å°å° Avro ä¸çå段å称ã
ç°å¨ï¼å¦ææ°æ®åºæ¨¡å¼åçååï¼ä¾å¦ï¼ä¸ä¸ªè¡¨ä¸æ·»å äºä¸åï¼å é¤äºä¸åï¼ï¼åå¯ä»¥ä»æ´æ°çæ°æ®åºæ¨¡å¼çææ°ç Avro 模å¼ï¼å¹¶å¨æ°ç Avro 模å¼ä¸å¯¼åºæ°æ®ãæ°æ®å¯¼åºè¿ç¨ä¸éè¦æ³¨æ模å¼çæ¹å - æ¯æ¬¡è¿è¡æ¶é½å¯ä»¥ç®åå°è¿è¡æ¨¡å¼è½¬æ¢ãä»»ä½è¯»åæ°æ°æ®æ件ç人é½ä¼çå°è®°å½çå段已ç»æ¹åï¼ä½æ¯ç±äºå段æ¯éè¿ååæ¥æ è¯çï¼æ以æ´æ°ç Writer 模å¼ä»ç¶å¯ä»¥ä¸æ§ç Reader 模å¼å¹é
ã
ç¸æ¯ä¹ä¸ï¼å¦æä½ ä¸ºæ¤ä½¿ç¨ Thrift æ Protocol Buffersï¼åå段æ ç¾å¯è½å¿
é¡»æå¨åé
ï¼æ¯æ¬¡æ°æ®åºæ¨¡å¼æ´æ¹æ¶ï¼ç®¡çåé½å¿
é¡»æå¨æ´æ°ä»æ°æ®åºååå°å段æ ç¾çæ å°ï¼è¿å¯è½ä¼èªå¨åï¼ä½æ¨¡å¼çæå¨å¿
é¡»é常å°å¿ï¼ä¸è¦åé
以å使ç¨çå段æ ç¾ï¼ãè¿ç§å¨æçæç模å¼æ ¹æ¬ä¸æ¯ Thrift æ Protocol Buffers ç设计ç®æ ï¼èæ¯ Avro çã
#### 代ç çæåå¨æç±»åçè¯è¨
Thrift å Protobuf ä¾èµäºä»£ç çæï¼å¨å®ä¹äºæ¨¡å¼ä¹åï¼å¯ä»¥ä½¿ç¨ä½ éæ©çç¼ç¨è¯è¨çæå®ç°æ¤æ¨¡å¼ç代ç ãè¿å¨ JavaãC++ æ C# çéæç±»åè¯è¨ä¸å¾æç¨ï¼å 为å®å
许å°é«æçå
åä¸ç»æç¨äºè§£ç çæ°æ®ï¼å¹¶ä¸å¨ç¼å访é®æ°æ®ç»æçç¨åºæ¶å
è®¸å¨ IDE ä¸è¿è¡ç±»åæ£æ¥åèªå¨è¡¥å
¨ã
å¨å¨æç±»åç¼ç¨è¯è¨ï¼å¦ JavaScriptãRuby æ Pythonï¼ä¸ï¼çæ代ç 没æ太å¤æä¹ï¼å 为没æç¼è¯æ¶ç±»åæ£æ¥å¨æ¥æ»¡è¶³ã代ç çæå¨è¿äºè¯è¨ä¸ç»å¸¸è¢«å¿½è§ï¼å 为å®ä»¬é¿å
äºæ¾å¼çç¼è¯æ¥éª¤ãèä¸ï¼å¯¹äºå¨æçæç模å¼ï¼ä¾å¦ä»æ°æ®åºè¡¨çæç Avro 模å¼ï¼ï¼ä»£ç çæ对è·åæ°æ®æ¯ä¸ä¸ªä¸å¿
è¦çéç¢ã
Avro 为éæç±»åç¼ç¨è¯è¨æä¾äºå¯éç代ç çæåè½ï¼ä½æ¯å®ä¹å¯ä»¥å¨ä¸çæä»»ä½ä»£ç çæ
åµä¸ä½¿ç¨ãå¦æä½ æä¸ä¸ªå¯¹è±¡å®¹å¨æ件ï¼å®åµå
¥äº Writer 模å¼ï¼ï¼ä½ å¯ä»¥ç®åå°ä½¿ç¨ Avro åºæå¼å®ï¼å¹¶ä»¥ä¸æ¥ç JSON æ件ç¸åçæ¹å¼æ¥çæ°æ®ã该æ件æ¯èªæè¿°çï¼å 为å®å
å«ææå¿
è¦çå
æ°æ®ã
è¿ä¸ªå±æ§ç¹å«éç¨äºå¨æç±»åçæ°æ®å¤çè¯è¨å¦ Apache Pig ã26ããå¨ Pig ä¸ï¼ä½ å¯ä»¥æå¼ä¸äº Avro æ件ï¼å¼å§åæå®ä»¬ï¼å¹¶ç¼åæ´¾çæ°æ®é以 Avro æ ¼å¼è¾åºæ件ï¼èæ éèè模å¼ã
### 模å¼çä¼ç¹
æ£å¦æ们æçå°çï¼Protocol BuffersãThrift å Avro é½ä½¿ç¨æ¨¡å¼æ¥æè¿°äºè¿å¶ç¼ç æ ¼å¼ãä»ä»¬ç模å¼è¯è¨æ¯ XML 模å¼æè
JSON 模å¼ç®åå¾å¤ï¼èåè
æ¯ææ´è¯¦ç»çéªè¯è§åï¼ä¾å¦ï¼â该å段çå符串å¼å¿
é¡»ä¸è¯¥æ£å表达å¼å¹é
â æ â该å段çæ´æ°å¼å¿
é¡»å¨ 0 å 100 ä¹é´ âï¼ãç±äº Protocol Buffersï¼Thrift å Avro å®ç°èµ·æ¥æ´ç®åï¼ä½¿ç¨èµ·æ¥ä¹æ´ç®åï¼æ以å®ä»¬å·²ç»åå±å°æ¯æç¸å½å¹¿æ³çç¼ç¨è¯è¨ã
è¿äºç¼ç æåºäºçæ³æ³ç»ä¸æ¯æ°çãä¾å¦ï¼å®ä»¬ä¸ ASN.1 æå¾å¤ç¸ä¼¼ä¹å¤ï¼å®æ¯ 1984 å¹´é¦æ¬¡è¢«æ ååç模å¼å®ä¹è¯è¨ã27ããå®è¢«ç¨æ¥å®ä¹åç§ç½ç»åè®®ï¼ä¾å¦å
¶äºè¿å¶ç¼ç ï¼DERï¼ä»ç¶è¢«ç¨äºç¼ç SSL è¯ä¹¦ï¼X.509ï¼ã28ãã ASN.1 æ¯æ使ç¨æ ç¾å·ç ç模å¼æ¼è¿ï¼ç±»ä¼¼äº Protocol Buffers å Thrift ã29ããç¶èï¼å®ä¹é常å¤æï¼èä¸æ²¡æ好çé
å¥ææ¡£ï¼æ以 ASN.1 å¯è½ä¸æ¯æ°åºç¨ç¨åºç好éæ©ã
许å¤æ°æ®ç³»ç»ä¹ä¸ºå
¶æ°æ®å®ç°äºæç§ä¸æçäºè¿å¶ç¼ç ãä¾å¦ï¼å¤§å¤æ°å
³ç³»æ°æ®åºé½æä¸ä¸ªç½ç»åè®®ï¼ä½ å¯ä»¥éè¿è¯¥åè®®åæ°æ®åºåéæ¥è¯¢å¹¶è·åååºãè¿äºåè®®é常ç¹å®äºç¹å®çæ°æ®åºï¼å¹¶ä¸æ°æ®åºä¾åºåæä¾å°æ¥èªæ°æ®åºçç½ç»åè®®çååºè§£ç 为å
åæ°æ®ç»æç驱å¨ç¨åºï¼ä¾å¦ä½¿ç¨ ODBC æ JDBC APIï¼ã
æ以ï¼æ们å¯ä»¥çå°ï¼å°½ç®¡ JSONãXML å CSV çææ¬æ°æ®æ ¼å¼é常æ®éï¼ä½åºäºæ¨¡å¼çäºè¿å¶ç¼ç ä¹æ¯ä¸ä¸ªå¯è¡çéæ©ãä»ä»¬æä¸äºå¾å¥½çå±æ§ï¼
* å®ä»¬å¯ä»¥æ¯åç§ âäºè¿å¶ JSONâ åä½æ´ç´§åï¼å 为å®ä»¬å¯ä»¥çç¥ç¼ç æ°æ®ä¸çå段å称ã
* 模å¼æ¯ä¸ç§æä»·å¼çæ档形å¼ï¼å 为模å¼æ¯è§£ç æå¿
éçï¼æ以å¯ä»¥ç¡®å®å®æ¯ææ°çï¼èæå¨ç»´æ¤çææ¡£å¯è½å¾å®¹æå离ç°å®ï¼ã
* ç»´æ¤ä¸ä¸ªæ¨¡å¼çæ°æ®åºå
è®¸ä½ å¨é¨ç½²ä»»ä½å
容ä¹åæ£æ¥æ¨¡å¼æ´æ¹çåååååå
¼å®¹æ§ã
* 对äºéæç±»åç¼ç¨è¯è¨çç¨æ·æ¥è¯´ï¼ä»æ¨¡å¼çæ代ç çè½åæ¯æç¨çï¼å 为å®å¯ä»¥å¨ç¼è¯æ¶è¿è¡ç±»åæ£æ¥ã
æ»èè¨ä¹ï¼æ¨¡å¼æ¼åä¿æäºä¸ JSON æ°æ®åºæä¾çæ æ¨¡å¼ / 读æ¶æ¨¡å¼ç¸åççµæ´»æ§ï¼è¯·åé
â[æ档模åä¸ç模å¼çµæ´»æ§](ch2.md#æ档模åä¸ç模å¼çµæ´»æ§)âï¼ï¼åæ¶è¿å¯ä»¥æ´å¥½å°ä¿è¯ä½ çæ°æ®å¹¶æä¾æ´å¥½çå·¥å
·ã
## æ°æ®æµçç±»å
å¨æ¬ç« çå¼å§é¨åï¼æ们æ¾ç»è¯´è¿ï¼æ 论ä½æ¶ä½ æ³è¦å°æäºæ°æ®åéå°ä¸å
±äº«å
åçå¦ä¸ä¸ªè¿ç¨ï¼ä¾å¦ï¼åªè¦ä½ æ³éè¿ç½ç»åéæ°æ®æå°å
¶åå
¥æ件ï¼å°±éè¦å°å®ç¼ç 为ä¸ä¸ªåèåºåãç¶åæ们讨论äºåè¿ä¸ªçåç§ä¸åçç¼ç ã
æ们讨论äºåååååçå
¼å®¹æ§ï¼è¿å¯¹äºå¯æ¼åæ§æ¥è¯´é常éè¦ï¼éè¿å
è®¸ä½ ç¬ç«å级系ç»çä¸åé¨åï¼èä¸å¿
ä¸æ¬¡æ¹åææå
容ï¼å¯ä»¥è½»æ¾å°è¿è¡æ´æ¹ï¼ãå
¼å®¹æ§æ¯ç¼ç æ°æ®çä¸ä¸ªè¿ç¨å解ç å®çå¦ä¸ä¸ªè¿ç¨ä¹é´çä¸ç§å
³ç³»ã
è¿æ¯ä¸ä¸ªç¸å½æ½è±¡çæ¦å¿µ - æ°æ®å¯ä»¥éè¿å¤ç§æ¹å¼ä»ä¸ä¸ªæµç¨æµåå¦ä¸ä¸ªæµç¨ãè°ç¼ç æ°æ®ï¼è°è§£ç ï¼å¨æ¬ç« çå
¶ä½é¨åä¸ï¼æ们å°æ¢è®¨æ°æ®å¦ä½å¨æµç¨ä¹é´æµå¨çä¸äºæ常è§çæ¹å¼ï¼
* éè¿æ°æ®åºï¼è¯·åé
â[æ°æ®åºä¸çæ°æ®æµ](#æ°æ®åºä¸çæ°æ®æµ)âï¼
* éè¿æå¡è°ç¨ï¼è¯·åé
â[æå¡ä¸çæ°æ®æµï¼REST ä¸ RPC](#æå¡ä¸çæ°æ®æµï¼RESTä¸RPC)âï¼
* éè¿å¼æ¥æ¶æ¯ä¼ éï¼è¯·åé
â[æ¶æ¯ä¼ éä¸çæ°æ®æµ](#æ¶æ¯ä¼ éä¸çæ°æ®æµ)âï¼
### æ°æ®åºä¸çæ°æ®æµ
å¨æ°æ®åºä¸ï¼åå
¥æ°æ®åºçè¿ç¨å¯¹æ°æ®è¿è¡ç¼ç ï¼ä»æ°æ®åºè¯»åçè¿ç¨å¯¹æ°æ®è¿è¡è§£ç ãå¯è½åªæä¸ä¸ªè¿ç¨è®¿é®æ°æ®åºï¼å¨è¿ç§æ
åµä¸ï¼è¯»è
åªæ¯ç¸åè¿ç¨çåç»çæ¬ - å¨è¿ç§æ
åµä¸ï¼ä½ å¯ä»¥èèå°æ°æ®åºä¸çå
容åå¨ä¸ºåæªæ¥çèªæåéæ¶æ¯ã
ååå
¼å®¹æ§æ¾ç¶æ¯å¿
è¦çãå¦åä½ æªæ¥çèªå·±å°æ æ³è§£ç ä½ ä»¥ååçä¸è¥¿ã
ä¸è¬æ¥è¯´ï¼å 个ä¸åçè¿ç¨åæ¶è®¿é®æ°æ®åºæ¯å¾å¸¸è§çãè¿äºè¿ç¨å¯è½æ¯å 个ä¸åçåºç¨ç¨åºææå¡ï¼æè
å®ä»¬å¯è½åªæ¯å 个ç¸åæå¡çå®ä¾ï¼ä¸ºäºå¯ä¼¸ç¼©æ§æ容éæ§è并è¡è¿è¡ï¼ãæ 论åªç§æ¹å¼ï¼å¨åºç¨ç¨åºåçååçç¯å¢ä¸ï¼è®¿é®æ°æ®åºçæäºè¿ç¨å¯è½ä¼è¿è¡è¾æ°ç代ç ï¼æäºè¿ç¨å¯è½ä¼è¿è¡è¾æ§ç代ç ï¼ä¾å¦ï¼å 为æ°çæ¬å½åæ£å¨é¨ç½²æ»å¨å级ï¼æ以æäºå®ä¾å·²ç»æ´æ°ï¼èå
¶ä»å®ä¾å°æªæ´æ°ã
è¿æå³çæ°æ®åºä¸çä¸ä¸ªå¼å¯è½ä¼è¢«æ´æ°çæ¬ç代ç åå
¥ï¼ç¶å被ä»æ§è¿è¡çæ§çæ¬ç代ç 读åãå æ¤ï¼æ°æ®åºä¹ç»å¸¸éè¦ååå
¼å®¹ã
ä½æ¯ï¼è¿æä¸ä¸ªé¢å¤çéç¢ãåè®¾ä½ å°ä¸ä¸ªå段添å å°è®°å½æ¨¡å¼ï¼å¹¶ä¸è¾æ°ç代ç å°è¯¥æ°å段çå¼åå
¥æ°æ®åºãéåï¼æ§çæ¬ç代ç ï¼å°ä¸ç¥éæ°å段ï¼å°è¯»åè®°å½ï¼æ´æ°è®°å½å¹¶å°å
¶ååãå¨è¿ç§æ
åµä¸ï¼çæ³çè¡ä¸ºé常æ¯æ§ä»£ç ä¿ææ°çå段ä¸åï¼å³ä½¿å®ä¸è½è¢«è§£éã
åé¢è®¨è®ºçç¼ç æ ¼å¼æ¯ææªç¥å段çä¿åï¼ä½æ¯ææ¶åéè¦å¨åºç¨ç¨åºå±é¢ä¿æè°¨æ
ï¼å¦å¾ 4-7 æ示ãä¾å¦ï¼å¦æå°æ°æ®åºå¼è§£ç 为åºç¨ç¨åºä¸ç模å对象ï¼ç¨åéæ°ç¼ç è¿äºæ¨¡å对象ï¼é£ä¹æªç¥å段å¯è½ä¼å¨è¯¥ç¿»è¯è¿ç¨ä¸ä¸¢å¤±ã解å³è¿ä¸ªé®é¢ä¸æ¯ä¸ä¸ªé¾é¢ï¼ä½ åªéè¦æè¯å°å®ã
![](img/fig4-7.png)
**å¾ 4-7 å½è¾æ§çæ¬çåºç¨ç¨åºæ´æ°ä»¥åç±è¾æ°çæ¬çåºç¨ç¨åºç¼åçæ°æ®æ¶ï¼å¦æä¸å°å¿ï¼æ°æ®å¯è½ä¼ä¸¢å¤±ã**
#### å¨ä¸åçæ¶é´åå
¥ä¸åçå¼
æ°æ®åºé常å
许任ä½æ¶åæ´æ°ä»»ä½å¼ãè¿æå³çå¨ä¸ä¸ªåä¸çæ°æ®åºä¸ï¼å¯è½æä¸äºå¼æ¯äºæ¯«ç§ååçï¼èä¸äºå¼æ¯äºå¹´ååçã
å¨é¨ç½²åºç¨ç¨åºçæ°çæ¬æ¶ï¼ä¹è®¸ç¨ä¸äºå åéå°±å¯ä»¥å°ææçæ§çæ¬æ¿æ¢ä¸ºæ°çæ¬ï¼è³å°æå¡å¨ç«¯åºç¨ç¨åºæ¯è¿æ ·çï¼ãä½æ°æ®åºå
容并éå¦æ¤ï¼å¯¹äºäºå¹´åçæ°æ®æ¥è¯´ï¼é¤é对å
¶è¿è¡æ¾å¼éåï¼å¦åå®ä»ç¶ä¼ä»¥åå§ç¼ç å½¢å¼åå¨ãè¿ç§ç°è±¡ææ¶è¢«æ¦æ¬ä¸ºï¼æ°æ®ççå½å¨æè¶
åºä»£ç ççå½å¨æã
å°æ°æ®éåï¼è¿ç§»ï¼å°ä¸ä¸ªæ°ç模å¼å½ç¶æ¯å¯è½çï¼ä½æ¯å¨ä¸ä¸ªå¤§æ°æ®éä¸æ§è¡æ¯ä¸ä¸ªæè´µçäºæ
ï¼æ以大å¤æ°æ°æ®åºå¦æå¯è½çè¯å°±é¿å
å®ã大å¤æ°å
³ç³»æ°æ®åºé½å
许ç®åç模å¼æ´æ¹ï¼ä¾å¦æ·»å ä¸ä¸ªé»è®¤å¼ä¸ºç©ºçæ°åï¼èä¸éåç°ææ°æ® [^v]ã读åæ§è¡æ¶ï¼å¯¹äºç£çä¸çç¼ç æ°æ®ç¼ºå°çä»»ä½åï¼æ°æ®åºå°å¡«å
空å¼ã LinkedIn çææ¡£æ°æ®åº Espresso ä½¿ç¨ Avro åå¨ï¼å
许å®ä½¿ç¨ Avro ç模å¼æ¼åè§åã23ãã
å æ¤ï¼æ¨¡å¼æ¼åå
许æ´ä¸ªæ°æ®åºçèµ·æ¥å¥½åæ¯ç¨å个模å¼ç¼ç çï¼å³ä½¿åºå±åå¨å¯è½å
å«ç¨åç§åå²çæ¬ç模å¼ç¼ç çè®°å½ã
[^v]: é¤äº MySQLï¼å³ä½¿å¹¶éççå¿
è¦ï¼å®ä¹ç»å¸¸ä¼éåæ´ä¸ªè¡¨ï¼æ£å¦ â[æ档模åä¸ç模å¼çµæ´»æ§](ch3.md#æ档模åä¸ç模å¼çµæ´»æ§)â ä¸ææå°çã
#### å½æ¡£åå¨
ä¹è®¸ä½ ä¸æ¶ä¸ºæ°æ®åºå建ä¸ä¸ªå¿«ç
§ï¼ä¾å¦å¤ä»½æå è½½å°æ°æ®ä»åºï¼è¯·åé
â[æ°æ®ä»åº](ch3.md#æ°æ®ä»åº)âï¼ãå¨è¿ç§æ
åµä¸ï¼å³ä½¿æºæ°æ®åºä¸çåå§ç¼ç å
å«æ¥èªä¸åæ¶ä»£ç模å¼çæ¬çæ··åï¼æ°æ®è½¬å¨é常ä¹å°ä½¿ç¨ææ°æ¨¡å¼è¿è¡ç¼ç ãæ¢ç¶ä½ ä¸ç®¡ææ ·é½è¦æ·è´æ°æ®ï¼é£ä¹ä½ å¯ä»¥å¯¹è¿ä¸ªæ°æ®æ·è´è¿è¡ä¸è´çç¼ç ã
ç±äºæ°æ®è½¬å¨æ¯ä¸æ¬¡åå
¥çï¼èä¸ä»¥åæ¯ä¸å¯åçï¼æ以 Avro 对象容å¨æ件çæ ¼å¼é常éåãè¿ä¹æ¯ä¸ä¸ªå¾å¥½çæºä¼ï¼å¯ä»¥å°æ°æ®ç¼ç 为é¢ååæçåå¼æ ¼å¼ï¼ä¾å¦ Parquetï¼è¯·åé
â[åå缩](ch3.md#åå缩)âï¼ã
å¨ [第åç« ](ch10.md) ä¸ï¼æ们å°è¯¦ç»è®¨è®ºä½¿ç¨æ¡£æ¡åå¨ä¸çæ°æ®ã
### æå¡ä¸çæ°æ®æµï¼RESTä¸RPC
å½ä½ éè¦éè¿ç½ç»è¿è¡éä¿¡çè¿ç¨æ¶ï¼å®æ该éä¿¡çæ¹å¼æå ç§ãæ常è§çå®ææ¯æ两个è§è²ï¼å®¢æ·ç«¯åæå¡å¨ãæå¡å¨éè¿ç½ç»å
¬å¼ APIï¼å¹¶ä¸å®¢æ·ç«¯å¯ä»¥è¿æ¥å°æå¡å¨ä»¥å该 API ååºè¯·æ±ãæå¡å¨å
¬å¼ç API 被称为æå¡ã
Web 以è¿ç§æ¹å¼å·¥ä½ï¼å®¢æ·ï¼Web æµè§å¨ï¼å Web æå¡å¨ååºè¯·æ±ï¼éè¿ GET 请æ±ä¸è½½ HTMLãCSSãJavaScriptãå¾åçï¼å¹¶éè¿ POST 请æ±æ交æ°æ®å°æå¡å¨ã API å
å«ä¸ç»æ åçåè®®åæ°æ®æ ¼å¼ï¼HTTPãURLãSSL/TLSãHTML çï¼ãç±äºç½ç»æµè§å¨ãç½ç»æå¡å¨åç½ç«ä½è
大å¤åæè¿äºæ åï¼ä½ å¯ä»¥ä½¿ç¨ä»»ä½ç½ç»æµè§å¨è®¿é®ä»»ä½ç½ç«ï¼è³å°å¨ç论ä¸ï¼ï¼ã
Web æµè§å¨ä¸æ¯å¯ä¸ç客æ·ç«¯ç±»åãä¾å¦ï¼å¨ç§»å¨è®¾å¤ææ¡é¢è®¡ç®æºä¸è¿è¡çæ¬å°åºç¨ç¨åºä¹å¯ä»¥åæå¡å¨ååºç½ç»è¯·æ±ï¼å¹¶ä¸å¨ Web æµè§å¨å
è¿è¡ç客æ·ç«¯ JavaScript åºç¨ç¨åºå¯ä»¥ä½¿ç¨ XMLHttpRequest æ为 HTTP 客æ·ç«¯ï¼è¯¥ææ¯è¢«ç§°ä¸º Ajax ã30ãï¼ãå¨è¿ç§æ
åµä¸ï¼æå¡å¨çååºé常ä¸æ¯ç¨äºæ¾ç¤ºç»äººç HTMLï¼èæ¯ä¾¿äºå®¢æ·ç«¯åºç¨ç¨åºä»£ç è¿ä¸æ¥å¤ççç¼ç æ°æ®ï¼å¦ JSONï¼ã尽管 HTTP å¯è½è¢«ç¨ä½ä¼ è¾åè®®ï¼ä½é¡¶å±å®ç°ç API æ¯ç¹å®äºåºç¨ç¨åºçï¼å®¢æ·ç«¯åæå¡å¨éè¦å°±è¯¥ API çç»èè¾¾æä¸è´ã
æ¤å¤ï¼æå¡å¨æ¬èº«å¯ä»¥æ¯å¦ä¸ä¸ªæå¡ç客æ·ç«¯ï¼ä¾å¦ï¼å
¸åç Web åºç¨æå¡å¨å
å½æ°æ®åºç客æ·ç«¯ï¼ãè¿ç§æ¹æ³é常ç¨äºå°å¤§ååºç¨ç¨åºæç
§åè½åºåå解为è¾å°çæå¡ï¼è¿æ ·å½ä¸ä¸ªæå¡éè¦æ¥èªå¦ä¸ä¸ªæå¡çæäºåè½ææ°æ®æ¶ï¼å°±ä¼åå¦ä¸ä¸ªæå¡ååºè¯·æ±ãè¿ç§æ建åºç¨ç¨åºçæ¹å¼ä¼ ç»ä¸è¢«ç§°ä¸º **é¢åæå¡çä½ç³»ç»æï¼service-oriented architectureï¼SOAï¼**ï¼æè¿è¢«æ¹è¿åæ´å为 **å¾®æå¡æ¶æ**ã31,32ãã
å¨æäºæ¹é¢ï¼æå¡ç±»ä¼¼äºæ°æ®åºï¼å®ä»¬é常å
许客æ·ç«¯æ交åæ¥è¯¢æ°æ®ãä½æ¯ï¼è½ç¶æ°æ®åºå
许使ç¨æä»¬å¨ [第äºç« ](ch2.md) ä¸è®¨è®ºçæ¥è¯¢è¯è¨è¿è¡ä»»ææ¥è¯¢ï¼ä½æ¯æå¡å
¬å¼äºä¸ä¸ªç¹å®äºåºç¨ç¨åºç APIï¼å®åªå
许ç±æå¡çä¸å¡é»è¾ï¼åºç¨ç¨åºä»£ç ï¼é¢å®çè¾å
¥åè¾åºã33ããè¿ç§éå¶æä¾äºä¸å®ç¨åº¦çå°è£
ï¼æå¡è½å¤å¯¹å®¢æ·å¯ä»¥åä»ä¹åä¸å¯ä»¥åä»ä¹æ½å ç»ç²åº¦çéå¶ã
é¢åæå¡ / å¾®æå¡æ¶æçä¸ä¸ªå
³é®è®¾è®¡ç®æ æ¯éè¿ä½¿æå¡ç¬ç«é¨ç½²åæ¼åæ¥ä½¿åºç¨ç¨åºæ´æäºæ´æ¹åç»´æ¤ãä¾å¦ï¼æ¯ä¸ªæå¡åºè¯¥ç±ä¸ä¸ªå¢éæ¥æï¼å¹¶ä¸è¯¥å¢éåºè¯¥è½å¤ç»å¸¸åå¸æ°çæ¬çæå¡ï¼èä¸å¿
ä¸å
¶ä»å¢éåè°ãæ¢å¥è¯è¯´ï¼æ们åºè¯¥æææå¡å¨å客æ·ç«¯çæ§çæ¬åæ°çæ¬åæ¶è¿è¡ï¼å æ¤æå¡å¨å客æ·ç«¯ä½¿ç¨çæ°æ®ç¼ç å¿
é¡»å¨ä¸åçæ¬çæå¡ API ä¹é´å
¼å®¹ ââ è¿æ£æ¯æ们å¨æ¬ç« æä¸ç´å¨è°è®ºçã
#### Webæå¡
**å½æå¡ä½¿ç¨ HTTP ä½ä¸ºåºå±éä¿¡åè®®æ¶ï¼å¯ç§°ä¹ä¸º Web æå¡**ãè¿å¯è½æ¯ä¸ä¸ªå°é误ï¼å 为 Web æå¡ä¸ä»
å¨ Web ä¸ä½¿ç¨ï¼èä¸å¨å 个ä¸åçç¯å¢ä¸ä½¿ç¨ãä¾å¦ï¼
1. è¿è¡å¨ç¨æ·è®¾å¤ä¸ç客æ·ç«¯åºç¨ç¨åºï¼ä¾å¦ï¼ç§»å¨è®¾å¤ä¸çæ¬å°åºç¨ç¨åºï¼æä½¿ç¨ Ajax ç JavaScript web åºç¨ç¨åºï¼éè¿ HTTP åæå¡ååºè¯·æ±ãè¿äºè¯·æ±é常éè¿å
Œ
±äºèç½è¿è¡ã
2. ä¸ç§æå¡ååä¸ç»ç»æ¥æçå¦ä¸é¡¹æå¡æåºè¯·æ±ï¼è¿äºæå¡é常ä½äºåä¸æ°æ®ä¸å¿å
ï¼ä½ä¸ºé¢åæå¡ / å¾®æå¡æ¶æçä¸é¨åã ï¼æ¯æè¿ç§ç¨ä¾ç软件ææ¶è¢«ç§°ä¸º **ä¸é´ä»¶ï¼middlewareï¼** ï¼
3. ä¸ç§æå¡éè¿äºèç½åä¸åç»ç»ææ¥æçæå¡æåºè¯·æ±ãè¿ç¨äºä¸åç»ç»å端系ç»ä¹é´çæ°æ®äº¤æ¢ãæ¤ç±»å«å
æ¬ç±å¨çº¿æå¡ï¼å¦ä¿¡ç¨å¡å¤çç³»ç»ï¼æä¾çå
Œ
± APIï¼æç¨äºå
±äº«è®¿é®ç¨æ·æ°æ®ç OAuthã
æ两ç§æµè¡ç Web æå¡æ¹æ³ï¼REST å SOAPãä»ä»¬å¨å²å¦æ¹é¢å ä¹æ¯æªç¶ç¸åçï¼å¾å¾ä¹æ¯åèªæ¯æè
ä¹é´çæ¿ç辩论çä¸»é¢ [^vi]ã
[^vi]: å³ä½¿å¨æ¯ä¸ªéµè¥å
ä¹æå¾å¤äºè®ºã ä¾å¦ï¼**HATEOASï¼è¶
åªä½ä½ä¸ºåºç¨ç¨åºç¶æçå¼æï¼** å°±ç»å¸¸å¼å讨论ã35ãã
REST ä¸æ¯ä¸ä¸ªåè®®ï¼èæ¯ä¸ä¸ªåºäº HTTP ååç设计å²å¦ã34,35ããå®å¼ºè°ç®åçæ°æ®æ ¼å¼ï¼ä½¿ç¨ URL æ¥æ è¯èµæºï¼å¹¶ä½¿ç¨ HTTP åè½è¿è¡ç¼åæ§å¶ï¼èº«ä»½éªè¯åå
容类åååãä¸ SOAP ç¸æ¯ï¼REST å·²ç»è¶æ¥è¶å欢è¿ï¼è³å°å¨è·¨ç»ç»æå¡éæçèæ¯ä¸ã36ãï¼å¹¶ç»å¸¸ä¸å¾®æå¡ç¸å
³ã31ããæ ¹æ® REST åå设计ç API 称为 RESTfulã
ç¸æ¯ä¹ä¸ï¼SOAP æ¯ç¨äºå¶ä½ç½ç» API 请æ±çåºäº XML çåè®® [^vii]ãè½ç¶å®æ常ç¨äº HTTPï¼ä½å
¶ç®çæ¯ç¬ç«äº HTTPï¼å¹¶é¿å
使ç¨å¤§å¤æ° HTTP åè½ãç¸åï¼å®å¸¦æåºå¤§èå¤æçå¤ç§ç¸å
³æ åï¼Web æå¡æ¡æ¶ï¼ç§°ä¸º `WS-*`ï¼ï¼å®ä»¬å¢å äºåç§åè½ã37ãã
[^vii]: 尽管é¦åæ¯ç¼©åè¯ç¸ä¼¼ï¼SOAP 并ä¸æ¯ SOA çè¦æ±ã SOAP æ¯ä¸ç§ç¹æ®çææ¯ï¼è SOA æ¯æ建系ç»çä¸è¬æ¹æ³ã
SOAP Web æå¡ç API 使ç¨ç§°ä¸º Web æå¡æè¿°è¯è¨ï¼WSDLï¼çåºäº XML çè¯è¨æ¥æè¿°ã WSDL æ¯æ代ç çæï¼å®¢æ·ç«¯å¯ä»¥ä½¿ç¨æ¬å°ç±»åæ¹æ³è°ç¨ï¼ç¼ç 为 XML æ¶æ¯å¹¶ç±æ¡æ¶å次解ç ï¼è®¿é®è¿ç¨æå¡ãè¿å¨éæç±»åç¼ç¨è¯è¨ä¸é常æç¨ï¼ä½å¨å¨æç±»åç¼ç¨è¯è¨ä¸å¾å°ï¼è¯·åé
â[代ç çæåå¨æç±»åçè¯è¨](#代ç çæåå¨æç±»åçè¯è¨)âï¼ã
ç±äº WSDL ç设计ä¸æ¯äººç±»å¯è¯»çï¼èä¸ç±äº SOAP æ¶æ¯é常å 为è¿äºå¤æèæ æ³æå¨æ建ï¼æ以 SOAP çç¨æ·å¨å¾å¤§ç¨åº¦ä¸ä¾èµäºå·¥å
·æ¯æï¼ä»£ç çæå IDEã38ããå¯¹äº SOAP ä¾åºåä¸æ¯æçç¼ç¨è¯è¨çç¨æ·æ¥è¯´ï¼ä¸ SOAP æå¡çéææ¯å°é¾çã
尽管 SOAP åå
¶åç§æ©å±è¡¨é¢ä¸æ¯æ ååçï¼ä½æ¯ä¸åååçå®ç°ä¹é´çäºæä½æ§å¾å¾ä¼é æé®é¢ã39ããç±äºææè¿äºåå ï¼å°½ç®¡è®¸å¤å¤§åä¼ä¸ä»ç¶ä½¿ç¨ SOAPï¼ä½å¨å¤§å¤æ°å°å
¬å¸ä¸å·²ç»ä¸ååå°éçã
REST é£æ ¼ç API å¾åäºæ´ç®åçæ¹æ³ï¼é常æ¶åè¾å°ç代ç çæåèªå¨åå·¥å
·ãå®ä¹æ ¼å¼ï¼å¦ OpenAPIï¼ä¹ç§°ä¸º Swagger ã40ãï¼å¯ç¨äºæè¿° RESTful API 并çæææ¡£ã
#### è¿ç¨è¿ç¨è°ç¨ï¼RPCï¼çé®é¢
Web æå¡ä»
ä»
æ¯éè¿ç½ç»è¿è¡ API 请æ±çä¸ç³»åææ¯çææ°çæ¬ï¼å
¶ä¸è®¸å¤ææ¯åå°äºå¤§éççä½ï¼ä½æ¯åå¨ä¸¥éçé®é¢ã Enterprise JavaBeansï¼EJBï¼å Java ç **è¿ç¨æ¹æ³è°ç¨ï¼RMIï¼** ä»
éäº Javaã**åå¸å¼ç»ä»¶å¯¹è±¡æ¨¡åï¼DCOMï¼** ä»
éäº Microsoft å¹³å°ã**å
Œ
±å¯¹è±¡è¯·æ±ä»£çä½ç³»ç»æï¼CORBAï¼** è¿äºå¤æï¼ä¸æä¾ååæååå
¼å®¹æ§ã41ãã
ææè¿äºé½æ¯åºäº **è¿ç¨è¿ç¨è°ç¨ï¼RPCï¼** çææ³ï¼è¯¥è¿ç¨è°ç¨èª 20 ä¸çºª 70 年代以æ¥ä¸ç´åå¨ã42ãã RPC 模åè¯å¾åè¿ç¨ç½ç»æå¡ååºè¯·æ±ï¼çèµ·æ¥ä¸å¨åä¸è¿ç¨ä¸è°ç¨ç¼ç¨è¯è¨ä¸çå½æ°ææ¹æ³ç¸åï¼è¿ç§æ½è±¡ç§°ä¸ºä½ç½®éæï¼ã尽管 RPC èµ·åçèµ·æ¥å¾æ¹ä¾¿ï¼ä½è¿ç§æ¹æ³æ ¹æ¬ä¸æ¯æ缺é·çã43,44ããç½ç»è¯·æ±ä¸æ¬å°å½æ°è°ç¨é常ä¸åï¼
* æ¬å°å½æ°è°ç¨æ¯å¯é¢æµçï¼å¹¶ä¸æåæ失败ä»
åå³äºåä½ æ§å¶çåæ°ãç½ç»è¯·æ±æ¯ä¸å¯é¢ç¥çï¼ç±äºç½ç»é®é¢ï¼è¯·æ±æååºå¯è½ä¼ä¸¢å¤±ï¼æè
è¿ç¨è®¡ç®æºå¯è½å¾æ
¢æä¸å¯ç¨ï¼è¿äºé®é¢å®å
¨ä¸å¨ä½ çæ§å¶èå´ä¹å
ãç½ç»é®é¢æ¯å¸¸è§çï¼æä»¥ä½ å¿
é¡»é¢æµä»ä»¬ï¼ä¾å¦éè¿éè¯å¤±è´¥ç请æ±ã
* æ¬å°å½æ°è°ç¨è¦ä¹è¿åç»æï¼è¦ä¹æåºå¼å¸¸ï¼æè
æ°¸è¿ä¸è¿åï¼å 为è¿å
¥æ é循ç¯æè¿ç¨å´©æºï¼ãç½ç»è¯·æ±æå¦ä¸ä¸ªå¯è½çç»æï¼ç±äºè¶
æ¶ï¼å®å¯è½ä¼è¿å没æç»æãå¨è¿ç§æ
åµä¸ï¼ä½ æ ¹æ¬ä¸ç¥éåçäºä»ä¹ï¼å¦æä½ æ²¡æå¾å°æ¥èªè¿ç¨æå¡çååºï¼ä½ æ æ³ç¥é请æ±æ¯å¦éè¿ï¼æ们å°å¨ [第å
«ç« ](ch8.md) æ´è¯¦ç»å°è®¨è®ºè¿ä¸ªé®é¢ï¼ã
* å¦æä½ éè¯å¤±è´¥çç½ç»è¯·æ±ï¼å¯è½ä¼åç请æ±å®é
ä¸æ£å¨éè¿ï¼åªæååºä¸¢å¤±ãå¨è¿ç§æ
åµä¸ï¼éè¯å°å¯¼è´è¯¥æä½è¢«æ§è¡å¤æ¬¡ï¼é¤éä½ å¨åè®®ä¸å¼å
¥å»éæºå¶ï¼**å¹ç**ï¼å³ idempotenceï¼ãæ¬å°å½æ°è°ç¨æ²¡æè¿ä¸ªé®é¢ã ï¼å¨ [第åä¸ç« ](ch11.md) æ´è¯¦ç»å°è®¨è®ºå¹çæ§ï¼
* æ¯æ¬¡è°ç¨æ¬å°åè½æ¶ï¼é常éè¦å¤§è´ç¸åçæ¶é´æ¥æ§è¡ãç½ç»è¯·æ±æ¯å½æ°è°ç¨è¦æ
¢å¾å¤ï¼èä¸å
¶å»¶è¿ä¹æ¯é常å¯åçï¼å¥½çæ¶åå®å¯è½ä¼å¨ä¸å°ä¸æ¯«ç§çæ¶é´å
å®æï¼ä½æ¯å½ç½ç»æ¥å¡æè
è¿ç¨æå¡è¶
è½½æ¶ï¼å¯è½éè¦å ç§éçæ¶é´å®æä¸æ ·çä¸è¥¿ã
* è°ç¨æ¬å°å½æ°æ¶ï¼å¯ä»¥é«æå°å°å¼ç¨ï¼æéï¼ä¼ éç»æ¬å°å
åä¸ç对象ãå½ä½ ååºä¸ä¸ªç½ç»è¯·æ±æ¶ï¼ææè¿äºåæ°é½éè¦è¢«ç¼ç æå¯ä»¥éè¿ç½ç»åéçä¸ç³»ååèãå¦æåæ°æ¯åæ°åæå符串è¿æ ·çåºæ¬ç±»ååæ¯æ²¡å
³ç³»ï¼ä½æ¯å¯¹äºè¾å¤§ç对象å¾å¿«å°±ä¼åæé®é¢ã
* 客æ·ç«¯åæå¡å¯ä»¥ç¨ä¸åçç¼ç¨è¯è¨å®ç°ï¼æ以 RPC æ¡æ¶å¿
é¡»å°æ°æ®ç±»åä»ä¸ç§è¯è¨ç¿»è¯æå¦ä¸ç§è¯è¨ãè¿å¯è½ä¼æ
åºå¤§ç¯åï¼å 为ä¸æ¯ææçè¯è¨é½å
·æç¸åçç±»å ââ ä¾å¦åæ³ä¸ä¸ JavaScript çæ°åå¤§äº $2^{53}$ çé®é¢ï¼è¯·åé
â[JSONãXML åäºè¿å¶åä½](#JSONãXMLåäºè¿å¶åä½)âï¼ãç¨åä¸è¯è¨ç¼åçå个è¿ç¨ä¸ä¸åå¨æ¤é®é¢ã
ææè¿äºå ç´ æå³çå°è¯ä½¿è¿ç¨æå¡çèµ·æ¥åç¼ç¨è¯è¨ä¸çæ¬å°å¯¹è±¡ä¸æ ·æ¯«æ æä¹ï¼å 为è¿æ¯ä¸ä¸ªæ ¹æ¬ä¸åçäºæ
ã REST çé¨åå¸å¼åå¨äºï¼å®å¹¶ä¸è¯å¾éèå®æ¯ä¸ä¸ªç½ç»åè®®çäºå®ï¼å°½ç®¡è¿ä¼¼ä¹å¹¶æ²¡æé»æ¢äººä»¬å¨ REST ä¹ä¸æ建 RPC åºï¼ã
#### RPCçå½åæ¹å
尽管æè¿æ ·é£æ ·çé®é¢ï¼RPC ä¸ä¼æ¶å¤±ãå¨æ¬ç« æå°çææç¼ç çåºç¡ä¸æ建äºåç§ RPC æ¡æ¶ï¼ä¾å¦ï¼Thrift å Avro 带æ RPC æ¯æï¼gRPC æ¯ä½¿ç¨ Protocol Buffers ç RPC å®ç°ï¼Finagle ä¹ä½¿ç¨ Thriftï¼Rest.li ä½¿ç¨ JSON over HTTPã
è¿ç§æ°ä¸ä»£ç RPC æ¡æ¶æ´å æç¡®çæ¯ï¼è¿ç¨è¯·æ±ä¸æ¬å°å½æ°è°ç¨ä¸åãä¾å¦ï¼Finagle å Rest.li ä½¿ç¨ futuresï¼promisesï¼æ¥å°è£
å¯è½å¤±è´¥çå¼æ¥æä½ã`Futures` è¿å¯ä»¥ç®åéè¦å¹¶è¡ååºå¤é¡¹æå¡å¹¶å°å
¶ç»æå并çæ
åµã45ãã gRPC æ¯ææµï¼å
¶ä¸ä¸ä¸ªè°ç¨ä¸ä»
å
æ¬ä¸ä¸ªè¯·æ±åä¸ä¸ªååºï¼è¿å¯ä»¥æ¯éæ¶é´çä¸ç³»å请æ±åååºã46ãã
å
¶ä¸ä¸äºæ¡æ¶è¿æä¾æå¡åç°ï¼å³å
许客æ·ç«¯æ¾åºå¨åªä¸ª IP å°åå端å£å·ä¸å¯ä»¥æ¾å°ç¹å®çæå¡ãæ们å°å¨ â[请æ±è·¯ç±](ch6.md#请æ±è·¯ç±)â ä¸åå°è¿ä¸ªä¸»é¢ã
使ç¨äºè¿å¶ç¼ç æ ¼å¼çèªå®ä¹ RPC åè®®å¯ä»¥å®ç°æ¯éç¨ç JSON over REST æ´å¥½çæ§è½ãä½æ¯ï¼RESTful API è¿æå
¶ä»ä¸äºæ¾èçä¼ç¹ï¼æ¹ä¾¿å®éªåè°è¯ï¼åªéä½¿ç¨ Web æµè§å¨æå½ä»¤è¡å·¥å
· curlï¼æ éä»»ä½ä»£ç çææ软件å®è£
å³å¯åå
¶è¯·æ±ï¼ï¼è½è¢«ææ主æµçç¼ç¨è¯è¨åå¹³å°ææ¯æï¼è¿æ大éå¯ç¨çå·¥å
·ï¼æå¡å¨ï¼ç¼åï¼è´è½½å¹³è¡¡å¨ï¼ä»£çï¼é²ç«å¢ï¼çæ§ï¼è°è¯å·¥å
·ï¼æµè¯å·¥å
·çï¼ççæç³»ç»ã
ç±äºè¿äºåå ï¼REST ä¼¼ä¹æ¯å
Œ
± API ç主è¦é£æ ¼ã RPC æ¡æ¶ç主è¦éç¹å¨äºåä¸ç»ç»æ¥æçæå¡ä¹é´ç请æ±ï¼é常å¨åä¸æ°æ®ä¸å¿å
ã
#### æ°æ®ç¼ç ä¸RPCçæ¼å
对äºå¯æ¼åæ§ï¼éè¦çæ¯å¯ä»¥ç¬ç«æ´æ¹åé¨ç½² RPC 客æ·ç«¯åæå¡å¨ãä¸éè¿æ°æ®åºæµå¨çæ°æ®ç¸æ¯ï¼å¦ä¸ä¸èæè¿°ï¼ï¼æ们å¯ä»¥å¨éè¿æå¡è¿è¡æ°æ®æµçæ
åµä¸åä¸ä¸ªç®åçå设ï¼åå®ææçæå¡å¨é½ä¼å
æ´æ°ï¼å
¶æ¬¡æ¯ææç客æ·ç«¯ãå æ¤ï¼ä½ åªéè¦å¨è¯·æ±ä¸å
·æååå
¼å®¹æ§ï¼å¹¶ä¸å¯¹ååºå
·æååå
¼å®¹æ§ã
RPC æ¹æ¡çåååå
¼å®¹æ§å±æ§ä»å®ä½¿ç¨çç¼ç æ¹å¼ä¸ç»§æ¿ï¼
* ThriftãgRPCï¼Protobufï¼å Avro RPC å¯ä»¥æ ¹æ®ç¸åºç¼ç æ ¼å¼çå
¼å®¹æ§è§åè¿è¡æ¼åã
* å¨ SOAP ä¸ï¼è¯·æ±åååºæ¯ä½¿ç¨ XML 模å¼æå®çãè¿äºå¯ä»¥æ¼åï¼ä½æä¸äºå¾®å¦çé·é±ã47ãã
* RESTful API éå¸¸ä½¿ç¨ JSONï¼æ²¡ææ£å¼æå®ç模å¼ï¼ç¨äºååºï¼ä»¥åç¨äºè¯·æ±ç JSON æ URI ç¼ç / 表åç¼ç ç请æ±åæ°ãæ·»å å¯éç请æ±åæ°å¹¶åååºå¯¹è±¡æ·»å æ°çå段é常被认为æ¯ä¿æå
¼å®¹æ§çæ¹åã
ç±äº RPC ç»å¸¸è¢«ç¨äºè·¨è¶ç»ç»è¾¹ççéä¿¡ï¼æ以æå¡çå
¼å®¹æ§åå¾æ´å å°é¾ï¼å æ¤æå¡çæä¾è
ç»å¸¸æ æ³æ§å¶å
¶å®¢æ·ï¼ä¹ä¸è½å¼ºè¿«ä»ä»¬å级ãå æ¤ï¼éè¦é¿æä¿æå
¼å®¹æ§ï¼ä¹è®¸æ¯æ éæçãå¦æéè¦è¿è¡å
¼å®¹æ§æ´æ¹ï¼åæå¡æä¾åé常ä¼å¹¶æç»´æ¤å¤ä¸ªçæ¬çæå¡ APIã
å
³äº API çæ¬ååºè¯¥å¦ä½å·¥ä½ï¼å³ï¼å®¢æ·ç«¯å¦ä½æ示å®æ³è¦ä½¿ç¨åªä¸ªçæ¬ç APIï¼æ²¡æä¸è´æè§ã48ãï¼ãå¯¹äº RESTful APIï¼å¸¸ç¨çæ¹æ³æ¯å¨ URL æ HTTP Accept 头ä¸ä½¿ç¨çæ¬å·ã对äºä½¿ç¨ API å¯é¥æ¥æ è¯ç¹å®å®¢æ·ç«¯çæå¡ï¼å¦ä¸ç§éæ©æ¯å°å®¢æ·ç«¯è¯·æ±ç API çæ¬åå¨å¨æå¡å¨ä¸ï¼å¹¶å
许éè¿åç¬ç管ççé¢æ´æ°è¯¥çæ¬é项ã49ãã
### æ¶æ¯ä¼ éä¸çæ°æ®æµ
æ们ä¸ç´å¨ç 究ä»ä¸ä¸ªè¿ç¨å°å¦ä¸ä¸ªè¿ç¨çç¼ç æ°æ®æµçä¸åæ¹å¼ãå°ç®å为æ¢ï¼æ们已ç»è®¨è®ºäº REST å RPCï¼å
¶ä¸ä¸ä¸ªè¿ç¨éè¿ç½ç»åå¦ä¸ä¸ªè¿ç¨åé请æ±å¹¶ææå°½å¯è½å¿«çååºï¼ä»¥åæ°æ®åºï¼ä¸ä¸ªè¿ç¨åå
¥ç¼ç æ°æ®ï¼å¦ä¸ä¸ªè¿ç¨å¨å°æ¥å次读åï¼ã
å¨æåä¸èä¸ï¼æ们å°ç®è¦ä»ç»ä¸ä¸ RPC åæ°æ®åºä¹é´çå¼æ¥æ¶æ¯ä¼ éç³»ç»ãå®ä»¬ä¸ RPC 类似ï¼å 为客æ·ç«¯ç请æ±ï¼é常称为æ¶æ¯ï¼ä»¥ä½å»¶è¿ä¼ éå°å¦ä¸ä¸ªè¿ç¨ãå®ä»¬ä¸æ°æ®åºç±»ä¼¼ï¼ä¸æ¯éè¿ç´æ¥çç½ç»è¿æ¥åéæ¶æ¯ï¼èæ¯éè¿ç§°ä¸ºæ¶æ¯ä»£çï¼ä¹ç§°ä¸ºæ¶æ¯éåæé¢åæ¶æ¯çä¸é´ä»¶ï¼çä¸ä»æ¥ä¸´æ¶åå¨æ¶æ¯ã
ä¸ç´æ¥ RPC ç¸æ¯ï¼ä½¿ç¨æ¶æ¯ä»£çæå 个ä¼ç¹ï¼
* å¦ææ¶ä»¶äººä¸å¯ç¨æè¿è½½ï¼å¯ä»¥å
å½ç¼å²åºï¼ä»èæé«ç³»ç»çå¯é æ§ã
* å®å¯ä»¥èªå¨å°æ¶æ¯éæ°åéå°å·²ç»å´©æºçè¿ç¨ï¼ä»èé²æ¢æ¶æ¯ä¸¢å¤±ã
* é¿å
å件人éè¦ç¥éæ¶ä»¶äººç IP å°åå端å£å·ï¼è¿å¨èææºç»å¸¸åºå
¥çäºé¨ç½²ä¸ç¹å«æç¨ï¼ã
* å®å
许å°ä¸æ¡æ¶æ¯åéç»å¤ä¸ªæ¶ä»¶äººã
* å°å件人ä¸æ¶ä»¶äººé»è¾å离ï¼å件人åªæ¯åå¸é®ä»¶ï¼ä¸å
³å¿ä½¿ç¨è
ï¼ã
ç¶èï¼ä¸ RPC ç¸æ¯ï¼å·®å¼å¨äºæ¶æ¯ä¼ ééä¿¡é常æ¯ååçï¼åéè
é常ä¸æææ¶å°å
¶æ¶æ¯çåå¤ãä¸ä¸ªè¿ç¨å¯è½åéä¸ä¸ªååºï¼ä½è¿é常æ¯å¨ä¸ä¸ªåç¬çééä¸å®æçãè¿ç§é信模å¼æ¯å¼æ¥çï¼åéè
ä¸ä¼çå¾
æ¶æ¯è¢«ä¼ éï¼èåªæ¯åéå®ï¼ç¶åå¿è®°å®ã
#### æ¶æ¯ä»£ç
è¿å»ï¼**æ¶æ¯ä»£çï¼Message Brokerï¼** 主è¦æ¯ TIBCOãIBM WebSphere å webMethods çå
¬å¸çåä¸è½¯ä»¶çç§åºãæè¿å RabbitMQãActiveMQãHornetQãNATS å Apache Kafka è¿æ ·çå¼æºå®ç°å·²ç»æµè¡èµ·æ¥ãæ们å°å¨ [第åä¸ç« ](ch11.md) ä¸å¯¹å®ä»¬è¿è¡æ´è¯¦ç»çæ¯è¾ã
详ç»ç交ä»è¯ä¹å å®ç°åé
ç½®èå¼ï¼ä½é常æ
åµä¸ï¼æ¶æ¯ä»£çç使ç¨æ¹å¼å¦ä¸ï¼ä¸ä¸ªè¿ç¨å°æ¶æ¯åéå°æå®çéåæ主é¢ï¼ä»£çç¡®ä¿å°æ¶æ¯ä¼ éç»é£ä¸ªéåæ主é¢çä¸ä¸ªæå¤ä¸ªæ¶è´¹è
æ订é
è
ãå¨åä¸ä¸»é¢ä¸å¯ä»¥æ许å¤ç产è
å许å¤æ¶è´¹è
ã
ä¸ä¸ªä¸»é¢åªæä¾ååæ°æ®æµãä½æ¯ï¼æ¶è´¹è
æ¬èº«å¯è½ä¼å°æ¶æ¯åå¸å°å¦ä¸ä¸ªä¸»é¢ä¸ï¼å æ¤ï¼å¯ä»¥å°å®ä»¬é¾æ¥å¨ä¸èµ·ï¼å°±åæ们å°å¨ [第åä¸ç« ](ch11.md) ä¸çå°çé£æ ·ï¼ï¼æè
åéç»åå§æ¶æ¯çåéè
使ç¨çåå¤éåï¼å
è®¸è¯·æ± / ååºæ°æ®æµï¼ç±»ä¼¼äº RPCï¼ã
æ¶æ¯ä»£çé常ä¸ä¼æ§è¡ä»»ä½ç¹å®çæ°æ®æ¨¡å ââ æ¶æ¯åªæ¯å
å«ä¸äºå
æ°æ®çåèåºåï¼å æ¤ä½ å¯ä»¥ä½¿ç¨ä»»ä½ç¼ç æ ¼å¼ãå¦æç¼ç æ¯åååååå
¼å®¹çï¼ä½ å¯ä»¥çµæ´»å°å¯¹åå¸è
åæ¶è´¹è
çç¼ç è¿è¡ç¬ç«çä¿®æ¹ï¼å¹¶ä»¥ä»»æ顺åºè¿è¡é¨ç½²ã
å¦ææ¶è´¹è
éæ°åå¸æ¶æ¯å°å¦ä¸ä¸ªä¸»é¢ï¼åå¯è½éè¦å°å¿ä¿çæªç¥å段ï¼ä»¥é²æ¢åé¢å¨æ°æ®åºç¯å¢ä¸æè¿°çé®é¢ï¼[å¾ 4-7](img/fig4-7.png)ï¼ã
#### åå¸å¼çActoræ¡æ¶
Actor 模åæ¯å个è¿ç¨ä¸å¹¶åçç¼ç¨æ¨¡åãé»è¾è¢«å°è£
å¨ actor ä¸ï¼èä¸æ¯ç´æ¥å¤ç线ç¨ï¼ä»¥åç«äºæ¡ä»¶ãéå®åæ»éçç¸å
³é®é¢ï¼ãæ¯ä¸ª actor é常代表ä¸ä¸ªå®¢æ·æå®ä½ï¼å®å¯è½æä¸äºæ¬å°ç¶æï¼ä¸ä¸å
¶ä»ä»»ä½è§è²å
±äº«ï¼ï¼å®éè¿åéåæ¥æ¶å¼æ¥æ¶æ¯ä¸å
¶ä»è§è²éä¿¡ãä¸ä¿è¯æ¶æ¯ä¼ éï¼å¨æäºé误æ
åµä¸ï¼æ¶æ¯å°ä¸¢å¤±ãç±äºæ¯ä¸ªè§è²ä¸æ¬¡åªè½å¤çä¸æ¡æ¶æ¯ï¼å æ¤ä¸éè¦æ
å¿çº¿ç¨ï¼æ¯ä¸ªè§è²å¯ä»¥ç±æ¡æ¶ç¬ç«è°åº¦ã
å¨åå¸å¼ Actor æ¡æ¶ä¸ï¼æ¤ç¼ç¨æ¨¡åç¨äºè·¨å¤ä¸ªèç¹ä¼¸ç¼©åºç¨ç¨åºãä¸ç®¡åéæ¹åæ¥æ¶æ¹æ¯å¨åä¸ä¸ªèç¹ä¸è¿æ¯å¨ä¸åçèç¹ä¸ï¼é½ä½¿ç¨ç¸åçæ¶æ¯ä¼ éæºå¶ãå¦æå®ä»¬å¨ä¸åçèç¹ä¸ï¼å该æ¶æ¯è¢«éæå°ç¼ç æåèåºåï¼éè¿ç½ç»åéï¼å¹¶å¨å¦ä¸ä¾§è§£ç ã
ä½ç½®éæå¨ actor 模åä¸æ¯å¨ RPC ä¸æææ´å¥½ï¼å 为 actor 模åå·²ç»åå®æ¶æ¯å¯è½ä¼ä¸¢å¤±ï¼å³ä½¿å¨å个è¿ç¨ä¸ä¹æ¯å¦æ¤ã尽管ç½ç»ä¸ç延è¿å¯è½æ¯åä¸ä¸ªè¿ç¨ä¸ç延è¿æ´é«ï¼ä½æ¯å¨ä½¿ç¨ actor 模åæ¶ï¼æ¬å°åè¿ç¨éä¿¡ä¹é´çåºæ¬ä¸å¹é
æ¯è¾å°çã
åå¸å¼ç Actor æ¡æ¶å®è´¨ä¸æ¯å°æ¶æ¯ä»£çå actor ç¼ç¨æ¨¡åéæå°ä¸ä¸ªæ¡æ¶ä¸ãä½æ¯ï¼å¦æè¦æ§è¡åºäº actor çåºç¨ç¨åºçæ»å¨å级ï¼åä»ç¶éè¦æ
å¿åååååå
¼å®¹æ§é®é¢ï¼å 为æ¶æ¯å¯è½ä¼ä»è¿è¡æ°çæ¬çèç¹åéå°è¿è¡æ§çæ¬çèç¹ï¼åä¹äº¦ç¶ã
ä¸ä¸ªæµè¡çåå¸å¼ actor æ¡æ¶å¤çæ¶æ¯ç¼ç å¦ä¸ï¼
* é»è®¤æ
åµä¸ï¼Akka ä½¿ç¨ Java çå
ç½®åºååï¼ä¸æä¾ååæååå
¼å®¹æ§ã ä½æ¯ï¼ä½ å¯ä»¥ç¨ç±»ä¼¼ Prototol Buffers çä¸è¥¿æ¿ä»£å®ï¼ä»èè·å¾æ»å¨å级çè½åã50ãã
* Orleans é»è®¤ä½¿ç¨ä¸æ¯ææ»å¨å级é¨ç½²çèªå®ä¹æ°æ®ç¼ç æ ¼å¼ï¼è¦é¨ç½²æ°çæ¬çåºç¨ç¨åºï¼ä½ éè¦è®¾ç½®ä¸ä¸ªæ°çé群ï¼å°æµéä»æ§é群è¿ç§»å°æ°é群ï¼ç¶åå
³éæ§é群ã51,52ãã å Akka ä¸æ ·ï¼å¯ä»¥ä½¿ç¨èªå®ä¹åºååæ件ã
* å¨ Erlang OTP ä¸ï¼å¯¹è®°å½æ¨¡å¼è¿è¡æ´æ¹æ¯é常å°é¾çï¼å°½ç®¡ç³»ç»å
·æ许å¤ä¸ºé«å¯ç¨æ§è®¾è®¡çåè½ï¼ã æ»å¨å级æ¯å¯è½çï¼ä½éè¦ä»ç»è®¡åã53ãã ä¸ä¸ªæ°çå®éªæ§ç `maps` æ°æ®ç±»åï¼2014 å¹´å¨ Erlang R17 ä¸å¼å
¥çç±»ä¼¼äº JSON çç»æï¼å¯è½ä½¿å¾è¿ä¸ªæ°æ®ç±»åå¨æªæ¥æ´å®¹æã54ãã
## æ¬ç« å°ç»
å¨æ¬ç« ä¸ï¼æ们ç 究äºå°æ°æ®ç»æ转æ¢ä¸ºç½ç»ä¸çåèæç£çä¸çåèçå ç§æ¹æ³ãæ们çå°äºè¿äºç¼ç çç»èä¸ä»
å½±åå
¶æçï¼æ´éè¦çæ¯ä¹å½±åäºåºç¨ç¨åºçä½ç³»ç»æåé¨ç½²å®ä»¬çé项ã
ç¹å«æ¯ï¼è®¸å¤æå¡éè¦æ¯ææ»å¨å级ï¼å
¶ä¸æ°çæ¬çæå¡éæ¥é¨ç½²å°å°æ°èç¹ï¼èä¸æ¯åæ¶é¨ç½²å°ææèç¹ãæ»å¨å级å
许å¨ä¸åæºçæ
åµä¸åå¸æ°çæ¬çæå¡ï¼ä»èé¼å±å¨ç½è§ç大åçæ¬ä¸é¢ç¹åå¸å°åçæ¬ï¼ï¼å¹¶ä½¿é¨ç½²é£é©éä½ï¼å
许å¨å½±å大éç¨æ·ä¹åæ£æµå¹¶åæ»ææ
éççæ¬ï¼ãè¿äºå±æ§å¯¹äºå¯æ¼åæ§ï¼ä»¥å对åºç¨ç¨åºè¿è¡æ´æ¹ç容ææ§é½æ¯é常æå©çã
å¨æ»å¨å级æé´ï¼æåºäºåç§å
¶ä»åå ï¼æ们å¿
é¡»å设ä¸åçèç¹æ£å¨è¿è¡æ们çåºç¨ç¨åºä»£ç çä¸åçæ¬ãå æ¤ï¼å¨ç³»ç»å¨å´æµå¨çæææ°æ®é½æ¯ä»¥æä¾ååå
¼å®¹æ§ï¼æ°ä»£ç å¯ä»¥è¯»åæ§æ°æ®ï¼åååå
¼å®¹æ§ï¼æ§ä»£ç å¯ä»¥è¯»åæ°æ°æ®ï¼çæ¹å¼è¿è¡ç¼ç æ¯éè¦çã
æ们讨论äºå ç§æ°æ®ç¼ç æ ¼å¼åå
¶å
¼å®¹æ§å±æ§ï¼
* ç¼ç¨è¯è¨ç¹å®çç¼ç ä»
éäºåä¸ç¼ç¨è¯è¨ï¼å¹¶ä¸å¾å¾æ æ³æä¾åååååå
¼å®¹æ§ã
* JSONãXML å CSV çææ¬æ ¼å¼é常æ®éï¼å
¶å
¼å®¹æ§åå³äºä½ å¦ä½ä½¿ç¨å®ä»¬ãä»ä»¬æå¯éç模å¼è¯è¨ï¼è¿ææ¶æ¯æç¨çï¼ææ¶æ¯ä¸ä¸ªéç¢ãè¿äºæ ¼å¼å¯¹äºæ°æ®ç±»åæäºæ¨¡ç³ï¼æä»¥ä½ å¿
é¡»å°å¿æ°ååäºè¿å¶å符串ã
* å ThriftãProtocol Buffers å Avro è¿æ ·çäºè¿å¶æ¨¡å¼é©±å¨æ ¼å¼å
许使ç¨æ¸
æ°å®ä¹çåååååå
¼å®¹æ§è¯ä¹è¿è¡ç´§åï¼é«æçç¼ç ãè¿äºæ¨¡å¼å¯ä»¥ç¨äºéæç±»åè¯è¨çææ¡£å代ç çæãä½æ¯ï¼ä»ä»¬æä¸ä¸ªç¼ºç¹ï¼å°±æ¯å¨æ°æ®å¯è¯»ä¹åéè¦å¯¹æ°æ®è¿è¡è§£ç ã
æ们è¿è®¨è®ºäºæ°æ®æµçå ç§æ¨¡å¼ï¼è¯´æäºæ°æ®ç¼ç éè¦æ§çä¸ååºæ¯ï¼
* æ°æ®åºï¼åå
¥æ°æ®åºçè¿ç¨å¯¹æ°æ®è¿è¡ç¼ç ï¼å¹¶ä»æ°æ®åºè¯»åè¿ç¨å¯¹å
¶è¿è¡è§£ç
* RPC å REST APIï¼å®¢æ·ç«¯å¯¹è¯·æ±è¿è¡ç¼ç ï¼æå¡å¨å¯¹è¯·æ±è¿è¡è§£ç 并对ååºè¿è¡ç¼ç ï¼å®¢æ·ç«¯æç»å¯¹ååºè¿è¡è§£ç
* å¼æ¥æ¶æ¯ä¼ éï¼ä½¿ç¨æ¶æ¯ä»£çæåä¸è
ï¼ï¼å
¶ä¸èç¹ä¹é´éè¿åéæ¶æ¯è¿è¡éä¿¡ï¼æ¶æ¯ç±åéè
ç¼ç 并ç±æ¥æ¶è
解ç
æ们å¯ä»¥å°å¿å°å¾åºè¿æ ·çç»è®ºï¼ååå
¼å®¹æ§åæ»å¨å级å¨æç§ç¨åº¦ä¸æ¯å¯ä»¥å®ç°çãæ¿ä½ çåºç¨ç¨åºçæ¼åè¿
éãææ·é¨ç½²ã
## åèæç®
1. â[Java Object Serialization Specification](http://docs.oracle.com/javase/7/docs/platform/serialization/spec/serialTOC.html),â *docs.oracle.com*, 2010.
1. â[Ruby 2.2.0 API Documentation](http://ruby-doc.org/core-2.2.0/),â *ruby-doc.org*, Dec 2014.
1. â[The Python 3.4.3 Standard Library Reference Manual](https://docs.python.org/3/library/pickle.html),â *docs.python.org*, February 2015.
1. â[EsotericSoftware/kryo](https://github.com/EsotericSoftware/kryo),â *github.com*, October 2014.
1. â[CWE-502: Deserialization of Untrusted Data](http://cwe.mitre.org/data/definitions/502.html),â Common Weakness Enumeration, *cwe.mitre.org*, July 30, 2014.
1. Steve Breen: â[What Do WebLogic, WebSphere, JBoss, Jenkins, OpenNMS, and Your Application Have in Common? This Vulnerability](http://foxglovesecurity.com/2015/11/06/what-do-weblogic-websphere-jboss-jenkins-opennms-and-your-application-have-in-common-this-vulnerability/),â *foxglovesecurity.com*, November 6, 2015.
1. Patrick McKenzie: â[What the Rails Security Issue Means for Your Startup](http://www.kalzumeus.com/2013/01/31/what-the-rails-security-issue-means-for-your-startup/),â *kalzumeus.com*, January 31, 2013.
1. Eishay Smith: â[jvm-serializers wiki](https://github.com/eishay/jvm-serializers/wiki),â *github.com*, November 2014.
1. â[XML Is a Poor Copy of S-Expressions](http://c2.com/cgi/wiki?XmlIsaPoorCopyOfEssExpressions),â *c2.com* wiki.
1. Matt Harris: â[Snowflake: An Update and Some Very Important Information](https://groups.google.com/forum/#!topic/twitter-development-talk/ahbvo3VTIYI),â email to *Twitter Development Talk* mailing list, October 19, 2010.
1. Shudi (Sandy) Gao, C. M. Sperberg-McQueen, and Henry S. Thompson: â[XML Schema 1.1](http://www.w3.org/XML/Schema),â W3C Recommendation, May 2001.
1. Francis Galiegue, Kris Zyp, and Gary Court: â[JSON Schema](http://json-schema.org/),â IETF Internet-Draft, February 2013.
1. Yakov Shafranovich: â[RFC 4180: Common Format and MIME Type for Comma-Separated Values (CSV) Files](https://tools.ietf.org/html/rfc4180),â October 2005.
1. â[MessagePack Specification](http://msgpack.org/),â *msgpack.org*. Mark Slee, Aditya Agarwal, and Marc Kwiatkowski: â[Thrift: Scalable Cross-Language Services Implementation](http://thrift.apache.org/static/files/thrift-20070401.pdf),â Facebook technical report, April 2007.
1. â[Protocol Buffers Developer Guide](https://developers.google.com/protocol-buffers/docs/overview),â Google, Inc., *developers.google.com*.
1. Igor Anishchenko: â[Thrift vs Protocol Buffers vs Avro - Biased Comparison](http://www.slideshare.net/IgorAnishchenko/pb-vs-thrift-vs-avro),â *slideshare.net*, September 17, 2012.
1. â[A Matrix of the Features Each Individual Language Library Supports](http://wiki.apache.org/thrift/LibraryFeatures),â *wiki.apache.org*.
1. Martin Kleppmann: â[Schema Evolution in Avro, Protocol Buffers and Thrift](http://martin.kleppmann.com/2012/12/05/schema-evolution-in-avro-protocol-buffers-thrift.html),â *martin.kleppmann.com*, December 5, 2012.
1. â[Apache Avro 1.7.7 Documentation](http://avro.apache.org/docs/1.7.7/),â *avro.apache.org*, July 2014.
1. Doug Cutting, Chad Walters, Jim Kellerman, et al.: â[[PROPOSAL] New Subproject: Avro](http://mail-archives.apache.org/mod_mbox/hadoop-general/200904.mbox/%[email protected]%3E),â email thread on *hadoop-general* mailing list, *mail-archives.apache.org*, April 2009.
1. Tony Hoare: â[Null References: The Billion Dollar Mistake](http://www.infoq.com/presentations/Null-References-The-Billion-Dollar-Mistake-Tony-Hoare),â at *QCon London*, March 2009.
1. Aditya Auradkar and Tom Quiggle: â[Introducing EspressoâLinkedIn's Hot New Distributed Document Store](https://engineering.linkedin.com/espresso/introducing-espresso-linkedins-hot-new-distributed-document-store),â *engineering.linkedin.com*, January 21, 2015.
1. Jay Kreps: â[Putting Apache Kafka to Use: A Practical Guide to Building a Stream Data Platform (Part 2)](http://blog.confluent.io/2015/02/25/stream-data-platform-2/),â *blog.confluent.io*, February 25, 2015.
1. Gwen Shapira: â[The Problem of Managing Schemas](http://radar.oreilly.com/2014/11/the-problem-of-managing-schemas.html),â *radar.oreilly.com*, November 4, 2014.
1. â[Apache Pig 0.14.0 Documentation](http://pig.apache.org/docs/r0.14.0/),â *pig.apache.org*, November 2014.
1. John Larmouth: [*ASN.1Complete*](http://www.oss.com/asn1/resources/books-whitepapers-pubs/larmouth-asn1-book.pdf). Morgan Kaufmann, 1999. ISBN: 978-0-122-33435-1
1. Russell Housley, Warwick Ford, Tim Polk, and David Solo: â[RFC 2459: Internet X.509 Public Key Infrastructure: Certificate and CRL Profile](https://www.ietf.org/rfc/rfc2459.txt),â IETF Network Working Group, Standards Track, January 1999.
1. Lev Walkin: â[Question: Extensibility and Dropping Fields](http://lionet.info/asn1c/blog/2010/09/21/question-extensibility-removing-fields/),â *lionet.info*, September 21, 2010.
1. Jesse James Garrett: â[Ajax: A New Approach to Web Applications](http://www.adaptivepath.com/ideas/ajax-new-approach-web-applications/),â *adaptivepath.com*, February 18, 2005.
1. Sam Newman: *Building Microservices*. O'Reilly Media, 2015. ISBN: 978-1-491-95035-7
1. Chris Richardson: â[Microservices: Decomposing Applications for Deployability and Scalability](http://www.infoq.com/articles/microservices-intro),â *infoq.com*, May 25, 2014.
1. Pat Helland: â[Data on the Outside Versus Data on the Inside](http://cidrdb.org/cidr2005/papers/P12.pdf),â at *2nd Biennial Conference on Innovative Data Systems Research* (CIDR), January 2005.
1. Roy Thomas Fielding: â[Architectural Styles and the Design of Network-Based Software Architectures](https://www.ics.uci.edu/~fielding/pubs/dissertation/fielding_dissertation.pdf),â PhD Thesis, University of California, Irvine, 2000.
1. Roy Thomas Fielding: â[REST APIs Must Be Hypertext-Driven](http://roy.gbiv.com/untangled/2008/rest-apis-must-be-hypertext-driven),â *roy.gbiv.com*, October 20 2008.
1. â[REST in Peace, SOAP](http://royal.pingdom.com/2010/10/15/rest-in-peace-soap/),â *royal.pingdom.com*, October 15, 2010.
1. â[Web Services Standards as of Q1 2007](https://www.innoq.com/resources/ws-standards-poster/),â *innoq.com*, February 2007.
1. Pete Lacey: â[The S Stands for Simple](http://harmful.cat-v.org/software/xml/soap/simple),â *harmful.cat-v.org*, November 15, 2006.
1. Stefan Tilkov: â[Interview: Pete Lacey Criticizes Web Services](http://www.infoq.com/articles/pete-lacey-ws-criticism),â *infoq.com*, December 12, 2006.
1. â[OpenAPI Specification (fka Swagger RESTful API Documentation Specification) Version 2.0](http://swagger.io/specification/),â *swagger.io*, September 8, 2014.
1. Michi Henning: â[The Rise and Fall of CORBA](http://queue.acm.org/detail.cfm?id=1142044),â *ACM Queue*, volume 4, number 5, pages 28â34, June 2006. [doi:10.1145/1142031.1142044](http://dx.doi.org/10.1145/1142031.1142044)
1. Andrew D. Birrell and Bruce Jay Nelson: â[Implementing Remote Procedure Calls](http://www.cs.princeton.edu/courses/archive/fall03/cs518/papers/rpc.pdf),â *ACM Transactions on Computer Systems* (TOCS), volume 2, number 1, pages 39â59, February 1984. [doi:10.1145/2080.357392](http://dx.doi.org/10.1145/2080.357392)
1. Jim Waldo, Geoff Wyant, Ann Wollrath, and Sam Kendall: â[A Note on Distributed Computing](http://m.mirror.facebook.net/kde/devel/smli_tr-94-29.pdf),â Sun Microsystems Laboratories, Inc., Technical Report TR-94-29, November 1994.
1. Steve Vinoski: â[Convenience over Correctness](http://steve.vinoski.net/pdf/IEEE-Convenience_Over_Correctness.pdf),â *IEEE Internet Computing*, volume 12, number 4, pages 89â92, July 2008. [doi:10.1109/MIC.2008.75](http://dx.doi.org/10.1109/MIC.2008.75)
1. Marius Eriksen: â[Your Server as a Function](http://monkey.org/~marius/funsrv.pdf),â at *7th Workshop on Programming Languages and Operating Systems* (PLOS), November 2013. [doi:10.1145/2525528.2525538](http://dx.doi.org/10.1145/2525528.2525538)
1. â[grpc-common Documentation](https://github.com/grpc/grpc-common),â Google, Inc., *github.com*, February 2015.
1. Aditya Narayan and Irina Singh: â[Designing and Versioning Compatible Web Services](http://www.ibm.com/developerworks/websphere/library/techarticles/0705_narayan/0705_narayan.html),â *ibm.com*, March 28, 2007.
1. Troy Hunt: â[Your API Versioning Is Wrong, Which Is Why I Decided to Do It 3 Different Wrong Ways](http://www.troyhunt.com/2014/02/your-api-versioning-is-wrong-which-is.html),â *troyhunt.com*, February 10, 2014.
1. â[API Upgrades](https://stripe.com/docs/upgrades),â Stripe, Inc., April 2015.
1. Jonas Bonér: â[Upgrade in an Akka Cluster](http://grokbase.com/t/gg/akka-user/138wd8j9e3/upgrade-in-an-akka-cluster),â email to *akka-user* mailing list, *grokbase.com*, August 28, 2013.
1. Philip A. Bernstein, Sergey Bykov, Alan Geller, et al.: â[Orleans: Distributed Virtual Actors for Programmability and Scalability](http://research.microsoft.com/pubs/210931/Orleans-MSR-TR-2014-41.pdf),â Microsoft Research Technical Report MSR-TR-2014-41, March 2014.
1. â[Microsoft Project Orleans Documentation](http://dotnet.github.io/orleans/),â Microsoft Research, *dotnet.github.io*, 2015.
1. David Mercer, Sean Hinde, Yinso Chen, and Richard A O'Keefe: â[beginner: Updating Data Structures](http://erlang.org/pipermail/erlang-questions/2007-October/030318.html),â email thread on *erlang-questions* mailing list, *erlang.com*, October 29, 2007.
1. Fred Hebert: â[Postscript: Maps](http://learnyousomeerlang.com/maps),â *learnyousomeerlang.com*, April 9, 2014.
------
| ä¸ä¸ç« | ç®å½ | ä¸ä¸ç« |
| ---------------------------- | ------------------------------- | --------------------------------- |
| [第ä¸ç« ï¼åå¨ä¸æ£ç´¢](ch3.md) | [设计æ°æ®å¯éååºç¨](README.md) | [第äºé¨åï¼åå¸å¼æ°æ®](part-ii.md) |