基于javamelody监控springboot项目过程详解

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

基于javamelody监控springboot项目过程详解

  2021-04-02 我要评论

JavaMelody是用来在QA和实际运行生产环境中监控Java或Java EE应用程序服务器的一个开源框架。它不是一个工具来模拟来自用户的请求,而是一个测量和计算用户在实际操作中应用程序的使用情况的工具,并以图表的形式显示,图表可以按天,周,月,年或自定义时间段查看。

JavaMelody基础的监控包括Java内存和Java CPU使用情况,用户Session数量,JDBC连接数,和http请求、sql请求、jsp页面与业务接口方法(EJB3、Spring、 Guice)的执行数量,平均执行时间,错误百分比等。如果要监控Jenkins,JIRA,Sonar等等一些,需要另外安装对应的插件,还有一些高级文档用于高级配置。此文仅以JavaMelody v1.63.0版本演示基础功能的集成及使用,更多功能请深入研究官方文档。

1. 相关链接

官方文档  https://github.com/javamelody/javamelody/wiki/UserGuide

下载地址 https://github.com/javamelody/javamelody/releases

2. 基础集成

1.pom中加入

<!-- https://mvnrepository.com/artifact/net.bull.javamelody/javamelody-core -->
    <dependency>
      <groupId>net.bull.javamelody</groupId>
      <artifactId>javamelody-core</artifactId>
      <version>1.79.0</version>
    </dependency>

2.springboot启动文件中加入

public class App {
  public static void main(String[] args) {
    SpringApplication.run(App.class, args);
  }
 
  /**
   * 配置javamelody监控 spring boot 会按照order值的大小,从小到大的顺序来依次过滤
   */
  @Bean
  @Order(Integer.MAX_VALUE - 1)
  public FilterRegistrationBean monitoringFilter() {
    FilterRegistrationBean registration = new FilterRegistrationBean();
    registration.setFilter(new MonitoringFilter());
    registration.addUrlPatterns("/*");
    registration.setName("monitoring");
    return registration;
  }
 
  /**
   * 配置javamelody监听器sessionListener
   */
  @Bean
  public ServletListenerRegistrationBean<SessionListener> servletListenerRegistrationBean() {
    ServletListenerRegistrationBean<SessionListener> slrBean = new ServletListenerRegistrationBean<SessionListener>();
    slrBean.setListener(new SessionListener());
    return slrBean;
  }
}

3,现在可以部署程序启动服务器,在浏览器打开http://<host>/<context>/monitoring。<host>:为你的主机名+端口地址,<context>:为你的应用配置地址。应该可以看到如下界面:


3. Spring方法级监控

前提是使用monitoring-spring.xml文件(不做修改),然后在需要监控的方法上使用@MonitoredWithSpring注解即可。

您可能感兴趣的文章:

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们