翼度科技»论坛 编程开发 PHP 查看内容

PHP使用enqueue/amqp-lib实现rabbitmq任务处理

6

主题

6

帖子

18

积分

新手上路

Rank: 1

积分
18
一:拓展安装
  1. composer require enqueue/amqp-lib
复制代码
文档地址:https://github.com/php-enqueue/enqueue-dev/blob/master/docs/transport/amqp_lib.md

二:方法介绍


1:连接rabbitmq
  1. $factory = new AmqpConnectionFactory([
  2.     'host' => '192.168.6.88',//host
  3.     'port' => '5672',//端口
  4.     'vhost' => '/',//虚拟主机
  5.     'user' => 'admin',//账号
  6.     'pass' => 'admin',//密码
  7. ]);
  8. $context = $factory->createContext();
复制代码
2:声明主题
  1. //声明并创建主题
  2. $exchangeName = 'exchange';
  3. $fooTopic = $context->createTopic($exchangeName);
  4. $fooTopic->setType(AmqpTopic::TYPE_FANOUT);
  5. $context->declareTopic($fooTopic);

  6. //删除主题
  7. $context->deleteTopic($fooTopic);
复制代码
3:声明队列
  1. //声明并创建队列
  2. $queueName = 'rabbitmq';
  3. $fooQueue = $context->createQueue($queueName);
  4. $fooQueue->addFlag(AmqpQueue::FLAG_DURABLE);
  5. $context->declareQueue($fooQueue);

  6. //删除队列
  7. $context->deleteQueue($fooQueue);
复制代码
4:将队列绑定到主题
  1. $context->bind(new AmqpBind($fooTopic, $fooQueue));
复制代码
5:发送消息
  1. //向队列发送消息
  2. $message = $context->createMessage('Hello world!');
  3. $context->createProducer()->send($fooQueue, $message);

  4. //向队列发送优先消息
  5. $queueName = 'rabbitmq';
  6. $fooQueue = $context->createQueue(queueName);
  7. $fooQueue->addFlag(AmqpQueue::FLAG_DURABLE);
  8. //设置队列的最大优先级
  9. $fooQueue->setArguments(['x-max-priority' => 10]);
  10. $context->declareQueue($fooQueue);

  11. $message = $context->createMessage('Hello world!');

  12. $context->createProducer()
  13.     ->setPriority(5) //设置优先级,优先级越高,消息越快到达消费者
  14.     ->send($fooQueue, $message);

  15. //向队列发送延时消息
  16. $message = $context->createMessage('Hello world!');

  17. $context->createProducer()
  18.     ->setDelayStrategy(new RabbitMqDlxDelayStrategy())
  19.     ->setDeliveryDelay(5000) //消息延时5秒
  20.     ->send($fooQueue, $message);
复制代码
6:消费消息【接收消息】
  1. //消费消息
  2. $consumer = $context->createConsumer($fooQueue);

  3. $message = $consumer->receive();

  4. // process a message
  5. //业务代码

  6. $consumer->acknowledge($message);//ack应答,通知rabbitmq成功,删除对应任务
  7. // $consumer->reject($message);ack应答,通知rabbitmq失败,不删除对应任务


  8. //订阅消费者
  9. $fooConsumer = $context->createConsumer($fooQueue);

  10. $subscriptionConsumer = $context->createSubscriptionConsumer();
  11. $subscriptionConsumer->subscribe($fooConsumer, function(Message $message, Consumer $consumer) {
  12.     // process message
  13.     //业务代码
  14.     $consumer->acknowledge($message);//ack应答,通知rabbitmq成功,删除对应任务
  15.     // $consumer->reject($message);ack应答,通知rabbitmq失败,不删除对应任务

  16.     return true;
  17. });
  18. $subscriptionConsumer->consume();

  19. //清除队列消息
  20. $queueName = 'rabbitmq';
  21. $queue = $context->createQueue($queueName);
  22. $context->purgeQueue($queue);
复制代码
三:简单实现


1:发送消息
  1. //连接rabbitmq$factory = new AmqpConnectionFactory([    'host' => '192.168.6.88',    'port' => '5672',    'vhost' => '/',    'user' => 'admin',    'pass' => 'admin',    'persisted' => false,]); $context = $factory->createContext();//声明主题$exchangeName = 'exchange';$fooTopic = $context->createTopic($exchangeName);$fooTopic->setType(AmqpTopic::TYPE_FANOUT);$context->declareTopic($fooTopic); //声明队列$queueName = 'rabbitmq';$fooQueue = $context->createQueue($queueName);$fooQueue->addFlag(AmqpQueue::FLAG_DURABLE);$context->declareQueue($fooQueue); //将队列绑定到主题$context->bind(new AmqpBind($fooTopic, $fooQueue)); //发送消息到队列$message = $context->createMessage('Hello world!'); $context->createProducer()->send($fooQueue, $message);
复制代码
2:消费消息
  1. $factory = new AmqpConnectionFactory([
  2.     'host' => '192.168.6.88',
  3.     'port' => '5672',
  4.     'vhost' => '/',
  5.     'user' => 'admin',
  6.     'pass' => 'admin',
  7.     'persisted' => false,
  8. ]);
  9. $context = $factory->createContext();


  10. $queueName = 'rabbitmq';
  11. $fooQueue = $context->createQueue($queueName);



  12. $fooConsumer = $context->createConsumer($fooQueue);

  13. $subscriptionConsumer = $context->createSubscriptionConsumer();
  14. $subscriptionConsumer->subscribe($fooConsumer, function(Message $message, Consumer $consumer) {
  15.     // process message
  16.     //业务代码
  17.     $consumer->acknowledge($message);//ack应答,通知rabbitmq成功,删除对应任务
  18.     // $consumer->reject($message);ack应答,通知rabbitmq失败,不删除对应任务

  19.     return true;
  20. });
  21. $subscriptionConsumer->consume();
复制代码
到此这篇关于PHP使用enqueue/amqp-lib实现rabbitmq任务处理的文章就介绍到这了,更多相关PHP rabbitmq任务处理内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

来源:https://www.jb51.net/program/317443dm3.htm
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

举报 回复 使用道具