Related concepts of WebService

1. Preface

  Everyone has heard of WebService (Web Service) more or less. For a period of time, many computer journals, books and websites have mentioned and promoted WebService technology, and many of them boasted and advertised. But I have to admit that WebService is really an emerging and promising technology, so what exactly is WebService? When should it be used?

  Current application development has gradually shown two very different trends: one is browser-based thin client applications, one is browser-based rich client applications (RIA), and then a technology Relatively speaking, it is more fashionable (such as the popular Html5 technology), and the former is mainly discussed here.

  Browser-based thin client applications are not because the thin client can provide a better user interface, but because it avoids the high cost of desktop application distribution. Publishing desktop applications is expensive, half because of application installation and configuration problems, and the other half because of communication problems between the client and the server. Traditional Windows rich client applications use DCOM to communicate with servers and invoke remote objects. Configuring DCOM to work properly in a large network can be a challenging task and a nightmare for many IT engineers. In fact, many IT engineers would rather live with the functional limitations of a browser than run a DCOM on a LAN. Regarding the communication problem between the client and the server, a perfect solution is to use the HTTP protocol to communicate. This is because any machine running a web browser is using the HTTP protocol. At the same time, many current firewalls are also configured to only allow HTTP connections. Another problem that many commercial programs face is interoperability with other programs. If all applications were written in COM or .NET languages ​​and all ran on the Windows platform, the world would be peaceful. In fact, however, most business data is still stored on mainframes as non-relational files (VSAMs) and accessed by mainframe programs written in the COBOL language. Moreover, many commercial programs continue to be written in C++, Java, Visual Basic, and a variety of other languages. Now, all but the simplest programs need to integrate and exchange data with applications running on other heterogeneous platforms. Such tasks are usually done by special methods such as file transfer and parsing, message queues, and APIs that only work in certain situations, such as IBM's Advanced Program-to-Program Communication (APPC). Previously, there was no application communication standard that was independent of platform, building model, and programming language. Only through the Web Service, the client and the server can communicate freely using HTTP, regardless of the platform and programming language of the two programs.

2. What is WebService?

   In a word: WebService is a remote invocation technology across programming languages ​​and operating system platforms.

   The so-called cross programming language and cross operating platform means that the server program is written in java, and the client program can be written in other programming languages, and vice versa! Cross-operating system platform means that the server program and client program can run on different operating systems.

    The so-called remote call is a method by which a program on one computer a can call an object on another computer b, for example, the POS card swiping system provided by UnionPay to the mall, and the code of the transfer method called by the POS machine transfer in the mall In fact, it is running on the bank server. For another example, amazon, weather forecast system, Taobao.com, intranet, Baidu, etc. expose their own system services in the form of webservice services, so that third-party websites and programs can call these service functions, thus expanding the market share of their own systems Rate, blowing on the big concept, is the so-called SOA application.

   In fact, WebService can be understood from multiple perspectives. On the surface, WebService is an application that exposes an API that can be called through the Web to the outside world, which means that the application can be called through the Web by programming. We call the application that calls this WebService the client, and the application that provides the WebService is called the server. From a deep level, WebService is a new platform for establishing interoperable distributed applications, a platform and a set of standards. It defines how applications can achieve interoperability on the Web. You can write Web services in any language you like and on any platform you like, as long as we can query and access these services through the Web service standard. 

   The WebService platform needs a set of protocols to realize the creation of distributed applications. Any platform has its data representation and type system. To achieve interoperability, the WebService platform must provide a standard type system for communicating different type systems in different platforms, programming languages, and component models. The Web service platform must provide a standard to describe the Web service, so that the client can get enough information to call the Web service. Finally, we must also have a way to make remote calls to this Web service, which is actually a Remote Procedure Call Protocol (RPC). To achieve interoperability, this RPC protocol must also be platform and programming language independent.

3. WebService platform technology

  XML+XSD, SOAP and WSDL are the three major technologies that constitute the WebService platform.

