Summary of the invention
The object of this invention is to provide the method for interchanging data that a kind of cross-system is cross-platform, the data processor different based on the present invention can exchange isomeric data easily.
The present invention is achieved through the following technical solutions: the described universal data switching method based on HTTP and object serialization, and adopt http protocol message pass-along message, transport layer, based on Transmission Control Protocol, comprises the following steps:
(1) extract a data record from data source or data source pushes a data record, this data record is called as object;
(2) according to the structure of object, object is turned to data flow by sequence, is encapsulated in the message body of a HTTP request message; Wherein, record data stream metamessage in HTTP request head Headers, comprises sequencing method and object size; Territory, the path Path specific data of HTTP request message exchanges object handling program; The method territory Method specific data swap operation method of HTTP request message;
(3) target processing system of exchanges data receives HTTP request, and according to the data element information that message carries, adopt corresponding unserializing method, converting objects is the data of native system compatible format;
(4) data processor processes data subsequently, and the mode with http response after having processed returns result.
The method of object serialization is consulted by a HTTP request-response process when setting up TCP and connecting; In a data exchange process, the side initiating TCP connection in two sides of participation exchanges data is called client, and the opposing party is called service end, and the detailed process of negotiation sequence method is as follows:
(1) after setting up TCP connection, client sends a HTTP request, and in request header, Accept field carries the sequencing method list that client is supported, multiple sequencing methods of client support use branches to separate;
(2), after service end receives request, read the sequencing method list that Accept field obtains client support, then compare with the sequencing method of native system support, choose the sequencing method that one or more both sides support;
(3) service end returns a http response, and in head response, Accept field carries the sequencing method chosen;
(4) client knows the sequencing method of negotiation after receiving the response that service end beams back, follow-up of adopting with the data exchange process of service end in the sequencing method consulted.
The object serialization method that the both sides participating in exchanges data support at least comprises JSON serializing.Make both sides at least can negotiate a kind of sequencing method.
Described service end and client are transmitted after specific compression algorithm can both be selected to compress serialized data stream again, namely deposit the data after compression in message body.
Described client and service end consult the compression algorithm of optional data flow, and the process of consulting compression algorithm is as follows:
(1) after setting up TCP connection, client sends a HTTP request, and in request header, Accept-Encoding field carries the compression algorithm list that client is supported, multiple compression algorithms of client support use branches to separate;
(2), after service end receives request, read the compression algorithm list that Accept-Encoding field obtains client support, then compare with the compression algorithm of native system support, choose the compression algorithm that one or more both sides support;
(3) service end returns a http response, and in head response, Accept-Encoding field carries available compression algorithm;
(4) client knows the compression algorithm of negotiation after receiving the response that service end beams back, follow-up with the data exchange process of service end in adopt the compression algorithm of consulting to compress when needing to compress data.
The negotiation of compression algorithm and the negotiation of sequencing method are incorporated in once request and realize.
Data compression is optional, and service end or client can not support any data compression algorithm.In a data exchange process, when client-side/server-side have employed compression algorithm, Content-Encoding field in HTTP request/response message, is used to indicate adopted compression algorithm.Client or service end carry out unserializing again according to Content-Encoding field decompressed data.
When service end returns result, in http response message, adopt the method swap data consistent with HTTP request message.
Advantage of the present invention is: object to be exchanged by with a kind of method sequence of consult to decide, makes object can seamless circulation between multiple exchanges data participant.One side does not need the concrete storage organization paying close attention to the opposing party.And based on the light-weight transport protocol of HTTP, this switching method and platform are had nothing to do, and can use by the platform of all support http protocol stacks.The equipment of a large amount of existing support http protocol and program can use we brightly to carry out exchanges data easily.
Embodiment
The present invention proposes a kind of universal data switching method based on HTTP and object serialization, adopt http protocol message pass-along message when data exchange to the opposing party from a side, transport layer is based on Transmission Control Protocol, and as shown in Figure 1, it comprises the following steps:
(1) (Extract) data record (passive request exchange) or data source propelling movement (Push) data record (active request exchange) is extracted from data source.This data record is called as object.
(2) according to the structure of object, object is turned to data flow by sequence, is encapsulated in the message body (Message Body) of a HTTP request message.Record data stream metamessage (Metadata) in HTTP request head (Headers), comprises sequencing method and object size.Territory, path (Path) specific data of HTTP request message exchanges object handling program.Method territory (Method) the specific data swap operation method of HTTP request message.
(3) target processing system of exchanges data receives HTTP request, and according to the data element information that message carries, adopt corresponding unserializing method, converting objects is the data of native system compatible format.
(4) data processor processes data subsequently.Mode with http response after having processed returns result.
In above-mentioned steps, the method that object serialization adopts is determined through consultation.Data after serializing can be compressed, and compression algorithm is also determined through consultation.In a data exchange process, the side initiating TCP connection in two sides of participation exchanges data is called client, and the opposing party is called service end.Sequencing method negotiations process is as follows:
(1) after setting up TCP connection, client sends a HTTP request, and in request header, Accept field carries the sequencing method list that client is supported.Multiple sequencing methods of client support use branch to separate.
(2), after service end receives request, read the sequencing method list that Accept field obtains client support, then compare with the sequencing method of native system support, choose the sequencing method that one or more both sides support.
(3) service end returns a http response, and in head response, Accept field carries the sequencing method chosen.
(4) client knows the sequencing method of negotiation after receiving the response that service end beams back.It is follow-up that to adopt with the data exchange process of service end in the sequencing method consulted some.
The process of consulting the process of compression algorithm and negotiation sequence method is similar, and detailed process is as follows:
(1) after setting up TCP connection, client sends a HTTP request, and in request header, Accept-Encoding field carries the compression algorithm list that client is supported.Multiple compression algorithms of client support use branch to separate.
(2), after service end receives request, read the compression algorithm list that Accept-Encoding field obtains client support, then compare with the compression algorithm of native system support, choose the compression algorithm that one or more both sides support.
(3) service end returns a http response, and in head response, Accept-Encoding field carries available compression algorithm.
(4) client knows the compression algorithm of negotiation after receiving the response that service end beams back.Follow-up with the data exchange process of service end in adopt the compression algorithm of consulting to compress when needing to compress data.
Sequencing method is necessary.The each side participating in exchanges data at least must realize the JSON sequencing method of object.Data compression algorithm is optional, and each side participating in exchanges data can not support compression.
Service end processes data exchange request (can be the read-write of a secondary data, deletion action), then returns result in the mode of http response.
The present invention includes a client-side program and a serve end program, initiate a side of data exchange request as client, deal with data exchanges a side of request as service end.Program in fact can be service end is also client.Communication between client with service end adopts TCP to be connected, and the message of exchange defers to http protocol.The concrete steps of exchanges data are as follows:
Below in conjunction with drawings and Examples, the invention will be further described.
Step 1, client prepare a data record to be exchanged (being called object), then set up TCP with service end and are connected.TCP between client and service end is connected to after an exchanges data completes and can keeps.Be connected if therefore existed between client with service end, the connection that can multiplexingly exist.
After step 2, connection establishment, first client sends a conversation initialization request, for negotiation sequence method and compression algorithm to service end.Client inserts field Accept and the Accept-Encoding that two are respectively used to sequencing method and the compression algorithm of specifying this client to support in the request header (Headers) of request message.Multiple sequencing method, compression algorithm directly adopt branch to separate.
Following conversation initialization message is pointed out, client supports JSON serializing and xml serializing two kinds of serializing modes, supports gzip and snappy compression algorithm simultaneously.
HEAD/HTTP/1.1
Accept:application/json;application/xml
Accept-Encoding:gzip;snappy
After step 3, service end receive conversation initialization request, the sequencing method of the sequencing method of client support, compression algorithm and the support of this service end, compression algorithm are contrasted.In http response message, use Accept and Accept-Encoding field to return sequencing method and the compression algorithm of negotiation.
Following conversation initialization response message, negotiation result is JSON serializing and xml serializing two kinds of methods, and gzip compression algorithm.
HTTP/1.1 200 OK
Accept:application/json;application/xml
Accept-Encoding:gzip
Server:des
Step 4, client send a data exchange request to service end, object serialization to be operated, compression are encapsulated in the message body of HTTP request message.Sequencing method and the compression algorithm of the use of this clearing house is indicated at request message head.
Following data exchange request message represents: client adopts JSON sequencing method (PUT) data record to stock_price write.Do not compress after Data Serialization.
PUT/stock_price HTTP/1.1
Content-Type:application/json
Content-Length:77
{″name″:″APPL″,″price″:″126.37″,"source":″NASDAQ″,″date″:"2015-03-30″}
Step 5, service end process request after receiving client-requested.The operation of specifying by client, destination path process, and then return a http response message.
Below respond expression: data exchange operation success, and service end there is no data to return.
HTTP/1.1 200 OK
Server:des。