蒋彪:Web Service的详细教程和协议分析

6 6 月, 2010 Categories: 技术

Web Service技术内幕

–Web Service的详细教程和协议分析

2010/2/25      蒋彪 于南京

1. Web Service的介绍

1.1 Web Service到底是什么?

研究一下当前的应用程序开发,你会发现一个绝对的倾向:人们开始偏爱基于浏览器的瘦客户应用程序。这当然不是因为瘦客户能够提供更好的用户界面,而是因为它能够避免花在桌面应用程序发布上的高成本。发布桌面应用程序成本很高,一半是因为应用程序安装和配置的问题,另一半是因为客户和服务器之间通信的问题。

传统的Windows富客户应用程序使用DCOM来与服务器进行通信和调用远程对象。配置好DCOM使其在一个大型的网络中正常工作将是一个极富挑战性的工作,同时也是许多IT工程师的噩梦。事实上,许多IT工程师宁愿忍受浏览器所带来的功能限制,也不愿在局域网上去运行一个DCOM。在我看来,结果就是一个发布容易,但开发难度大而且用户界面极其受限的应用程序。极端的说,就是你花了更多的资金和时间,却开发出从用户看来功能更弱的应用程序。不信你问问你的会计师对新的基于浏览器的会计软件有什么想法:绝大多数商用程序用户希望使用更加友好的Windows用户界面。

关于客户端与服务器的通信问题,一个完美的解决方法是使用HTTP协议来通信。这是因为任何运行Web浏览器的机器都在使用HTTP协议。同时,当前许多防火墙也配置为只允许HTTP连接。

许多商用程序还面临另一个问题,那就是与其他程序的互操作性。如果所有的应用程序都是使用COM或.NET语言写的,并且都运行在Windows平台上,那就天下太平了。然而,事实上大多数商业数据仍然在大型主机上以非关系文件(VSAM)的形式存放,并由COBOL语言编写的大型机程序访问。而且,目前还有很多商用程序继续在使用C++、Java、Visual Basic和其他各种各样的语言编写。现在,除了最简单的程序之外,所有的应用程序都需要与运行在其他异构平台上的应用程序集成并进行数据交换。这样的任务通常都是由特殊的方法,如文件传输和分析,消息队列,还有仅适用于某些情况的的API,如IBM的”高级程序到程序交流(APPC)”等来完成的。在以前,没有一个应用程序通信标准,是独立于平台、组建模型和编程语言的。只有通过Web Service,客户端和服务器才能够自由的用HTTP进行通信,不论两个程序的平台和编程语言是什么。

Web Service

对这个问题,我们至少有两种答案。从表面上看,Web Service 就是一个应用程序,它向外界暴露出一个能够通过Web进行调用的API。这就是说,你能够用编程的方法通过Web来调用这个应用程序。我们把调用这个Web Service 的应用程序叫做客户。例如,你想创建一个Web Service ,它的作用是返回当前的天气情况。那么你可已建立一个ASP页面,它接受邮政编码作为查询字符串,然后返回一个由逗号隔开的字符串,包含了当前的气温和天气。要调用这个ASP页面,客户端需要发送下面的这个HTTP GET请求:

http://host.company.com/weather.asp?zipcode=20171

返回的数据就应该是这样:

这个ASP页面就应该可以算作是Web Service 了。因为它基于HTTP GET请求,暴露出了一个可以通过Web调用的API。当然,Web Service 还有更多的东西。

下面是对Web Service 更精确的解释: Web Services是建立可互操作的分布式应用程序的新平台。作为一个Windows程序员,你可能已经用COM或DCOM建立过基于组件的分布式应用程序。COM是一个非常好的组件技术,但是我们也很容易举出COM并不能满足要求的情况。

Web Service平台是一套标准,它定义了应用程序如何在Web上实现互操作性。你可以用任何你喜欢的语言,在任何你喜欢的平台上写Web Service ,只要我们可以通过Web Service标准对这些服务进行查询和访问。

新平台

