# 第åç« ï¼ç·¨ç¢¼èæ¼å
![](../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]: é¤ä¸äºç¹æ®æ
æ³å¤ï¼ä¾å¦æäºè¨æ¶é«å°æ æªæ¡æç´æ¥å¨å£ç¸®è³æä¸æä½ï¼å¦ â[åå£ç¸®](ch3.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ï¼å³ä½¿ä¸¦éççå¿
è¦ï¼å®ä¹ç¶å¸¸æé寫æ´å表ï¼æ£å¦ â[æ件模åä¸ç模å¼éæ´»æ§](ch2.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*.
1. 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) |