译-PHP rabbitMQ Tutorial-6
Remote procedure call (RPC)
(using php-amqplib)
In the second tutorial we learned how to use Work Queues to distribute time-consuming tasks among multiple workers.
在第二个指导中,我们学习了如何运用工作队列在多个worker之间分发耗时的任务。
But what if we need to run a function on a remote computer and wait for the result? Well, that's a different story. This pattern is commonly known as Remote Procedure Call or RPC.
但是假使我们需要运行一个远程的函数并等待其返回结果时怎么办?好吧,那是两码事。这种模式通常被称为远程过程调用或者RPC。
In this tutorial we're going to use RabbitMQ to build an RPC system: a client and a scalable RPC server. As we don't have any time-consuming tasks that are worth distributing, we're going to create a dummy RPC service that returns Fibonacci numbers.
这回我们要用RabbitMQ打造一个RPC系统:一个客户端和一个可扩展的RPC服务端。由于我们没有什么值得分发的耗时任务,所以我们将创建一个伪RPC服务,用来返回斐波那契数列。
Client interface(客户端接口)
To illustrate how an RPC service could be used we're going to create a simple client class. It's going to expose a method named call which sends an RPC request and blocks until the answer is received:
我们将创建一个简单的客户端类来阐述RPC服务是如何使用的。这个类将揭示方法调用??发送一个RPC请求并阻塞直至收到回复。
$fibonacci_rpc = new FibonacciRpcClient();$response = $fibonacci_rpc->call(30);echo " [.] Got ", $response, "\n";
A note on RPC(关于RPC需要注意一点)
Although RPC is a pretty common pattern in computing, it's often criticised. The problems arise when a programmer is not aware whether a function call is local or if it's a slow RPC. Confusions like that result in an unpredictable system and adds unnecessary complexity to debugging. Instead of simplifying software, misused RPC can result in unmaintainable spaghetti code.
尽管RPC在电脑运算中很常见,但它十分挑剔。这个问题出现的原因是程序员不知道是否调用一个本地的方法或是否是一个很慢的RPC。这样的困惑便导致不可预测的系统并增加不必要的调试复杂性。比起简化的软件,误用RPC会导致不可维护的无头绪代码。
Bearing that in mind, consider the following advice:
铭记刚才问题,考虑下面的建议:
When in doubt avoid RPC. If you can, you should use an asynchronous pipeline - instead of RPC-like blocking, results are asynchronously pushed to a next computation stage.
如果有疑问,则尽量避免使用RPC。如果可以话,你应该使用异步管道??而不是RPC??像阻塞,结果被异步推送到下个计算阶段。
Callback queue(回调队列)
In general doing RPC over RabbitMQ is easy. A client sends a request message and a server replies with a response message. In order to receive a response we need to send a 'callback' queue address with the request. We can use the default queue. Let's try it:
一般来讲在RabbitMQ上搞RPC很容易??客户端发送请求消息,服务端回复响应消息。为了收到响应(消息)我们得在发送请求时附带一个回调队列地址。可以试试默认队列:
list($queue_name, ,) = $channel->queue_declare("", false, false, true, false);$msg = new AMQPMessage( $payload, array('reply_to' => $queue_name));$channel->basic_publish($msg, '', 'rpc_queue');# ... then code to read a response message from the callback_queue ...
Message properties(消息属性)
The AMQP protocol predefines a set of 14 properties that go with a message. Most of the properties are rarely used, with the exception of the following:
AMQP协议预定义了14个消息属性。大部分属性都很少用到,下面的除外:
Correlation Id(关联ID)
In the method presented above we suggest creating a callback queue for every RPC request. That's pretty inefficient, but fortunately there is a better way - let's create a single callback queue per client.
上面的方法我们暗示为每个RPC请求创建一个回调队列。这非常低效,但幸运的是,有一种更好的方式??我们可以为每个客户端只创建一个回调队列。
That raises a new issue, having received a response in that queue it's not clear to which request the response belongs. That's when the correlation_id property is used. We're going to set it to a unique value for every request. Later, when we receive a message in the callback queue we'll look at this property, and based on that we'll be able to match a response with a request. If we see an unknown correlation_id value, we may safely discard the message - it doesn't belong to our requests.
可这将带来新的问题,队列收到一个响应时,我们不清楚它属于哪个请求。这正是correlation_id发挥作用的地方。我们将为每一个请求设置一个唯一correlation_id。之后,当在回调队列中收到(响应)消息的时候我们来查看这个属性,基于它我们就可以把请求和响应进行匹配。要是我们检测到未知correlation_id,可能会安全丢弃这条消息??因为它不属于我们的请求嘛。
You may ask, why should we ignore unknown messages in the callback queue, rather than failing with an error? It's due to a possibility of a race condition on the server side. Although unlikely, it is possible that the RPC server will die just after sending us the answer, but before sending an acknowledgment message for the request. If that happens, the restarted RPC server will process the request again. That's why on the client we must handle the duplicate responses gracefully, and the RPC should ideally be idempotent.
你可能会问,为喵我们该忽略回调队列中的未知消息呢,而不是置为处理失败并返回一个错误?是因为存在服务端紊乱的可能性。尽管几率很小,可还是有可能??RPC服务在给我们发送完响应后宕掉,但还没来得进行消息确认。那样的话,重启的RPC服务会再次处理这个请求。 这也就是为喵客户端必须优雅地处理重复响应,而RPC服务最好的幂等的。
Summary
Our RPC will work like this:
RPC运行流程:
Putting it all together(合体!!!终极~\(???)/~啦啦啦!)
The Fibonacci task:
斐波那契任务:
function fib($n) {
if ($n == 0) return 0; if ($n == 1) return 1; return fib($n-1) + fib($n-2);}
We declare our fibonacci function. It assumes only valid positive integer input. (Don't expect this one to work for big numbers, and it's probably the slowest recursive implementation possible).
声明斐波那契函数。假定它只接收正整数。(别指望它能处理很大的数字,它可能是最慢的递归实现)
The code for our RPC server rpc_server.php looks like this:
rpc_server.php代码:
<?phprequire_once __DIR__ . '/vendor/autoload.php';use PhpAmqpLib\Connection\AMQPConnection;use PhpAmqpLib\Message\AMQPMessage;$connection = new AMQPConnection('localhost', 5672, 'guest', 'guest');$channel = $connection->channel();$channel->queue_declare('rpc_queue', false, false, false, false);function fib($n) { if ($n == 0) return 0; if ($n == 1) return 1; return fib($n-1) + fib($n-2);}echo " [x] Awaiting RPC requests\n";$callback = function($req) { $n = intval($req->body); echo " [.] fib(", $n, ")\n"; $msg = new AMQPMessage( (string) fib($n), array('correlation_id' => $req->get('correlation_id')) ); $req->delivery_info['channel']->basic_publish( $msg, '', $req->get('reply_to')); $req->delivery_info['channel']->basic_ack( $req->delivery_info['delivery_tag']);};$channel->basic_qos(null, 1, null);$channel->basic_consume('rpc_queue', '', false, false, false, false, $callback);while(count($channel->callbacks)) { $channel->wait();}$channel->close();$connection->close();?>
The server code is rather straightforward:
服务端代码相当直白:
The code for our RPC client rpc_client.php:
rpc_client.php代码:
connection = new AMQPConnection( 'localhost', 5672, 'guest', 'guest'); $this->channel = $this->connection->channel(); list($this->callback_queue, ,) = $this->channel->queue_declare( "", false, false, true, false); $this->channel->basic_consume( $this->callback_queue, '', false, false, false, false, array($this, 'on_response')); } public function on_response($rep) { if($rep->get('correlation_id') == $this->corr_id) { $this->response = $rep->body; } } public function call($n) { $this->response = null; $this->corr_id = uniqid(); $msg = new AMQPMessage( (string) $n, array('correlation_id' => $this->corr_id, 'reply_to' => $this->callback_queue) ); $this->channel->basic_publish($msg, '', 'rpc_queue'); while(!$this->response) { $this->channel->wait(); } return intval($this->response); }};$fibonacci_rpc = new FibonacciRpcClient();$response = $fibonacci_rpc->call(30);echo " [.] Got ", $response, "\n";?>
Now is a good time to take a look at our full example source code for rpc_client.php and rpc_server.php.
是时候看看整个例子的源码了rpc_client.php and rpc_server.php.
Our RPC service is now ready. We can start the server:
RPC服务准备就绪,启动!
$ php rpc_server.php [x] Awaiting RPC requests
To request a fibonacci number run the client:
运行客户端请求一个斐波那契数字:
$ php rpc_client.php [x] Requesting fib(30)
The design presented here is not the only possible implementation of a RPC service, but it has some important advantages:
这里展现的设计不是RPC服务的唯一可能实现,但它却有一些重要的优势:
Our code is still pretty simplistic and doesn't try to solve more complex (but important) problems, like:
我们的代码还是忒简化,并没有想解决更为复杂(但重要)的问题,像:
If you want to experiment, you may find the rabbitmq-management plugin useful for viewing the queues.
想尝试吗? rabbitmq-management plugin 这里你可能会发现一些有用的插件来查看队列。

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

