The use of messaging middleware Rabbitmq

A: Description messaging middleware
1 Introduction
messaging middleware can also be called the message queue, refers to data exchange with a platform-independent mechanism for efficient and reliable messaging, and data communication is performed based on the distributed system integration. By providing messaging and message queuing model, the communication process can be extended in a distributed environment.
Current mainstream messaging middleware has RabbitMQ, Kafka, ActiveMQ, RocketMQ etc.

2, the main role messaging middleware
• redundancy (storing)
• Scalability
• recoverability
• order guarantee
• Buffer
• asynchronous communication
• Clipping: message queue common scenarios is generally used widely enough to grab or spike in activity. Usually because of too much traffic, application system configuration can not carry this unit instantaneous flow, cause the system to hang directly, that is the legendary "down" phenomenon. To solve this problem, huge traffic we will reject that kind of system in the upper, ie not directly transferred to the influx of MQ our interface.

Two. RabbiMQ About one message queue
1, RabbiMQ Use Erang is developed, the cluster comes in handy ⽅ it, because Erlang days ⽣ is ⼀ ⻔ distributed Language, but does not in itself ⽀ support load balancing, high concurrency support, support Scalable. Support AJAX, persistence, for store and forward messages in a distributed system, in terms of ease of use, scalability, high availability, and so doing well.

2, RabbiMQ features
• Reliability
• Scalability
• High availability
• Multiple protocols
• Multi-language client
• Management Interface
• plug-in mechanism

3. What is the message queue
MQ called the Message Queue, the message queue. It is an application communication method according to the application. Application to communicate via a message (data for the application program) read out of the queue, without the need for a dedicated connection to link them.
It refers to message passing communication between a program by transmitting data in a message, rather than a direct call to communicate with each other.

4.RabbiMQ Mode
Note: Approximate RabbitMQ mode is divided into the following three:
(1) single-mode ⼀ (a host deployment RabbitMQ)
(2) Normal mode (default trunked mode).
(3) the mirror mode (made of the required image queue queues, in the presence of a plurality of nodes belonging to the HA ⽅ RabbiMQ case, ⽐ more business is useful when the reliability ADVANCED more occasions). To achieve mirror mode, you need to build ⼀ ordinary cluster mode, and then configure mirror mode on the basis of this model on to achieve ADVANCED can use, be provided in the mirror mode policy data can be the main rabbitmq synchronized to other cluster nodes in

+++++++++++ + RabbitMQ cluster deployment base (normal mode) ++++++++++++

A: basic deployment in the cluster hosts need to implement the operation

1, if three servers as three nodes, and are linked ⽹, turn off the firewall, selinux, edit DNS / etc / hosts (ip and host name should correspond to good, or else susceptible to experimental)
[root @ RabbitMQ-1 ~ ] # Vim / etc / the hosts
192.168.50.138 RabbitMQ. 1-
192.168.50.139 RabbitMQ-2
192.168.50.140. 3-RabbitMQ

2, three nodes arranged rabbitmq installation software
installation depends:
[the root-rabbitmq @ ~. 1] # yum the install GCC -Y-C ++ the unixODBC the unixODBC-devel-OpenSSL the ncurses-devel devel
[the root-rabbitmq @ ~. 1] # wget - Disposition-Content https://packagecloud.io/rabbitmq/erlang/packages/el/7/erlang-20.3-1.el7.centos.x86_64.rpm/download.rpm
[the root-RabbitMQ @ ~. 1] # RPM -ivh erlang-20.3-1.el7.centos.x86_64.rpm

Test;
[the root-RabbitMQ @ ~. 1] # ERL

安装rabbitmq:
[root@rabbitmq-1 ~]# wget https://dl.bintray.com/rabbitmq/all/rabbitmq-server/3.7.5/rabbitmq-server-3.7.5-1.el7.noarch.rpm
[root@rabbitmq-1 ~]# yum install rabbitmq-server-3.7.5-1.el7.noarch.rpm

