One, Callback queue callback queues
A client sends a request to the server, and after the server processes the request, it saves its processing results in a storage body.
The client, in order to obtain the processing result, sends a callback queue address reply_to when the client sends a request to the server.
second, Correlation ID Association identification
A client may send multiple requests to the server, and when the server finishes processing, the client cannot discern the response in the callback queue that corresponds to the specific and that request. To handle this situation, the client sends each request with a unique correlation_id property, so that the client can tell which request the response belongs to in the callback queue based on the value of the correlation_id field.
Three, the legend
The client sends the request: An app sends the request information to the client, and then the client posts the RPC request, sending the RPC request to the RPC request queue with
at least two attributes
with reply_to and correlation_id Server-side workflow: waits for the receiving client to send an RPC request, when the request occurs, the server pulls the request from the RPC request queue, and then processes the
response to the client in the callback queue specified by reply_to to
accept the processing result: The client waits for a response in the callback queue, and when the response appears, it
returns it to the corresponding app based on the value of the correlation_id field in the response.
A Send data and queue name 123abc--"Switch--" queue rpc_queue--"b take data--" to data processing-"B sent the processing of information-" switch-"queue 123abc--" a get B processing data
Iv. Implementation of the Code
================== Service End =================
#!/usr/bin/env python Import Pika # establishes the connection, the server address is localhost, you can specify the IP address connection = Pika. Blockingconnection (Pika. Connectionparameters (host= ' localhost ') # establishes the session channel = Connection.channel () # declares the RPC request queue Channel.queue_declare
(queue= ' Rpc_queue ') # Data processing method def fib (n): if n = = 0:return 0 elif n = = 1:return 1 Else: return fib (n-1) + fib (n-2) # processes requests in the RPC request queue def on_request (ch, method, props, body): n = Int (body) print (" [.]
FIB (%s) "% n" # Call Data processing Method response = FIB (n) # sends the processing result (response) to the callback queue Ch.basic_publish (exchange= ", Routing_key=props.reply_to, Properties=pika.
Basicproperties (correlation_id = \ props.correlation_id), BODY=STR (response)) Ch.basic_ack (Delivery_tag = method.delivery_tag) # load Balancing, no more than one channel is sent to the server at the same time . Basic_qos (Prefetch_count=1) channel.basic_consume (on_request, queue= ' Rpc_queue ') Print ("[x] awaiting RPC requests") channel.start_consuming () ================== Client =================
#!/usr/bin/env python import pika Import UUID class Fibonaccirpcclient (object): Def __init__ (self): "" " When the client starts, the callback queue is created, the session is opened for sending RPC requests, and the accept Response "" "# establishes the connection, specifying the IP address of the server self.connection = Pi Ka. Blockingconnection (Pika. Connectionparameters (host= ' localhost ') # establishes a session, each channel represents a session task self. Channel = Self.connection.channel () # declares the callback queue, which is declared again because the server and client may be turned on, the declaration is idempotent, declared multiple times, but only once result =
Self.channel.queue_declare (exclusive=true) # The secondary queue is specified as the current client's callback queue Self.callback_queue = Result.method.queue
# The client subscribes to the callback queue and calls the ' On_response ' method to process the response when there is a response in the callback queue;
Self.channel.basic_consume (Self.on_response, No_ack=true, Queue=self.callback_queue) # function to process response in callback queue Def on_response (self, ch, method, props, body): if self.corr_id = = props.correlation_id : Self.response = body # IssuedRPC Request Def call (self, n): # Initialize Response Self.response = None #生成correlation_id self.corr_id = str (UUID.UUID4 ()) # sends RPC request contents to RPC request queue ' Rpc_queue ', and also sends ' reply_to ' and ' correlation_id '
' Self.channel.basic_publish (exchange= ', routing_key= ' rpc_queue ', Properties=pika.
Basicproperties (reply_to = Self.callback_queue, correlation_id = self.corr_id,), bod Y=STR (n)) while Self.response is NONE:SELF.CONNECTION.PR Ocess_data_events () return int (self.response) # Establish client FIBONACCI_RPC = fibonaccirpcclient () # Send RPC Request print ("[x] Requesting Fib (")") Response = Fibonacci_rpc.call ("[.]" Print ("[.] Got%r "% response)