The principle of distributed architecture cornerstone RPC

RPC origin

With the development of the Internet, Web applications continue to expand the size of conventional vertical application architecture has been unable to cope, distributed service architecture and flow computing architecture imperative need a control system to ensure an orderly evolution of architecture.

  • Single Application Architecture
  • When the site traffic is small, only one application, all functions are deployed together to reduce costs and deploy nodes.
  • In this case, for simplifying CRUD workload data access frame (ORM) is the key.
  • Vertical Application Architecture
  • When the traffic gradually increased, a single application to bring the machine to increase acceleration getting smaller and smaller, the application is split into several disparate applications to improve efficiency.
  • In this case, a Web framework to accelerate the development of the front page (MVC) is the key.
  • Distributed Services Architecture
  • As more and more vertical applications, the inevitable interaction between applications, will be drawn out of the core business, as an independent service, and gradually form a stable service center, the front-end applications to more quickly respond to changing market demands.
  • In this case, for improving distributed service framework (RPC) service multiplexing and integration, to provide a unified service is the key.

For example: each team's service provider do not realize their own set of serialization, deserialization, network framework, connection pooling, transceiver thread, time-out processing, state and other "business beyond the" duplication of technical work, resulting in overall low effect.

Therefore, a unified framework to resolve RPC provides a unified service.

Below I will explain each RPC from the following four aspects.

The principle of RPC

 
High concurrency architecture series: RPC framework of realization of the principle, calling the whole process, and RPC architectural components

That is two servers A, B, deployed on an application server A, wants to call a function / method B on a server provided by the application, because it is not a memory, not be called directly, needs to be invoked by the network semantics and convey data call.

For example, A server you want to call a method on the server B:

Employee getEmployeeByName(String fullName)

The entire call process, mainly through the following steps:

1, establish communication

We must first solve the problem of communication: A machine that is the machine you want to call B, must first establish a communication link.

Mainly in this connection are transmitted in all data exchange through the establishment of a TCP connection, remote procedure calls between the client and the server. Connection can be connected on demand, after the end of the call cut off, it can be a long connection, multiple remote procedure calls to share the same connection.

2. Services Addressing

To solve the problem addressed, that is, on the application server A tell how the underlying RPC framework, how to connect to server B (such as a host or IP address) and what the name of the name of a specific port, methods of yes.

Under normal circumstances we need to provide B machine (host name or IP address) and a specific port, then specify the name of the method or function calls and parameter information into the parameters, so as to complete a service call.

Reliable way of addressing (mainly to provide discovery services) is the cornerstone of RPC, for example, can be used to register redis zookeeper or services.

 
High concurrency architecture series: RPC framework of realization of the principle, calling the whole process, and RPC architectural components
  1. From the perspective of the service provider's point of view: When the provider service starts, the need for automatic registration services to the registry;
  2. When the service provider to stop, you need to log off service to the registry;
  3. After the providers need to regularly send heartbeat to the registration center, a period of time did not receive a heartbeat from the provider that the service provider has stopped, picked out from the registry corresponding service.
  4. From the caller's point of view: Subscribe registry when the caller starts the message and get the address of the provider from the registry;
  5. When there is a line or offline provider registry informs the caller;
  6. When the caller off the assembly line, unsubscribe.

3, network transmission

3.1 Serialization

When an application on the A machine initiates a RPC call, the call methods and which into the reference information to be transmitted such as TCP to the machine B through the underlying network protocol, since the network protocol is based on binary, all parameter data of our transmission are required to sequence of the form (the serialize) or consist (Marshal) into binary to be transmitted in the network. Then the binary data of the sequence or grouping of the addressing operation and then transmitted over the network is sent to B machines.

3.2 deserialization

When the machine B A machine receives a request sent by an application, and information required docking parameters deserialize the received operation (sequence of the inverse operation), i.e. the binary information back to the expression in memory, then the method to find the corresponding (part of the address) for local calls (usually by generating a proxy proxy to call,
there is usually a JDK dynamic proxies, CGLIB dynamic proxy, Javassist bytecode generation technology, etc.), to give after return of a call.

4, service call

After local calling machine B (via proxy Proxy) to obtain a return value, the return value at this time need then sent back to A machine, also need to go through a sequence of operations, and then transmitted through the network to send data back to binary A machine, a machine when after receiving the return to the values, then again deserialize, expression was restored in memory, and finally to the application on the machine a performs the correlation process (usually the service logic processing operations).

Typically, after more than four steps, a complete RPC call to be completed.

PRC architectural components

A basic RPC framework which should contain at least the following four components:

1, the client (Client): caller to the service (the service consumer)

2, the client stub (Client Stub): storing the address information of the server, the data request parameter information packed into the client network message before sending it to the server through the network transmission

3, the server stubs (Server Stub): receiving a request sent by the client unpacks the message and then call the local service processing

4, the server (Server): true service providers

RPC procedure calls

 
High concurrency architecture series: RPC framework of realization of the principle, calling the whole process, and RPC architectural components

1, the service consumer (client client) calls the service by way of a local call

2, the client stub (client stub) after receiving a method invocation request is responsible, and other information into the reference sequence of (assembled) into the body of the message can be transmitted over the network

3, the client stub (client stub) to find the remote service address, and sends a message to the server over the network

4, the server stub (server stub) decoding (deserialization) after receiving the message

5, the server stub (server stub) calls the local service based on the decoded result of the correlation process

6, local services and the implementation of specific service logic returns the processing result to the server stub (server stub)

7, the server stub (server stub) returns a result message repackaged into (serialized) and is sent to the consumer via a network

8, the client stub (client stub) receives the message, and decodes (deserialize)

9, the service consumer to get the final result



Guess you like

Origin www.cnblogs.com/windpoplar/p/11967635.html