5.1 为什么需要消息追踪
消息中心的消息追踪需要Trace实现,Trace是RabbitMQ用于记录每一次发送的消息,方便使用mq的开发者进行开发、调试和排查错误。可通过插件的形式提供可视化界面。Trace启用后会自动创建系统Exchange:amq.rabbitmq.trace,每个队列会自动绑定该Exchange,绑定后发送到队列的消息都会被记录到Trace日志中。
5.2 MQ消息追踪启用与查看
如下所示,RabbitMQ中启用Trace的相关命令和使用说明:
命令 |
描述 |
rabbitmq-plugins list |
查看插件列表 |
rabbitmq-plugins enable rabbitmq_tracing |
RabbitMQ启用trace插件 |
rabbitmqctl trace_on |
打开trace开关 |
rabbitmqctl trace_on -p / |
打开需要日志追踪的vhost |
rabbitmqctl trace_off |
关闭trace 的开关 |
rabbitmq-plugins disable rabbitmq_tracing |
RabbitMQ关闭Trace插件 |
rabbitmqctl set_user_tags admin_ha administrator |
设置只有管理员的角色才能查看日志界面 |
5.2.1 查看插件列表
执行:rabbitmq-plugins list,如下图所示:
[localhost ~]# rabbitmq-plugins listListing plugins with pattern ".*" ... Configured: E = explicitly enabled; e = implicitly enabled | Status: * = running on rabbit@CRM-RabbitMQ-T01 |/[ ] rabbitmq_amqp1_0 3.8.5[ ] rabbitmq_auth_backend_cache 3.8.5[ ] rabbitmq_auth_backend_http 3.8.5[ ] rabbitmq_auth_backend_ldap 3.8.5[ ] rabbitmq_auth_backend_oauth2 3.8.5[ ] rabbitmq_auth_mechanism_ssl 3.8.5[ ] rabbitmq_consistent_hash_exchange 3.8.5[ ] rabbitmq_event_exchange 3.8.5[ ] rabbitmq_federation 3.8.5[ ] rabbitmq_federation_management 3.8.5[ ] rabbitmq_jms_topic_exchange 3.8.5[E*] rabbitmq_management 3.8.5[e*] rabbitmq_management_agent 3.8.5[ ] rabbitmq_mqtt 3.8.5[ ] rabbitmq_peer_discovery_aws 3.8.5[ ] rabbitmq_peer_discovery_common 3.8.5[ ] rabbitmq_peer_discovery_consul 3.8.5[ ] rabbitmq_peer_discovery_etcd 3.8.5[ ] rabbitmq_peer_discovery_k8s 3.8.5[ ] rabbitmq_prometheus 3.8.5[ ] rabbitmq_random_exchange 3.8.5[ ] rabbitmq_recent_history_exchange 3.8.5[ ] rabbitmq_sharding 3.8.5[ ] rabbitmq_shovel 3.8.5[ ] rabbitmq_shovel_management 3.8.5[ ] rabbitmq_stomp 3.8.5[ ] rabbitmq_top 3.8.5[ ] rabbitmq_tracing 3.8.5[ ] rabbitmq_trust_store 3.8.5[e*] rabbitmq_web_dispatch 3.8.5[ ] rabbitmq_web_mqtt 3.8.5[ ] rabbitmq_web_mqtt_examples 3.8.5[ ] rabbitmq_web_stomp 3.8.5[ ] rabbitmq_web_stomp_examples 3.8.5
上述为RabbitMQ所有的插件信息,前面带有[e*]标识已经启用的插件。
5.2.2 启用Trace插件
执行:rabbitmq-plugins enable rabbitmq_tracing,如下图所示:
[localhost ~]# rabbitmq-plugins enable rabbitmq_tracingEnabling plugins on node rabbit@localhost:rabbitmq_tracingThe following plugins have been configured: rabbitmq_management rabbitmq_management_agent rabbitmq_tracing rabbitmq_web_dispatchApplying plugin configuration to rabbit@localhost...The following plugins have been enabled: rabbitmq_tracingstarted 1 plugins.
rabbitmq_tracing 已经启动。
5.2.3 打开Trace开关
执行:rabbitmqctl trace_on,如下图所示:
[localhost ~]# rabbitmqctl trace_onStarting tracing for vhost "/" ...Trace enabled for vhost /
5.2.4 添加tracing日志文件
5.2.5 编写发送测试消息方法
/** * * 功能描述: trace 日志追踪 */@Testpublic void traceLogTest() { rabbitTemplate.convertAndSend("test-queue", "发送test-queue测试日志消息!");}
5.2.5 测试
执行测试方法后,打开日志文件: