RabbitMQ简介:
1、是实现AMQP(高级消息队列协议)的消息中间件的一种。
2、主要是为了实现系统之间的双向解耦而实现的。当生产者大量产生数据时,消费者无法快速消费,那么需要一个中间层。保存这个数据。
一般提到 RabbitMQ 和消息,都会用到以下一些专有名词:
(1)生产(Producing)意思就是发送。发送消息的程序就是一个生产者(producer)。我们一般用 “P” 来表示。
(2)队列(queue)就是邮箱的名称。消息通过你的应用程序和 RabbitMQ 进行传输,它们能够只存储在一个队列(queue)中。 队列(queue)没有任何限制,你要存储多少消息都可以——基本上是一个无限的缓冲。多个生产者(producers)能够把消息发送给同一个队列,同样,多个消费者(consumers)也能够从同一个队列(queue)中获取数据。
(3)消费(Consuming)和获取消息是一样的意思。一个消费者(consumer)就是一个等待获取消息的程序。
那么开始了解一下 RabbitMQ 在centos7下的安装于运用吧
1.准备机器,做好域名解析,主机名解析,时间同步
192.168.42.151 node2 [haproxy] 192.168.42.152 node3 [rabbitmq master] 192.168.42.153 node4 [rabbitmq slave]
node2上操作:
vim /etc/hosts 192.168.42.151 node2 192.168.42.152 node3 192.168.42.153 node4 hostnamectl set-hostname node2
rabbitmq依赖短格式的主机名,因此我们需要配置主机名并加入hosts解析
每个节点都要配置哦,因此我们把node2上的hosts推送到node3,node4上
scp /etc/hosts 192.168.42.152:/etc/ scp /etc/hosts 192.168.42.153:/etc/
node3上操作:
hostnamectl set-hostname node3
node4上操作:
hostnamectl set-hostname node4
2.安装rabbitmq
node3上操作:
(1).安装rabbitmq
yum install rabbitmq-server -y [root@node4 ~]# cd /etc/rabbitmq/ [root@node4 rabbitmq]# ls rabbitmq.config
默认只有rabbitmq.config的配置文件
(3).启动rabbitmq-management管理
我们启用WEB管理。
rabbitmq-plugins enable rabbitmq_management [root@node3 rabbitmq]# rabbitmq-plugins enable rabbitmq_management The following plugins have been enabled: mochiweb webmachine rabbitmq_web_dispatch amqp_client rabbitmq_management_agent rabbitmq_management Plugin configuration has changed. Restart RabbitMQ for changes to take effect. cd /etc/rabbitmq/ [root@node3 rabbitmq]# ls enabled_plugins rabbitmq.config
再次查看/etc/rabbitmq/,可以看到生成了一个enabled_pluginsd的配置文件
(3).启动rabbitmq
systemctl start rabbitmq-server
查看一下状态
[root@node3 ~]# ss -tnl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:4369 *:* LISTEN 0 128 *:22 *:* LISTEN 0 128 *:15672 *:* LISTEN 0 100 127.0.0.1:25 *:* LISTEN 0 128 *:25672 *:* LISTEN 0 128 :::4369 :::* LISTEN 0 128 :::22 :::* LISTEN 0 100 ::1:25 :::* LISTEN 0 128 :::5672 :::*
3.去浏览器中访问测试一把:
默认账户密码 guest/guest
http://192.168.42.152:15672/ 可以进入管理界面了
node4 同上
4.把node3,node4组成集群
我们先来查看一下这两个节点的集群状态
node3:
[root@node3 rabbitmq]# rabbitmqctl cluster_status Cluster status of node rabbit@node3 ... [{nodes,[{disc,[rabbit@node3]}]}, {running_nodes,[rabbit@node3]}, {cluster_name,<<"rabbit@node3">>}, {partitions,[]}] ...done.
node4:
[root@node4 ~]# rabbitmqctl cluster_status Cluster status of node rabbit@node4 ... [{nodes,[{disc,[rabbit@node4]}]}, {running_nodes,[rabbit@node4]}, {cluster_name,<<"rabbit@node4">>}, {partitions,[]}] ...done.
我们让node3作为主节点 (1).停掉主节点的应用
[root@node3 rabbitmq]# rabbitmqctl stop_app Stopping node rabbit@node3 ... ...done.
(2).添加集群节点(指cluster_name)
rabbitmqctl join_cluster rabbit@node4 Clustering node rabbit@node3 with rabbit@node4 ... Error: unable to connect to nodes [rabbit@node4]: nodedown DIAGNOSTICS =========== attempted to contact: [rabbit@node4] rabbit@node4: * connected to epmd (port 4369) on node4 * epmd reports node 'rabbit' running on port 25672 * TCP connection succeeded but Erlang distribution failed * suggestion: hostname mismatch? * suggestion: is the cookie set correctly? current node details: - node name: rabbitmqctl7692@node3 - home dir: /var/lib/rabbitmq - cookie hash: BbXCvnYxz/Q/iwctrCP/bQ==
会发现报错,告诉我们cookie信息不一致,因此我们需要把作为主节点的cookie信息推送到各个从节点上
cookie信息放在/var/lib/rabbitmq/下,我们cd进去,查看一下子
cd /var/lib/rabbitmq/ [root@node3 rabbitmq]# ll -a total 8 drwxr-x--- 3 rabbitmq rabbitmq 42 Jun 29 16:24 . drwxr-xr-x. 27 root root 4096 Jun 29 16:23 .. -r-------- 1 rabbitmq rabbitmq 20 Jun 29 00:00 .erlang.cookie drwxr-xr-x 4 rabbitmq rabbitmq 85 Jun 29 17:13 mnesia
(3).推送cookie信息,并保证权限400及属主属组是rabbitmq 如下:
-r-------- 1 rabbitmq rabbitmq 20 Jun 29 21:14 .erlang.cookie
使用scp命令推送cookie
scp -rp .erlang.cookie 192.168.42.153:/var/lib/rabbitmq/
然后,我们再执行
rabbitmqctl join_cluster rabbit@node4 [root@node3 rabbitmq]# rabbitmqctl join_cluster rabbit@node4 Clustering node rabbit@node3 with rabbit@node4 ... ...done.
可见我们的集群成功了
查看一下状态:
[root@node3 rabbitmq]# rabbitmqctl cluster_status Cluster status of node rabbit@node3 ... [{nodes,[{disc,[rabbit@node3,rabbit@node4]}]}] ...done.
可以看到有两个节点添加进去了
(4).最后启动我们关闭的节点应用
[root@node3 rabbitmq]# rabbitmqctl start_app Starting node rabbit@node3 ... ...done.
(5).去浏览器访问两个
http://192.168.42.152:15672 http://192.168.42.153:15672
都可以看到有两个node3,node4的信息 至此我们的rabbitmq集群,高可用已经做好了
5.用haproxy实现node3,node4的rabbitmq 负载均衡
在node2上操作:
(1).haproxy的安装
yum install haproxy -y
(2).配置haproxy
配置文件如下:
#--------------------------------------------------------------------- # Example configuration for a possible web application. See the # full configuration options online. # # http://haproxy.1wt.eu/download/1.4/doc/configuration.txt # #--------------------------------------------------------------------- #--------------------------------------------------------------------- # Global settings #--------------------------------------------------------------------- global # to have these messages end up in /var/log/haproxy.log you will # need to: # # 1) configure syslog to accept network log events. This is done # by adding the '-r' option to the SYSLOGD_OPTIONS in # /etc/sysconfig/syslog # # 2) configure local2 events to go to the /var/log/haproxy.log # file. A line like the following can be added to # /etc/sysconfig/syslog # # local2.* /var/log/haproxy.log # log 127.0.0.1 local2 chroot /var/lib/haproxy pidfile /var/run/haproxy.pid maxconn 4000 user haproxy group haproxy daemon # turn on stats unix socket stats socket /var/lib/haproxy/stats #--------------------------------------------------------------------- # common defaults that all the 'listen' and 'backend' sections will # use if not designated in their block #--------------------------------------------------------------------- defaults mode tcp log global option httplog option dontlognull option http-server-close option forwardfor except 127.0.0.0/8 option redispatch retries 3 timeout http-request 10s timeout queue 1m timeout connect 10s timeout client 1m timeout server 1m timeout http-keep-alive 10s timeout check 10s maxconn 3000 listen http_front bind *:1080 #监听端口 mode http stats refresh 30s #统计页面自动刷新时间 stats uri /haproxy?status #统计页面url stats realm Haproxy Manager #统计页面密码框上提示文本 stats auth admin:admin #统计页面用户名和密码设置 #stats hide-version #隐藏统计页面上HAProxy的版本信息 #--------------------------------------------------------------------- # main frontend which proxys to the backends #--------------------------------------------------------------------- frontend rabbitmq_web bind *:15672 default_backend websrvs frontend rabbitmq_server bind *:5672 default_backend servers #--------------------------------------------------------------------- # static backend for serving up images, stylesheets and such #--------------------------------------------------------------------- backend websrvs balance roundrobin server node3 192.168.42.152:15672 check server node4 192.168.42.153:15672 check #--------------------------------------------------------------------- # round robin balancing between the various backends #--------------------------------------------------------------------- backend servers balance roundrobin server node3 192.168.42.152:5672 check server node4 192.168.42.153:5672 check
(3)启动haproxy
systemctl start haproxy [root@node2 haproxy]# ss -tnl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:22 *:* LISTEN 0 3000 *:15672 *:* LISTEN 0 3000 *:1080 *:* LISTEN 0 100 127.0.0.1:25 *:* LISTEN 0 3000 *:5672 *:* LISTEN 0 128 :::22 :::* LISTEN 0 100 ::1:25 :::*
(4).去浏览器访问
http://192.168.42.151:15672/ 可以看到rabbitmq的web界面
http://192.168.42.151:1080/haproxy?status 可以看到haproxy的监控界面
原创文章,作者:srayban,如若转载,请注明出处:http://www.178linux.com/78877