xmlrpc

xml rpc是使用http協定做為傳輸協定rpc機制,使用xml文本的方式傳輸命令和數據。一個rpc系統,必然包括2個部分:1.rpc client,用來向rpc server調用方法,並接收方法的返回數據;2.rpc server,用於回響rpc client的請求,執行方法,並回送方法執行結果。RPC是Remote Procedure Call的縮寫,翻譯成中文就是遠程過程調用,是一種在本地的機器上調用遠端機器上的一個過程(方法)的技術,這個過程也被大家稱為“分散式計算”,是為了提高各個分立機器的“互操作性”而發明出來的技術。

基本介紹

  • 外文名:xmlrpc
  • 使用:http協定
  • 提高:各個分立機器的“互操作性
  • 建立:在SUN RPC技術基礎之上的
簡介,工作原理,對應關係,可用版本,實現版本,相關信息,例子,中文版,

簡介

按照“數據即程式”的觀點來看,RPC無非是藉助一些通信手段來互相傳遞數據(信息),所以它也是“高”層次的通信手段,無非是這種通信手段看起來更像是“過程的調用”,因為它往往以一個“函式”的面目示人,從而掩蓋了她交換信息的實質。
在各種RPC技術中,我想應該以Sun的RPC最為著名,比較流行的網路檔案系統NFS就是建立在SUN RPC技術基礎之上的。
XMLRPC,顧名思義就是套用了XML(標準通用標記語言的子集)技術的RPC。
在Sun RPC中,調用雙方傳遞的數據是二進制的,而在XMLRPC中數據將是XML格式的。那么為什麼用XML而不用二進制呢?我想一方面應該是為了兼容更多的語言,因為這個世界上除了C/C++等編譯語言,還有很多類似python,perljavascript等的腳本語言(有些文章也稱其為“動態語言”,因為他們通常不需要自己管理記憶體),另一方面是為了隔離作業系統的差異,比如說Little Endian和Big Endian的差異等。基於種種原因,XMLRPC選擇了XML這種中間語言作為其信息的格式,然後由各個語言負責將其轉變成各自native(本土)的數據類型。關於為了兼容各個語言所發明的中間語言還有IDL(Interface Definition Language:接口定義語言),它被用於CORBA接口的定義。

工作原理

完整的需要參考xmlrpc規範
簡單描述:
rpcclient的工作原理:rpcclient根據URL找到rpcserver -> 構造命令包,調用rpcserver上的某個服務的某個方法 -> 接收到rpcserver的返回,解析回響包,拿出調用的返回結果。
rpcserver的工作原理:啟動一個webserver(在使用內置的webserver的情況下) -> 註冊每個能提供的服務,每個服務對應一個Handler類 ->進入服務監聽狀態

對應關係

參考附錄中test2.XmlRPCClient類與test2.JavaServer類。
在這裡簡單描述一下:
>xmlrpc中的Array類型,對應到java中的Vector類型
例如:在RPC Server中的某個服務方法的返回值的聲明類型是String[],但在Client中接收到的將是Vector對象;
反之,如果Client傳送過去的調用參數為String[],但在RPC Server中所接收到的將是Vector對象
當然,如果我們不使用String[],直接聲明為Vector,也是可以的。
>xmlrpc中的struct類型,對應到java中的Hashtable類型
> 其它的類型,如:String,int,double,boolean,Date等,都比較好對應。需要注意的是:在rpc Client中,如果使用到int/double/boolean這些基本類型時,需要將他們封裝成一個相應的Object,例如:Integer /Double/Boolean

可用版本

xml rpc client和xml rpc server都有很多版本的實現。一般而言,一個實現版本都會同時實現client/server。但由於都滿足xml rpc規範,從理論上講,任何一個版本的rpc client實現與任何一個版本的rpc server都能配套使用。
更進一步,由於xml rpc以xml文本的方式,使用http協定傳輸,所以與程式語言無關。例如:rpc client的已實現版本包括了:perl,php,python,c/c++,java,等等;rpc server的實現語言包括perl,java,等。
同一種程式語言所實現的版本也不止一個。例如java版的實現有:Marque的xmlrpc實現,apache的xmlrpc 實現

實現版本