JWT是一種基於JSON的開放標準,用於在各方之間安全地傳輸信息,主要用於身份驗證和信息交換。 1.JWT由Header、Payload和Signature三部分組成。 2.JWT的工作原理包括生成JWT、驗證JWT和解析Payload三個步驟。 3.在PHP中使用JWT進行身份驗證時,可以生成和驗證JWT,並在高級用法中包含用戶角色和權限信息。 4.常見錯誤包括簽名驗證失敗、令牌過期和Payload過大,調試技巧包括使用調試工具和日誌記錄。 5.性能優化和最佳實踐包括使用合適的簽名算法、合理設置有效期、

會話劫持可以通過以下步驟實現:1.獲取會話ID,2.使用會話ID,3.保持會話活躍。在PHP中防範會話劫持的方法包括:1.使用session_regenerate_id()函數重新生成會話ID,2.通過數據庫存儲會話數據,3.確保所有會話數據通過HTTPS傳輸。

在PHP中,異常處理通過try,catch,finally,和throw關鍵字實現。 1)try塊包圍可能拋出異常的代碼;2)catch塊處理異常;3)finally塊確保代碼始終執行;4)throw用於手動拋出異常。這些機制幫助提升代碼的健壯性和可維護性。

PHP中有四種主要錯誤類型:1.Notice:最輕微,不會中斷程序,如訪問未定義變量;2.Warning:比Notice嚴重,不會終止程序,如包含不存在文件;3.FatalError:最嚴重,會終止程序,如調用不存在函數;4.ParseError:語法錯誤,會阻止程序執行,如忘記添加結束標籤。

