RabbitMQ消息队列———安装(一)
一、RabbitMQ队列
不同进程间的通信,简称IPC。不同的进程间的通信,可以基于队列解决,将生产者和消费者进行解耦,中间的队列作为其桥梁。RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统。他遵循Mozilla Public License开源协议。
MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法。应用程序通过读写出入队列的消息(针对应用程序的数据)来通信,而无需专用连接来链接它们。消息传递指的是程序之间通过在消息中发送数据进行通信,而不是通过直接调用彼此来通信,直接调用通常是用于诸如远程过程调用的技术。排队指的是应用程序通过队列来通信。队列的使用除去了接收和发送应用程序同时执行的要求。RabbitMQ可以,多个程序同时使用RabbitMQ ,但是必须队列名称不一样。采用erlang语言,属于爱立信公司开发的。
消息中间件 --->就是消息队列
异步方式:不需要立马得到结果,需要排队
同步方式:需要实时获得数据,坚决不能排队
subprocess 的Q也提供不同进程之间的沟通
应用场景:
电商秒杀活动
抢购小米手机
堡垒机批量发送文件
二、Centos6.x系统编译安装RabbitMQ
1、查看环境:
[root@rabbitmq ~]# cat /etc/redhat-release CentOS release 6.6 (Final) [root@rabbitmq ~]# uname -r 2.6.32-504.el6.x86_64
2、安装erlang环境
1、安装依赖包: yum install gcc ncurses ncurses-base ncurses-devel ncurses-libs ncurses-static ncurses-term ocaml-curses -y yum install ocaml-curses-devel openssl-devel zlib-devel openssl-devel perl xz xmlto m4 kernel-devel -y 2、下载otp_src_19.3.tar.gz wget http://erlang.org/download/otp_src_19.3.tar.gz 3、tar xvf otp_src_19.3.tar.gz 4、cd otp_src_19.3 5、./configure --prefix=/usr/local/erlang --with-ssl --enable-threads --enable-smp-support --enable-kernel-poll --enable-hipe --without-javac 6、make && make install 7、配置erlang环境: echo "export PATH=$PATH:/usr/local/erlang/bin" >>/etc/profile #使环境变量配置生效 source /etc/profile 7、配置解析 [root@rabbitmq otp_src_19.3]# echo "127.0.0.1 自己的主机名" >>/etc/hosts #rabbitmq改成你自己主机名 备注: 启动rabbitmq报错: [root@bogon sbin]# /usr/local/rabbitmq/sbin/rabbitmq-server -detache ERROR: epmd error for host bogon: timeout (timed out) 原因:解析不了主机名 解决办法:先查看主机名 [root@rabbitmq ~]# hostname rabbitmq 然后再执行下面这步 echo "127.0.0.1 主机的主机名" >>/etc/hosts
3、安装rabbitMQ
1、下载rabbitmq-server-generic-unix-3.6.5.tar.xz 2、tar xvf rabbitmq-server-generic-unix-3.6.5.tar.xz 3、mv rabbitmq_server-3.6.5/ /usr/local/rabbitmq 4、启动: #启动rabbitmq服务 /usr/local/rabbitmq/sbin/rabbitmq-server #后台启动 /usr/local/rabbitmq/sbin/rabbitmq-server -detached #关闭rabbitmq服务 /usr/local/rabbitmq/sbin/rabbitmqctl stop 或 ps -ef | grep rabbit 和 kill -9 xxx #开启插件管理页面 /usr/local/rabbitmq/sbin/rabbitmq-plugins enable rabbitmq_management #创建用户 /usr/local/rabbitmq/sbin/rabbitmqctl add_user rabbitadmin 123456 /usr/local/rabbitmq/sbin/rabbitmqctl set_user_tags rabbitadmin administrator #给用户授权 /usr/local/rabbitmq/sbin/rabbitmqctl set_permissions -p / rabbitadmin ".*" ".*" ".*" #语法: 1 set_permissions [-p <vhost>] <user> <conf> <write> <read>
4、登录RabbitMQ_web页面
三、Centos7.x系统编译安装RabbitMQ
1、系统环境
[root@rabbitmq sbin]# cat /proc/version
Linux version 3.10.0-327.el7.x86_64 (builder@kbuilder.dev.centos.org) (gcc version 4.8.3 20140911 (Red Hat 4.8.3-9) (GCC) ) #1 SMP Thu Nov 19 22:10:57 UTC 2015
1.1、Centos7.x关闭防火墙
1 [root@rabbitmq /]# systemctl stop firewalld.service 2 3 [root@rabbitmq /]# systemctl disable firewalld.service 4 Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service. 5 Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service.
如果不想关闭防火墙,可以通过如下方法处理:
1 开放5672端口: 2 3 firewall-cmd --zone=public --add-port=5672/tcp --permanent 4 firewall-cmd --reload
2、下载erlang和rabbitmq-server的rpm
http://www.rabbitmq.com/releases/erlang/erlang-19.0.4-1.el7.centos.x86_64.rpm
http://www.rabbitmq.com/releases/rabbitmq-server/v3.6.6/rabbitmq-server-3.6.6-1.el7.noarch.rpm
3、安装erlang
[root@rabbitmq ~]# cd /server/scripts/
[root@rabbitmq scripts]# ll
total 23508
-rw-r--r--. 1 root root 18580960 Jan 28 10:04 erlang-19.0.4-1.el7.centos.x86_64.rpm
-rw-r--r--. 1 root root 5487706 Jan 28 10:04 rabbitmq-server-3.6.6-1.el7.noarch.rpm
[root@rabbitmq scripts]# rpm -ivh erlang-19.0.4-1.el7.centos.x86_64.rpm
测试erlang是否安装成功:
[root@rabbitmq scripts]# erl
Erlang/OTP 19 [erts-8.0.3] [source] [64-bit] [smp:2:2] [async-threads:10] [hipe] [kernel-poll:false]
Eshell V8.0.3 (abort with ^G)
1> 5+6.
11
2> halt(). #退出
4、安装socat (备注:安装RabbitMQ必须先安装socat依赖,否则会报错)
[root@rabbitmq scripts]# yum install socat
5、安装RabbitMQ
[root@rabbitmq scripts]# rpm -ivh rabbitmq-server-3.6.6-1.el7.noarch.rpm
启动和关闭:
/sbin/service rabbitmq-server start #启动服务
/sbin/service rabbitmq-server stop #关闭服务
/sbin/service rabbitmq-server status #查看服务状态
示例:
1 [root@rabbitmq ~]# service rabbitmq-server status 2 Redirecting to /bin/systemctl status rabbitmq-server.service 3 ● rabbitmq-server.service - RabbitMQ broker 4 Loaded: loaded (/usr/lib/systemd/system/rabbitmq-server.service; disabled; vendor preset: disabled) 5 Active: active (running) since Sat 2017-01-28 20:20:46 CST; 8h ago 6 Main PID: 2892 (beam.smp) 7 Status: "Initialized" 8 CGroup: /system.slice/rabbitmq-server.service 9 ├─2892 /usr/lib64/erlang/erts-8.0.3/bin/beam.smp -W w -A 64 -P 1048576 -t 5000000 -st... 10 ├─3027 /usr/lib64/erlang/erts-8.0.3/bin/epmd -daemon 11 ├─3143 erl_child_setup 1024 12 ├─3153 inet_gethost 4 13 └─3154 inet_gethost 4 14 15 Jan 28 20:20:43 rabbitmq rabbitmq-server[2892]: RabbitMQ 3.6.6. Copyright (C) 2007-2016 Pivot...nc. 16 Jan 28 20:20:43 rabbitmq rabbitmq-server[2892]: ## ## Licensed under the MPL. See http...om/ 17 Jan 28 20:20:43 rabbitmq rabbitmq-server[2892]: ## ## 18 Jan 28 20:20:43 rabbitmq rabbitmq-server[2892]: ########## Logs: /var/log/rabbitmq/rabbit@ra...log #日志存放地址 19 Jan 28 20:20:43 rabbitmq rabbitmq-server[2892]: ###### ## /var/log/rabbitmq/rabbit@ra...log 20 Jan 28 20:20:43 rabbitmq rabbitmq-server[2892]: ########## 21 Jan 28 20:20:43 rabbitmq rabbitmq-server[2892]: Starting broker... 22 Jan 28 20:20:45 rabbitmq rabbitmq-server[2892]: systemd unit for activation check: "rabbitmq-...ce" 23 Jan 28 20:20:46 rabbitmq systemd[1]: Started RabbitMQ broker. 24 Jan 28 20:20:46 rabbitmq rabbitmq-server[2892]: completed with 0 plugins. 25 Hint: Some lines were ellipsized, use -l to show in full.
#查看端口
1 [root@rabbitmq sbin]# ps -ef|grep rabbitmq 2 rabbitmq 2892 1 0 Jan28 ? 00:01:39 /usr/lib64/erlang/erts-8.0.3/bin/beam.smp -W w -A 64 -P 1048576 -t 5000000 -stbt db -zdbbl 32000 -K true -- -root /usr/lib64/erlang -progname erl -- -home /var/lib/rabbitmq -- -pa /usr/lib/rabbitmq/lib/rabbitmq_server-3.6.6/ebin -noshell -noinput -s rabbit boot -sname rabbit@rabbitmq -boot start_sasl -kernel inet_default_connect_options [{nodelay,true}] -sasl errlog_type error -sasl sasl_error_logger false -rabbit error_logger {file,"/var/log/rabbitmq/rabbit@rabbitmq.log"} -rabbit sasl_error_logger {file,"/var/log/rabbitmq/rabbit@rabbitmq-sasl.log"} -rabbit enabled_plugins_file "/etc/rabbitmq/enabled_plugins" -rabbit plugins_dir "/usr/lib/rabbitmq/lib/rabbitmq_server-3.6.6/plugins" -rabbit plugins_expand_dir "/var/lib/rabbitmq/mnesia/rabbit@rabbitmq-plugins-expand" -os_mon start_cpu_sup false -os_mon start_disksup false -os_mon start_memsup false -mnesia dir "/var/lib/rabbitmq/mnesia/rabbit@rabbitmq" -kernel inet_dist_listen_min 25672 -kernel inet_dist_listen_max 25672 3 rabbitmq 3027 1 0 Jan28 ? 00:00:00 /usr/lib64/erlang/erts-8.0.3/bin/epmd -daemon 4 rabbitmq 3143 2892 0 Jan28 ? 00:00:01 erl_child_setup 1024 5 rabbitmq 3153 3143 0 Jan28 ? 00:00:00 inet_gethost 4 6 rabbitmq 3154 3153 0 Jan28 ? 00:00:00 inet_gethost 4 7 root 24739 21359 0 03:18 pts/0 00:00:00 grep --color=auto rabbitmq