按照“数据即程序”的观点来看,RPC无非是借助一些通信手段来互相传递数据(信息),所以她也是“高”层次的通信手段,无非是这种通信手段看起来更像是“过程的调用”,因为她往往以一个“函数”的面目示人,从而掩盖了她交换信息的实质。
在各种
RPC技术中,我想应该以Sun的RPC最为著名,比较流行的网络文件系统NFS就是建立在SUN RPC技术基础之上的。
在Sun RPC中,调用双方传递的数据是
二进制的,而在XMLRPC中数据将是
XML格式的。那么为什么用XML而不用二进制呢?我想一方面应该是为了兼容更多的语言,因为这个世界上除了C/C++等编译语言,还有很多类似python,
perl,
javascript等的
脚本语言(有些文章也称其为“
动态语言”,因为他们通常不需要自己管理内存),另一方面是为了隔离操作系统的差异,比如说Little Endian和Big Endian的差异等。基于种种原因,XMLRPC选择了XML这种
中间语言作为其信息的格式,然后由各个语言负责将其转变成各自native(本土)的数据类型。关于为了兼容各个语言所发明的中间语言还有
IDL(Interface Definition Language:接口定义语言),它被用于
CORBA接口的定义。
2工作原理编辑
完整的需要参考xmlrpc规范
简单描述:
rpcclient的工作原理:rpcclient根据URL找到rpcserver -> 构造命令包,调用rpcserver上的某个服务的某个方法 -> 接收到rpcserver的返回,解析响应包,拿出调用的返回结果。
rpcserver的工作原理:启动一个webserver(在使用内置的webserver的情况下) -> 注册每个能提供的服务,每个服务对应一个
Handler类 ->进入服务
监听状态。
3对应关系编辑
参考附录中test2.XmlRPCClient类与test2.JavaServer类。
在这里简单描述一下:
>xmlrpc中的
Array类型,对应到java中的Vector类型
例如:在RPC Server中的某个服务方法的返回值的声明类型是String[],但在Client中接收到的将是Vector对象;
反之,如果Client传送过去的调用参数为String[],但在RPC Server中所接收到的将是Vector对象
当然,如果我们不使用String[],直接声明为Vector,也是可以的。
> 其它的类型,如:String,int,double,
boolean,Date等,都比较好对应。需要注意的是:在rpc Client中,如果使用到int/double/boolean这些基本类型时,需要将他们封装成一个相应的Object,例如:Integer /Double/
Boolean。
4可用版本编辑
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 实现
5实现版本编辑
这个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方法。
<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
6相关信息编辑
3.1.能否通过introspect方式,使得自定义类型的vo的
序列化/反序列化工作自动完成.
castor可以完成对象的xml binding,似乎可参考
3.2.soap协议比xmlrpc复杂并强大。soap不熟悉。另,soap一定与web service在一起用?能否不用web service,但使用soap做rmi的协议。
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服务。
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应答样式
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 < and &. 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