优化Web服务的XML响应
在我们Web服务的性能测试中,我们发现响应生成的流量超出了我们的预期。我们正在查询包含行和列的数据库和加载列表。优化Web服务的XML响应
该列的类型是AnyType所以在响应中需要一个类型信息。因此,Web服务引擎(Axis2或JAXWS)多次添加命名空间信息。请看下面的例子响应:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<ns3:loadListResponse xmlns:ns3="http://example.com/test/service-types-1.0"
xmlns:ns2="http://example.com/lists/lists-types-1.0" >
<ns3:value>
<ns2:row>
<ns2:column xsi:type="xs:int" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema">12345</ns2:column>
<ns2:column xsi:type="xs:string" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema">XYZ</ns2:column>
<ns2:column xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<ns2:column xsi:type="xs:string" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema">ABC</ns2:column>
</ns2:row>
<ns2:row>
<ns2:column xsi:type="xs:int" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema">32345</ns2:column>
<ns2:column xsi:type="xs:string" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema">OPC</ns2:column>
<ns2:column xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<ns2:column xsi:type="xs:string" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema">QWE</ns2:column>
</ns2:row>
.
.
.
</ns3:value>
</ns3:loadListResponse>
</soapenv:Body>
</soapenv:Envelope>
我想优化由在顶部加入所需的命名空间和从每一列中除去他们这个XML响应(一般有每行约30列)。结果应该是这样的:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<soapenv:Body>
<ns3:loadListResponse xmlns:ns3="http://example.com/test/service-types-1.0"
xmlns:ns2="http://example.com/lists/lists-types-1.0" >
<ns3:value>
<ns2:row>
<ns2:column xsi:type="xs:int" >12345</ns2:column>
<ns2:column xsi:type="xs:string" >XYZ</ns2:column>
<ns2:column xsi:nil="true" />
<ns2:column xsi:type="xs:string" >ABC</ns2:column>
</ns2:row>
<ns2:row>
<ns2:column xsi:type="xs:int" >32345</ns2:column>
<ns2:column xsi:type="xs:string" >OPC</ns2:column>
<ns2:column xsi:nil="true" />
<ns2:column xsi:type="xs:string" >QWE</ns2:column>
</ns2:row>
.
.
.
</ns3:value>
</ns3:loadListResponse>
</soapenv:Body>
</soapenv:Envelope>
你会怎么做这样的事情?
有没有办法告诉Axis2或JAXWS这样做?
或者我是否需要手动操作生成的XML?
你有没有考虑尝试压缩响应,而不是以适当的透明方式?这可能更容易做到,并且对于所有重复的数据都会非常有效。
AXIS 1.x的servlet compression filter的示例。
本指南介绍了如何在Apache Axis中使用SOAP压缩。请求和响应消息都被压缩。为了在客户端压缩和解压SOAP消息,使用了一个gzip的Axis扩展。服务器端的对应部分是一个Servlet过滤器。
如果您对您的Web服务的传输和/或处理效率的关注,你应该考虑启用Fast Infoset:
的Fast Infoset(或FI)是一个 国际标准,规定 一个用于XML 信息集(XML信息集)的二进制编码格式作为 替代XML格式的文档 格式。它旨在提供比 基于文本的XML格式更多的 高效序列化。
我们可以把FI作为gzip的对XML, 虽然FI旨在优化两者 文件大小和处理 性能,而gzip的优化 只有大小。
它对高容量网络服务的影响是戏剧性的,我现在在可能的情况下使用它作为理所当然的事情。
我们有一个。需要处理响应的.NET客户端......这可以通过Fast Infoset完成吗? – 2009-12-21 10:29:00
FastInfoset是一个微软发明,所以这不应该是一个问题。 – skaffman 2009-12-21 10:37:50
它看起来像.NET,它是一个[商业解决方案](http://www.noemax.com/products/fastinfoset/index.html),你必须付钱。 – 2014-03-20 02:50:47
是的,我们正在考虑这一点。在服务器端有一个Apache,所以mod_deflate应该做的工作......但.NET客户端需要能够处理压缩的响应。 – 2009-12-21 10:25:47