您现在的位置是:首页 > PHP框架交流PHP框架交流
yii2 使用RabbitMQ mikemadisonweb/yii2-rabbitmq
上善若水2021-11-28 22:12:02【PHP框架交流】 3074人已围观
简介yii2 使用RabbitMQ,入门搭建RabbitMQ并使用一个简单的demo,前提 准备好RabbitMQ 一个yii2项目mq默认登录地址http://127.0.0.1:15672/默认账号密
yii2 使用RabbitMQ,入门搭建RabbitMQ并使用一个简单的demo,
前提 准备好RabbitMQ 一个yii2项目
mq默认登录地址http://127.0.0.1:15672/
默认账号密码 guest guest
1.安装扩展.
composer require mikemadisonweb/yii2-rabbitmq
2.配置RabbitMQ连接
打开config/console.php
components
中追加连接信息.
'components' => [
'cache' => [
'class' => 'yii\caching\FileCache',
],
'log' => [
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'db' => $db,
'rabbitmq' => [
'class' => \mikemadisonweb\rabbitmq\Configuration::class,
'connections' => [
[
// You can pass these parameters as a single `url` option: https://www.rabbitmq.com/uri-spec.html
'host' => '127.0.0.1',
'port' => '5672',
'user' => 'guest',
'password' => 'guest',
'vhost' => '/',
]
// When multiple connections is used you need to specify a `name` option for each one and define them in producer and consumer configuration blocks
],
],
],
注意端口号为5672 非web端登录的端口15672
3.编写一个生产者推送消息到队列中
class DemoProducerController extends Controller
{
const DEMO_PRODUCER = 'DEMO_PRODUCER';
const DEMO_ROUTING_KEY = 'DEMO_ROUTING_KEY';
const DEMO_ROUTING_KEY2 = 'DEMO_ROUTING_KEY2';
const DEMO_EXCHANGE = 'DEMO_EXCHANGE';
/**
* php yii demo-producer/index
*/
public function actionIndex()
{
/** @var \mikemadisonweb\rabbitmq\Configuration $rabbitmq */
$rabbitmq = \Yii::$app->rabbitmq;
/** @var \mikemadisonweb\rabbitmq\components\Producer $producer */
$producer = $rabbitmq->getProducer(self::DEMO_PRODUCER);
$msg = json_encode(['DEMO_ROUTING_KEY2插入随机数' . rand(1, 1000)]);
for($i=0;$i<100;$i++)
{
sleep(1);
$producer->publish($msg, self::DEMO_EXCHANGE, self::DEMO_ROUTING_KEY2);
}
}
}
4.配置一个交换机 配置一个队列 并将队列和交换机绑定
'producers' => [
[
'name' => 'DEMO_PRODUCER',
],
],
'exchanges' => [
[
'name' => 'DEMO_EXCHANGE',
'type' => 'direct'
],
],
'queues' => [
[
'name' => 'DEMO_QUEUES',
],
],
'bindings' => [
[
'queue' => 'DEMO_QUEUES',
'exchange' => 'DEMO_EXCHANGE',
'routing_keys' => ['DEMO_ROUTING_KEY','DEMO_ROUTING_KEY2'],
],
],
5.生产者发送推送消息
输入命令php yii demo-producer/index
向交换机推送数据.
可以看到已经小消息进来并且进入就绪状态 等待消费.
6.编写一个消费者
<?php
namespace app\commands\rabbitmq\consumer;
use mikemadisonweb\rabbitmq\components\ConsumerInterface;
use PhpAmqpLib\Message\AMQPMessage;
class DemoConsumer implements ConsumerInterface
{
/**
* @param AMQPMessage $msg
* @return int|mixed
*/
public function execute(AMQPMessage $msg)
{
var_dump($msg->getBody());
return ConsumerInterface::MSG_ACK;
}
}
6.绑定消费者回调消费
'bindings' => [
[
'queue' => 'DEMO_QUEUES',
'exchange' => 'DEMO_EXCHANGE',
'routing_keys' => ['DEMO_ROUTING_KEY','DEMO_ROUTING_KEY2'],
],
],
'consumers' => [
[
'name' => 'DEMO_CONSUMER',
// Every consumer should define one or more callbacks for corresponding queues
'callbacks' => [
// queue name => callback class name
'DEMO_QUEUES' => app\commands\rabbitmq\consumer\DemoConsumer::class,
],
],
],
7执行消费命令
php yii rabbitmq/consume DEMO_CONSUMER
成功消费,至此 RabbitMQ 简单使用完成.
Tags: RabbitMQ
很赞哦! (1)
相关文章
随机图文
-
shiro 匹配md5加密后的密码HashedCredentialsMatcher
shiro 匹配md5加密后的密码,shiro为我们提供好了使用md5规则,同时还可以设置md5的加密次数.我们在SimpleAuthenticationInfo里面就只用将加密后的密码丢进去,shi -
linux 查看当前文件大小 当前文件夹数量
linux如何查看当前文件夹大小,文件夹文件个数,查看文件详情的命令是啥,linux命令只有用到时候才会搜。查看当前文件大小:du -sh查看当前文件数量:ls-l |grep “^-“|wc -l -
yii2伪静态 使用apach在phpstudy配置伪静态
yii2 在phpstudy配置伪静态使用阿帕奇作为服务器,yii中文网找了安装了yii2,安装完composer之后发现路径需要重写,就去官网加上伪静态,只找到一下方式. # 开启 mo -
git 分支开发dev,release本地提交代码总结
git分支是个好东西,可以在自己分支上开发,然后将代码合并到其他分支上便于管理,首次接触分支还是有点迷糊,结合公司代码提交规范,自己回现公司代码提交规范,理清楚的话其实还是蛮简单的.首先准备一个码云仓