Related concepts of Web Service
1、 Preface
We have all heard of WebService (Web Service) more or less. For a while, many computer journals, books and websites have wantonly mentioned and publicized WebService technology, including many elements of boasting and advertising. However, we have to admit that WebService is really a new and promising technology, so what is WebService and when should it be used?
The current application development gradually presents two completely different tendencies: one is browser based thin client application, and the other is browser based rich client application (RIA). Of course, the latter technology is relatively more fashionable (such as the popular HTML5 Technology). Here we mainly talk about the former.
Browser based thin client applications are not because thin clients can provide a better user interface, but because they can avoid the high cost of desktop application publishing. Publishing desktop applications is expensive, half because of application installation and configuration problems, and the other half because of communication problems between customers and servers. Traditional windows rich client applications use DCOM to communicate with the server and call remote objects. Configuring DCOM to work normally in a large network will be a very challenging job, and it is also a nightmare for many IT engineers. In fact, many IT engineers would rather endure the functional limitations brought by the browser than run a DCOM on the LAN. As for the communication between the client and the server, a perfect solution is to use HTTP protocol to communicate. This is because any machine running a web browser is using the HTTP protocol. At the same time, many firewalls are currently configured to allow only HTTP connections. Another problem many commercial programs face is interoperability with other programs. If all applications use COM or Net language, and all run on the windows platform, then the world is at peace. However, in fact, most business data is still stored in the form of non relational files (VSAM) on the mainframe and accessed by mainframe programs written in COBOL language. Moreover, many commercial programs continue to be written in C + +, Java, visual basic and various other languages. Now, except for the simplest program, all applications need to integrate and exchange data with applications running on other heterogeneous platforms. Such tasks are usually completed by special methods, such as file transfer and analysis, message queuing, and APIs that are only suitable for some situations, such as IBM's advanced program to program communication (APPC). Before, there was no application communication standard, which was independent of platform, build model and programming language. Only through web service can the client and server communicate freely with HTTP, regardless of the platform and programming language of the two programs.
2、 What is WebService
In a word: WebService is a remote calling 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, while the client program can be written in other programming languages, and vice versa! Cross operating system platform means that server programs and client programs can run on different operating systems.
The so-called remote call is a method that a program on one computer a can call an object on another computer B. for example, UnionPay provides the POS card swiping system of the mall, and the code of the transfer method called by the POS machine of the mall actually runs on the bank server. For another example, Amazon, weather forecast system, Taobao, Xiaonei, 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, which expands the market share of their own system. In a big sense, it is the so-called SOA application.
In fact, WebService can be understood from many angles. On the surface, WebService is an application that exposes an API that can be called through the web to the outside world, that is, the application can be called through the web through programming methods. We call the application that calls the web service the client, and the application that provides the web service the server. At a deeper level, WebService is a new platform for building interoperable distributed applications. It is 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 web service standards.
Web service platform needs a set of protocols to realize the creation of distributed applications. Any platform has its data representation method and type system. To achieve interoperability, the web service platform must provide a set of standard type systems 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 customers can get enough information to call the web service. Finally, we must also have a method to make a remote call to the web service. This method is actually a remote procedure call protocol (RPC). In order to achieve interoperability, this RPC Protocol must also be platform and programming language independent.
3、 Web service platform technology
XML + XSD, soap and WSDL are the three technologies that constitute the web service platform.
3.1、XML+XSD
WebService uses HTTP protocol to transmit data, Encapsulate data in XML format (that is, XML describes which method to call the remote service object, what parameters to pass, and what the return result of the service object is). XML is the format for representing data in the WebService platform. In addition to being easy to establish and analyze, the main advantage of XML is that it is both platform independent and vendor independent. Independence is more important than technical superiority: soft Component manufacturers will not choose a technology invented by competitors.
XML solves the problem of data representation, but it does not define a set of standard data types, let alone how to extend this set of data types. For example, what exactly does the integer number represent? 16 bit, 32-bit, 64 bit? These details are important for interoperability. XML schema (XSD) is a set of standards to solve this problem. It defines a set of standard data types and gives a language to extend this set of data types. The web service platform uses XSD as its data type system. When you construct a web service in a certain language (such as vb.net or c#), in order to comply with the web service standard, all the data types you use must be converted to XSD types. The tool you use may have automatically completed the conversion, but you are likely to modify the conversion process according to your needs.
3.2、SOAP
When WebService sends requests and receives results through HTTP protocol, the sent request content and result content are encapsulated in XML format, and some specific HTTP message headers are added to explain the content format of HTTP messages. These specific HTTP message headers and XML content formats are SOAP Protocol. Soap provides standard RPC methods to call web services.
SOAP Protocol = http protocol + XML data format
Soap protocol defines the format of SOAP message. Soap protocol is based on HTTP protocol, soap is also based on XML and XSD, and XML is the data encoding method of soap. Make an analogy: http is an ordinary highway, XML is the green isolation belt in the middle and the protective fence on both sides, and soap is the highway transformed by adding isolation belt and protective fence.
3.3、WSDL
For example, when we go to the store to buy something, we must first know what is available in the store, and then buy again. The merchant's practice is to put up advertising posters. WebService is the same. To call a WebService service, the WebService client must first know the address of the service and what methods can be called in the service. Therefore, the WebService server must first explain what services can be called externally through a WSDL file, What is the service (what methods are there in the service, what parameters are accepted by the method, and what is the return value), which URL address is used to represent the network address of the service, and how the service is called.
WSDL (Web service description language) is such an XML based language, which is used to describe Web services and their functions, parameters and return values. It is a standard format that web service clients and servers can understand. Because it is based on XML, WSDL is both machine readable and human readable, which will be a great benefit. Some of the latest development tools can not only generate WSDL documents according to your web service, but also import WSDL documents and generate proxy class code to call the corresponding web service.
The WSDL file is saved on the web server and can be accessed through a URL address. Before calling a web service, the client should know the address of the WSDL file of the service. The web service provider can expose its WSDL file address in two ways: 1 Register with the UDDI server so that it can be found by others; 2. Tell the client caller directly.
4、 Web service development
Web service development can be divided into server-side development and client-side development
4.1. Server development
Publish the business methods of the company's internal system into web service services for remote cooperation units and individuals. (with some WebService frameworks, you can easily publish your business objects as WebService services. Typical WebService frameworks in Java include axis, xfire, CXF, etc. Java EE servers usually support publishing WebService services, such as JBoss.)
4.2 client development
Call the WebService service published by others. Most people are engaged in the development of this aspect. For example, call the weather forecast WebService service. (use tools such as wsdl2java of the manufacturer to generate proxy class code for static calls; use the client programming API class provided by the manufacturer; use the Jax RPC development package of the early standard of Sun company; use the Jax WS development package of the latest standard of Sun company. Of course, sun has been acquired by Oracle)
4.3 working call principle of webservice
For the client, we pass the URL address of the WSDL file to the various WebService client APIs, and these APIs will create the underlying proxy class. I call these proxies to access the WebService service. The proxy class turns the client's method call into soap format request data, sends it through HTTP protocol, and turns the received soap data into return value. For the server, the essence of various WebService frameworks is a large servlet. When the remote calling client sends soap format request data to it through HTTP protocol, it analyzes the data and knows which Java class and method to call, so it finds or creates this object and calls its method, Then wrap the result returned by the method into data in soap format and send it back to the client through HTTP response message.
5、 Applicable occasions
1. Cross firewall communication
If the application has thousands of users and is distributed all over the world, the communication between client and server will be a thorny problem. Because there is usually a firewall or proxy server between the client and the server. In this case, using DCOM is not so simple, and it is usually not convenient to publish the client program to every user with such a large number. The traditional approach is to use the browser as the client, write a lot of ASP pages, and expose the middle layer of the application to the end user. As a result, it is difficult to develop and maintain the program. If the middle tier component is replaced by WebService, the middle tier component can be called directly from the user interface. From the experience of most people, in an application with more interaction between the user interface and the middle layer, using the structure of WebService can save 20% of the development time spent on user interface programming.
2. Application Integration
Enterprise application developers know that enterprises often have to integrate various programs written in different languages and running on different platforms, and this integration will cost a lot of development power. Applications often need to obtain data from programs running on IBM hosts; Or send the data to the host or UNIX application. Even on the same platform, all kinds of software produced by different software manufacturers often need to be integrated. Through web service, you can easily integrate applications with different structures.
3. B2B integration
Integrating applications with WebService can make the business processing within the company more automated. But what happens when transactions cross suppliers and customers and break through the boundaries of the company? Cross company business transaction integration is usually called B2B integration. Web service is the key to the success of B2B integration. Through WebService, companies can "expose" key business applications to designated suppliers and customers. For example, by "exposing" the electronic ordering system and electronic invoice system, customers can send orders electronically, and suppliers can send raw material purchase invoices electronically. Of course, this is not a new concept. EDI (electronic document exchange) has been like this for a long time. However, the implementation of WebService is much simpler than EDI, and WebService runs on the Internet and can be easily implemented anywhere in the world, so its operation cost is relatively low. However, unlike EDI, web service is a complete solution for document exchange or B2B integration. Web service is only a key part of B2B integration, and many other parts are needed to realize integration.
The biggest advantage of B2B integration with WebService is that interoperability can be easily realized. As long as the business logic is "exposed" and become a web service, any designated partner can call these business logic, regardless of what platform their system runs on and what development language they use. This greatly reduces the time and cost spent on B2B integration, and enables many small and medium-sized enterprises that could not afford EDI to realize B2B integration.
4. Software and data reuse
Software reuse is a big topic. There are many forms and degrees of software reuse. The most basic form is reuse at the source code module or class level, and one form is component reuse in binary form. Using web service applications, functions and data can be "exposed" by standard methods for use by other applications to achieve business level reuse.
6、 Not applicable
1. Stand alone application
At present, enterprises and individuals still use many desktop applications. Some of them only need to communicate with other programs on the machine. In this case, it's best not to use web service, just use the local API. COM is ideal for working in this situation because it is small and fast. The same is true for server software running on the same server. It is best to use COM or other local APIs to call between applications directly. Of course, web service can also be used in these occasions, but that will not only consume too much, but also bring no benefits.
2. Homogeneous application of LAN
In many applications, all programs are developed with VB or VC, use COM under Windows platform, and run on the same LAN. For example, there are two server applications that need to communicate with each other, or there is a WIN32 or WinForm client program that needs to connect to another server on the LAN. In these programs, using DCOM is much more effective than soap / HTTP. Similarly, if one Net program to connect to another on the LAN Net program, you should use NETremoting。 Interestingly, in Net remoting, you can also specify to use soap / HTTP for web service calls. However, it is better to make RPC calls directly through TCP, which will be much more effective.