1、更新
composer update
2、安装,需要开启sockets扩展才能安装
开启扩展后就能安装了composer require php-amqplib/php-amqplib
如果不成功在composer.json加入"php-amqplib/php-amqplib":"^2.6"
安装composer install
1、新建配置文件config/rabbit_mq.php
<?php
return [
'host'=>'127.0.0.1',
'port'=>'5672',
'user'=>'guest',
'password'=>'guest',
'vhost'=>'/',
'exchange_name' => 'email_dade3',//Exchange,交换机名称
'queue_name' => 'dade3',//Queue,队列名称
'route_key' => 'email_dade3',//路由
'consumer_tag' => 'consumer',
];
2、生产者Index.php
<?php
namespace app\controller;
use app\BaseController;
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Exchange\AMQPExchangeType;
use PhpAmqpLib\Message\AMQPMessage;
class Index extends BaseController
{
private $connection;
private $channel;
private $mq_config;
/**
* 初始化
* */
public function __construct()
{
$this->mq_config = config('rabbit_mq');
$this->connection = new AMQPStreamConnection(
$this->mq_config['host'],
$this->mq_config['port'],
$this->mq_config['user'],
$this->mq_config['password']
);
//创建通道
$this->channel = $this->connection->channel();
}
/**
* 消息
* */
public function indexs($params)
{
/**
* 创建队列(Queue)
* name: hello // 队列名称
* passive: false // 如果设置true存在则返回OK,否则就报错。设置false存在返回OK,不存在则自动创建
* durable: true // 是否持久化,设置false是存放到内存中的,RabbitMQ重启后会丢失;设置true,则代表是一个持久化的队列,服务重启后也会存在,因为服务会把持久化的queue存放到磁盘上当服务重启的时候,会重新加载之前被持久化的queue
* exclusive: false // 是否排他,指定该选项为true则队列只对当前连接有效,连接断开后自动删除
* auto_delete: false // 是否自动删除,当最后一个消费者断开连接之后队列是否自动被删除
*/
$this->channel->queue_declare($this->mq_config['queue_name'], false, true, false, false);
/**
* 创建交换机(Exchange)
* name: vckai_exchange// 交换机名称
* type: direct // 交换机类型,分别为direct/fanout/topic,参考另外文章的Exchange Type说明。
* passive: false // 如果设置true存在则返回OK,否则就报错。设置false存在返回OK,不存在则自动创建
* durable: false // 是否持久化,设置false是存放到内存中的,RabbitMQ重启后会丢失
* auto_delete: false // 是否自动删除,当最后一个消费者断开连接之后队列是否自动被删除
*/
$this->channel->exchange_declare($this->mq_config['exchange_name'], AMQPExchangeType::DIRECT, false, true, false);
// 绑定消息交换机和队列
$this->channel->queue_bind($this->mq_config['queue_name'], $this->mq_config['exchange_name'],$this->mq_config['route_key']);
$messageBody = json_encode($params,JSON_UNESCAPED_UNICODE);//将要发送数据变为json字符串
/**
* 创建AMQP消息类型
* delivery_mode 消息是否持久化
* AMQPMessage::DELIVERY_MODE_NON_PERSISTENT 不持久化
* AMQPMessage::DELIVERY_MODE_PERSISTENT 持久化
*/
$message = new AMQPMessage($messageBody, array('content_type' => 'text/plain', 'delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT));
/**
* 发送消息
* msg: $message // AMQP消息内容
* exchange: vckai_exchange // 交换机名称
* routing_key: hello // 路由key
*/
$this->channel->basic_publish($message, $this->mq_config['exchange_name'], $this->mq_config['route_key']);
//关闭连接
$this->stop();
return json(333);
}
/**
* 关闭
* */
public function stop()
{
$this->channel->close();
$this->connection->close();
}
/**
* 添加消息
*/
public function add()
{
$data = new Index();
$params ='time:'.date("Y-m-d H:i:s").' dade大得';
$data->indexs($params);
echo 'success';
}
}
3.消费者Dade.php
<?php
namespace app\controller;
use app\BaseController;
use app\Request;
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Exchange\AMQPExchangeType;
use think\db\exception\PDOException;
use think\facade\Log;
class Dade extends BaseController
{
private $connection;
private $channel;
private $mq_config;
public function __construct()
{
$this->mq_config = config('rabbit_mq');
$this->connection = new AMQPStreamConnection(
$this->mq_config['host'],
$this->mq_config['port'],
$this->mq_config['user'],
$this->mq_config['password']
);
//创建通道
$this->channel = $this->connection->channel();
}
/**
* 启动
*/
public function start()
{
// 设置消费者(Consumer)客户端同时只处理一条队列
// 这样是告诉RabbitMQ,再同一时刻,不要发送超过1条消息给一个消费者(Consumer),直到它已经处理了上一条消息并且作出了响应。这样,RabbitMQ就会把消息分发给下一个空闲的消费者(Consumer)。
//消费者端要把自动确认autoAck设置为false,basic_qos才有效。
//$this->channel->basic_qos(0, 1, false);
// 同样是创建路由和队列,以及绑定路由队列,注意要跟producer(生产者)的一致
// 这里其实可以不用设置,但是为了防止队列没有被创建所以做的容错处理
$this->channel->queue_declare($this->mq_config['queue_name'], false, true, false, false);
$this->channel->exchange_declare($this->mq_config['exchange_name'], AMQPExchangeType::DIRECT, false, true, false);
$this->channel->queue_bind($this->mq_config['queue_name'], $this->mq_config['exchange_name'], $this->mq_config['route_key']);
/**
*
* queue: queue_name // 被消费的队列名称
* consumer_tag: consumer_tag // 消费者客户端身份标识,用于区分多个客户端
* no_local: false // 这个功能属于AMQP的标准,但是RabbitMQ并没有做实现
* no_ack: true // 收到消息后,是否不需要回复确认即被认为被消费
* exclusive: false // 是否排他,即这个队列只能由一个消费者消费。适用于任务不允许进行并发处理的情况下
* nowait: false // 不返回执行结果,但是如果排他开启的话,则必须需要等待结果的,如果两个一起开就会报错
* callback: $callback // 回调逻辑处理函数
*
*/
$this->channel->basic_consume($this->mq_config['queue_name'], $this->mq_config['consumer_tag'], false, false, false, false, array($this, 'index'));
register_shutdown_function(array($this, 'shutdown'), $this->channel, $this->connection);
while (count($this->channel->callbacks)) {
//执行读取,看效果
//sleep(1);
$this->channel->wait();
}
}
/**
* 消息处理
* */
public function index($message){
//消息处理逻辑
echo $message->body . "\n";
if ($message->body !== 'quit') {
$obj = json_decode($message->body);
if (!isset($obj->id)) {
Log::write("error data:" . $message->body, 2);
} else {
try {
Log::write("data:" . json_encode($message));
} catch (\Think\Exception $e) {
Log::write($e->getMessage(), 2);
Log::write(json_encode($message), 2);
} catch (PDOException $pe) {
Log::write($pe->getMessage(), 2);
Log::write(json_encode($message), 2);
}
}
}
// 手动确认ack,确保消息已经处理
$message->delivery_info['channel']->basic_ack($message->delivery_info['delivery_tag']);
// Send a message with the string "quit" to cancel the consumer.
if ($message->body === 'quit') {
$message->delivery_info['channel']->basic_cancel($message->delivery_info['consumer_tag']);
}
}
/**
* 关闭进程
*/
function shutdown($channel, $connection)
{
$channel->close();
$connection->close();
}
}
4、执行php think make:command Dade会在command目录下生成Dade.php文件
<?php
declare (strict_types = 1);
namespace app\command;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use app\controller\Dade as da;
class Dade extends Command
{
protected function configure()
{
// 指令配置
$this->setName('dade')
->setDescription('the dade command');
}
protected function execute(Input $input, Output $output)
{
// 指令输出,修改这里调用方法
//$output->writeln('dade');
$dade = new da();
$dade->start();
}
}
5、修改 config/console.php 添加下面代码
<?php
// +----------------------------------------------------------------------
// | 控制台配置
// +----------------------------------------------------------------------
return [
// 指令定义
'commands' => [
'dade' => 'app\command\Dade',
],
];
控制台执行测试
php think dade
结果消费者接收到的信息
代码地址
php安装rebb扩展地址
windows安装rabbitmq的php扩展_大得369的博客-CSDN博客