重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

SpringMVCInterceptor实现性能监控的功能代码

利用Spring MVC 的 Interceptor 实现个简易的性能监控,计算一下每个url的执行时间。

十多年的巴林右旗网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。全网营销推广的优势是能够根据用户设备显示端的尺寸不同,自动调整巴林右旗建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联从事“巴林右旗网站设计”,“巴林右旗网站推广”以来,每个客户项目都认真落实执行。

在 preHandle 方法中记录当前的时间戳到线程局部变量里,然后在afterCompletion方法中,用当前的时间戳剪掉线程局部变量里的时间戳得出个运行时间,并记录到log里。

这只是个简易的性能监控,如果想长期正式的监控性能,请选择其它成熟的产品。

Interceptor

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.NamedThreadLocal;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
 
 
public class PerformanceInterceptor implements HandlerInterceptor {
 
  private final static Logger LOGGER = LoggerFactory.getLogger(PerformanceInterceptor.class);
 
  private NamedThreadLocal startTimeThreadLocal = new NamedThreadLocal("performance");
 
  @Override
  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
      throws Exception {
    startTimeThreadLocal.set(System.currentTimeMillis());
    return true;
  }
 
  @Override
  public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
      ModelAndView modelAndView) throws Exception {
    return;
  }
 
  @Override
  public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
      throws Exception {
    long currentTime = System.currentTimeMillis();
    long executeTime = currentTime - startTimeThreadLocal.get();
    LOGGER.info("uri:{} 执行了 {} 毫秒", request.getRequestURI(), executeTime);
  }
 
}

这里的是NamedThreadLocal没啥神器的,源码如下:

package org.springframework.core;
 
import org.springframework.util.Assert;
 
public class NamedThreadLocal extends ThreadLocal {
 
  private final String name;
 
  public NamedThreadLocal(String name) {
    Assert.hasText(name, "Name must not be empty");
    this.name = name;
  }
 
  @Override
  public String toString() {
    return this.name;
  }
 
}

定义完Interceptor,注册即可。

import java.util.List;
 
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.web.filter.OrderedCharacterEncodingFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
 
import com.vnierlai.scm.webapp.interceptor.PerformanceInterceptor;
 
@Configuration
public class WebMvcConfigurer extends WebMvcConfigurerAdapter {
 
  @Bean
  @ConditionalOnMissingBean(CharacterEncodingFilter.class)
  public CharacterEncodingFilter characterEncodingFilter() {
    CharacterEncodingFilter filter = new OrderedCharacterEncodingFilter();
    filter.setEncoding("UTF-8");
    return filter;
  }
 
  @Override
  public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(new PerformanceInterceptor()).addPathPatterns("/**");
    super.addInterceptors(registry);
  }
 
  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    // registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
  }
 
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。


当前标题:SpringMVCInterceptor实现性能监控的功能代码
地址分享:http://cqcxhl.cn/article/ihgsdp.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP