In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article focuses on "introduction to message queuing RabbitMQ and PHP instance analysis". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "introduction to message queuing RabbitMQ and PHP instance Analysis".
Message queuing introduction and message queuing application scenario RabbitMQ
Description
MQ (Message Queue), that is, message queue, is a way of communication between applications, which can be returned immediately after the message is sent, and the message system ensures the reliable delivery of the message. "message queue" is a container for storing messages in the process of message transmission. It is typical: producer and consumer model. Producers continue to produce messages to the message queue, and consumers continue to get messages from the queue. Because the production and consumption of messages are asynchronous, and only care about the sending and receiving of messages, there is no intrusion of business logic, so as to realize the decoupling of producers and consumers.
Why use message middleware?
Message queue is an important component in distributed system, which solves the problems of application decoupling, asynchronous message and traffic peaking, and realizes high concurrency, high availability, scalability and ultimate consistency architecture.
Asynchronous processing
Users need to send email and registration text messages after registering information.
1. Even if the user registration information is written to the database, the information of successful registration is returned.
2. Sending mail and registering SMS messages are executed asynchronously through message queues. Users do not need to wait for these two operations.
Application decoupling
After the user places an order, the order system needs to notify the inventory system. Traditionally, the order system calls the interface of the inventory system to increase or decrease inventory.
1. The user issues an order for production and returns a success prompt.
2. Queue consumption inventory system to increase or decrease inventory
Flow peaking
Traffic peaking is also a common scenario in message queues, which is widely used in second kill or group robbery activities.
1. When a group of users requests to join the queue, control the number of users in the queue, and return to the end of the second kill beyond a certain number.
2. Then the queue consumes one by one according to the first-in-first-out
Rabbitmq characteristics
Reliability RabbitMQ uses some mechanisms to ensure reliability, such as persistence, transmission confirmation, and release confirmation.
Flexible routing (Flexible Routing) routes messages through Exchange before they enter the queue. For typical routing functions, RabbitMQ already provides some built-in Exchange to implement. For more complex routing functions, you can bind multiple Exchange together and implement your own Exchange through a plug-in mechanism.
Message clustering (Clustering) multiple RabbitMQ servers can form a cluster to form a logical Broker.
High availability (Highly Available Queues) queues can be mirrored on machines in the cluster so that queues are still available in the event of a problem with some nodes.
Multiple protocols (Multi-protocol) RabbitMQ supports multiple message queuing protocols, such as STOMP, MQTT, and so on.
The multilingual client (Many Clients) RabbitMQ supports almost all common languages, such as PHP Java, .NET, Ruby, and so on.
The management interface (Management UI) RabbitMQ provides an easy-to-use user interface that allows users to monitor and manage many aspects of the messaging Broker.
Tracking mechanism (Tracing) if the message is abnormal, RabbitMQ provides a message tracking mechanism so that the consumer can find out what happened.
The plug-in mechanism (Plugin System) RabbitMQ provides many plug-ins to extend in many ways, or you can write your own plug-ins.
How RabbitMQ works
Broker: an application that receives and distributes messages. RabbitMQ Server is Message Broker.
Virtual host: a database similar to mysql, when multiple different users use the services provided by the same RabbitMQ server, they can be divided into multiple vhost, and each user creates an exchange/queue in their own vhost, etc.
Connection: the TCP connection between publisher/consumer and broker.
Channel: if you set up a Connection every time you visit RabbitMQ, it will be expensive and inefficient to set up TCP Connection when there are a lot of messages. Channel is a logical connection established within connection. As a lightweight Connection, Channel greatly reduces the cost of establishing TCP connection by the operating system.
Exchange: message arrives at the first stop of broker, matches the routing key in the query table according to the distribution rules, and distributes the message to queue. The common types are: direct (point-to-point), topic (publish-subscribe) and fanout (multicast).
Queue: the message is finally sent here for consumer to pick up. A message can be copied to multiple queue simultaneously.
Rabbitmq installation startup
Official address of RabbitMQ: http://www.rabbitmq.com
You need to install erlang first to install rabbitmq
Step 1: erlang installation
Installation of rabbitmq requires installation of erlang,centos7 that does not support installation of erlang 24 version
Download:
# system centos download the erlang package, download it manually and upload it to the server. I can't install it after downloading it with wget. I don't understand here # install yum install erlang-23.3.4.4-1.el7.x86_64.rpm# to verify whether the installation is successful or not erl
Step 2: install rabbitmq
# system centos download the rabbitmq package, download it manually and upload it to the server. I can't install it after downloading using wget. I don't understand # install yum install rabbitmq-server-3.8.19-1.el7.noarch.rpm # start systemctl start rabbitmq-server# and close systemctl stop rabbitmq-server# to check whether the default port service starts netstat-tunlp.
Php message queuing rabbitmq various modes use rabbitmq management interface and command line to use
4369:epmd (Erlang Port Mapper Daemon), erlang service port
5672: client communication port
15672:HTTP API client, management UI (only if the management plug-in is enabled) does not necessarily start
25672: for communication between nodes (Erlang Distributor port)
Rabbitmq management command
Start the 15672:HTTP API client to manage UI (only if the management plug-in is enabled)
# start the rabbitmq_management plug-in rabbitmq-plugins enable rabbitmq_management# to view all plug-ins rabbitmq-plugins list
Test access to the UI interface: (non-localhost addresses cannot be logged in at this time)
Http://192.168.10.105:15672/
Rabbitmq configuration Management Interface
# add a user rabbitmqctl add_user [username Username] [password Password] rabbitmqctl add_user root root# delete a user rabbitmqctl delete_user Username# modify the user's password rabbitmqctl change_password Username Newpassword # View the current user list rabbitmqctl list_users# set the user role command: rabbitmqctl set_user_tags User Tag rabbitmqctl set_user_tags root administrator# User is the user name, Tag is the role name (corresponding to the above administrator,monitoring,policymaker,management Or other custom name).
Command line creation vhost and php extension installation
Similar to the mysql database, when multiple different users use the services provided by the same RabbitMQ server, they can be divided into multiple vhost, and each user creates an exchange/queue in their own vhost, and so on.
1) View the vhost of different users
Create vhost and assign permissions
# add vhostrabbitmqctl add_vhost vhostnamerabbitmqctl add_vhost order# to view vhost list rabbitmqctl list_vhosts# add user rabbitmqctl set_permissions-p vhostname username ". *" rabbitmqctl set_permissions-p order root ". * the following three. * represent: configuration permission, write permission and read permission, respectively.
2) install the rabbitmq extension installation for php
Https://github.com/php-amqplib/php-amqplib extension installation
Modify Aliyun image
Composer config-g repo.packagist composer https://mirrors.aliyun.com/composer/
Start downloading-here sometimes it will be downloaded to a lower version of 2.8, which requires a specified version
If the download is not successful, upgrade composer, php.ini open sockets extension and switch domestic images
# upgrade composercomposer self-update#php.ini to open the sockets extension # download the specified version of composer require php-amqplib/php-amqplib= ^ 3.0
Simple pattern producer messages are pushed to message queues
Documentation:
Https://www.rabbitmq.com/tutorials/tutorial-one-php.html
Simple producers and messengers
Producer code
Http://localhost/rabbitmq/simple/pro.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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.