[Node. js] implementation principle of RPC (Remote process call)

Source: Internet
Author: User

When I first came into contact with RPC (Remote process call), I was able to locally call the method of the Program on the remote host. I saw a simple nodejs implementation, which is used to learn the RPC principle well: nodejs light_rpc example: // server var light_rpc = require ('. /index. js '); var port = 5556; var rpc = new light_rpc ({combine: function (a, B, callback) {callback (a + B) ;}, multiply: function (t, cb) {cb (t * 2 );}}). listen (port); Sample client: // client rpc. connect (5556, 'localhost', function (remote, conn) {remote. combine (1, 2, function (res) {if (res! = 3) {console. log ('error', res) ;}}) ;}); briefly introduces the entire process: 1. the server starts the program and listens on the port to implement functions provided to the client (such as combine and multiply in the preceding example) and stores them in an object. 2. The client starts the program and connects to the server. After the connection is complete, the describe command is sent, requiring the server to return the function name that can be called. Connection. on ('connect ', function () {connection. write (command (descrCmd) ;}); 3. the server receives the describe command and packs the function names that can be called and sends them out ("combine", "multiply") 4. the client receives the function name sent by the server, registers it to its own object, and packs a method for each function name so that the local call to these functions actually sends a request to the server: for (var p in cmd. data) {remoteObj [p] = getRemoteCallFunction (p, self. callbacks, connection); // The implementation of getRemoteCallFunction can be found below} 5. the client calls the server function: 1) generates a unique ID for the incoming callback function, called callbackI. D. Record it to an object of the client. 2) wrap the following data and send it to the server: Call function name, JSON serialized parameter list, callbackId function getRemoteCallFunction (callback name, callbacks, connection) {return function () {var id = uuid. generate (); if (typeof arguments [arguments. length-1] = 'function') {callbacks [id] = arguments [arguments. length-1];} var args = parseArgumentsToArray. call (this, arguments); var newCmd = command (Region name, {id: id, args: args}); connection. write (newCmd );} 6. The server receives the above information, parses the data, deserializes the parameter list, and calls the function based on the function name and parameter. Var args = cmd. data. args; args. push (getSendCommandBackFunction (c, cmd. data. id); self. wrapper [cmd. command]. apply ({}, args); 7. after the function is run, serialize the result and send the received callbackId back to the client-side function getSendCommandBackFunction (connection, callback ID) {return function () {var innerArgs = parseArgumentsToArray. call ({}, arguments); var resultCommand = command (resultCmd, {id: Region id, args: innerArgs}); connection. write (resultCommand) ;}}8. The client receives the function running result and callbackId, extracts the callback function based on the callbackId, and passes the running result to the callback function for execution. Self. callbacks [cmd. data. id]. apply (this, cmd. data. args); 9. the whole process is completed, see the source code: https://github.com/romulka/nodejs-light_rpc several points of attention: 1. the client and server keep connected throughout the process. The connection is not closed after the http protocol is sent or received. Therefore, you cannot judge whether a data transfer is completed by disconnecting the connection. To determine whether the data is received, the client and server follow a simple protocol: add the length and separator of the data packet before the data. For example, the separator is \ n: [packet length \ n data], so that after receiving the data, the length of the data packet is first taken out, and then the accumulated received data packets are constantly judged to be equal to or greater than this length, if the data transfer is complete, parse and extract the data. 2. this RPC is simple because there is no function type in the parameter. For example, if a parameter is an object with a function member, the function is ignored during JSON serialization, this function cannot be executed on the server. To solve this problem, we need to perform complex processing: traverse each parameter to be sent to the remote end in depth, extract the function members, generate a unique id for the function, and put it in a local object, replace this function member with this id string and identify the member as a function. In this way, the object can be serialized and sent out. The server receives the call. When the function in the parameter object is used, it is determined that this is a function processed by the client. There is an id that is sent back to the client, send the callback function id to the client in the same way, and wait for the client to callback. The client receives this function id, finds this function entity, calls it, and sends the result back to the server based on the callback id sent by the server, finds the callback function, and continues the execution, complete. The function recording method can be completed in other ways. The general idea is to replace the function with something that can be serialized, and record the function so that this function can be found locally when called by the remote end. See dnode implementation.

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.