3. Start
Start a way:
[the root-RabbitMQ @ ~. 1] # systemctl daemon-reload
[the root-RabbitMQ @ ~. 1] # systemctl RabbitMQ Start-Server
[the root-RabbitMQ @ ~. 1] # systemctl enable RabbitMQ-Server
[the root @ rabbitmq-1 ~] # systemctl status rabbitmq-server
start Second way:
[root @ rabbitmq-1 ~] # / sbin / RabbitMQ-Service-View Server status status ---
[root @ rabbitmq-1 ~] # / sbin / service rabbitmq-server start --- start

4, the web access interface opening rabbitmq:
[the root-rabbitmq @ ~. 1] # rabbitmq-plugins enable rabbitmq_management


5. Create user:
Note: Create a user can just add, that as the administrator who where created on a host
(1) to add users and passwords who
[root @ rabbitmq-1 ~] # rabbitmqctl SOHO 123 add_user
Creating User "SOHO" ...
... DONE.
(2) this is for the administrator
[the root-RabbitMQ @ ~. 1] # SOHO administrator rabbitmqctl set_user_tags
the Setting Tags for User "SOHO" to [administrator] .. .
... DONE.
(. 3) To view the user
[root @ rabbitmq-1 ~] # rabbitmqctl list_users
Listing users ...
Guest [Administrator]
SOHO [Administrator]
... DONE.
(. 4) rights
[root @ rabbitmq-1 ~] # rabbitmqctl set_permissions -p "/" SOHO. " ". " ". " "
Setting the Permissions for the User "SOHO" in vhost "/" ...
...done.
Note that when you set permissions here '.
'You need to have a space between the three' * 'representing the conf permissions, read permissions and write permissions For example: When not to
pre-set the permissions of these three soho there is no authority inquiries queue, can not see the ui interface


6, all machines are operating: Open user to remotely log:
[the root-RabbitMQ @ ~. 1] # CD / etc / RabbitMQ /
[the root @-RabbitMQ RabbitMQ. 1] # CP /usr/share/doc/rabbitmq-server-3.7. . 5 / rabbitmq.config.example /etc/rabbitmq/rabbitmq.config
[@ the root-RabbitMQ RabbitMQ. 1] # LS
enabled_plugins rabbitmq.config
[@ the root-RabbitMQ RabbitMQ. 1] # Vim rabbitmq.config
modified as follows:

7. The three machines are operating restart the service service:
[root @ RabbitMQ-1 ~] # systemctl restart RabbitMQ-Server
View port all machines are operating: Open the user to remotely log:
[root @ RabbitMQ-1 ~] # cd / etc / RabbitMQ /
[the root @-RabbitMQ RabbitMQ. 1] /usr/share/doc/rabbitmq-server-3.7.5/rabbitmq.config.example /etc/rabbitmq/rabbitmq.config CP #
[@ the root-RabbitMQ RabbitMQ. 1] LS #
rabbitmq.config enabled_plugins
[@ the root-RabbitMQ RabbitMQ. 1] # Vim rabbitmq.config
modified as follows:
The use of messaging middleware Rabbitmq

8. The three machines are operating restart Services:
[the root-RabbitMQ @ ~. 1] # systemctl the restart-Server RabbitMQ
view ports
The use of messaging middleware Rabbitmq
4369 - erlang found port
5672-- program port
15672-- management interface port ui
25672 - server internal communication between the port

9, note that if a cloud server, remember to add the security group to release the port.
web access: 192.168.50.138: 15672
to note here:
RabbitMQ default administrator user: guest Password: guest
newly added users to: soho Password: 123

++++++++++++ RabbitMQ cluster formally deployed (normal mode) +++++++++++++

Second, the formal deployment of cluster

1. First created storage directory data and log storage directory
[the root-RabbitMQ @ ~. 1] # mkdir -p / Data / RabbitMQ / Data
[@ the root-RabbitMQ. 1 ~] # mkdir -p / Data / RabbitMQ / logs
[the root ~. 1-RabbitMQ @] -R & lt # the chmod 777 / Data / RabbitMQ
[the root-RabbitMQ @ ~. 1] # chown rabbitmq.rabbitmq / Data / -R & lt