Web Service平台需要一套协议来实现分布式应用程序的创建。任何平台都有它的数据表示方法和类型系统。要实现互操作性,Web Service平台必须提供一套标准的类型系统,用于沟通不同平台、编程语言和组件模型中的不同类型系统。在传统的分布式系统中,基于界面(interface)的平台提供了一些方法来描述界面、方法和参数(译注:如COM和COBAR中的IDL语言)。同样的,Web Service平台也必须提供一种标准来描述Web Service,让客户可以得到足够的信息来调用这个Web Service。最后,我们还必须有一种方法来对这个Web Service进行远程调用。这种方法实际是一种远程过程调用协议(RPC)。为了达到互操作性,这种RPC协议还必须与平台和编程语言无关。下面几个小节就简要介绍了组成Web Service平台的这三个技术。

XMLXSD

可扩展的标记语言(XML)是Web Service平台中表示数据的基本格式。除了易于建立和易于分析外,XML主要的优点在于它既是平台无关的,又是厂商无关的。无关性是比技术优越性更重要的:软件厂商是不会选择一个由竞争对手所发明的技术的。

XML解决了数据表示的问题,但它没有定义一套标准的数据类型,更没有说怎么去扩展这套数据类型。例如,整形数到底代表什么?16位,32位,还是64位?这些细节对实现互操作性都是很重要的。W3C制定的XML Schema(XSD)就是专门解决这个问题的一套标准。它定义了一套标准的数据类型,并给出了一种语言来扩展这套数据类型。Web Service平台就是用XSD来作为其数据类型系统的。当你用某种语言(如VB.NET或C#)来构造一个Web Service时,为了符合Web Service标准,所有你使用的数据类型都必须被转换为XSD类型。你用的工具可能已经自动帮你完成了这个转换,但你很可能会根据你的需要修改一下转换过程。在第二章中,我们将深入XSD,学习怎样转换自定义的数据类型(例如类)到XSD的类型。

SOAP

Web Service建好以后,你或者其他人就会去调用它。简单对象访问协议(SOAP)提供了标准的RPC方法来调用Web Service。实际上,SOAP在这里有点用词不当:它意味着下面的Web Service是以对象的方式表示的,但事实并不一定如此:你完全可以把你的Web Service写成一系列的C函数,并仍然使用SOAP进行调用。SOAP规范定义了SOAP消息的格式,以及怎样通过HTTP协议来使用SOAP。SOAP也是基于XML和XSD的,XML是SOAP的数据编码方式。第三章我们会讨论SOAP,并结识SOAP消息的各种元素。

WSDL

你会怎样向别人介绍你的Web Service有什么功能,以及每个函数调用时的参数呢?你可能会自己写一套文档,你甚至可能会口头上告诉需要使用你的Web Service的人。这些非正式的方法至少都有一个严重的问题:当程序员坐到电脑前,想要使用你的Web Service的时候,他们的工具(如Visual Studio)无法给他们提供任何帮助,因为这些工具根本就不了解你的Web

service。解决方法是:用机器能阅读的方式提供一个正式的描述文档。Web Service描述语言(WSDL)就是这样一个基于XML的语言,用于描述Web Service及其函数、参数和返回值。因为是基于XML的,所以WSDL既是机器可阅读的,又是人可阅读的,这将是一个很大的好处。一些最新的开发工具既能根据你的Web Service生成WSDL文档,又能导入WSDL文档,生成调用相应Web Service的代码。

一句话总结:Web Service就是高级版的DCOMCORBA

1.2 Web Service的体系结构图

通过上图,我们能看出来,WebService的运行机制有点类似于CORBA。

1.通过WSDL描述WebService

2.通过SOAP在互联网环境内传递数据

3.通过JAXB实现Java和XML之间的互相转换。

具体的Web Service的体系结构可以参照以下文件:

======================================================

深入浅出JAX-WS 2.0

http://blog.csdn.net/nanjingjiangbiao/archive/2010/02/11/5306034.aspx

SOA研究之 JAX-WS

http://blog.csdn.net/nanjingjiangbiao/archive/2010/02/10/5305049.aspx

手把手教你在Interstage上部署WebService

http://blog.csdn.net/nanjingjiangbiao/archive/2010/02/22/5317356.aspx

======================================================

.

1.3 一个简单的WebService运行实例

WebService代码:

package stock.server;

@javax.jws.WebService

public class StockQuoteProvider {

public StockQuoteProvider () {

}

public float getLastTradePrice (String tickerSymbol) {

return “abc”.equals(tickerSymbol)? 1234.0f : 0.0f;

}

}

客户端代码:

import java.lang.annotation.Annotation;

import stock.server.*;

import javax.xml.ws.Service;

public class StockQuoteClient {

public static void main(String[] args) throws Exception {

StockQuoteProviderService service = new StockQuoteProviderService();

StockQuoteProvider port = service.getStockQuoteProviderPort();

System.out.println(port.getLastTradePrice(args[0]));

}

}

2. Web Service技术研究的环境准备

2.1 安装运行环境

推荐安装GlassFish服务器,具体安装方法可以参见以下文章:

手把手教你 怎么 安装 GlassFish

http://blog.csdn.net/nanjingjiangbiao/archive/2010/01/28/5264913.aspx

2.2 下载WebService API(JAX-WS)的源代码

具体的下载URL参见以下地址:

https://jax-ws-sources.dev.java.net/source/browse/jax-ws-sources/

2.3 部署服务器端

1.把1.3中的服务器端部署到GlassFish中

2.用wsimport命令生成stub中间程序

3.把1.3中的客户端代码和stub代码,以及2.2下载的WebService源代码部署到Eclipse工程中,就可以DEBUG了。

具体的可以参照以下文章:

SOA研究之 JAX-WS

http://blog.csdn.net/nanjingjiangbiao/archive/2010/02/10/5305049.aspx

3. Web Service的技术内幕

3.1 客户端是如何和WSDL建立关系的?

1. 首先,我们看到客户端程序中有以下这样一行

StockQuoteProviderService service = new

StockQuoteProviderService();

2. 这行代码,会调用到

public StockQuoteProviderService() {

super(STOCKQUOTEPROVIDERSERVICE_WSDL_LOCATIONnew QName(“http://server.stock/”, “StockQuoteProviderService”));

}

3. 这行代码,会调用到com.sun.xml.ws.spi.ProviderImpl的

@Override

public ServiceDelegate createServiceDelegate( URL wsdlDocumentLocation, QName serviceName, Class serviceClass) {

return new WSServiceDelegate(wsdlDocumentLocation, serviceName, serviceClass);

}

4. 这行代码,会调用到com.sun.xml.ws.clientWSServiceDelegate的

/**

@param serviceClass

*      Either {@link Service}.class or other generated service-derived classes.

*/

public WSServiceDelegate(@Nullable Source wsdl, @NotNull QName serviceName, @NotNull final Class<? extends Service> serviceClass) {

~省略~

}

5. 而其中最关键的就是以下这段,解析阅读WSDL的代码,这段代码主要是用XMLStream来构建WSDL代码,不足为奇。

WSDLServiceImpl service=null;

if (wsdl != null) {

try {

URL url = wsdl.getSystemId()==nullnullnew URL(wsdl.getSystemId());

WSDLModelImpl model = parseWSDL(url, wsdl);

service = model.getService(this.serviceName);

if (service == null)

throw new WebServiceException(

ClientMessages.INVALID_SERVICE_NAME(this.serviceName,

buildNameList(model.getServices().keySet())));

// fill in statically known ports

for (WSDLPortImpl port : service.getPorts())

ports.put(port.getName(), new PortInfo(this, port));

catch (MalformedURLException e) {

throw new WebServiceException(ClientMessages.INVALID_WSDL_URL(wsdl.getSystemId()), e);

}

}

this.wsdlService = service;

3.2 客户端是如何和SEI建立关系的?

1. 首先,我们看到客户端程序中有以下这样一行

StockQuoteProvider port = service.getStockQuoteProviderPort();

2. 这行代码,会调用到com.sun.xml.ws.clientWSServiceDelegate的

private <T> T getPort(WSEndpointReference wsepr, QName portName, Class<T> portInterface,

WebServiceFeature… features) {

SEIPortInfo spi = addSEI(portName, portInterface, features);

return createEndpointIFBaseProxy(wsepr,portName,portInterface,features, spi);

}

3. 这行代码,会调用到com.sun.xml.ws.clientWSServiceDelegate的

/**

* Creates a new pipeline for the given port name.

*/

private Tube createPipeline(PortInfo portInfo, WSBinding binding) {

//Check all required WSDL extensions are understood

checkAllWSDLExtensionsUnderstood(portInfo,binding);

SEIModel seiModel = null;

if(portInfo instanceof SEIPortInfo) {

seiModel = ((SEIPortInfo)portInfo).model;

}

BindingID bindingId = portInfo.bindingId;

TubelineAssembler assembler = TubelineAssemblerFactory.create(

Thread.currentThread().getContextClassLoader(), bindingId);

if (assembler == null)

throw new WebServiceException(“Unable to process bindingID=” + bindingId);    // TODO: i18n

return assembler.createClient(

new ClientTubeAssemblerContext(

portInfo.targetEndpoint,

portInfo.portModel,

this, binding, container,((BindingImpl)binding).createCodec(),seiModel));

}

在这段代码中,使用了TUBE技术,把客户端和服务器之间建立了关系。

4. 这行代码,会调用到com.sun.xml.ws.util.pipe.StandaloneTubeAssembler的

@NotNull

public Tube createClient(ClientTubeAssemblerContext context) {

Tube head = context.createTransportTube();

head = context.createSecurityTube(head);

if (dump) {

// for debugging inject a dump pipe. this is left in the production code,

// as it would be very handy for a trouble-shooting at the production site.

head = context.createDumpTube(“client”, System.out, head);

}

head = context.createWsaTube(head);

head = context.createClientMUTube(head);

head = context.createValidationTube(head);

return context.createHandlerTube(head);

}

在以上代码中,开始和SOAP绑定,准备发送请求和调用函数等等。

3.3 客户端是如何发送请求的?

1. 首先,我们看到客户端程序中有以下这样一行

port.getLastTradePrice(args[0])

2. 这行代码,会调用到com.sun.xml.ws.client.sei.SEIStub的

public Object invoke(Object proxy, Method method, Object[] args)throws Throwable {

MethodHandler handler = methodHandlers.get(method);

if (handler != null) {

return handler.invoke(proxy, args);

else {

// we handle the other method invocations by ourselves

try {

return method.invoke(this, args);

catch (IllegalAccessException e) {

// impossible

throw new AssertionError(e);

catch (IllegalArgumentException e) {

throw new AssertionError(e);

catch (InvocationTargetException e) {

throw e.getCause();

}

}

}

3. 这行代码,会调用到com.sun.xml.ws.client.stub的(中间省去了2层不重要的代码)

/**

* Passes a message to a pipe for processing.

* <p>

* Unlike {@link Tube} instances,

* this method is thread-safe and can be invoked from

* multiple threads concurrently.

*

@param packet         The message to be sent to the server

@param requestContext The {@link RequestContext} when this invocation is originally scheduled.

*                       This must be the same object as {@link #requestContext} for synchronous

*                       invocations, but for asynchronous invocations, it needs to be a snapshot

*                       captured at the point of invocation, to correctly satisfy the spec requirement.

@param receiver       Receives the {@link ResponseContext}. Since the spec requires

*                       that the asynchronous invocations must not update response context,

*                       depending on the mode of invocationthey have to go to different places.

*                       So we take a setter that abstracts that away.

*/

protected final Packet process(Packet packet, RequestContext requestContext, ResponseContextReceiver receiver) {

configureRequestPacket(packet, requestContext);

Pool<Tube> pool = tubes;

if (pool == null)

throw new WebServiceException(“close method has already been invoked”); // TODO: i18n

Fiber fiber = engine.createFiber();

// then send it away!

Tube tube = pool.take();

try {

return fiber.runSync(tube, packet);

finally {

// this allows us to capture the packet even when the call failed with an exception.

// when the call fails with an exception it’s no longer a ‘reply’ but it may provide some information

// about what went wrong.

// note that Packet can still be updated after

// ResponseContext is created.

Packet reply = (fiber.getPacket() == null) ? packet : fiber.getPacket();

receiver.setResponseContext(new ResponseContext(reply));

pool.recycle(tube);

}

}

4. 这行代码,会调用到com.sun.xml.ws.api.pipe. __doRun的(中间省去了3层不重要的代码)

这段代码开始发送打成数据包的http请求

/**

* To be invoked from {@link #doRun(Tube)}.

*

@see #doRun(Tube)

*/

private Tube __doRun(Tube next) {

final Fiber old = CURRENT_FIBER.get();

CURRENT_FIBER.set(this);

// if true, lots of debug messages to show what’s being executed

final boolean traceEnabled = LOGGER.isLoggable(Level.FINER);

try {

while(!isBlocking() && !needsToReenter) {

try {

NextAction na;

Tube last;

if(throwable!=null) {

if(contsSize==0) {

// nothing else to execute. we are done.

return null;

}

last = popCont();

if(traceEnabled)

LOGGER.finer(getName()+’ ‘+last+”.processException(“+throwable+’)’);

na = last.processException(throwable);

else {

if(next!=null) {

if(traceEnabled)

LOGGER.finer(getName()+’ ‘+next+”.processRequest(“+packet+’)’);

na = next.processRequest(packet);

last = next;

else {

if(contsSize==0) {

// nothing else to execute. we are done.

return null;

}

last = popCont();

if(traceEnabled)

LOGGER.finer(getName()+’ ‘+last+”.processResponse(“+packet+’)’);

na = last.processResponse(packet);

}

}

~省略~

5. 这行代码,会调用到com.sun.xml.ws.encoding. StreamSOAPCodec的(中间省去了无数层不重要的代码)的

public ContentType encode(Packet packet, OutputStream out) {

if (packet.getMessage() != null) {

XMLStreamWriter writer = XMLStreamWriterFactory.create(out);

try {

packet.getMessage().writeTo(writer);

writer.flush();

catch (XMLStreamException e) {

throw new WebServiceException(e);

}

XMLStreamWriterFactory.recycle(writer);

}

return getContentType(packet.soapAction);

}

大家可以看到,他发出请求了。

3.4 服务器端是如何接受请求的?

1. 首先,服务器端有一个名叫JAXWSServlet的Servlet常驻服务器,监听请求。所以,请求会首先被转发给com.sun.enterprise.webservice.JAXWSServlet的

protected void doPost(HttpServletRequest request,

HttpServletResponse response)

throws ServletException ,IOException{

/**

* This requirement came from the jbi team. If the WebServiceEndpoint

* is a jbi endpoint which is private throw an error whenever a get

* or a post request is made

*/

Endpoint endpt =

wsEngine_.getEndpoint(request.getServletPath());

~省略~

2.最后,代码会调转到以下com.sun.xml.ws.transport.http.HttpAdapter的

final class HttpToolkit extends Adapter.Toolkit {

public void handle(WSHTTPConnection con) throws IOException {

boolean invoke = false;

try {

Packet packet = new Packet();

try {

packet = decodePacket(con, codec);

invoke = true;

catch(ExceptionHasMessage e) {

LOGGER.log(Level.SEVERE, “JAXWS2015: An ExceptionHasMessage occurred. ” + e.getMessage(), e);

packet.setMessage(e.getFaultMessage());

catch(UnsupportedMediaException e) {

LOGGER.log(Level.SEVERE, “JAXWS2016: An UnsupportedMediaException occurred. ” + e.getMessage(), e);

con.setStatus(WSHTTPConnection.UNSUPPORTED_MEDIA);

catch(Exception e) {

LOGGER.log(Level.SEVERE, “JAXWS2017: A ServerRtException occurred. ” + e.getMessage(), e);

con.setStatus(HttpURLConnection.HTTP_INTERNAL_ERROR);

}

if (invoke) {

try {

packet = head.process(packet, con.getWebServiceContextDelegate(),

packet.transportBackChannel);

catch(Exception e) {

LOGGER.log(Level.SEVERE, “JAXWS2018: An Exception occurred. ” + e.getMessage(), e);

if (!con.isClosed()) {

writeInternalServerError(con);

}

return;

}

}

encodePacket(packet, con, codec);

finally {

if (!con.isClosed()) {

con.close();

}

}

}

}

以上代码分为三块大的处理,分别是

packet = decodePacket(con, codec);

packet = head.process(packet, con.getWebServiceContextDelegate(),

packet.transportBackChannel);

encodePacket(packet, con, codec);

用来解析数据包,调用服务,发送回复数据包。这里就不详细介绍了。

4. 总结

希望能够通过这样的文章,更清晰的,直白的把WebService的详细技术内幕公布给大家,为开源软件运动作一份自己的贡献。

##以上##

发表于 @ 2010年02月25日 11:44:00 | 评论( 87 ) 举报收藏

Tags:
尚无评论.

留言回复