首页 > 其他分享 >rabbitmq模式 RPC

rabbitmq模式 RPC

时间:2022-09-20 23:47:24浏览次数:48  
标签:false req RPC 模式 queue rpc rabbitmq id channel

rabbitmq模式 RPC

rpc_server.php

<?php

require_once __DIR__ . '/../../vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;

$connection = new AMQPStreamConnection('localhost', 5672, 'admin', 'admin','vhost1');
$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->ack();
};

$channel->basic_qos(null, 1, null);
$channel->basic_consume('rpc_queue', '', false, false, false, false, $callback);

while ($channel->is_open()) {
$channel->wait();
}

$channel->close();
$connection->close();

rpc_client.php

<?php

require_once __DIR__ . '/../../vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;

class FibonacciRpcClient
{
    private $connection;
    private $channel;
    private $callback_queue;
    private $response;
    private $corr_id;

    public function __construct()
    {
        $this->connection = new AMQPStreamConnection('localhost',5672,'admin','admin','vhost1');
$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,
true,
false,
false,
array(
$this,
'onResponse'
)
);
}

public function onResponse($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";

运行调试

php rpc_server.php
php rpc_client.php

标签:false,req,RPC,模式,queue,rpc,rabbitmq,id,channel
From: https://www.cnblogs.com/hu308830232/p/16714092.html

相关文章

  • rabbitmq模式 topics
    rabbitmq模式topicsemit_log_topic.php<?phprequire_once__DIR__.'/../../vendor/autoload.php';usePhpAmqpLib\Connection\AMQPStreamConnection;usePhpAmqp......
  • 单例模式
    理论单例模式(Singleton),保证一个类仅有一个实例,并提供一个访问它的全局访问点。单例模式的好处:单例模式因为Singleton类封装它的唯一实例,这样它可以严格地控制客户怎......
  • Java单例模式
    单例模式  单例模式核心是保证一个类只有一个实例,并且提供一个访问实例的全局访问点。使用场景  需要频繁的进行创建和销毁的对象、创建对象时耗时过多或耗费资源过......
  • CSDN(markdown模式下)如何调整图片大小以及如何调整位置
    版权声明:本文为CSDN博主「圣喵」的原创文章,遵循CC4.0BY-SA版权协议,转载请附上原文出处链接及本声明。原文链接:https://blog.csdn.net/m0_66769266/article/details/1240......
  • 设计模式 -- Decorator(装饰模式)
    装饰模式(Decorator)在某些情况下,我们可能会“过度的使用继承来扩展对象的功能”,由于继承为类型引入静态特质,使得这种扩展方式缺乏灵活性;并且随着子类的增多(扩展功能的在......
  • java流之装饰者模式
    扩展类在于继承FilterInputStream,从而增强read和writer方法的能力输出流同样如此。   推荐文章:https://blog.csdn.net/hustzw07/article/details/80795855......
  • RabbitMQ集群脑裂故障处理
    网络抖动或故障均可能会导致RabbitMQ集群发生脑裂故障,在不同节点的RabbitMQ管理界面上可以看出其他节点存在红色不可用,显示错误信息如下NetworkpartitiondetectedMnesi......
  • 【设计模式】之桥接模式
    桥接模式(BridgePattern)定义:将抽象化与实现化分离,使得双方可以独立变化。当然好多人看到这定义头都大了,什么叫抽象化?什么又叫实现化?还是举个例子吧。假如说,有个图形(G......
  • 我的设计模式之旅、14 模板方法模式
    编程旅途是漫长遥远的,在不同时刻有不同的感悟,本文会一直更新下去。思考总结思考问题多个类中包含许多相似代码,只是小部分代码不同。思考如何在保持算法结构完整的情况......
  • JS使用策略模式优化条件选择结构
    这段代码是采用if-else的方式判断多个不同的条件。functionpermission(role){if(role==="operations"){getOperationPermission()}elseif(role=......