一、理解Erlang节点和Erlang应用程序的区别
1、Erlang节点
我们的mq服务是运行在Erlang的上面。Erlang和Erlang节点相当于java中java应用程序和jvm实例的区别
当一个Erlang应用程序启动后会有对应的一个节点的概念。Erlang也有虚拟机,当运行erlang程序的时候一个erlang虚拟机实例会生成,这个虚拟机实例被我们称为一个节点。
2、Erlang应用程序
Erlang的特别之处在于多个Erlang应用程序可以运行在一个Erlang节点上,这里所说的Erlang应用程序我们可以理解为我们自己的mq服务实例。
3、验证Erlang节点和Erlang应用程序的关系
3.1、启动Erlang节点和他上面RabbitMQ服务实例
进入自己安装的RabbitMQ的bin目录下执行
#以后台运行的方式启动Erlang节点和Erlang应用程序
[root@bogon bin]# ./rabbitmq-server -detached
这是笔者RabbitMQ的web管理页面 可以访问并进行操作
说明:如上的 ./rabbitmq-server 启动了Erlang节点和其上的应用程序(rabbitMQ服务)
3.2、停止Erlang应用程序(RabbitMQ服务实例)
#stop-app仅仅停止Erlang节点上的Erlang应用程序 并没有停止Erlang节点
#验证再次访问RabbitMQ的web管理页面,该页面不存在
[root@bogon bin]# ./rabbitmqctl stop_app
Stopping rabbit application on node rabbit@bogon ...
#验证Erlang节点没有被关闭,./rabbitmq-server -detached 重新启动Erlang节点
#会提示Erlang节点已经存在
[root@bogon bin]# ./rabbitmq-server -detached
Warning: PID file not written; -detached was passed.
ERROR: node with name "rabbit" already running on "bogon"
#停止Erlang节点
[root@bogon bin]# ./rabbitmqctl stop
Stopping and halting node rabbit@bogon ...
#再次重新启动 节点和应用程序均可以正确启动了
[root@bogon bin]# ./rabbitmq-server -detached
页面无法访问
4、总结
开启服务在控制台执行: ./rabbitmq-server
开启服务且服务已后台程序执行 : ./rabbitmq-server -detached
关闭服务但是不关闭节点: ./rabbitmqctl stop_app
关闭服务并关闭节点:./rabbitmqctl stop
二、RabbitMQ的配置文件
默认使用/etc/RabbitMQ/rabbitmq.config
在服务启动的过程中默认会加载该config文件,来设置初始化的配置文件
#一定要按照这种方式进行配置 不要忘记后面的config文件
[
{ mnesia,[
{dump_log_write_threshold,1000 }
]
},
{ rabbit,[
{vm_memory_high_watermark,0.4}
]
}
].
三、RabbitMQ的用户权限系统设置
1、用户相关操作
1.1 添加用户
#添加用户格式为
#./rabbitmqctl add_user username password
../rabbitmqctl add_user aaa bbb
1.2 查看用户列表
#查看用户列表
[root@bogon bin]# ./rabbitmqctl list_users
Listing users ...
aaa []
guest [administrator]
1.3 更改用户密码
#更改用户密码
# ./rabbitmqctl change_password username newpassword
[root@bogon bin]# ./rabbitmqctl change_password guest 123456
Changing password for user "guest" ...
1.4 删除用户
#删除用户
[root@bogon bin]# ./rabbitmqctl delete_user guest
Deleting user "guest" ...
#查看用户 已经只存在刚才建立的用户了
[root@bogon bin]# ./rabbitmqctl list_users
Listing users ...
aaa []
2、用户角色
用户角色可分为五类,超级管理员, 监控者, 策略制定者, 普通管理者以及其他。
(1) 超级管理员(administrator)
可登陆管理控制台(启用management plugin的情况下),可查看所有的信息,并且可以对用户,策略(policy)进行操作。
(2) 监控者(monitoring)
可登陆管理控制台(启用management plugin的情况下),同时可以查看rabbitmq节点的相关信息(进程数,内存使用情况,磁盘使用情况等)
(3) 策略制定者(policymaker)
可登陆管理控制台(启用management plugin的情况下), 同时可以对policy进行管理。但无法
与administrator的对比,administrator能看到这些内容
(4) 普通管理者(management)
仅可登陆管理控制台(启用management plugin的情况下),无法看到节点信息,也无法对策略进行管理。
(5) 其他
无法登陆管理控制台,通常就是普通的生产者和消费者。
了解了这些后,就可以根据需要给不同的用户设置不同的角色,以便按需管理。
设置用户角色的命令为:
rabbitmqctl set_user_tags User Tag
User为用户名, Tag为角色名(对应于上面的administrator,monitoring,policymaker,management,或其他自定义名称)。
也可以给同一用户设置多个角色,例如
rabbitmqctl set_user_tags hncscwc monitoring policymaker查看节点的相关信息(上图红框标识的部分)。
#先查看用户列表
[root@bogon bin]# ./rabbitmqctl list_users
Listing users ...
aaa []
#设置用户角色
[root@bogon bin]# ./rabbitmqctl set_user_tags aaa administrator
Setting tags for user "aaa" to [administrator] ...
#再次查看用户列表
[root@bogon bin]# ./rabbitmqctl list_users
Listing users ...
aaa [administrator]
3、权限相关控制
2.1、RabbitMQ的权限系统提供了三种不同的权限控制
- 有关消息消费的读操作权限
- 有关发布消息的写操作权限
- 配置队列和交换器的权限
2.2 权限控制规则组成
一条权限控制由4部分组成
- 需要权限控制的whost
- 被授予权限的用户
- 需要授予的读/写/配置的权限组合
- 权限范围(不太懂)
如下的命令即为一个权限控制语句
VHostPath 虚拟主机命令
User 用户
其余的为权限控制的权限组合
ConfP (配置) WriteP(写) ReadP(读)
rabbitmqctl set_permissions -p VHostPath User ConfP WriteP ReadP
#设置用户的权限信息
[root@bogon bin]# ./rabbitmqctl set_permissions -p / aaa ".*" ".*" ".*"
Setting permissions for user "aa" in vhost "/" ...
#查看用户的权限信息 可读可写可配置
[root@bogon bin]# ./rabbitmqctl list_user_permissions aaa
Listing permissions for user "aaa" ...
/ .* .* .*
#清除用户在指定的vhost下的权限信息
[root@bogon bin]# ./rabbitmqctl clear_permissions -p / aaa
Clearing permissions for user "aaa" in vhost "/" ...
#查看用户的权限信息 已经清除
[root@bogon bin]# ./rabbitmqctl list_user_permissions aaa
Listing permissions for user "aaa" ...
------没有任何权限信息-----------
四、rabbitmqctl的相关命令
补充:在liunx命令行下执行 rabbitmqctl 后面不添加任何参数 会显示该命令下的所有可配置参数信息
Commands:
add_user <username> <password>
add_vhost <vhost>
authenticate_user <username> <password>
await_online_nodes <count> [-t <timeout>]
cancel_sync_queue [-p <vhost>] queue
change_cluster_node_type <disc|ram>
change_password <username> <password>
clear_global_parameter <key>
clear_operator_policy [-p <vhost>] <key>
clear_parameter [-p <vhost>] <component_name> <key>
clear_password <username>
clear_permissions [-p vhost] <username>
clear_policy [-p <vhost>] <key>
clear_topic_permissions [-p vhost] <username> [<exchange>]
clear_vhost_limits [-p <vhost>]
close_all_connections [-p <vhost> --limit <limit>] [-n <node> --global] [--per-connection-delay <delay>] <explanation>
close_connection <connectionpid> <explanation>
cluster_status
decode value passphrase [--cipher cipher] [--hash hash] [--iterations iterations]
delete_queue queue_name [--if_empty|-e] [--if_unused|-u] [-t <timeout>]
delete_user <username>
delete_vhost <vhost>
encode value passphrase [--cipher cipher] [--hash hash] [--iterations iterations]
environment
eval <expr>
exec <expr> [--offline]
force_boot
force_reset
forget_cluster_node [--offline] <existing_cluster_member_node>
help <command>
hipe_compile <directory>
join_cluster [--disc|--ram] <existing_cluster_member_node>
list_bindings [-p <vhost>] [<bindinginfoitem> ...] [-t <timeout>]
list_channels [<channelinfoitem> ...]
list_ciphers
list_connections [<connectioninfoitem> ...] [-t <timeout>]
list_consumers [-p vhost] [<consumerinfoitem> ...] [-t <timeout>]
list_exchanges [-p <vhost>] [<exchangeinfoitem> ...] [-t <timeout>]
list_global_parameters [-t <timeout>]
list_hashes
list_operator_policies [-p <vhost>] [-t <timeout>]
list_parameters [-p <vhost>] [-t <timeout>]
list_permissions [-p <vhost>] [-t <timeout>]
list_policies [-p <vhost>] [-t <timeout>]
list_queues [-p <vhost>] [--online] [--offline] [--local] [<queueinfoitem> ...] [-t <timeout>]
list_topic_permissions [-p <vhost>] [-t <timeout>]
list_unresponsive_queues [--local] [--queue-timeout <queue-timeout>] [<unresponsiveq_ueueinfoitem> ...] [-t <timeout>]
list_user_permissions <username> [-t <timeout>]
list_user_topic_permissions <username> [-t <timeout>]
list_users [-t <timeout>]
list_vhost_limits [-p <vhost>] [--global]
list_vhosts [<vhostinfoitem> ...] [-t <timeout>]
node_health_check [-t <timeout>]
ping [-t <timeout>]
purge_queue <queue> [-t <timeout>]
rename_cluster_node <oldnode1> <newnode1> [oldnode2] [newnode2] ...
report
reset
restart_vhost [-p <vhost>] [-t <timeout>]
rotate_logs
set_cluster_name <name>
set_disk_free_limit <disk_limit>
set_disk_free_limit mem_relative <fraction>
set_global_parameter <name> <value>
set_operator_policy [-p <vhost>] [--priority <priority>] [--apply-to <apply-to>] <name> <pattern> <definition>
set_parameter [-p <vhost>] <component_name> <name> <value>
set_permissions [-p <vhost>] <username> <conf> <write> <read>
set_policy [-p <vhost>] [--priority <priority>] [--apply-to <apply-to>] <name> <pattern> <definition>
set_topic_permissions [-p <vhost>] <username> <exchange> <write_pattern> <read_pattern>
set_user_tags <username> <tag> [...]
set_vhost_limits [-p <vhost>] <definition>
set_vm_memory_high_watermark <fraction>
set_vm_memory_high_watermark absolute <value>
shutdown
start_app
status
stop [<pidfile>]
stop_app
sync_queue [-p <vhost>] queue
trace_off [-p <vhost>]
trace_on [-p <vhost>]
update_cluster_nodes <existing_cluster_member_node_to_seed_from>
wait [<pid_file>] [--pid|-P <pid>] [-t <timeout>]
<timeout> - operation timeout in seconds. Default is "infinity".
<queueinfoitem> must be a member of the list [name, durable, auto_delete, arguments, policy, pid, owner_pid, exclusive, exclusive_consumer_pid, exclusive_consumer_tag, messages_ready, messages_unacknowledged, messages, messages_ready_ram, messages_unacknowledged_ram, messages_ram, messages_persistent, message_bytes, message_bytes_ready, message_bytes_unacknowledged, message_bytes_ram, message_bytes_persistent, head_message_timestamp, disk_reads, disk_writes, consumers, consumer_utilisation, memory, slave_pids, synchronised_slave_pids, state].
<bindinginfoitem> must be a member of the list [source_name, source_kind, destination_name, destination_kind, routing_key, arguments].
<connectioninfoitem> must be a member of the list [pid, name, port, host, peer_port, peer_host, ssl, ssl_protocol, ssl_key_exchange, ssl_cipher, ssl_hash, peer_cert_subject, peer_cert_issuer, peer_cert_validity, state, channels, protocol, auth_mechanism, user, vhost, timeout, frame_max, channel_max, client_properties, recv_oct, recv_cnt, send_oct, send_cnt, send_pend, connected_at].
<channelinfoitem> must be a member of the list [pid, connection, name, number, user, vhost, transactional, confirm, consumer_count, messages_unacknowledged, messages_uncommitted, acks_uncommitted, messages_unconfirmed, prefetch_count, global_prefetch_count].
<exchangeinfoitem> must be a member of the list [name, type, durable, auto_delete, internal, arguments, policy].
<vhostinfoitem> must be a member of the list [name, tracing, cluster_state].
<consumerinfoitem> must be a member of the list [queue_name, channel_pid, consumer_tag, ack_required, prefetch_count, arguments].
<unresponsive_queueinfoitem> must be a member of the list [name, durable, auto_delete, arguments, pid, recoverable_slaves].
1、查看队列信息
#查看当前Erlang节点下的所有队列信息
#显示的列表格式为: queuenName messageCount
[root@bogon /]# rabbitmqctl list_queues
Timeout: 60.0 seconds ...
Listing queues for vhost / ...
fanout.A 0
topic.message 0
hello 0
fanout.C 0
direct.course 0
direct.teacher 0
user 0
queueOne 1000
direct.student 0
topic.messages 0
fanout.B 0
#查看指定vhost下的所有队列信息 -p 后面的参数为vhost 名称
[root@bogon /]# rabbitmqctl list_queues -p /
Timeout: 60.0 seconds ...
Listing queues for vhost / ...
fanout.A 0
topic.message 0
hello 0
fanout.C 0
direct.course 0
direct.teacher 0
user 0
queueOne 1000
direct.student 0
topic.messages 0
fanout.B 0
[root@bogon /]#
#查看队列的更多信息 队列的名称(name) 是否可持久化(durable) 是否完成后自动删除(auto_delete) arguments
[root@bogon /]# rabbitmqctl list_queues name durable auto_delete arguments
Timeout: 60.0 seconds ...
Listing queues for vhost / ...
fanout.A true false []
topic.message true false []
hello true false []
fanout.C true false []
direct.course true false []
direct.teacher true false []
user true false []
queueOne false false []
direct.student true false []
topic.messages true false []
fanout.B true false []
2、查看交换器信息
#查看所有的交换器信息 简单格式 交换器name 交换器类型type
[root@bogon /]# rabbitmqctl list_exchanges
Listing exchanges for vhost / ...
amq.match headers
amq.direct direct
amq.headers headers
amq.rabbitmq.trace topic
amq.fanout fanout
directExchange direct
direct
topicExchange topic
exchangeOne direct
amq.topic topic
fanoutExchange fanout
#查看指定vhost下的交换器信息
[root@bogon /]# rabbitmqctl list_exchanges -p /
Listing exchanges for vhost / ...
amq.match headers
amq.direct direct
amq.headers headers
amq.rabbitmq.trace topic
amq.fanout fanout
directExchange direct
direct
topicExchange topic
exchangeOne direct
amq.topic topic
fanoutExchange fanout
#查看更多的交换器信息属性 交换器name 交换器type 是否持久化durable 自动删除auto_delete
[root@bogon /]# rabbitmqctl list_exchanges -p / name, type, durable, auto_delete
Listing exchanges for vhost / ...
amq.match headers true false
amq.direct direct true false
amq.headers headers true false
amq.rabbitmq.trace topic true
amq.fanout fanout true false
directExchange direct true false
direct true false false
topicExchange topic true false
exchangeOne direct false false
amq.topic topic true false
fanoutExchange fanout true false
3、查看绑定信息
#查看所有的并绑定信息
#具体的信息格式为 分成两个部分
# 第一部分将所有的交换器信息都显示出来(所有的)
#第一部分比较混乱 主要介绍第二部分 交换器名称 类型 绑定的队列名称 队列 路由键 参数信息
[root@bogon /]# rabbitmqctl list_bindings -p /
Listing bindings for vhost /...
exchange direct.course queue direct.course []
exchange direct.student queue direct.student []
exchange direct.teacher queue direct.teacher []
exchange fanout.A queue fanout.A []
exchange fanout.B queue fanout.B []
exchange fanout.C queue fanout.C []
exchange hello queue hello []
exchange queueOne queue queueOne []
exchange topic.message queue topic.message []
exchange topic.messages queue topic.messages []
exchange user queue user []
directExchange exchange direct.course queue direct.course []
directExchange exchange direct.student queue direct.student []
directExchange exchange direct.teacher queue direct.teacher []
exchangeOne exchange queueOne queue queueOne []
fanoutExchange exchange fanout.A queue []
fanoutExchange exchange fanout.B queue []
fanoutExchange exchange fanout.C queue []
topicExchange exchange topic.message queue topic.* []
topicExchange exchange topic.message queue topic.message []
五、RabbitMQ的日志信息
1、查看rabbitMQ运行过程中的日志信息 liunx下默认在var/log/rabbitmq 下
2、AMOP中提供了一个amq.rabbitmq.log的topic交换器它根据不同的日志级别路由发送消息
,我们可以声明对应的队列来接受消息 并进行保存,从而便于查看和调试。