3.1、XML+XSD

  WebService uses HTTP protocol to transmit data, and uses XML format to encapsulate data (that is, the XML specifies which method to call the remote service object, what parameters are passed, and what is the return result of the service object). XML is a format for representing data in the WebService platform. In addition to being easy to build and easy to parse, the main advantage of XML is that it is both platform-independent and vendor-independent. Irrelevance is more important than technical superiority: software vendors do not choose a technology invented by a competitor. 

  XML solves the problem of data representation, but it does not define a standard set of data types, let alone how to extend this set of data types. For example, what exactly does an integer number represent? 16-bit, 32-bit, 64-bit? These details are important to achieve interoperability. XML Schema (XSD) is a set of standards that specifically addresses this problem. It defines a standard set of data types and provides a language to extend this set of data types. The WebService platform uses XSD as its data type system. When you use a language (such as VB.NET or C#) to construct a Web service, in order to conform to the WebService standard, all the data types you use must be converted to XSD types. The tool you use may already do this conversion for you automatically, but you will most likely modify the conversion process to suit your needs.

3.2、SOAP

   WebService通过HTTP协议发送请求和接收结果时,发送的请求内容和结果内容都采用XML格式封装,并增加了一些特定的HTTP消息头,以说明 HTTP消息的内容格式,这些特定的HTTP消息头和XML内容格式就是SOAP协议。SOAP提供了标准的RPC方法来调用Web Service。

  SOAP协议 = HTTP协议 + XML数据格式

  SOAP协议定义了SOAP消息的格式,SOAP协议是基于HTTP协议的,SOAP也是基于XML和XSD的,XML是SOAP的数据编码方式。打个比 喻:HTTP就是普通公路,XML就是中间的绿色隔离带和两边的防护栏,SOAP就是普通公路经过加隔离带和防护栏改造过的高速公路。

3.3、WSDL

  好比我们去商店买东西,首先要知道商店里有什么东西可买,然后再来购买,商家的做法就是张贴广告海报。 WebService也一样,WebService客户端要调用一个WebService服务,首先要有知道这个服务的地址在哪,以及这个服务里有什么方 法可以调用,所以,WebService务器端首先要通过一个WSDL文件来说明自己家里有啥服务可以对外调用,服务是什么(服务中有哪些方法,方法接受 的参数是什么,返回值是什么),服务的网络地址用哪个url地址表示,服务通过什么方式来调用。

  WSDL(Web Services Description Language)就是这样一个基于XML的语言,用于描述Web Service及其函数、参数和返回值。它是WebService客户端和服务器端都 能理解的标准格式。因为是基于XML的,所以WSDL既是机器可阅读的,又是人可阅读的,这将是一个很大的好处。一些最新的开发工具既能根据你的 Web service生成WSDL文档,又能导入WSDL文档,生成调用相应WebService的代理类代码。

  WSDL 文件保存在Web服务器上,通过一个url地址就可以访问到它。客户端要调用一个WebService服务之前,要知道该服务的WSDL文件的地址。 WebService服务提供商可以通过两种方式来暴露它的WSDL文件地址:1.注册到UDDI服务器,以便被人查找;2.直接告诉给客户端调用者。

四、WebService开发

  WebService开发可以分为服务器端开发和客户端开发两个方面

4.1、服务端开发

  把公司内部系统的业务方法发布成WebService服务,供远程合作单位和个人调用。(借助一些WebService框架可以很轻松地把自己的业务对象发布成WebService服务,Java方面的典型WebService框架包括:axis,xfire,cxf 等,java ee服务器通常也支持发布WebService服务,例如JBoss。)

4.2、客户端开发 

  调用别人发布的WebService服务,大多数人从事的开发都属于这个方面,例如,调用天气预报WebService服务。(使用厂 商的WSDL2Java之类的工具生成静态调用的代理类代码;使用厂商提供的客户端编程API类;使用SUN公司早期标准的jax-rpc开发包;使用 SUN公司最新标准的jax-ws开发包。当然SUN已被ORACLE收购)

4.3、WebService 的工作调用原理

   对客户端而言,我们给这各类WebService客户端API传递wsdl文件的url地址,这些API就会创建出底层的代理类,我调用 这些代理,就可以访问到webservice服务。代理类把客户端的方法调用变成soap格式的请求数据再通过HTTP协议发出去,并把接收到的soap 数据变成返回值返回。对服务端而言,各类WebService框架的本质就是一个大大的Servlet,当远程调用客户端给它通过http协议发送过来 soap格式的请求数据时,它分析这个数据,就知道要调用哪个java类的哪个方法,于是去查找或创建这个对象,并调用其方法,再把方法返回的结果包装成 soap格式的数据,通过http响应消息回给客户端。

五、适用场合

1、跨防火墙通信

  如果应用程序有成千上万的用户,而且分布在世界各地,那么客户端和服务器之间的通信将是一个棘手的问题。因为客户端和服务器之间通常会有防火墙或者代理服 务器。在这种情况下,使用DCOM就不是那么简单,通常也不便于把客户端程序发布到数量如此庞大的每一个用户手中。传统的做法是,选择用浏览器作为客户 端,写下一大堆ASP页面,把应用程序的中间层暴露给最终用户。这样做的结果是开发难度大,程序很难维护。如果中间层组件换成WebService的话, 就可以从用户界面直接调用中间层组件。从大多数人的经验来看,在一个用户界面和中间层有较多交互的应用程序中,使用WebService这种结构,可以节 省花在用户界面编程上20%的开发时间。

2、应用程序集成

  企业级的应用程序开发者都知道,企业里经常都要把用不同语言写成的、在不同平台上运行的各种程序集成起来,而这种集成将花费很大的开发力量。应用程序经常 需要从运行在IBM主机上的程序中获取数据;或者把数据发送到主机或UNIX应用程序中去。即使在同一个平台上,不同软件厂商生产的各种软件也常常需要集 成起来。通过WebService,可以很容易的集成不同结构的应用程序。

3、B2B集成

  用WebService集成应用程序,可以使公司内部的商务处理更加自动化。但当交易跨越供应商和客户、突破公司的界限时会怎么样呢?跨公司的商务交易集成通常叫做B2B集成。WebService是B2B集成成功的关键。通过WebService,公司可以把关键的商务应用“暴露”给指定的供应商和客户。例如,把电子下单系统和电子发票系统“暴露”出来,客户就可以以电子的方式发送订单,供应商则可以以电子的方式发送原料采购发票。当然,这并不是一个 新的概念,EDI(电子文档交换)早就是这样了。但是,WebService的实现要比EDI简单得多,而且WebService运行在Internet 上,在世界任何地方都可轻易实现,其运行成本就相对较低。不过,WebService并不像EDI那样,是文档交换或B2B集成的完整解决方案。 WebService只是B2B集成的一个关键部分,还需要许多其它的部分才能实现集成。

  用WebService来实现B2B集成的最大好处在于可以轻易实现互操作性。只要把商务逻辑“暴露”出来,成为WebService,就可以让任何指定 的合作伙伴调用这些商务逻辑,而不管他们的系统在什么平台上运行,使用什么开发语言。这样就大大减少了花在B2B集成上的时间和成本,让许多原本无法承受 EDI的中小企业也能实现B2B集成。

4、软件和数据重用

      软件重用是一个很大的主题,重用的形式很多,重用的程度有大有小。最基本的形式是源代码模块或者类一级的重用,一种形式是二进制形式的组件重用。采用 WebService应用程序可以用标准的方法把功能和数据“暴露”出来,供其它应用程序使用,达到业务级重用。

六、不适用场合

1、单机应用程序

      目前,企业和个人还使用着很多桌面应用程序。其中一些只需要与本机上的其它程序通信。在这种情况下,最好就不要用WebService,只要用本地的 API就可以了。COM非常适合于在这种情况下工作,因为它既小又快。运行在同一台服务器上的服务器软件也是这样。最好直接用COM或其它本地的API来 进行应用程序间的调用。当然WebService也能用在这些场合,但那样不仅消耗太大,而且不会带来任何好处。

2、局域网的同构应用程序

      在许多应用中,所有的程序都是用VB或VC开发的,都在Windows平台下使用COM,都运行在同一个局域网上。例如,有两个服务器应用程序需要相互通 信,或者有一个Win32或WinForm的客户程序要连接局域网上另一个服务器的程序。在这些程序里,使用DCOM会比SOAP/HTTP有效得多。与 此相类似,如果一个.NET程序要连接到局域网上的另一个.NET程序,应该使用.NETremoting。有趣的是,在.NETremoting 中,也可以指定使用SOAP/HTTP来进行WebService调用。不过最好还是直接通过TCP进行RPC调用,那样会有效得多。

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326658088&siteId=291194637