您好,登錄后才能下訂單哦!
利用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/"); } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。