這個sample中,假設所傳輸的object都實現了XmlRPCSerializable接口。這個例子的目的是:模擬unionmon中的command對象。當假設所傳輸的數據是一個 Object[]時,可以用這種方式傳輸。結合unionmon中的代碼生成機制,每個vo的序列化/反序列化方法可以在代碼生成過程中完成。同樣地,vox的序列化/反序列化方法需要手寫。
參考代碼:附錄中的test3.XmlRPCSerializable , test3.AccountVO , test3.XmlRPCClient , test3.JavaServer讓tomcat支持rpc server 做法:1.實現一個Servlet,並配置到tomcat中;2.讓rpc client直接找這個servlet,獲得服務。
注意rpc client使用的是http post方法,所以該servlet中只需要實現doPost方法。
過程:在tomcat中的web.xml增加如下配置:
<servlet>
<servlet-name>SampleServiceServlet</servlet-name>
<servlet-class>test4.ServletXmlRPCServer</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SampleServiceServlet</servlet-name>
<url-pattern>/SampleServiceServlet</url-pattern>
</servlet-mapping>
參考類:附屬檔案中的test4.SampleService,test4.ServletXmlRPCServer,test4.XmlRPCClient

相關信息

3.1.能否通過introspect方式,使得自定義類型的vo的序列化/反序列化工作自動完成.castor可以完成對象的xml binding,似乎可參考
3.2.soap協定比xmlrpc複雜並強大。soap不熟悉。另,soap一定與web service在一起用?能否不用web service,但使用soap做rmi的協定。

例子

2.0.環境準備:下載如下包並設定到CLASSPATH
apache xmlrpc軟體包
commons-httpclient-3.0-rc4.jar
commons-codec-1.3.jar

中文版

