gpt4 book ai didi

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

转载 作者:qq735679552 更新时间:2022-09-28 22:32:09 27 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章Spring MVC Interceptor 实现性能监控的功能代码由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

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

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

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

Interceptor 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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没啥神器的,源码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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,注册即可.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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/");
   }
 
}

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

原文链接:http://www.school1024.com/spring-framework/spring-mvc-interceptor-%E5%AE%9E%E7%8E%B0%E6%80%A7%E8%83%BD%E7%9B%91%E6%8E%A7.html?utm_source=tuicool&utm_medium=referral 。

最后此篇关于Spring MVC Interceptor 实现性能监控的功能代码的文章就讲到这里了,如果你想了解更多关于Spring MVC Interceptor 实现性能监控的功能代码的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com