2, create a configuration file:
[the root-RabbitMQ @ ~. 1] # Vim /etc/rabbitmq/rabbitmq-env.conf (add the following)
RABBITMQ_MNESIA_BASE = / Data / RabbitMQ / Data
RABBITMQ_LOG_BASE = / Data / RabbitMQ / logs

[root@rabbitmq-1 ~]# cat /etc/rabbitmq/rabbitmq-env.conf

3 to restart the service
[root @ rabbitmq-1 ~] # systemctl restart rabbitmq-server

4. Copy ⻉ erlang.cookie
reasons: the nodes in the cluster via a cookie ⼀ achieved, it is necessary to ensure that all nodes cookie ⼀ activated, or else the position between nodes on a communication method ⽆ (cookie stored: decompression mounted rabbitmq deployed installation package is stored in other ways /home/.erlang.cookie.rpm installation / var / lib / rabbitmq)

[the root ~ @ rabbitmq-1] # CAT /var/lib/rabbitmq/.erlang.cookie
HOUCUGJDZYTFZDSWXTHJ
Use scp copy .erlang.cookie place index value rabbitmq-1 two nodes to other nodes.
[the root-RabbitMQ @ ~. 1] # SCP /var/lib/rabbitmq/.erlang.cookie [email protected]: / var / lib / RabbitMQ /
[the root-RabbitMQ @ ~. 1] # SCP / var / lib / RabbitMQ /.erlang.cookie [email protected]: / var / lib / rabbitmq /

3. mq-2, as the filling with the memory of the node cluster node mq-1 .mq-1 as the disk node mq-3, in RabbitMQ cluster, You must be at little ⼀ disk nodes, or process queue metadata ⽆ is writing to a cluster node when the disk dawdle away, the cluster will write a new queue metadata information START not be effective.
In mq-2, mq-3 Perform the following commands:
[2-RabbitMQ the root @ ~] # # rabbitmqctl stop_app stop node, remember not starved
[root @ rabbitmq-2 ~] # rabbitmqctl reset # if the data needs to be reset without the need
[root @ rabbitmq-2 ~] # rabbitmqctl join_cluster --ram rabbit @ rabbitmq-1 # added to the disk node
Clustering node 'Rabbit @ RabbitMQ-2' with '@ Rabbit-RabbitMQ. 1' ...
[the root @ rabbitmq-2 ~] # rabbitmqctl start_app # start node
starting node 'rabbit @ rabbitmq-2 ' ...

3.1.0 Here node problem solving:
(1) If you execute this command rabbitmqctl stop_app error: the need to perform
#chmod 400 .erlang.cookie the file permissions are 400
#chown rabbitmq.rabbitmq .erlang.cookie
(2) Due to change the hostname file, each rabbitmqctl stop or rabbitmqctl cluster_status, as long as the command is rabbitmq error, suggesting something like
Cluster Status of the Node the Rabbit @ web2 ...
Error: Unable to Connect to the Node the Rabbit @ web2: nodedown
... ...........
At this first ps aux | grep mq, then all mq kill -9 process, then rabbitmq-server -detached can be solved. (That is, first strong kill and restart)
(3) use rabbitmqctl stop, after rabbitmq-server -detached restart, add the original user admin, web hosting etc coresystem loss, also need to re-add.
(4) rabbitmqctl join_cluster --ram rabbit @ rabbitmq-1 when checking DNS error
(5) other questions about using rabbitmq-server -detached command to start rabbitmq, the following prompt appears Warning: PID file not written; -detached was passed, use this time
rabbitmqctl status alert service has started, we can see this problem not solved.
3.1.1 node added:
(1) the default boot disk after rabbitmq node in this cluster command, mq-2 and the mq-3 is the memory node,
MQ-1 is the disk node.
(2) If you want mq-2, mq-3 disk nodes are removed --ram parameters.
(3) If you want to change the node type, can use the command rabbitmqctl change_cluster_node_type disc (ram), provided that the rabbit must be stopped to apply it
Note:
# If you need to use disk node joins the cluster
[root @ rabbitmq-2 ~] # rabbitmqctl Rabbit @ RabbitMQ. 1-join_cluster
[the root-RabbitMQ. 3 @ ~] rabbitmqctl join_cluster Rabbit # @. 1-RabbitMQ

