php使用rabbitMQ遇到的坑

1,要使用rabbitMQ队列需要引入文件

  require_once __DIR__.'/vendor/autoload.php';

  use PhpAmqpLib\Connection\AMQPConnection;

  use PhpAmqpLib\Message\AMQPMessage;

  use PhpAmqpLib\Wire\AMQPReader;

2,若已创建队列和交换机,不能加$ch->exchange_declare($exchange, $mqtype, false, true, false);声明,加这行需要添加交换机和队列的权限

3,大致代码

  $conn = new AMQPConnection($this->ServerAddress, $this->Port, $this->mquser, $this->mqpassw, $this->vhost);//建立连接

  $ch = $conn->channel(); 
  //$ch->exchange_declare($exchange, $mqtype, false, true, false);//声明交换机 
  $msg = new AMQPMessage(json_encode($msg_body),array('content_type' => 'text/plain'));
  $ch->basic_publish($msg, $exchange); //推送消息
  $ch->close();
  $conn->close(); //关闭连接

posted @ 2020-12-14 15:43  u-ttXY  阅读(1757)  评论(0编辑  收藏  举报