When using RabbitMQ for asynchronous message processing, it is always found that after one or more consumer processes are started, they are automatically disconnected after a period of time, such as a few minutes or 10 minutes, resulting in a large number of messages in the queue and can not be processed.
In order to ensure the continuous high-performance operation of consumers, supervisor is used for supervision, and kill will restart automatically.
Its principle:
rabbitmq enables heartbeat detection after receiving the connection.tune-ok signaling from the client,
rabbitmq will create two processes for heartbeat detection for each tcp connection:
A process that regularly detects whether data is sent on the tcp connection (sending here means rabbitmq sends data to the client). If no data is sent to the client for a period of time, it immediately sends a heartbeat packet to the client (that is, the consumer), and then performs the next detection in a loop;
Another process regularly detects whether there is data reception on the tcp connection. If no data is received within a period of time, it is determined that the heartbeat has timed out, and finally the tcp connection will be closed.
Final solution:
Install and configure the supervisor to guard the consumer process. Once the process is closed, it will restart automatically.
Environment: Centos
Step 1: Download
-
yum install -y epel-release
-
yum install -y supervisor
Step 2: Configure
cd /etc/ mkdir supervisord.d echo_supervisord_conf > supervisord.conf vim /etc/supervisord.conf #Add the following configuration information [include] files = /etc/supervisord.d/*.conf
Step 3: start the service file (write the file location vim / etc/init.d/supervisord )
#!/bin/sh # # /etc/init.d/supervisord # # Supervisor is a client/server system that # allows its users to monitor and control a # number of processes on UNIX-like operating # systems. # # chkconfig: - 64 36 # description: Supervisor Server # processname: supervisord # Source init functions . /etc/rc.d/init.d/functions prog="supervisord" prog_bin="/usr/bin/supervisord" PIDFILE="/var/run/$prog.pid" start() { echo -n $"Starting $prog: " ###Note that the following line must have - C / etc / supervisor.conf, otherwise the modified configuration file will not take effect at all! daemon $prog_bin -c /etc/supervisord.conf --pidfile $PIDFILE [ -f $PIDFILE ] && success $"$prog startup" || failure $"$prog startup" echo } stop() { echo -n $"Shutting down $prog: " [ -f $PIDFILE ] && killproc $prog || success $"$prog shutdown" echo } case "$1" in start) start ;; stop) stop ;; status) status $prog ;; restart) stop start ;; *) echo "Usage: $0 {start|stop|restart|status}" ;; esac
3, Execute command
chmod +x /etc/init.d/supervisord chkconfig --add supervisord chkconfig supervisord on service supervisord start
Step 4: open web view
# If you want to view the managed process through the web, add the following code to listen to 9001, the default user user, and the default password 123 [inet_http_server] port=9001 username=user password=123
Step 5: create a listening task (created in vim) / (etc / Supervisor. D / rabbitmq_worker. Conf file)
[program:rabbitmq_worker] command=curl http://IP/Delay.Consumer/start; command line operation at startup autostart=true ;Whether to follow supervisor start-up autorestart=true ;Whether to restart after hanging, and restart after accidental shutdown, such as kill Drop! startsecs=10 ;How long does it last? The operation is considered successful startretries=3 ;Number of startup attempts stderr_logfile=/tmp/rabbitmq_worker_err.log ;Location of standard error output stdout_logfile=/tmp/rabbitmq_worker_out.log ;Location of standard output
Step 6: restart the supervisor service
service supervisord restart
Step 7: check the service startup status
[kingfly@kingfly supervisord.d]# lsof -i:9001 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME superviso 9452 root 4u IPv4 40107676 0t0 TCP *:etlservicemgr (LISTEN)
Step 8: view in web mode (login is required after opening, default login account: user, default password: 123)
http://Server IP:9001
If you want to modify the login account password, you can modify it in the configuration file.
vim /etc/supervisord.conf
Well, here, the solution and actual combat of guarding the consumer process through Supervisor are completed.
Start the actual combat soon ~ ask if you don't understand
If you want to learn more about the various commands of the Supervisor, see the last one