With the continuous advancement and development of Internet technology, in order to ensure the efficiency and stability of the technology, various technical means are constantly proposed and applied. Among them, Message Queue (hereinafter referred to as MQ) technology is currently the most popular and widely used technical means. MQ aims to solve the problem of asynchronous communication between different modules in the system, enabling loose coupling and better scalability between applications. This article will introduce how to develop message queue in PHP7.0.
1. MQ basics
1. Message queue concept
MQ is a message passing or asynchronous processing technology. Message queues allow loosely coupled communication between senders and receivers, improving interconnectivity and maintainability between applications. MQ uses third-party middleware to send messages to the queue through producers, and consumers extract messages from the queue for consumption. MQ can be used in distributed systems, high-concurrency systems, and data backlog scenarios.
2. Message Queue Type
MQ includes multiple types:
P2P: Point-to-point communication, the producer sends the message to a queue, which is received by a consumer Consumption can ensure the reliability of message delivery.
Pub/Sub: Publish-subscribe mode. Producers publish messages to topics, and subscribers subscribe to messages from the topics and consume them, which can achieve elasticity and scalability of message delivery.
3. MQ components
MQ is mainly composed of the following components:
Message: the basic unit of the message
Producer: the generator of the message
Consumer: Message consumer
Queue: Message storage area
Exchange: Message routing
Binding: Message binding relationship
two , How to develop MQ in PHP7.0
1. Install RabbitMQ
RabbitMQ is a popular MQ implementation that can support P2P and Pub/Sub modes, and supports the development of multiple programming languages. , high ease of use. Using RabbitMQ in PHP7.0 requires installing the PHP-AMQP extension. The steps are as follows:
(1) Download and unzip RabbitMQ, the official website address is as follows: http://www.rabbitmq.com/
(2) Install Erlang: RabbitMQ is developed based on Erlang language, please First install the Erlang environment
(3) Start the RabbitMQ server:
$ sudo rabbitmq-server
(4) Install the PHP-AMQP extension:
$ pecl install amqp
After the installation is complete, add the following configuration items in the php.ini file: extension=amqp.so
(5) Restart the web server to ensure that the PHP-AMQP extension configuration takes effect
2. Write code
To use RabbitMQ on the PHP side, you need to install the amqp extension and composer, and then install the amqp package.
(1) Install amqp package
$ composer require php-amqplib/php-amqplib
(2) Producer code
The following is a use PHP producer code for sending messages to the queue:
First, you need to instantiate the client, as follows:
$connection = new AMQPConnection();
$connection-> setHost('localhost');
$connection->setPort('5672');
$connection->setLogin('guest');
$connection->setPassword('guest' );
$connection->connect();
Implementation of message sending:
$exchange = 'test-exchange';
$queue = 'test-queue ';
$message = 'Hello World';
try {
$channel = new AMQPChannel($connection); $exchange = new AMQPExchange($channel); $exchange->setName($exchange); $exchange->setType(AMQP_EX_TYPE_DIRECT); $exchange->declareExchange(); $queue = new AMQPQueue($channel); $queue->setName($queue); $queue->setFlags(AMQP_DURABLE); $queue->declareQueue(); $queue->bind($exchange->getName(), $queue->getName()); $exchange->publish($message, $queue->getName());
}
catch (AMQPException $e) {
var_dump($e);
}
$connection- >disconnect();
In the above code, a queue test-queue and an exchanger test-exchange are created, and then the queue and the exchange are bound, and then the message is sent.
(3) Consumer code
The following is a consumer code for consuming messages from the queue:
First, instantiate the client as follows:
$connection = new AMQPConnection();
$connection->setHost('localhost');
$connection->setPort('5672');
$connection-> ;setLogin('guest');
$connection->setPassword('guest');
$connection->connect();
Implementation of message consumption:
$exchange = 'test-exchange';
$queue = 'test-queue';
try {
$channel = new AMQPChannel($connection); $exchange = new AMQPExchange($channel); $exchange->setName($exchange); $exchange->setType(AMQP_EX_TYPE_DIRECT); $exchange->declareExchange(); $queue = new AMQPQueue($channel); $queue->setName($queue); $queue->setFlags(AMQP_DURABLE); $queue->declareQueue(); $queue->bind($exchange->getName(), $queue->getName()); $queue->consume(function(AMQPEnvelope $message, AMQPQueue $queue) { echo $message->getBody(); $queue->ack($message->getDeliveryTag()); });
}
catch (AMQPException $e) {
var_dump($e);
}
$connection->disconnect();
In the above code, a queue test-queue and an exchanger test-exchange are created, and then the queue and the exchanger are bound , then the consumer gets the message from the queue and prints it to the console, and confirms that the message is consumed through the $queue->ack() method.
3. Summary
This article introduces how to develop MQ in PHP7.0. First, you need to install RabbitMQ and PHP-AMQP extensions; secondly, you need to install composer and install the amqp package; finally, write code to implement message sending and consumption. The emergence of MQ technology provides a more convenient and efficient message delivery method for Internet applications. Especially in high-concurrency scenarios and distributed systems, MQ is an indispensable part. By studying this article, I hope readers can understand the specific steps and implementation methods of MQ development in PHP7.0, and provide a reference for trying MQ technology in their own business applications.
The above is the detailed content of How to develop message queue in PHP7.0?. For more information, please follow other related articles on the PHP Chinese website!