4, view the cluster status, run rabbitmqctl cluster_status on any node in the cluster configuration to see if successful.
[root @ rabbitmq-1 ~] # rabbitmqctl cluster_status
The use of messaging middleware Rabbitmq
each machine shows three nodes, said that it has successfully added!

5. Log rabbitmq web management console, create a new queue
to open the browser lose START http://192.168.50.138:15672 ,
lost clicks into default Username: guest
lose START Default Password: guest
The use of messaging middleware Rabbitmq

Creating a queue in accordance with Article ⼀ community ⾯ tips
The use of messaging middleware Rabbitmq

++++++由以上RABBITMQ普通模式+镜像模式+++++++++++++

三,添加镜像模式的原因:

     上面已经完成RabbitMQ默认集群模式,但并不保证队列的高可用性,队列内容不会复制。如果队列节点宕机直接导致该队列无法应用,只能等待重启,所以要想在队列节点宕机或故障也能正常应用,就要复制队列内容到集群里的每个节点,必须要创建镜像队列。    镜像队列是基于普通的集群模式的。

1,创建镜像集群:三台机器相同操作
rabbitmq set_policy :设置策略
[root@rabbitmq-1 ~]#rabbitmqctl set_policy ha-all "^" '{"ha-mode":"all"}'
Setting policy "ha-all" for pattern "^" to "{"ha-mode":"all"}" with priority "0" for vhost "/" ...

2,再次查看队列已经同步到其他两台节点:
The use of messaging middleware Rabbitmq

3,此时镜像队列设置成功。已经部署完成,将所有队列设置为镜像队列,即队列会被复制到各个节点,各个节点状态保持一致。``

++++++++rabbitmq默认的集群模式+镜像模式+HA+负载均衡++++++++++++
RabbitMQ 高可用集群搭建完成,最后一个步骤就是搭建均衡器。
The use of messaging middleware Rabbitmq

简单安装并配置负载均衡器HA

注意:如果使用阿里云,可以使用阿里云的内网slb来实现负载均衡,不用自己搭建HA。
安装并配置负载均衡器HA
1、在192.168.50.138安装HAProxy
yum -y install haproxy
2、修改 /etc/haproxy/haproxy.cfg
[root@rabbitmq-1 ~]# cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.bak
[root@rabbitmq-1 ~]# vim /etc/haproxy/haproxy.cfg
global
log 127.0.0.1 local2

chroot      /var/lib/haproxy
pidfile     /var/run/haproxy.pid
maxconn     4000
user        haproxy
group       haproxy
nbproc      4
daemon
# turn on stats unix socket
stats socket /var/lib/haproxy/stats

#---------------------------------------------------------------------
defaults
mode http
log global
retries 3
timeout connect 10s
timeout client 1m
timeout server 1m
timeout check 10s
maxconn 2048
#---------------------------------------------------------------------
##监控查看本地状态#####
listen admin_stats
bind :80
mode http
option httplog
option httpclose
log 127.0.0.1 local0 err
stats uri /haproxy
stats auth xingdian:123 (用户+密码)
stats refresh 30s
####################################
###反代监控
frontend server
bind
:5670
log global
mode tcp
#option forwardfor
default_backend rabbitmq
maxconn 3
backend rabbitmq
mode tcp
log global
balance roundrobin
server rabbitmq1 192.168.50.138:5672 check inter 2000s rise 2 fall 3
server rabbitmq2 192.168.50.139:5672 check inter 2000s rise 2 fall 3
server rabbitmq3 192.168.50.140:5672 check inter 2000s rise 2 fall 3

[root@rabbitmq-1 ~]# systemctl restart haproxy

Enter http://192.168.50.138/haproxy browser to view rabbitmq state.

Guess you like

Origin blog.51cto.com/14535577/2443782