zoukankan      html  css  js  c++  java
  • RabbitMQ学习之队列监控

    对于RabbitMQ的监控,除了服务器基本信息(硬盘、CPU、内存、IO等)以及MQ的进程和端口,我们也可以通过请求url访问管理API监控其集群和队列的情况。在Java api 3.6.0以后,channel接口为我们提供了如下接口:

    /**
     * Returns the number of messages in a queue ready to be delivered
     * to consumers. This method assumes the queue exists. If it doesn't,
     * an exception will be closed with an exception.
     * @param queue the name of the queue
     * @return the number of messages in ready state
     * @throws IOException Problem transmitting method.
     */
        long messageCount(String queue) throws IOException;
    
      /**
       * Returns the number of consumers on a queue.
       * This method assumes the queue exists. If it doesn't,
       * an exception will be closed with an exception.
       * @param queue the name of the queue
       * @return the number of consumers
       * @throws IOException Problem transmitting method.
       */
        long consumerCount(String queue) throws IOException;

    messageCount:查询队列未消费的消息数,可以监控消息堆积的情况。 
    consumerCount:队列的消费者个数,可以对消费者进行监控 
    Java实例:

    public class JavaAPIMonitor {
    
        private static String queue_name = "test_queue";
    
        public static void main(String[] args) throws Exception {
            ConnectionFactory factory = new ConnectionFactory();
            // amqp://userName:password@hostName:portNumber/virtualHost
            factory.setUri("amqp://test:test@10.1.199.169:5672/test");
            Connection conn = factory.newConnection();
            // System.out.println(conn.getChannelMax());
            Channel channel = conn.createChannel();
            // System.out.println(channel.getChannelNumber());
            System.out.println(channel.messageCount(queue_name));
            System.out.println(channel.consumerCount(queue_name));
            channel.close();
            conn.close();
        }
    }
    运行结果:
    277782
    3

    除此之外,也可以通过连接的状态变化,进行监控,如:连接被阻塞(内存,CPU等不足)

    ConnectionFactory factory = new ConnectionFactory();
            Connection connection = factory.newConnection();
            connection.addBlockedListener(new BlockedListener() {
    
                public void handleUnblocked() throws IOException {
                    // 连接取消阻塞
                }
                public void handleBlocked(String reason) throws IOException {
                    // 连接阻塞
                }
            });
            connection.addShutdownListener(new ShutdownListener() {
                public void shutdownCompleted(ShutdownSignalException cause) {
                    //连接关闭
                }
            });

    先关监控参考文章: 
    1.RabbitMQ之管理与监控 
    2.REST API监控RabbitMQ 
    3.如何监控RabbitMQ 
    4.使用AMQP模拟检测来确认RabbitMQ是否运行 
    5.监控队列状态

  • 相关阅读:
    sql2slack alash3al 开源的又个轻量级工具
    pgspider fetchq 扩展docker镜像
    godns 集成coredns 的demo
    godns 简单dnsmasq 的dns 替换方案
    aviary.sh 一个基于bash的分布式配置管理工具
    使用coredns 的template plugin实现一个xip 服务
    nginx 代理 coredns dns 服务
    基于nginx proxy dns server
    几个不错的geodns server
    spring boot rest api 最好添加servlet.context-path
  • 原文地址:https://www.cnblogs.com/telwanggs/p/7124883.html
Copyright © 2011-2022 走看看