• rebbit 代码示例


    https://blog.csdn.net/qq2942713658/article/details/119716410

    https://www.cnblogs.com/yxhblogs/p/10739137.html

        public function queue_declare(
            $queue = '', #队列名称
            $passive = false,#如果为 true , 则执行声明或者检查队列是否存在
            $durable = false, #设置是否持久化
            $exclusive = false,# 设置是否排他。为 true 则设置队列为排他的
            $auto_delete = true, #是否自动删除
            $nowait = false,#如果设置为 false, 则不期望 RabbitMQ 服务器有一个 Exchange.DeclareOk 这样响应。
            $arguments = array(), #设置队列的其他一些参数,如 x-message-ttl、x-expires、x-max-length
            $ticket = null
        )
    composer require php-amqplib/php-amqplib

    1.Hello World

    (1)send.php

    <?php
     
    require_once __DIR__ . '/vendor/autoload.php';
    use PhpAmqpLib\Connection\AMQPStreamConnection;
    use PhpAmqpLib\Message\AMQPMessage;
     
    $connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
    $channel = $connection->channel();
     
    $channel->queue_declare('hello', false, false, false, false);
     
    $msg = new AMQPMessage('Hello World!');
    $channel->basic_publish($msg, '', 'hello');
     
    echo " [x] Sent 'Hello World!'\n";
     
    $channel->close();
    $connection->close();
    ?>

    (2)receive.php

    <?php
     
    require_once __DIR__ . '/vendor/autoload.php';
    use PhpAmqpLib\Connection\AMQPStreamConnection;
     
    $connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
    $channel = $connection->channel();
     
    $channel->queue_declare('hello', false, false, false, false);
     
    echo " [*] Waiting for messages. To exit press CTRL+C\n";
     
    $callback = function ($msg) {
        echo ' [x] Received ', $msg->body, "\n";
    };
     
    $channel->basic_consume('hello', '', false, true, false, false, $callback);
     
    while ($channel->is_open()) {
        $channel->wait();
    }
     
    $channel->close();
    $connection->close();
    ?>

    2.工作队列模式

     new_task.php

    <?php
     
    require_once __DIR__ . '/vendor/autoload.php';
    use PhpAmqpLib\Connection\AMQPStreamConnection;
    use PhpAmqpLib\Message\AMQPMessage;
     
    $connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
    $channel = $connection->channel();
     
    $channel->queue_declare('task_queue', false, true, false, false);
     
    $data = implode(' ', array_slice($argv, 1));
    if (empty($data)) {
        $data = "Hello World!";
    }
    $msg = new AMQPMessage(
        $data,
        array('delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT)
    );
     
    $channel->basic_publish($msg, '', 'task_queue');
     
    echo ' [x] Sent ', $data, "\n";
     
    $channel->close();
    $connection->close();
    ?>

    (2)worker.php

    <?php
     
    require_once __DIR__ . '/vendor/autoload.php';
    use PhpAmqpLib\Connection\AMQPStreamConnection;
     
    $connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
    $channel = $connection->channel();
     
    $channel->queue_declare('task_queue', false, true, false, false);
     
    echo " [*] Waiting for messages. To exit press CTRL+C\n";
     
    $callback = function ($msg) {
        echo ' [x] Received ', $msg->body, "\n";
        sleep(substr_count($msg->body, '.'));
        echo " [x] Done\n";
        $msg->ack();
    };
     
    $channel->basic_qos(null, 1, null);
    $channel->basic_consume('task_queue', '', false, false, false, false, $callback);
     
    while ($channel->is_open()) {
        $channel->wait();
    }
     
    $channel->close();
    $connection->close();
    ?>

    3.发布/订阅模式

    <?php
     
    require_once __DIR__ . '/vendor/autoload.php';
    use PhpAmqpLib\Connection\AMQPStreamConnection;
     
    $connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
    $channel = $connection->channel();
     
    $channel->exchange_declare('logs', 'fanout', false, false, false);
     
    list($queue_name, ,) = $channel->queue_declare("", false, false, true, false);
     
    $channel->queue_bind($queue_name, 'logs');
     
    echo " [*] Waiting for logs. To exit press CTRL+C\n";
     
    $callback = function ($msg) {
        echo ' [x] ', $msg->body, "\n";
    };
     
    $channel->basic_consume($queue_name, '', false, true, false, false, $callback);
     
    while ($channel->is_open()) {
        $channel->wait();
    }
     
    $channel->close();
    $connection->close();
    ?>
    <?php
     
    require_once __DIR__ . '/vendor/autoload.php';
    use PhpAmqpLib\Connection\AMQPStreamConnection;
    use PhpAmqpLib\Message\AMQPMessage;
     
    $connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
    $channel = $connection->channel();
     
    $channel->exchange_declare('logs', 'fanout', false, false, false);
     
    $data = implode(' ', array_slice($argv, 1));
    if (empty($data)) {
        $data = "info: Hello World!";
    }
    $msg = new AMQPMessage($data);
     
    $channel->basic_publish($msg, 'logs');
     
    echo ' [x] Sent ', $data, "\n";
     
    $channel->close();
    $connection->close();
    ?>

    4.路由模式

    php send.php info 发送内容

    php work1.php info

    <?php
     
    require_once __DIR__ . '/vendor/autoload.php';
    use PhpAmqpLib\Connection\AMQPStreamConnection;
     
    $connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
    $channel = $connection->channel();
     
    $channel->exchange_declare('direct_logs', 'direct', false, false, false);
     
    list($queue_name, ,) = $channel->queue_declare("", false, false, true, false);
     
    $severities = array_slice($argv, 1);
    if (empty($severities)) {
        file_put_contents('php://stderr', "Usage: $argv[0] [info] [warning] [error]\n");
        exit(1);
    }
     
    foreach ($severities as $severity) {
        $channel->queue_bind($queue_name, 'direct_logs', $severity);
    }
     
    echo " [*] Waiting for logs. To exit press CTRL+C\n";
     
    $callback = function ($msg) {
        echo ' [x] ', $msg->delivery_info['routing_key'], ':', $msg->body, "\n";
    $msg->ack();
    }; 
    $channel
    ->basic_consume($queue_name, '', false, false, false, false, $callback); while ($channel->is_open()) { $channel->wait(); } $channel->close(); $connection->close(); ?>
    <?php
     
    require_once __DIR__ . '/vendor/autoload.php';
    use PhpAmqpLib\Connection\AMQPStreamConnection;
    use PhpAmqpLib\Message\AMQPMessage;
     
    $connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
    $channel = $connection->channel();
     
    $channel->exchange_declare('direct_logs', 'direct', false, false, false);
     
    $severity = isset($argv[1]) && !empty($argv[1]) ? $argv[1] : 'info';
     
    $data = implode(' ', array_slice($argv, 2));
    if (empty($data)) {
        $data = "Hello World!";
    }
     
    $msg = new AMQPMessage($data);
     
    $channel->basic_publish($msg, 'direct_logs', $severity);
     
    echo ' [x] Sent ', $severity, ':', $data, "\n";
     
    $channel->close();
    $connection->close();
    ?>

    5.主题模式

    <?php
     
    require_once __DIR__ . '/vendor/autoload.php';
    use PhpAmqpLib\Connection\AMQPStreamConnection;
     
    $connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
    $channel = $connection->channel();
     
    $channel->exchange_declare('topic_logs', 'topic', false, false, false);
     
    list($queue_name, ,) = $channel->queue_declare("", false, false, true, false);
     
    $binding_keys = array_slice($argv, 1);
    if (empty($binding_keys)) {
        file_put_contents('php://stderr', "Usage: $argv[0] [binding_key]\n");
        exit(1);
    }
     
    foreach ($binding_keys as $binding_key) {
        $channel->queue_bind($queue_name, 'topic_logs', $binding_key);
    }
     
    echo " [*] Waiting for logs. To exit press CTRL+C\n";
     
    $callback = function ($msg) {
        echo ' [x] ', $msg->delivery_info['routing_key'], ':', $msg->body, "\n";
    };
     
    $channel->basic_consume($queue_name, '', false, true, false, false, $callback);
     
    while ($channel->is_open()) {
        $channel->wait();
    }
     
    $channel->close();
    $connection->close();
    ?>
    <?php
     
    require_once __DIR__ . '/vendor/autoload.php';
    use PhpAmqpLib\Connection\AMQPStreamConnection;
    use PhpAmqpLib\Message\AMQPMessage;
     
    $connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
    $channel = $connection->channel();
     
    $channel->exchange_declare('topic_logs', 'topic', false, false, false);
     
    $routing_key = isset($argv[1]) && !empty($argv[1]) ? $argv[1] : 'anonymous.info';
    $data = implode(' ', array_slice($argv, 2));
    if (empty($data)) {
        $data = "Hello World!";
    }
     
    $msg = new AMQPMessage($data);
     
    $channel->basic_publish($msg, 'topic_logs', $routing_key);
     
    echo ' [x] Sent ', $routing_key, ':', $data, "\n";
     
    $channel->close();
    $connection->close();
    ?>

    6.RPC模式

    <?php
     
    require_once __DIR__ . '/vendor/autoload.php';
    use PhpAmqpLib\Connection\AMQPStreamConnection;
    use PhpAmqpLib\Message\AMQPMessage;
     
    $connection = new AMQPStreamConnection('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->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();
    ?>
    <?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,
                '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,
                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";
    ?>

    rabbitmq 延迟队列 php

    <?php
     
    require_once '../vendor/autoload.php';
    use PhpAmqpLib\Connection\AMQPStreamConnection;
    use PhpAmqpLib\Message\AMQPMessage;
    use PhpAmqpLib\Wire\AMQPTable;
    use PhpAmqpLib\Exchange\AMQPExchangeType;
     
    $connection = new AMQPStreamConnection('192.168.0.5', 5672, 'test', 'test', '/');
    $channel = $connection->channel();
    //给cache发送  使其过期然后定向到另一个
    //声明两个队列
    $channel->exchange_declare('delay_exchange', 'direct',false,false,false);
    $channel->exchange_declare('cache_exchange', 'direct',false,false,false);
      
    $tale = new AMQPTable();
    $tale->set('x-dead-letter-exchange', 'delay_exchange');//****很关键  表示过期后由哪个exchange处理
    $tale->set('x-dead-letter-routing-key','delay_exchange');//****很关键  表示过期后由哪个exchange处理
    //$tale->set('x-message-ttl',15000);  //存活时长   下面的过期时间不能超过
      
    $channel->queue_declare('cache_queue',false,true,false,false,false,$tale);
    $channel->queue_bind('cache_queue', 'cache_exchange','cache_exchange');
      
    $channel->queue_declare('delay_queue',false,true,false,false,false);
    $channel->queue_bind('delay_queue', 'delay_exchange','delay_exchange');
      
      
    $msg = new AMQPMessage($argv[1], array(
        'expiration' => intval($argv[2]),
        'delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT
      
    ));
      
    $channel->basic_publish($msg,'cache_exchange','cache_exchange');
    echo date('Y-m-d H:i:s')." [x] Sent {$argv[1]} ".PHP_EOL;
      
    $channel->close();
    $connection->close();
    <?php
    require_once '../vendor/autoload.php';
    use PhpAmqpLib\Connection\AMQPStreamConnection;
     
    $connection = new AMQPStreamConnection('192.168.0.5', 5672, 'test', 'test', '/');
    $channel = $connection->channel();
    $channel->exchange_declare('delay_exchange', 'direct',false,false,false);
      
    $channel->queue_declare('delay_queue',false,true,false,false,false);
    $channel->queue_bind('delay_queue', 'delay_exchange','delay_exchange');
      
    echo ' [*] Waiting for message. To exit press CTRL+C '.PHP_EOL;
      
    $callback = function ($msg){
        echo date('Y-m-d H:i:s')." [x] Received",$msg->body,PHP_EOL;
      
        // $msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
      
    };
      
    //只有consumer已经处理并确认了上一条message时queue才分派新的message给它
    // $channel->basic_qos(null, 1, null);
    $channel->basic_consume('delay_queue','',false,true,false,false,$callback);
      
      
    while (count($channel->callbacks)) {
        $channel->wait();
    }
    $channel->close();
    $connection->close()

    添加队列确认发送ack  https://www.jianshu.com/p/a2044d871877

    <?php
    
    require_once __DIR__ . '/vendor/autoload.php';
    
    use PhpAmqpLib\Connection\AMQPStreamConnection;
    use PhpAmqpLib\Message\AMQPMessage;
    use PhpAmqpLib\Wire\AMQPTable;
    
    $connection = new AMQPStreamConnection('192.168.1.187', 5672, 'guest', 'guest');
    $channel = $connection->channel();
    
    $channel->exchange_declare('logs', 'fanout', false, true, false);
    
    $data = implode(' ', array_slice($argv, 1));
    if (empty($data)) {
        $data = "info: Hello World!";
    }
    $msg = new AMQPMessage($data);
    $channel->confirm_select();
    $channel->basic_publish($msg, 'logs');
    //注册ack回调
    $channel->set_nack_handler(function (AMQPMessage $msg) {
        var_dump('nack');
    });
    $channel->set_ack_handler(function (AMQPMessage $msg) {
        var_dump("ack");
    });
    //等待接收ack
    $channel->wait_for_pending_acks();
    
    $channel->close();
    $connection->close();
  • 相关阅读:
    window.location.href问题,点击,跳转到首页
    JS indexOf() lastIndexOf()与substring()截取字符串的区别
    原来的方法增加参数时,必须为新增的参数加个默认值
    Chrome不支持本地Ajax请求解决?
    Smarty中{literal}的使用详解
    windows不能在本地计算机启动apache
    Apache虚拟主机配置
    wamp环境网站根目录更改
    Java 读书笔记 (十三) for each 循环
    Java 读书笔记 (十二) Java Character 类
  • 原文地址:https://www.cnblogs.com/chengfengchi/p/15599073.html
Copyright © 2020-2023  润新知