本規範說明的XML-RPC協定實現UserLand Frontier5.1。
關於非技術性說明,請訪問XML-RPC for Newbies。
文檔提供了實現XML-RPC所需要的所有信息。
前言
XML-RPC是一種基於Internet的遠程函式調用協定。
XML-RPC訊息都是HTTP-POST請求。請求的主要部分的XML。伺服器端執行後的返回結果同樣也是XML格式
函式調用的參數可以是scalars, numbers, strings, dates等等;也可以是混合型的記錄和結構體。
Request請求樣式
關於請求頭
第一行的URI格式不是特定的。可以為空,如果伺服器只處理XML-RPC請求甚至可以只是簡單的一個斜線。可是,如果伺服器除了XML-RPC外還提供其他的HTTP請求,URI可以幫助我們把請求指向特定的XML-RPC服務。
User-Agent和Host項是必須的。
Content-Type的值必須是text/xml.
Content-Length必須指定,而且必須是正確的值。
有效的格式
XML-RPC具有和XML一樣的有效格式,並且是一個<methodCall>結構。
<methodCall>必須包含一個值為字元型的<methodName>子元素,用來表明被調用的方法。這個字元必須符合以下規定:大小寫字母、數字0-9、下劃線、點、冒號和斜線。至於怎么解釋這個字元串將有伺服器端來決定。
Scalar <value>s <value>
<value>值被嵌入類型標籤中,支持的類型如下表:
Tag
Type
Example
<i4> or <int>
four-byte signed integer
-12
<boolean>
0 (false) or 1 (true)
1
<string>
string
hello world
<double>
double-precision signed floating point number
-12.214
<dateTime.iso8601>
date/time
19980717T14:08:55
<base64>
base64-encoded binary
eW91IGNhbid0IHJlYWQgdGhpcyE=
如果沒有指定類型,默認為字元串。
<struct>s
參數值可以是<struct>。
每個<struct>包含若干<member>,每個<member>包含一個<name>和一個<value>.
如果所示為包含兩個值的<struct>
<struct>是可以遞歸使用的,任何<value>都里還可以<struct>或其他任何類型,包括後面將要說明的<array>。
<array>s
值可以個<array>
一個<array>簡單的有一個<data>元素。<data>可以是任何合法類型。
下面是一個有4個值的array:
<array> <data> <value><i4>12</i4></value> <value><string>Egypt</string></value> <value><boolean>0</boolean></value> <value><i4>-31</i4></value> </data> </array>
<array> elements do not have names.
<array> 元素沒有名字。
你可以混合使用上面列出的幾種類型。
<arrays>可以遞歸使用,其值可以是<array>或其他類型,包括上面說明的<strut>。
Response應答樣式
下面是一個 XML-RPC請求:
HTTP/1.1 200 OK Connection: close Content-Length: 158 Content-Type: text/xml Date: Fri, 17 Jul 1998 19:55:08 GMT Server: UserLand Frontier/5.1.2-WinNT <?xml version="1.0"?> <methodResponse> <params> <param> <value><string>South Dakota</string></value> </param> </params> </methodResponse>
Respnse應答格式
除非底層操作出現錯,否則總是返回200 OK.
Content-Type是text/xml。必須設定Content-Length,並且必須是正確的值。
應到內容是一個簡單的XML,可是是<methodResponse>包含一個<params>,<params>包含一個<param>,<param>包含一個<value>。
<methodResponse>可能含有一個< fault>標籤。<fault>的值為<struct>類型,<struct>有兩個元素,值為< int>的<faultCode>和值為<string>的<faultString>。
<methodResponse>不能既有<fault>又有<params>。
Fault example
HTTP/1.1 200 OK Connection: close Content-Length: 426 Content-Type: text/xml Date: Fri, 17 Jul 1998 19:55:02 GMT Server: UserLand Frontier/5.1.2-WinNT <?xml version="1.0"?> <methodResponse> <fault> <value> <struct> <member> <name>faultCode</name> <value><int>4</int></value> </member> <member> <name>faultString</name> <value><string>Too many parameters.</string></value> </member> </struct> </value> </fault> </methodResponse>
Strategies/Goals
Firewalls. The goal of this protocol is to lay a compatible foundation across different environments, no new power is provided beyond the capabilities of the CGI interface. Firewall software can watch for POSTs whose Content-Type is text/xml.
Discoverability. We wanted a clean, extensible format that's very simple. It should be possible for an HTML coder to be able to look at a file containing an XML-RPC procedure call, understand what it's doing, and be able to modify it and have it work on the first or second try.
Easy to implement. We also wanted it to be an easy to implement protocol that could quickly be adapted to run in other environments or on other operating systems.
Updated 1/21/99 DW
The following questions came up on the UserLanddiscussion groupas XML-RPC was being implemented in Python.
The Response Format section says "The body of the response is a single XML structure, a <methodResponse>, which can contain a single <params>..." This is confusing. Can we leave out the <params>?
No you cannot leave it out if the procedure executed successfully. There are only two options, either a response contains a <params> structure or it contains a <fault> structure. That's why we used the word "can" in that sentence.
Is "boolean" a distinct data type, or can boolean values be interchanged with integers (e.g. zero=false, non-zero=true)?
Yes, boolean is a distinct data type. Some languages/environments allow for an easy coercion from zero to false and one to true, but if you mean true, send a boolean type with the value true, so your intent can't possibly be misunderstood.
What is the legal syntax (and range) for integers? How to deal with leading zeros? Is a leading plus sign allowed? How to deal with whitespace?
An integer is a 32-bit signed number. You can include a plus or minus at the beginning of a string of numeric characters. Leading zeros are collapsed. Whitespace is not permitted. Just numeric characters preceeded by a plus or minus.
What is the legal syntax (and range) for floating point values (doubles)? How is the exponent represented? How to deal with whitespace? Can infinity and "not a number" be represented?
There is no representation for infinity or negative infinity or "not a number". At this time, only decimal point notation is allowed, a plus or a minus, followed by any number of numeric characters, followed by a period and any number of numeric characters. Whitespace is not allowed. The range of allowable values is implementation-dependent, is not specified.
What characters are allowed in strings? Non-printable characters? Null characters? Can a "string" be used to hold an arbitrary chunk of binary data?
Any characters are allowed in a string except < and &, which are encoded as &lt; and &amp;. A string can be used to encode binary data.
Does the "struct" element keep the order of keys. Or in other words, is the struct "foo=1, bar=2" equivalent to "bar=2, foo=1" or not?
The struct element does not preserve the order of the keys. The two structs are equivalent.
Can the <fault> struct contain other members than <faultCode> and <faultString>? Is there a global list of faultCodes? (so they can be mapped to distinct exceptions for languages like Python and Java)?
A <fault> struct may not contain members other than those specified. This is true for all other structures. We believe the specification is flexible enough so that all reasonable data-transfer needs can be accomodated within the specified structures. If you believe strongly that this is not true, please post a message on the discussion group.
There is no global list of fault codes. It is up to the server implementer, or higher-level standards to specify fault codes.
What timezone should be assumed for the dateTime.iso8601 type? UTC? localtime?
Don't assume a timezone. It should be specified by the server in its documentation what assumptions it makes about timezones.
Additions
<base64> type. 1/21/99 DW.
Updated 6/30/03 DW
Removed "ASCII" from definition of string.
Changed copyright dates, below, to 1999-2003 from 1998-99.
Copyright and disclaimer
? Copyright 1998-2003 UserLand Software. All Rights Reserved.
This document and translations of it may be copied and furnished to others, and derivative works that comment on or otherwise explain it or assist in its implementation may be prepared, copied, published and distributed, in whole or in part, without restriction of any kind, provided that the above copyright notice and these paragraphs are included on all such copies and derivative works.
This document may not be modified in any way, such as by removing the copyright notice or references to UserLand or other organizations. Further, while these copyright restrictions apply to the written XML-RPC specification, no claim of ownership is made by UserLand to the protocol it describes. Any party may, for commercial or non-commercial purposes, implement this protocol without royalty or license fee to UserLand. The limited permissions granted herein are perpetual and will not be revoked by UserLand or its successors or assigns.
This document and the information contained herein is provided on an "AS IS" basis and USERLAND DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE INFORMATION HEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.

相關詞條

熱門詞條

聯絡我們