欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

mongo-java应用监控

程序员文章站 2024-03-13 20:54:22
...

近期项目中需要监控mongo驱动的线程池的信息。
类似mysql中有类型Druid 可以监控数据库的线程池

参考mongo-driver驱动的文档:

http://mongodb.github.io/mongo-java-driver/3.0/driver/reference/management/logging/

http://mongodb.github.io/mongo-java-driver/3.0/driver/reference/management/monitoring/

文档上说明 有通过日志监控 和通过jmx的mbean进行监控
我们这里采用mbean监控

Monitoring
The driver uses JMX to create MXBeans that allow an application or end user to monitor various aspects of the driver.

The driver creates MXBean instances of a single type: ConnectionPoolStatisticsMBean. The driver registers one ConnectionPoolStatisticsMBean instance per each server it connects to. For example, in the case of a replica set, the driver creates an instance per each non-hidden member of the replica set.

Each MXBean instance is required to be registered with a unique object name, which consists of a domain and a set of named properties. All MXBean instances created by the driver are under the domain "org.mongodb.driver". Instances of ConnectionPoolStatisticsMBean will have the following properties:

clusterId: a client-generated unique identifier, required to ensure object name uniqueness in situations where an application has multiple MongoClient instances connected to the same MongoDB server deployment
host: the host name of the server
port: the port on which the server is listening
minSize: the minimum allowed size of the pool, including idle and in-use members
maxSize: the maximum allowed size of the pool, including idle and in-use members
size: the current size of the pool, including idle and and in-use members
waitQueueSize: the current size of the wait queue for a connection from this pool
checkedOutCount: the current count of connections that are currently in use

谷歌上对应的翻译

监控
驱动程序使用JMX创建允许应用程序或最终用户监视驱动程序各个方面的MXBean。

驱动程序创建单个类型的MXBean实例:ConnectionPoolStatisticsMBean。驱动程序为每个连接的服务器注册一个ConnectionPoolStatisticsMBean实例。例如,在副本集的情况下,驱动程序为副本集合中的每个非隐藏成员创建一个实例。

每个MXBean实例都需要注册一个唯一的对象名称,它由一个域和一组命名的属性组成。由驱动程序创建的所有MXBean实例都位于域“org.mongodb.driver”下。 ConnectionPoolStatisticsMBean的实例将具有以下属性:

clusterId:客户端生成的唯一标识符,用于在应用程序将多个MongoClient实例连接到相同的MongoDB服务器部署的情况下确保对象名称唯一性
主机:服务器的主机名
port:服务器正在侦听的端口
minSize:池的最小允许大小,包括空闲和使用中的成员
maxSize:池的最大允许大小,包括空闲和正在使用的成员
size:池的当前大小,包括空闲和正在使用的成员
waitQueueSize:此池连接的等待队列的当前大小
checkedOutCount:当前正在使用的连接的当前计数

这里可以需要运用到jconsole 和jmx的知识。

首先创建一个监控日志的核心代码
LogTask.java

package com.test.task;

import org.apache.log4j.Logger;

import javax.management.AttributeList;
import javax.management.MBeanServer;
import javax.management.ObjectInstance;
import javax.management.ObjectName;
import java.lang.management.ManagementFactory;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class LogTask {

    private int initialDelay = 3;

    private int period = 3;

    private Logger logger = Logger.getLogger(LogTask.class);

    private MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();

    public void init() {
        logger.info("初始化mongo线程池监控日志");
        ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(3);
        scheduledThreadPool.scheduleAtFixedRate(new Runnable() {

            public void run() {
                try {

                    Set<ObjectInstance> set = mbeanServer.queryMBeans(new ObjectName("org.mongodb.driver:type=ConnectionPool,*"), null);
                    for (ObjectInstance oi : set) {
                        String className = oi.getClassName();
                        if (className.contains("mongo")) {
                            ObjectName objectName = oi.getObjectName();
                            logger.info(objectName);
                            String[] attrs = new String[]{"CheckedOutCount", "Host", "Port",
                                    "MinSize", "MaxSize", "Size", "WaitQueueSize"};
                            for (String attr : attrs) {
                                logger.info(attr + "==" + mbeanServer.getAttribute(objectName, attr));
                            }
                        }

                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    logger.error("执行日志打印失败", e);
                }

            }
        }, initialDelay, period, TimeUnit.SECONDS);

    }


    public int getInitialDelay() {
        return initialDelay;
    }

    public void setInitialDelay(int initialDelay) {
        this.initialDelay = initialDelay;
    }

    public int getPeriod() {
        return period;
    }

    public void setPeriod(int period) {
        this.period = period;
    }
}

这里参考springMVC的代码
http://download.csdn.net/detail/u010050904/9899194