Distributed Systems笔记-middlewares
CMU 95702 Distributed Systems 筆記。簡單介紹分布式系統中解決 interoperability concern 的幾種方案 Cobra’s CDR, Java serialization 和 XML/JSON。這章整理的比較簡單。
一言以蔽之,middleware 是為了更好的與 remote server 交流。
Interoperability concern
分布式系統里的互操作性問題。
- Big/Little Endian byte ordering may differ
- Floating point representation may differ
- Binary vs Unicode
如果 j=3, binary 表示就是 00…011,而 unicode 表示是 0000000000110011,如果兩端沒有達成一致,那么就會出錯。
The receiver had better know which one we are using。
假設我們用 C++ 寫了 TCP server,那么我們可以寫個 JAVA TCP connection 來連接 server 嗎?可以!
C++ 和 JAVA 都知道怎么 open 一個 TCP connection。
假設 client 把一個 java object 發給了 server,這個 object 的內容可以重新被封裝成 c++ 的 object 嗎?不可以!
三種解決方案
CORBA’s CDR
雙方都知道 message 的 data type 和 order。雙方在交流前都有一個 IDL(Interface description language 接口描述語言),這和 google 的 protocol buffers 差不多。XML, XSDL, WSDL 都可以作為 IDL。
如下面一段 C 的代碼。
| 12345 | struct Person {string name;string place;long year;} |
我們可以讓 CORBA Interface Compiler 來做合適的 marshalling 和 unmarshalling operation,無論是 C 還是 JAVA。
CORBA’s CDR 的特點是 - 非???#xff01;所以傳送的信息不包括 data type,只有表格中的右邊一欄數據。
Java serialization
Java’s serialization 本身可以用來 marshal 和 unmarshal,所以并不需要 IDL。雙方事先也不知道 data type。
如下面一段 Java 的代碼。
| 123456789 | public class Person implements Serializable{string name;string place;long year;public Person(String nm,place,year) {nm=name;this.place=place;this.year=year;}// more methods} |
Java 序列化的特點是有很多 data (如 class name, version number, data type 等)來 describe 真正的 data。
Web Service use of XML
格式:
| 12345 | <p:person xmlns:p=“http://www.andrew.cmu.edu/~mm6”><p:name>Smith</p:name><p:place>London</p:place><p:year>1934</p:year></p:person> |
- 相對前兩種方法來說會比較慢。因為它是 text 形式而前兩種方法是 binary 形式。
- HTTP header 需要聲明 Content-Type: text/xml; charset: ISO-8859-1
- 可以表示任何 binary message,因為 binary data(圖片和其它加密的元素)可以被表示成 Base64
- 必須遵循 XSDL 的語法。
- 支持各平臺。
Web Service use of JSON
格式:
| 1234 | { “person” : { “name” : “Smith”“place”:”London”“year”:”1934”}} |
- 可以表示任何 binary message,因為 binary data(圖片和其它加密的元素)可以被表示成 Base64
- 必須遵循 JSON 的語法。
比較
- Marshalling and external data representation
binary, xml/json text - Interoperability
corba flexibility, java requires both sides, xml/json interoperable - Security
- Reliability
TCP: reliable as it checks if the message is arrived
UDP: not reliable - Performance
corba > java > xml/json(package and unpackage) - Remote references
- Full OOP
- Describe how the protocols of the internet allow for heterogeneity
- Describe how middleware allows for heterogenity
hides low level implementation
Pass pointers
在分布式的 OOP 中,我們需要傳送 pointers,包括以下信息。
UDP Based Request-Reply Protocol
直接上圖和代碼。
代碼:
| 123456789101112131415 | Client side:public byte[] doOperation (RemoteObjectRef o, int methodId, byte[] arguments)sends a request message to the remote object and returns the reply.The arguments specify the remote object, the method to be invoked and thearguments of that method.Server side:public byte[] getRequest ();acquires a client request via the server port.coolOperationselect object, execute, methodpublic void sendReply (byte[] reply, InetAddress clientHost, int clientPort);sends the reply message reply to the client at its Internet address and port. |
Failure model
doOperation 可能在 waiting 的時候 timeout,我們要做什么?
- 返回給 caller 一個錯誤信息
- response 可能會丟失,所以我們告訴 client 讓 client try and try 直到確認服務器掛了。這帶來的結果是 client 可能會收到同樣的信息。
Handle duplicates
根據 client 的 acknowledgement 來清空歷史。
Request-Reply Message Structure
| 12345 | messageType: int (0=Request, 1=Reply)requestId: intobjectReference: RemoteObjectRefmethodId: int or Methodargument: array of bytes |
原文地址: http://www.shuang0420.com/2016/11/02/Web-service-middlewares/
總結
以上是生活随笔為你收集整理的Distributed Systems笔记-middlewares的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Distributed Systems笔
- 下一篇: Distributed Systems笔