RPC (remote Procedure call Protocol)--Remoting procedure invocation protocol
At runtime, a client-to-server RPC call, the internal operation of the following roughly 10 steps: 1. Call the client handle, execute the transfer parameter 2. Call the Local system core to send a network message 3. The message is sent to the remote host 4. The server handle gets the message and takes the parameter 5. Execute Remote procedure 6. The execution process returns the result to the server handle 7. The server handle returns the result, calling the remote system Kernel 8. The message is returned to the local host 9. The client handle is received by the kernel for message 10. The client receives the data returned by the handle REBBITMQ the invocation of the remote method through the message interaction. The main process consists of both ends: the called side:
1 //Create a link2 varFactory =NewConnectionFactory () {HostName ="localhost" };3 using(varConnection =Factory. CreateConnection ())4 {5 //Create a channel6 using(varChannel =connection. Createmodel ())7 {8 //Create a message queue to receive RPC requests9Channel. Queuedeclare (Queue:"Rpc_queue", Durable:false, Exclusive:false, Autodelete:false, arguments:NULL);Ten One //listen and consume messages, and receive messages to notify the sender A varConsumer =NewQueueingbasicconsumer (channel); -Channel. Basicconsume (Queue:"Rpc_queue", Noack:false, Consumer:consumer); - while(true) the { -Thread.Sleep ( +); -Console.WriteLine (string. Format ("wait for RPC request ...")); - stringResponse =NULL; + varEA =(Basicdelivereventargs) consumer. Queue.dequeue (); - varBODY =ea. Body; + varProps =ea. basicproperties; A varReplyprops =Channel. Createbasicproperties (); at //Contract ID -Replyprops.correlationid =props. Correlationid; - varMessage =Encoding.UTF8.GetString (body); - Try - { -Console.WriteLine (string. Format ("Execute method {0}", message)); inResponse =Request (message); - } to Catch(Exception ex) + { -Console.WriteLine (string. Format ("Execute {0} exception, exception information: {1}", message, ex. Message)); theResponse =""; * } $ finallyPanax Notoginseng { - varResponseBytes =Encoding.UTF8.GetBytes (response); the //returns the message as requested by the sender (waiting for the returned receive queue, contract ID) +Channel. Basicpublish (Exchange:"", Routingkey:props. ReplyTo, Basicproperties:replyprops, body:responsebytes); A //you need to notify the sender when you receive a message theChannel. Basicack (Deliverytag:ea. Deliverytag, multiple:false); + } - } $ } $}
Call End:
1 //Create a link2 varFactory =NewConnectionFactory () {HostName ="localhost" };3 using(varConnection =Factory. CreateConnection ())4 {5 //Create a channel6 using(varChannel =connection. Createmodel ())7 {8 //to create a queue that receives a return message9 stringReplyqueuename =Channel. Queuedeclare (). QueueName;Ten One //listen and consume messages, receive messages without notifying the sender A varConsumer =NewQueueingbasicconsumer (channel); -Channel. Basicconsume (Queue:replyqueuename, Noack:true, Consumer:consumer); - the varProps =Channel. Createbasicproperties (); -Props. ReplyTo =Replyqueuename; - varCorrid=Guid.NewGuid (). ToString (); -Props. Correlationid =Corrid; + - varMessagebyte =Encoding.UTF8.GetBytes (message); + //sends a message and carries the relevant content (receive queue, contract ID) that receives the return message AChannel. Basicpublish (Exchange:"", Routingkey:"Rpc_queue", Basicproperties:props, body:messagebyte); at - while(true) - { -Thread.Sleep ( +); -Console.WriteLine (string. Format ("wait for request requst:{0} return ...", message)); - varEA =(Basicdelivereventargs) consumer. Queue.dequeue (); in if(EA.) basicproperties.correlationid==Corrid) - { toConsole.WriteLine (string. Format ("Request Requst:{0};response:{1}", message, Encoding.UTF8.GetString (ea. Body )); + Break; - } the } * } $}
Rebbitmq-rpc (C #)