ThinkPHP8集成RabbitMQ的完整案例實現
- 一、安裝依賴:需通過Composer安裝php-amqplib庫?
- 二、配置RabbitMQ
- 三、生產者
- 1、發送一個郵件,將任務發送到RabbitMQ隊列中。
- 2、運行結果展示
- 四、啟動消費者:命令行執行php think rabbitmq:consumer
- 1,在command文件夾下創建consumer.php文件
- 2,配置指令
- 3、執行結果展示
- 五、補充:寶塔安裝rabbitmq
一、安裝依賴:需通過Composer安裝php-amqplib庫?
composer require php-amqplib/php-amqplib
二、配置RabbitMQ
在服務器開放RabbitMQ端口5672
return ['default' => 'rabbitmq','connections' => ['rabbitmq' => ['driver' => 'rabbitmq','host' => '127.0.0.1', // RabbitMQ服務器地址'port' => 5672, // RabbitMQ端口'user' => 'guest', // 用戶名'password' => 'guest', // 密碼'vhost' => '/', // 虛擬主機'queue' => 'email_queue', // 隊列名稱'exchange' => 'email_exchange', // 交換機名稱'routing_key' => 'email_queue', // 路由鍵'durable' => true, // 是否持久化隊列和消息]]
];
三、生產者
1、發送一個郵件,將任務發送到RabbitMQ隊列中。
app/controller/SendEMail.php
namespace app\controller;
use app\common\SendEmailJob;
use think\facade\Config;
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;
class SendEmail
{public function sendemail(){$config = config('queue.connections.rabbitmq');// dd($config);$connection = new AMQPStreamConnection($config['host'], $config['port'],$config['user'], $config['password'], $config['vhost']);$channel = $connection->channel();$channel->exchange_declare($config['exchange'], 'direct', false, true, false);$channel->queue_declare($config['queue'], false, true, false, false);$channel->queue_bind($config['queue'], $config['exchange'], $config['routing_key']);$data = ['to' => '11user@example.com','subject' => 'ThinkPHP8 RabbitMQ測試','content' => '這是一封通過消息隊列發送的郵件'];$msg = new AMQPMessage(json_encode($data), ['delivery_mode' => 2]);$channel->basic_publish($msg, $config['exchange'], $config['routing_key']);$channel->close();$connection->close();return '郵件任務已發送到隊列';}}
2、運行結果展示
四、啟動消費者:命令行執行php think rabbitmq:consumer
1,在command文件夾下創建consumer.php文件
接收任務,從RabbitMQ隊列中獲取任務執行。
app/command/consumer.php
namespace app\command;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use PhpAmqpLib\Connection\AMQPStreamConnection;class Consumer extends Command {protected function configure() {$this->setName('rabbitmq:consumer')->setDescription('RabbitMQ消費者');}protected function execute(Input $input, Output $output) {$config = config('queue.connections.rabbitmq');$connection = new AMQPStreamConnection($config['host'], $config['port'],$config['user'], $config['password'], $config['vhost']);$channel = $connection->channel();$channel->queue_declare($config['queue'], false, true, false, false);$callback = function($msg) use ($output) {$data = json_decode($msg->body, true);$output->writeln("收到郵件任務: {$data['to']}");// 實際發送郵件邏輯$msg->ack();};$channel->basic_qos(null, 1, null);$channel->basic_consume($config['queue'], '', false, false, false, false, $callback);while ($channel->is_consuming()) {$channel->wait();}$channel->close();$connection->close();}
}
2,配置指令
config/console.php
'commands' => ['rabbitmq:consumer' => 'app\command\Consumer',],
執行命令:
php think rabbitmq:consumer
3、執行結果展示
五、補充:寶塔安裝rabbitmq
在寶塔軟件里面安裝rabbitmq 3.12.4
登錄可直觀展示