rabbitmq3.4.2 shell script analysis | http://aperise.iteye.com/blog/2346584 |
rabbitmq3.4.2 stand-alone installation and cluster installation | http://aperise.iteye.com/blog/2346179 |
Rabbitmq3.4.1 split brain problem (reproduced) | http://blog.csdn.net/u013256816/article/details/53291907 |
spring-amqp official documentation (reproduced) | http://projects.spring.io/spring-amqp/#quick-start |
spring-amqp source code in GITHUB (reproduced) | https://github.com/spring-projects/spring-amqp |
1. Preparations (executed by root user)
Involving public linux libraries, public JDK, and public ERLANG environments are installed by the root user, and the environment variables are configured to /etc/profile for all users to use
1.1 Necessary linux dependencies
Install GCC GCC-C++ Openssl ncurses and other modules, otherwise there will be some failures due to software modules without their dependencies during operations such as make&&make install Erlang.
yum -y install make gcc gcc-c++ kernel-devel m4 ncurses-devel openssl-devel ncurses-devel xmlto libxslt
1.2 JDK installation
- In the Erlang compilation (make) link, the default is to use javac;
- For erlang, the compilation of its JInterface requires the java compiler javac (JInterface is JDK5, if the default JDK is lower than 1.5, an error will still occur when make to JInterface);
- JDK is not necessary, you can use the following method to set without javac
cd /home/nmscmq/rabbitmq-server-3.4.2-source ./configure --prefix=/opt/erlang --without-javac //Set without java compilationHere is to download JDK7 from oracle's official website. Now it's really not easy to find a JDK7 on oracle's official website. I don't know if it is to push JDK8. The download link of JDK7 is hard to find! ! ! http://download.oracle.com/otn-pub/java/jdk/7u79-b15/jdk-7u79-linux-x64.tar.gz The operation of downloading the JDK historical version from the oracle official website is as follows:
#1. First of all, it is not recommended to use openjdk, it is recommended to use oracle official website JDK #2. First uninstall the low version that comes with the system or the built-in openjdk #First use the command java -version to view the original java version in the system #Then use the rpm -qa | java-1.7.0-openjdk-1.7.0.79-2.5.5.4.el6.x86_64 command to view the specific information, rpm -e --nodeps java-1.7.0-openjdk-1.7.0.79-2.5.5.4.el6.x86_64 rpm -e --nodeps java-1.6.0-openjdk-1.6.0.35-1.13.7.1.el6_6.x86_64 #3. Install jdk-7u79-linux-x64.gz #Download jdk-7u79-linux-x64.gz and place it in /opt/java/jdk-7u79-linux-x64.gz and unzip it cd /opt/java/ #Here is to download JDK7 from oracle's official website. Now it is really difficult to find a JDK7 on oracle's official website. I wonder if it is to push JDK8. The download link of JDK7 is hard to find! ! ! wget http://download.oracle.com/otn-pub/java/jdk/7u79-b15/jdk-7u79-linux-x64.tar.gz tar -zxvf jdk-7u79-linux-x64.gz #4. Configure environment variables #Configure linux system environment variables vi /etc/profile #Append the following to the end of the file export JAVA_HOME=/opt/java/jdk1.7.0_79 export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar export PATH=$PATH:$JAVA_HOME/bin #Make the configuration take effect source /etc/profile #5. Check if the JDK environment is configured successfully java -version
1.3 ERLANG installation
To install rabbitmq, you must first install Erlang, because rabbitmq is developed with erlang, just like many programs developed by JAVA need to install JDK first.
At present, the official website version of Erlang (http://www.erlang.org) has reached 19.2. Here I downloaded Erlang version 17.0.
#1. Download, compile and install erlang cd /opt/software #download erlang source package wget http://erlang.org/download/otp_src_17.0.tar.gz #Unzip the erlang source package tar -zxvf otp_src_17.0.tar.gz cd /opt/software/otp_src_17.0 #Create installation directory mkdir -p /opt/erlang #Set the erlang installation directory, whether you need to use javac #If you don't need it, set it like this./configure --prefix=/opt/erlang --without-javac ./configure --prefix=/opt/erlang #compile and install make make install #2.ERLANG environment variable configuration vi /etc/profile #Append the following to the end of the file export ERLANG_HOME=/opt/erlang export PATH=$PATH:$ERLANG_HOME/bin #Make the configuration file take effect after modification source /etc/profile #3.ERLANG installation success detection erl -version #Erlang (SMP,ASYNC_THREADS,HIPE) (BEAM) emulator version 6.0
When installing on centos5.11, the error and solution are as follows:
********************************************************************* ********************** APPLICATIONS INFORMATION ******************* ********************************************************************* wx : wxWidgets not found, wx will NOT be usable ********************************************************************* ********************************************************************* ********************** DOCUMENTATION INFORMATION ****************** ********************************************************************* documentation : fop is missing. Using fakefop to generate placeholder PDF files. *********************************************************************
The solution is as follows:
wget https://github.com/wxWidgets/wxWidgets/releases/download/v2.8.12/wxWidgets-2.8.12.zip unzip wxWidgets-2.8.12.zip cd wxWidgets-2.8.12 ./configure --with-gtk --enable-unicode --disable-shared make make install ldconfig
1.4 Create a Linux server account for managing the rabbitmq cluster later
#Create nmscmq user and group groupadd nmscmq useradd -g nmscmq nmscmq passwd nmscmq
2.rabbitmq3.4.2 installation (linux account nmscmq executes all the following commands)
The download links of all current versions can be found through http://www.rabbitmq.com/releases/rabbitmq-server/ . The screenshots are as follows:
Here I choose version 3.4.2 for the explanation. There is no special reason for choosing this version. The reason why I did not choose 3.4.1 is because of the split-brain problem in version 3.4.0 and version 3.4.1 (see http://blog .csdn.net/u013256816/article/details/53291907 ), about the 3.4.2 official website http://www.rabbitmq.com/releases/rabbitmq-server/v3.4.2/ The installation package provided is as follows: The
installation method is RPM And the compilation and installation based on the tar.gz package , I personally prefer the compilation and installation based on the tar.gz package , the reason is that this method can flexibly control the installation location and control the placement location of all configurations. The RPM package method is an introduction, and the RPM method is related by default. The configuration is as follows:
# Inherit the sub-configuration file already configured by rabbitmq-env, it will automatically add the suffix .config to find this file, note that it is not a directory CONFIG_FILE=${SYS_PREFIX}/etc/rabbitmq/conf/rabbitmq # log file directory, note that it is not a file LOG_BASE=${SYS_PREFIX}/var/log/rabbitmq # Database directory, note that it is not a file MNESIA_BASE=${SYS_PREFIX}/var/lib/rabbitmq/mnesia # Record the file of the enabled plugin, note that it is not a directory ENABLED_PLUGINS_FILE=${SYS_PREFIX}/etc/rabbitmq/enabled_plugins # The directory where the plugin is located PLUGINS_DIR="${RABBITMQ_HOME}/plugins" # rabbitmq related environment configuration files CONF_ENV_FILE=${SYS_PREFIX}/etc/rabbitmq/rabbitmq-env.conf
2.1 RPM installation and uninstallation
If you choose the rpm package, then the installation method is as follows:
cd / opt wget http://www.rabbitmq.com/releases/rabbitmq-server/v3.4.2/rabbitmq-server-3.4.2-1.noarch.rpm rpm --nodeps -Uvh rabbitmq-server-3.4.2-1.noarch.rpm #--nodeps is to avoid the following problems (it will report that no erlang dependencies are found, but in fact we have installed Erlang) warning: rabbitmq-server-3.4.2-1.noarch.rpm: Header V4 DSA/SHA1 Signature, key ID 056e8e56: NOKEY error: Failed dependencies: erlang >= R13B-03 is needed by rabbitmq-server-3.4.2-1.noarch
If you want to uninstall an RPM-installed package, do the following:
1) First, you can query the name of the rpm package through rpm -q rabbitmq-server 2) Then call rpm -e rabbitmq-server-3.4.2-1.noarch to delete the specific rpm package 3) If you encounter dependencies and cannot be deleted, use rpm -e --nodeps rabbitmq-server-3.4.2-1.noarch to delete the rpm package without checking the dependencies 4) If there happen to be multiple packages with the same name, use rpm -e --allmatches --nodeps rabbitmq-server to delete all packages with the same name and ignore dependencies
2.2 Compile and install based on tar.gz
And I chose to compile and install the source code (see http://www.rabbitmq.com/build-server.html for the source code compilation and installation ), mainly to facilitate the planning and management of the installation directory of rabbitmq , so my installation method is as follows:
#Switch to account nmscmq su - nmscmq #download cd /home/nmscmq wget http://www.rabbitmq.com/releases/rabbitmq-server/v3.4.2/rabbitmq-server-3.4.2.zip #decompress cd /home/nmscmq unzip rabbitmq-server-3.4.2.zip #Rename source directory mv /home/nmscmq/rabbitmq-server-3.4.2 /home/nmscmq/rabbitmq-server-3.4.2-source #Create installation directory mkdir -p /home/nmscmq/rabbitmq-server-3.4.2 cd /home/nmscmq/rabbitmq-server-3.4.2-source #Compile and install #Install to the root directory TARGET_DIR=/home/nmscmq/rabbitmq-server-3.4.2 #Basic placement location after installation SBIN_DIR=/home/nmscmq/rabbitmq-server-3.4.2/sbin #Installed MAN placement MAN_DIR=/home/nmscmq/rabbitmq-server-3.4.2/man #Installed DOC placement DOC_INSTALL_DIR=/home/nmscmq/rabbitmq-server-3.4.2/doc make make install TARGET_DIR=/home/nmscmq/rabbitmq-server-3.4.2 SBIN_DIR=/home/nmscmq/rabbitmq-server-3.4.2/sbin MAN_DIR=/home/nmscmq/rabbitmq-server-3.4.2/man DOC_INSTALL_DIR=/home/nmscmq/rabbitmq-server-3.4.2/doc
2.3 rabbitmq environment variable settings
Rabbitmq is installed under the linux user nmscmq . Here only the rabbitmq script is configured into the environment variable of the user nmscmq to prevent other linux accounts from operating rabbitmq at will, causing unnecessary problems.
cd /home/nmscmq vi .bashrc #add by wuzhongwen RABBITMQ_HOME=/home/nmscmq/rabbitmq-server-3.4.2 export PATH=$PATH:$RABBITMQ_HOME/sbin #Make the configuration take effect source /home/nmscmq/.bashrc
So far, rabbitmq has been installed.
3. Stand-alone rabbitmq service management
3.1 Configuration file storage directory and configuration file creation
Next, we need to start the stand-alone rabbitmq. Before that, we need to create our own relevant directory storage location and configuration file name, which is convenient for subsequent configuration. Create the following:
rm -rf /home/nmscmq/rabbitmq/* #log file directory mkdir -p /home/nmscmq/rabbitmq/log #Database directory mkdir -p /home/nmscmq/rabbitmq/data #Configuration file directory mkdir -p /home/nmscmq/rabbitmq/conf echo "">>/home/nmscmq/rabbitmq/conf/rabbitmq.config echo "">>/home/nmscmq/rabbitmq/conf/rabbitmq-env.conf #Record files of enabled plugins echo "">>/home/nmscmq/rabbitmq/enabled_plugins
3.2 Modify rabbitmq-defaults
Modify the file /home/nmscmq/rabbitmq-3.4.2/sbin/rabbitmq-defaults as follows:
#modify by nmscmq #CONFIG_FILE=${SYS_PREFIX}/etc/rabbitmq/rabbitmq #LOG_BASE=${SYS_PREFIX}/var/log/rabbitmq #MNESIA_BASE=${SYS_PREFIX}/var/lib/rabbitmq/mnesia #ENABLED_PLUGINS_FILE=${SYS_PREFIX}/etc/rabbitmq/enabled_plugins CONFIG_FILE=${SYS_PREFIX}/home/nmscmq/rabbitmq/conf/rabbitmq LOG_BASE=${SYS_PREFIX}/home/nmscmq/rabbitmq/log MNESIA_BASE=${SYS_PREFIX}/home/nmscmq/rabbitmq/data ENABLED_PLUGINS_FILE=${SYS_PREFIX}/home/nmscmq/rabbitmq/enabled_plugins #modify by nmscmq #CONF_ENV_FILE=${SYS_PREFIX}/etc/rabbitmq/rabbitmq-env.conf CONF_ENV_FILE=${SYS_PREFIX}/home/nmscmq/rabbitmq/conf/rabbitmq-env.conf
CONFIG_FILE configures /home/nmscmq/rabbitmq/conf/rabbitmq, but the suffix ".config" is actually added, and the actual load is /home/nmscmq/rabbitmq/conf/rabbitmq.config
If you have any doubts about rabbitmq configuration and scripts, please refer to my other blog about rabbitmq3.4.2 shell script analysis
3.3 Modify rabbitmq-env.conf
Modify the file /home/nmscmq/rabbitmq/conf/rabbitmq-env.conf, the configuration content is as follows:
NODENAME=rabbit@nmscmq1
3.4 Modify rabbitmq.config
Modify the file /home/nmscmq/rabbitmq/conf/rabbitmq.config, the configuration content is as follows:
[ {rabbit, [{vm_memory_high_watermark, 0.8}]} ].
For the configuration example of /home/nmscmq/rabbitmq/conf/rabbitmq.config, see the file /home/nmscmq/rabbitmq-3.4.2/doc/rabbitmq-example. The vm_memory_high_watermark=0.8 configured here means that rabbitmq uses memory first. When the memory exceeds the threshold of 80% * machine memory, start using the disk to store message data
3.5 Start and stop the rabbitmq service
#Start the rabbitmq service in the background rabbitmq-server –detached #Close the rabbitmq service rabbitmqctl stop
3.6 Activate rabbitmq plugin
rabbitmq-plugins enable rabbitmq_management rabbitmq-plugins enable rabbitmq_mqtt
After activation, the following content is generated in the file /home/nmscmq/rabbitmq/enabled_plugins:
3.7 Create rabbitmq WEB management terminal link account
#Create rabbitmq WEB management account rabbitmq-test, its password is rabbitmq-test rabbitmqctl add_user rabbitmq-test rabbitmq-test #Set the account rabbitmq-test as the administrator account rabbitmqctl set_user_tags rabbitmq-test administrator
After creating the account, open http://localhost:15672/ in the browser, enter the account rabbitmq-test/rabbitmq-test, and log in to the screenshot as follows:
4. Rabbitmq cluster service management
On the 3 machines nmscmq1, nmscmq2, nmscmq3, follow the previous steps 1, 2 and 3 to build the stand-alone version of the rabbitmq service, and then operate as follows:
4.0 Modify /etc/hosts and add the following:
192.168.62.32 nmscmq1 192.168.62.33 nmscmq2 192.168.62.34 nmscmq3
4.1 Modify rabbitmq-env.conf
#The configuration of /home/nmscmq/rabbitmq/conf/rabbitmq-env.conf on the machine nmscmq1 is as follows: NODENAME=rabbit@nmscmq1 #The configuration of /home/nmscmq/rabbitmq/conf/rabbitmq-env.conf on the machine nmscmq2 is as follows: NODENAME=rabbit@nmscmq2 #The configuration of /home/nmscmq/rabbitmq/conf/rabbitmq-env.conf on the machine nmscmq3 is as follows: NODENAME=rabbit@nmscmq3
4.2 Synchronizing erlang cookies
The cluster of Rabbitmq relies on the cluster of erlang to work, so the cluster environment of erlang must be built first. Each node in the Erlang cluster is implemented by a magic cookie. This cookie is stored in /home/nmscmq/.erlang.cookie, and the file has the permission of 400. Therefore, it is necessary to ensure that the cookie of each node is consistent, otherwise the communication between nodes will not be possible.
#First modify the permissions of /home/nmscmq/.erlang.cookie on the hostname=nmscmq3 machine chmod 777 /home/nmscmq/.erlang.cookie #Execute the following command from hostname= nmscmq3 to copy to the other two machines scp /home/nmscmq/.erlang.cookie [email protected]:/home/nmscmq/.erlang.cookie scp /home/nmscmq/.erlang.cookie [email protected]:/home/nmscmq/.erlang.cookie #After copying, reply to 3 machines /home/nmscmq/.erlang.cookie permission is 400 chmod 400 /home/nmscmq/.erlang.cookie
4.3 Cluster Start and Join
#nmscmq3 executes the following on the machine: rabbitmqctl stop rabbitmq-server -detached Execute the following on the #nmscmq1 machine: #Stop all node RabbitMq services, and then use the detached parameter to run independently. This step is very critical, especially if you add a node and stop the node and start again, you can refer to this order if you cannot start it. rabbitmqctl stop rabbitmq-server -detached rabbitmqctl stop_app rabbitmqctl join_cluster --ram rabbitmq@nmscmq3 rabbitmqctl start_app Execute the following on the #nmscmq2 machine: #Stop all node RabbitMq services, and then use the detached parameter to run independently. This step is very critical, especially if you add a node and stop the node and start again, you can refer to this order if you cannot start it. rabbitmqctl stop rabbitmq-server -detached rabbitmqctl stop_app rabbitmqctl join_cluster --ram rabbitmq@nmscmq3 rabbitmqctl start_app
The above command first stops the rabbitmq application, and then invokes the cluster command to add nmscmq1 and nmscmq2 to rabbit@nmscmq3 as memory nodes, so that the three become a cluster, and finally restart the rabbitmq application. Under this cluster command, nmscmq1 and nmscmq2 are memory nodes, and nmscmq3 is a disk node (after RabbitMQ is started, the default is a disk node).
To make nmscmq1 or nmscmq2 also a disk node in the cluster, remove the --ram parameter from the join_cluster command.
As long as it includes itself in the node list, it becomes a disk node. In a RabbitMQ cluster, at least one disk node must exist.
Open http://localhost:15672/ in the browser to log in to view the cluster as follows:
4.4 Create vitual host in rabbitmq cluster
4.5 rabbitmq cluster service mirror configuration
The default cluster mode of RabbitMQ is configured above, but it does not guarantee the high availability of the queue. Although switches and bindings can be copied to any node in the cluster, the content of the queue will not be copied. Although this mode solves part of the node pressure, the queue node The downtime directly makes the queue unusable and can only wait for restart. Therefore, if the queue node is down or fails, it is necessary to copy the queue content to each node in the cluster, and a mirror queue needs to be created.
The mirror queue ensures that after data is written to a certain node, it can be synchronized to other nodes. After the primary replica node goes down, the replicas of other nodes still exist, which will not cause the loss of message queue data. Even if all nodes go down, the main function It is guaranteed that the last downtime is a disk node. When the cluster starts again, all message data retained by the disk node at the last moment can be recovered and synchronized to other memory nodes.
The configuration of the mirror queue is completed by adding a policy. The command added to the policy is:
rabbitmqctl set_policy [-p Vhost] Name Pattern Definition [Priority]
[-p Vhost] | Optional parameter, set for the queue under the specified vhost |
Name | the name of the policy |
Pattern | queue matching pattern (regular expression) |
Definition | Mirror definition, including three parts ha-mode, ha-params, ha-sync-mode ha-mode: Indicates the mode of the mirror queue, valid values are all/exactly/nodes all means mirroring on all nodes in the cluster Exactly means mirroring on the specified number of nodes, the number of nodes is specified by ha-params nodes means mirroring on the specified node, the node name is specified by ha-params
ha-params: parameters required for ha-mode mode
ha-sync-mode: The synchronization mode of messages in the mirror queue, the valid values are automatic , manually |
[Priority] | Optional parameter, the priority of the policy |
For example, to mirror all queues whose queue names start with q_ismp_ and complete the mirroring on two nodes of the cluster, the policy setting command is:
rabbitmqctl set_policy ismp ismp_mirror "^q_ismp_" '{"ha-mode":"all","ha-sync-mode":"automatic"}' 0