rabbitMQ之安装和配置(一)
erlang是一门面向并发的编程语言,流行的消息队列rabbitMQ是基于erlang环境运行的;
yum安装
## primary RabbitMQ signing key
rpm --import https://github.com/rabbitmq/signing-keys/releases/download/2.0/rabbitmq-release-signing-key.asc
## modern Erlang repository
rpm --import https://packagecloud.io/rabbitmq/erlang/gpgkey
## RabbitMQ server repository
rpm --import https://packagecloud.io/rabbitmq/rabbitmq-server/gpgkey
向/etc/yum.repos.d/下添加rabbitmq.repo文件
##
## Zero dependency Erlang
##
[rabbitmq_erlang]
name=rabbitmq_erlang
baseurl=https://packagecloud.io/rabbitmq/erlang/el/7/$basearch
repo_gpgcheck=1
gpgcheck=1
enabled=1
# PackageCloud's repository key and RabbitMQ package signing key
gpgkey=https://packagecloud.io/rabbitmq/erlang/gpgkey
https://github.com/rabbitmq/signing-keys/releases/download/2.0/rabbitmq-release-signing-key.asc
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
metadata_expire=300
[rabbitmq_erlang-source]
name=rabbitmq_erlang-source
baseurl=https://packagecloud.io/rabbitmq/erlang/el/7/SRPMS
repo_gpgcheck=1
gpgcheck=0
enabled=1
gpgkey=https://packagecloud.io/rabbitmq/erlang/gpgkey
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
metadata_expire=300
##
## RabbitMQ server
##
[rabbitmq_server]
name=rabbitmq_server
baseurl=https://packagecloud.io/rabbitmq/rabbitmq-server/el/7/$basearch
repo_gpgcheck=1
gpgcheck=1
enabled=1
# PackageCloud's repository key and RabbitMQ package signing key
gpgkey=https://packagecloud.io/rabbitmq/rabbitmq-server/gpgkey
https://github.com/rabbitmq/signing-keys/releases/download/2.0/rabbitmq-release-signing-key.asc
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
metadata_expire=300
[rabbitmq_server-source]
name=rabbitmq_server-source
baseurl=https://packagecloud.io/rabbitmq/rabbitmq-server/el/7/SRPMS
repo_gpgcheck=1
gpgcheck=0
enabled=1
gpgkey=https://packagecloud.io/rabbitmq/rabbitmq-server/gpgkey
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
metadata_expire=300
yum update -y
yum install -y make gcc gcc-c++ m4 openssl openssl-devel ncurses-devel unixODBC unixODBC-devel java java-devel
yum install socat logrotate -y
yum install erlang rabbitmq-server -y
以上安装转自官网 centos7安装步骤 https://www.rabbitmq.com/install-rpm.html
启动RabbitMQ:
systemctl start rabbitmq-server
RabbitMQ启动出错:- unable to connect to epmd on xxxx: timeout (timed out)
因为本机主机名到IP地址的解析对应不起来;
vim /etc/hosts
192.168.100.251 test-web2
保存退出,再次启动
设置RabbitMQ开机自启动:
systemctl enable rabbitmq-server
RabbitMQ其他操作:
查看启动后的情况
rabbitmqctl status
关闭服务
rabbitmqctl stop
重启rabbitmq
systemctl restart rabbitmq-server
rabbitmq启动配置文件
/etc/rabbitmq/rabbitmq.conf
rabbitmq的组件
rabbitmqctl : rabbitmq的客户端,用来连接管理rabbitmq;
rabbitmq-env :配置环境变量的管理者;
rabbitmq-plugins:rabbitmq后台插件管理;
rabbitmq-server: rabbitmq守护进程;
rabbitmq的管理
rabbitmq使用rabbitmqctl管理,常用的命令有:
rabbitmq-server -detached # 使用守护进程方式启动
rabbitmq-server start # 使用阻塞方式启动
rabbitmqctl stop # 关闭rabbitmq
rabbitmqctl list_users # 查看后台管理员名单
rabbitmqctl list_queues # 查看当前的所有的队列
rabbitmqctl list_exchanges # 查看所有的交换机
rabbitmqctl list_bindings # 查看所有的绑定
rabbitmqctl list_connections # 查看所有的tcp连接
rabbitmqctl list_channels # 查看所有的信道
rabbitmqctl stop_app # 关闭应用
rabbitmqctl start_app # 打开应用
rabbitmqctl reset # 清空队列
查看状态信息
rabbitmqctl status
# 得到如下的信息
[{pid,3086}, # rabbitmq服务运行的进程ID
{running_applications,
[{rabbit,"RabbitMQ","3.6.10"},
{ranch,"Socket acceptor pool for TCP protocols.","1.3.0"},
{ssl,"Erlang/OTP SSL application","8.2"},
{public_key,"Public key infrastructure","1.4.1"},
{asn1,"The Erlang ASN1 compiler version 5.0","5.0"},
{os_mon,"CPO CXC 138 46","2.4.2"},
{rabbit_common,
"Modules shared by rabbitmq-server and rabbitmq-erlang-client",
"3.6.10"},
{syntax_tools,"Syntax tools","2.1.2"},
{xmerl,"XML parser","1.3.15"},
{crypto,"CRYPTO","4.0"},
{mnesia,"MNESIA CXC 138 12","4.15"},
{compiler,"ERTS CXC 138 10","7.1"},
{sasl,"SASL CXC 138 11","3.0.4"},
{stdlib,"ERTS CXC 138 10","3.4"},
{kernel,"ERTS CXC 138 10","5.3"}]},
{os,{unix,linux}},
{erlang_version,
"Erlang/OTP 20 [erts-9.0] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:64] [hipe] [kernel-poll:true]\n"},
{memory, # 内存
[{total,56444960}, # 总共消耗的内存,单位字节
{connection_readers,0}, # tcp订阅连接消耗
{connection_writers,0}, # tcp发布者连接消耗
{connection_channels,0}, # 信道消耗
{connection_other,0}, # 其他消耗
{queue_procs,2840}, # 队列进程消耗
{queue_slave_procs,0}, # 队列子进程消耗
{plugins,0}, # 插件消耗
{other_proc,22726336}, # 其他进程消耗
{mnesia,60944},
{metrics,184544},
{mgmt_db,0},
{msg_index,45048}, # 消息标记
{other_ets,1695784},
{binary,94472}, # 一些被引用的数据
{code,21374813}, # 代码
{atom,891849},
{other_system,9549962}]},
{alarms,[]},
{listeners,[{clustering,25672,"::"},{amqp,5672,"::"}]}, # mq监听端口5672,erlang端口25672
{vm_memory_high_watermark,0.4}, # 开启流控的内存阀值
{vm_memory_limit,6568098201}, # 消息持久化阀值
{disk_free_limit,50000000}, # 磁盘开启流控阀值
{disk_free,48187891712}, # 磁盘空闲量
{file_descriptors, # 文件句柄
[{total_limit,924}, # 文件句柄上限
{total_used,2}, # 已使用
{sockets_limit,829}, # 允许的TCP连接上限
{sockets_used,0}]}, # 已使用连接数
{processes,
[{limit,1048576}, # 允许的最大进程数
{used,156}]}, # 已使用进程数
{run_queue,0}, # 运行的队列数
{uptime,97},
{kernel,{net_ticktime,60}}]
2. 配置RabbitMQ
1.启用网页版后台管理插件:
rabbitmq-plugins enable rabbitmq_management
2.新建一个用户名为admin,密码为admin的管理员,并授予管理员(administrator)权限。
rabbitmqctl add_user admin admin
rabbitmqctl set_user_tags admin administrator
3.设置admin可以使用的虚机权限。
添加 admin 虚拟机
rabbitmqctl add_vhost admin
设置admin可以使用的虚机admin权限
rabbitmqctl set_permissions -p admin admin "." "." "."
3.1用户管理
查看所有用户
rabbitmqctl list_users
添加一个用户
rabbitmqctl add_user zhaobl 123456
配置权限
rabbitmqctl set_permissions -p "/" zhaobl "." "." "."
查看用户权限
rabbitmqctl list_user_permissions zhaobl
设置tag
rabbitmqctl set_user_tags zhaobl administrator
删除用户(安全起见,删除默认用户)
rabbitmqctl delete_user guest
4.插件管理:
插件列表:
rabbitmq-plugins list
启动插件:
rabbitmq-plugins enable XXX (XXX为插件名)
停用插件:
rabbitmq-plugins disable XXX
5.打开后台web管理界面:http://localhost:15672/,刚才我们设置了admin用户,所以可以使用admin登录,也可以使用默认账号和密码都是guest登录。进入管理界面后可以看到:
我们可以在后台管理用户、队列等信息。
6.修改RabbitMQ默认端口
RabbitMQ默认开启了几个端口:
4369:erlang发现口
5672:client端通信口,客户端要连接RabbitMQ服务时要用到
15672:后台管理界面ui端口,进入管理后台时访问url如:http://localhost:15672/
25672:server间内部通信口
有时候在生产环境下,我们出于安全等原因希望修改掉默认的端口号。
我们安装新版的RabbitMQ-3.9.3后,发现找不到它的配置文件,按常理,服务的端口开发都可以在配置文件里配置的。但是事实上RabbitMQ-3.9.3并没有生成配置文件,需要我们手动添加配置文件。
官方建议配置文件的位置:/etc/rabbitmq/rabbitmq.conf
我们现在要做的是把默认端口5672改成56720,并且允许远程访问。把web管理默认端口15672改成56271。只需如下配置:
image.png
7.其他配置
我们可以到官网地址:https://www.rabbitmq.com/configure.html,了解RabbitMQ的性能优化方面的配置。
使用Docker安装RabbitMQ
首先,我们得安装docker环境,docker环境的安装本站后面会有文章介绍,本文假设你的机器上已经安装好了docker环境。
先拉取RabbitMQ镜像:
docker pull rabbitmq:3.8.1-management
然后查看镜像:
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
rabbitmq 3.8.1-management 36ed80b6a1b1 5 weeks ago 180MB
然后运行容器:
docker run --name rabbitmq -d -p 5672:5672 -p 15672:15672 -v /data:/var/lib/rabbitmq rabbitmq:3.8.1-management
最后,使用docker ps查看运行的容器。
这样,一个RabbitMQ的docker环境就装好了。
RabbitMQ技术入门与实战
https://blog.csdn.net/super_rd/category_9268807.html