Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement php-amqplib/php-amqplib instance RabbitMq by PHP

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you "PHP how to achieve php-amqplib/php-amqplib instance RabbitMq", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "PHP how to achieve php-amqplib/php-amqplib instance RabbitMq" this article.

Project code

Https://gitee.com/owenzhang24/tp5

Other notes:

1: list queues (Listing queues)

If you want to view the Rabbitmq queue and want to know how many messages exist in it, you (as a privileged user) can use the rabbitmqctl tool:

Rabbitmqctl list_queues .

In Windows, omit sudo:

Rabbitmqctl.bat list_queues

2: work queue

We found that even if you kill a worker process with CTRL+C, the message is not lost. When the worker (worker) hangs up, all messages that do not respond will be resent.

An easy mistake to make is to forget basic_ack, which has serious consequences. Messages will be re-sent after your program exits, and if it cannot release unresponsive messages, RabbitMQ will take up more and more memory.

To troubleshoot this error, you can use the rabbitmqctl command to output the messages_unacknowledged field:

$sudo rabbitmqctl list_queues name messages_ready messages_unacknowledged

To run on the window system, remove sudo:

$rabbitmqctl.bat list_queues name messages_ready messages_unacknowledged

3: rabbitmqctl can list all the switches on the server:

$sudo rabbitmqctl list_exchanges

There are some switches called amq.* in this list. These are created by default, but you don't need to use them at this time.

4: list all existing bound rabbitmqctl list_bindings

5: if you want to save the log to a file, just open the console and type: (receive_logs.php source code)

$php receive_logs.php > logs_from_rabbit.log

If you want all the log information to be output to the screen, open a new terminal and type:

$php receive_logs_direct.php info warning error

# = > [*] Waiting for logs To exit press CTRL+C

If you want to trigger an error-level log, just type:

Php emit_log_direct.php error "Run. Run. Or it will explode."

# = > [x] Sent 'error':'Run. Run. Or it will explode.'

First: install the RabbitMq environment

Rabbitmq installation and startup of windows environment

Https://my.oschina.net/owenzhang24/blog/5051652

Second: composer require php-amqplib/php-amqplib third: code class

The basic class of rabbitMq implementation: application/common/lib/classes/rabbitmq/RabbitMq.php

RabbitMq class for external calls: application/common/lib/classes/RabbitMqWork.php

Test the method of sending messages to rabbitMq: application/index/controller/Index.php

Add php think command to receive the message in rabbitMq: application/common/command/*.php

Fourth: instructions for use

When you send a message, you can call several methods in the RabbitMqWork.php class directly in your own method.

The classes under application/common/command/ are all classes that add php think commands. Set the command name in setName () in the configure method, and the execute () method is used to receive messages in rabbitMq. At the same time, return the set command name and the corresponding command directory address in application/command.php.

Contribution document

RabbitMQ Chinese document-PHP version. Https://xiaoxiami.gitbook.io/rabbitmq_into_chinese_php/

RabbitMQ official documentation. Https://www.rabbitmq.com/getstarted.html

Fifth: source code

Application/common/lib/classes/rabbitmq/RabbitMq.php

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report