Design and practice of large distributed Web site Architecture Chapter One "Service-oriented Architecture (SOA)" 1.1 RPC based on TCP protocol

Source: Internet
Author: User
Tags serialization

1.1 RPC based on TCP protocol

1.1.1RPC noun Understanding

The full name of RPC is remote process call, which is widely used in the industry because of its wide application, many ways to realize it, and a lot of mature schemes such as Rmi,webservice. The processing power of a single server is limited by the cost of hardware, it is impossible to upgrade the infinite system, RPC will transform the original local call to the remote server to call the method, to the system's processing capacity and throughput of the near infinite system of ascension, this is the system to a certain stage of the inevitable reform, but also to achieve the basis of distributed computing.

As shown in Figure 1-2, the implementation of RPC includes the client and server side, which is the provider of the service, the caller and the service, the service caller sends the RPC request to the service provider, the service provider executes the request method based on the parameters provided by the service caller, returns the execution result to the caller, and completes the RPC call. The procedure for returning the result after the caller initiates the request and the service provider performs the requested method, and the sequence number and deserialization of the call parameters and response results involved, is described in detail in the next section.


With the development of the business, the size of the service caller has developed to a certain stage, there is also increasing pressure on service providers, and as a result, services need to be expanded, and as service providers increase and business grows, different services need to be grouped to isolate different businesses and avoid interaction, in which case The Routing and load balancing of services becomes an issue that must be considered. As shown in Figure 1-3.


The service consumer routes by obtaining the packet information and address information of the service provider. If a service provider is a cluster rather than a single machine, you need to select one of the calls based on a load-balancing strategy, and the routing and load balancing of the services, which are described in detail in the following sections, are not discussed here.


Serialization of 1.1.2 Objects

No matter what data type data, ultimately need to convert to binary stream on the network for transmission, then in object-oriented programming, how to transfer a well-defined object to the remote. The sender of the data needs to convert the object into a binary stream in order to transmit on the network, and the receiver of the data needs to revert the binary stream to the object.

The process of converting an object into a binary stream is called serialization of objects. The process of restoring a binary stream to an object is called deserialization of an object.

There are many mature solutions to the serialization and deserialization of objects, the more commonly used are Google's protocal buffers, the built-in serialization method of Java itself, the Hessian, and the JSON and XML that are described later, each with its own scenarios and advantages and disadvantages. Figure 1-4 shows a performance comparison of how an object is serialized into a network-accessible byte array using various serialization schemes, and then deserialized into an object's time.


Google's Protocol buffers real open source time is not long, but its performance, in a short period of time caused widespread concern, its advantage is excellent performance, support Cross-platform, but use its programming code intrusive, need to write proto files, Objects that cannot be directly used in object-oriented programming languages such as Java are slightly less efficient than Protocol Buffers,hessian, but they have good support for a variety of programming languages and are stable in performance, much more efficient than the built-in serialization of Java itself. The Java built-in serialization method does not require the introduction of a Third-party package, simple to use, in a scenario that is not very sensitive to efficiency requirements, it may not be a good choice, and the JSON and XML formats described in the following sections, in the Internet arena, especially in the current mobile Internet arena, benefit from its cross-platform features, has been widely used.

This section focuses on Java-built serialization and Java-based Hessian serialization, and demonstrates concrete implementation methods with code.

The following is a Java-built serialization approach and key code to implement deserialization:

Defines a byte array output stream

Bytearrayoutputstream OS = new Bytearrayoutputstream ();

Object output stream

ObjectOutputStream out = new ObjectOutputStream (OS);

Writes an object to the byte array output for serialization

Out.writeobject (Zhansan);

byte[] Zhanbyte = Os.tobytearray ();


Defines a byte array input stream

Bytearrayinputstream is = new Bytearrayinputstream ();

Perform deserialization to read an object from the stream

ObjectInputStream in = new ObjectInputStream (IS);

person who = (person) in.readobject ();

The instance Zhansan of the person class is serialized into a byte array through the WriteObject method of the ObjectOutputStream under the Java.io package, and then the word is converted by ObjectInputStream method The section array is deserialized to the person object.


Serialization using Hessian requires the introduction of its two-party package Hessian-4.0.7.jar for java-based Hessian serialization and deserialization, with key code as follows:

Defines a byte array output stream

Bytearrayoutputstream OS = new Bytearrayoutputstream ();

Hessian Serialization output

Hessianoutput ho = new hessianoutput (OS);

Ho.writeobject (Zhansan);

byte[] Zhansanbyte = Os.tobytearray ();


Defines a byte array input stream

Bytearrayinputstream is = new Bytearrayinputstream ();

Hessian read objects by deserialization

Hessianiput hi = new Hessianinput (IS);

person who = (person) hi.readobject ();

The instance Zhansan of person is serialized into a byte array by means of the Hessianoutput WriteObject method under the Com.aucho.hessian.io package, and then through Hessianinput's ReadObject method Deserializes an array of bytes to revert to an object.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.