在PHP中,include,require,include_once,require_once的區別在於:1)include產生警告並繼續執行,2)require產生致命錯誤並停止執行,3)include_once和require_once防止重複包含。這些函數的選擇取決於文件的重要性和是否需要防止重複包含,合理使用可以提高代碼的可讀性和可維護性。

PHP和Python各有優勢,選擇依據項目需求。 1.PHP適合web開發,尤其快速開發和維護網站。 2.Python適用於數據科學、機器學習和人工智能,語法簡潔,適合初學者。

HTTP請求方法包括GET、POST、PUT和DELETE,分別用於獲取、提交、更新和刪除資源。 1.GET方法用於獲取資源,適用於讀取操作。 2.POST方法用於提交數據,常用於創建新資源。 3.PUT方法用於更新資源,適用於完整更新。 4.DELETE方法用於刪除資源,適用於刪除操作。

PHP是一種廣泛應用於服務器端的腳本語言,特別適合web開發。 1.PHP可以嵌入HTML,處理HTTP請求和響應,支持多種數據庫。 2.PHP用於生成動態網頁內容,處理表單數據,訪問數據庫等,具有強大的社區支持和開源資源。 3.PHP是解釋型語言,執行過程包括詞法分析、語法分析、編譯和執行。 4.PHP可以與MySQL結合用於用戶註冊系統等高級應用。 5.調試PHP時,可使用error_reporting()和var_dump()等函數。 6.優化PHP代碼可通過緩存機制、優化數據庫查詢和使用內置函數。 7
