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

Spring MVC Interceptor 实现性能监控的功能代码

程序员文章站 2024-02-25 08:02:16
利用spring mvc 的 interceptor 实现个简易的性能监控,计算一下每个url的执行时间。 在 prehandle 方法中记录当前的时间戳到线程局部变量里...

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

在 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<long> starttimethreadlocal = new namedthreadlocal<long>("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<t> extends threadlocal<t> {
 
  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/");
  }
 
}

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