SpringBoot基于过滤器实现接口请求耗时统计

版权申明:本文为原创文章,转载请注明原文出处

原文链接:https://blog.it-follower.com/posts/2142349990.html

Spring Boot中实现一个过滤器相当简单,实现javax.servlet.Filter接口即可。下面以实现一个记录接口访问日志及请求耗时的过滤器为例:

定义ApiAccessFilter类,并实现Filter接口

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
@Slf4j
@WebFilter(filterName = "ApiAccessFilter", urlPatterns = "/*")
public class ApiAccessFilter implements Filter {


@Override
public void init(FilterConfig filterConfig) {

}

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
FilterChain filterChain) throws IOException, ServletException {

HttpServletRequest request = (HttpServletRequest) servletRequest;

Long requestId = IdGenUtils.nextIdByMem(); // 请求ID,这个是我业务中的id,大家可自行决定是否需要
long start = System.currentTimeMillis(); // 请求进入时间

log.info("[Api Access] start. id: {}, uri: {}, method: {}, client: {}", requestId,
request.getRequestURI(), request.getMethod(), getIP(request));
filterChain.doFilter(servletRequest, servletResponse);
log.info("[Api Access] end. id: {}, duration: {}ms", requestId,
System.currentTimeMillis() - start);

}

@Override
public void destroy() {

}

/**
* 获取IP地址
*
* @param request 请求
* @return request发起客户端的IP地址
*/
private String getIP(HttpServletRequest request) {
if (request == null) {
return "0.0.0.0";
}

String Xip = request.getHeader("X-Real-IP");
String XFor = request.getHeader("X-Forwarded-For");

String UNKNOWN_IP = "unknown";
if (StringUtils.isNotEmpty(XFor) && !UNKNOWN_IP.equalsIgnoreCase(XFor)) {
//多次反向代理后会有多个ip值,第一个ip才是真实ip
int index = XFor.indexOf(",");
if (index != -1) {
return XFor.substring(0, index);
} else {
return XFor;
}
}

XFor = Xip;
if (StringUtils.isNotEmpty(XFor) && !UNKNOWN_IP.equalsIgnoreCase(XFor)) {
return XFor;
}

if (StringUtils.isBlank(XFor) || UNKNOWN_IP.equalsIgnoreCase(XFor)) {
XFor = request.getHeader("Proxy-Client-IP");
}
if (StringUtils.isBlank(XFor) || UNKNOWN_IP.equalsIgnoreCase(XFor)) {
XFor = request.getHeader("WL-Proxy-Client-IP");
}
if (StringUtils.isBlank(XFor) || UNKNOWN_IP.equalsIgnoreCase(XFor)) {
XFor = request.getHeader("HTTP_CLIENT_IP");
}
if (StringUtils.isBlank(XFor) || UNKNOWN_IP.equalsIgnoreCase(XFor)) {
XFor = request.getHeader("HTTP_X_FORWARDED_FOR");
}
if (StringUtils.isBlank(XFor) || UNKNOWN_IP.equalsIgnoreCase(XFor)) {
XFor = request.getRemoteAddr();
}
return XFor;
}
}

启用该过滤器

SpringBoot中两种启用过滤器的方式,第一种在FilterRegistrationBean中注册该Filter,第二种,采用注解的方式启用。个人觉得注册方式比较麻烦,所有本例中使用的是第二种。在Filter中添加注解@WebFilter(filterName = “ApiAccessFilter”, urlPatterns = “/*”),配置了过滤器名和需要过滤的请求地址,/*表示过滤所有请求。然后在启动类上,通过@ServletComponentScan注解,指明该过滤器即可

1
2
3
4
5
6
7
8
@SpringBootApplication
@ServletComponentScan("com.yclouds.service.demo")
public class YCloudsServiceDemoApplication {

public static void main(String[] args) {
SpringApplication.run(YCloudsServiceDemoApplication.class, args);
}
}

效果展示

评论