zoukankan      html  css  js  c++  java
  • java 应用基于mongo driver监控简单说明

    mongo java 版本的driver提供了比较多的metrics,我们之需要实现对应的Listener就可以方便的监控mongo 应用了,以下是一些简单的说明

    直接使用java driver 模式

    public class TestCommandListener implements CommandListener {                        
        @Override                                                                                                        
        public void commandStarted(final CommandStartedEvent event) {                                                    
            System.out.println(String.format("Sent command '%s:%s' with id %s to database '%s' "                         
                                             + "on connection '%s' to server '%s'",                                      
                                             event.getCommandName(),                                                     
                                             event.getCommand().get(event.getCommandName()),                             
                                             event.getRequestId(),                                                       
                                             event.getDatabaseName(),                                                    
                                             event.getConnectionDescription()                                            
                                                  .getConnectionId(),                                                    
                                             event.getConnectionDescription().getServerAddress()));                      
        }                                                                                                                
        @Override                                                                                                        
        public void commandSucceeded(final CommandSucceededEvent event) {                                                
            System.out.println(String.format("Successfully executed command '%s' with id %s "                            
                                             + "on connection '%s' to server '%s'",                                      
                                             event.getCommandName(),                                                     
                                             event.getRequestId(),                                                       
                                             event.getConnectionDescription()                                            
                                                  .getConnectionId(),                                                    
                                             event.getConnectionDescription().getServerAddress()));                      
        }                                                                                                                
        @Override                                                                                                        
        public void commandFailed(final CommandFailedEvent event) {                                                      
            System.out.println(String.format("Failed execution of command '%s' with id %s "                              
                                             + "on connection '%s' to server '%s' with exception '%s'",                  
                                             event.getCommandName(),                                                     
                                             event.getRequestId(),                                                       
                                             event.getConnectionDescription()                                            
                                                  .getConnectionId(),                                                    
                                             event.getConnectionDescription().getServerAddress(),                        
                                             event.getThrowable()));                                                     
        }                                                                                                                
    }   
    MongoClientSettings settings = MongoClientSettings.builder()
            .addCommandListener(new TestCommandListener())
            .build();
    MongoClient client = MongoClients.create(settings);

    spring boot 集成

    核心是基于micrometer包装的一些mongodb提供的metrics,我们需要自定义MongoClientFactoryBean进行相关
    Listener 暴露metrics 的注册

     
    package com.example.demo;
    import com.mongodb.ConnectionString;
    import com.mongodb.MongoClientSettings;
    import io.micrometer.core.instrument.MeterRegistry;
    import io.micrometer.core.instrument.binder.mongodb.MongoMetricsCommandListener;
    import io.micrometer.core.instrument.binder.mongodb.MongoMetricsConnectionPoolListener;
    import org.springframework.boot.autoconfigure.mongo.MongoProperties;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.mongodb.core.MongoClientFactoryBean;
    @Configuration
    public class MongoConfiguration {
        @Bean
        public MongoClientFactoryBean mongoClientFactoryBean(MongoProperties properties, MeterRegistry meterRegistry) {
            MongoClientFactoryBean mongoClientFactoryBean = new MongoClientFactoryBean();
            mongoClientFactoryBean.setConnectionString(new ConnectionString(properties.getUri()));
            MongoClientSettings settings = MongoClientSettings.builder()
                    .addCommandListener(new MongoMetricsCommandListener(meterRegistry))
                    .applyToConnectionPoolSettings(builder ->
                            builder.addConnectionPoolListener(new MongoMetricsConnectionPoolListener(meterRegistry)))
                    .build();
            mongoClientFactoryBean.setMongoClientSettings(settings);
            return mongoClientFactoryBean;
        }
    }

    说明

    对于spring 以及spring boot 应用推荐集成micrometer进行应用的监控,灵活高效而且支持多种监控后端,是一把利器,集成prometheus
    以及grafana 我们可以比较方便的观测系统的健康情况

    参考资料

    https://mongodb.github.io/mongo-java-driver/3.8/driver/reference/monitoring/

  • 相关阅读:
    集训笔记——dp继续
    集训笔记——各种dp(dp杂谈)
    集训笔记——dp
    洛谷P3197 [HNOI2008]越狱 题解
    集训笔记——杂题选讲(图论,dp)
    集训笔记——杂题选讲(带数学推导的递推、递归和dp,卡特兰数)
    滑动窗口+二分--P3957 跳房子
    差分+二分答案--P1083 借教室
    逆序对--P1966 火柴排队
    数位dp--P2657 [SCOI2009] windy 数
  • 原文地址:https://www.cnblogs.com/rongfengliang/p/13755841.html
Copyright © 2011-2022 走看看