国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術(shù)文章
文章詳情頁

springboot基于過濾器實(shí)現(xiàn)接口請求耗時(shí)統(tǒng)計(jì)操作

瀏覽:15日期:2023-04-21 13:24:52

Spring Boot中實(shí)現(xiàn)一個(gè)過濾器相當(dāng)簡單,實(shí)現(xiàn)javax.servlet.Filter接口即可。

下面以實(shí)現(xiàn)一個(gè)記錄接口訪問日志及請求耗時(shí)的過濾器為例:

1、定義ApiAccessFilter類,并實(shí)現(xiàn)Filter接口

@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,這個(gè)是我業(yè)務(wù)中的id,大家可自行決定是否需要 long start = System.currentTimeMillis(); // 請求進(jìn)入時(shí)間 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發(fā)起客戶端的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)) { //多次反向代理后會有多個(gè)ip值,第一個(gè)ip才是真實(shí)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; }}

2、啟用該過濾器

springboot中兩種啟用過濾器的方式,第一種在FilterRegistrationBean中注冊該Filter,第二種,采用注解的方式啟用

個(gè)人覺得注冊方式比較麻煩,所有本例中使用的是第二種。

在Filter中添加注解@WebFilter(filterName = 'ApiAccessFilter', urlPatterns = '/*'),配置了過濾器名和需要過濾的請求地址,/*表示過濾所有請求。然后在啟動(dòng)類上,通過@ServletComponentScan注解,指明該過濾器即可

@SpringBootApplication@ServletComponentScan('com.yclouds.service.demo')public class YCloudsServiceDemoApplication { public static void main(String[] args) { SpringApplication.run(YCloudsServiceDemoApplication.class, args); }}

3、效果展示

springboot基于過濾器實(shí)現(xiàn)接口請求耗時(shí)統(tǒng)計(jì)操作

發(fā)送了兩次http請求,每次都會打印日志內(nèi)容,并且統(tǒng)計(jì)了整個(gè)請求的耗時(shí)情況。

GitHub地址:https://github.com/ye17186/spring-boot-learn

補(bǔ)充知識:spring cloud gateway 之zuul通過filter配置接口請求的時(shí)間耗時(shí)記錄到日志

zuul中的Filter的配置,zuul中提供了三種類型的Filter,preFilter,routeFilter和postFilter,分別對應(yīng)請求中的不同的階段,針對同一個(gè)請求,有一個(gè)RequestContext對象,在三個(gè)階段的Filter中進(jìn)行共享

springboot基于過濾器實(shí)現(xiàn)接口請求耗時(shí)統(tǒng)計(jì)操作

假設(shè)我們要開發(fā)一個(gè)統(tǒng)計(jì)請求時(shí)間的功能,需要在preFilter里邊記錄開始時(shí)間,并將整個(gè)開始時(shí)間放在RequestContext中,在postFilter里邊拿到開始時(shí)間,用當(dāng)前的時(shí)間減去開始時(shí)間,就是請求執(zhí)行的時(shí)間

定義一個(gè)preFilter:

package com.jiaoyiping.springcloud.zuul.filter; import com.netflix.zuul.ZuulFilter;import com.netflix.zuul.context.RequestContext;import com.netflix.zuul.exception.ZuulException;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants; /** * Created with Intellij IDEA * * @author: * Mail: * Date: * Time: * To change this template use File | Settings | Editor | File and Code Templates */ public class TimeCostPreFilter extends ZuulFilter { public static final String START_TIME_KEY = 'start_time'; private Logger logger = LoggerFactory.getLogger(TimeCostPreFilter.class); @Override public String filterType() { return FilterConstants.PRE_TYPE; } @Override public int filterOrder() { return 0; } /** * 判斷是否要攔截的邏輯 * * @return */ @Override public boolean shouldFilter() { return true; } @Override public Object run() throws ZuulException { long startTime = System.currentTimeMillis(); RequestContext.getCurrentContext().set(START_TIME_KEY, startTime); return null; }}

定義以postFilter:

package com.jiaoyiping.springcloud.zuul.filter; import com.netflix.zuul.ZuulFilter;import com.netflix.zuul.context.RequestContext;import com.netflix.zuul.exception.ZuulException;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants; /** * Created with Intellij IDEA * * @author: * Mail: * Date: * Time: * To change this template use File | Settings | Editor | File and Code Templates */ public class TimeCostPostFilter extends ZuulFilter { private static final String START_TIME_KE = 'start_time'; private Logger logger = LoggerFactory.getLogger(TimeCostPostFilter.class); @Override public String filterType() { return FilterConstants.POST_TYPE; } @Override public int filterOrder() { return 0; } @Override public boolean shouldFilter() { return true; } @Override public Object run() throws ZuulException { long startTime = (long) RequestContext.getCurrentContext().get(START_TIME_KE); logger.info('請求完成,耗時(shí){}秒', (System.currentTimeMillis() - startTime) / 1000); return null; }}

在一個(gè)配置類中將這兩個(gè)Filter注入:

package com.jiaoyiping.springcloud.zuul.config; import com.jiaoyiping.springcloud.zuul.filter.PDSFilter;import com.jiaoyiping.springcloud.zuul.filter.TimeCostPostFilter;import com.jiaoyiping.springcloud.zuul.filter.TimeCostPreFilter;import com.netflix.zuul.ZuulFilter;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration; /** * Created with Intellij IDEA * * @author: * Mail: * Date: * Time: * To change this template use File | Settings | Editor | File and Code Templates */@Configurationpublic class FilterConfig { @Bean public ZuulFilter timeCostPreFilter() { return new TimeCostPreFilter(); } @Bean public ZuulFilter timeCostPostFilter() { return new TimeCostPostFilter(); } @Bean public ZuulFilter pdsFilter() { return new PDSFilter(); }}

啟動(dòng)項(xiàng)目,可以發(fā)現(xiàn),zuul網(wǎng)關(guān)已經(jīng)注冊到了eureka上:

springboot基于過濾器實(shí)現(xiàn)接口請求耗時(shí)統(tǒng)計(jì)操作

請求provide對應(yīng)的地址,發(fā)現(xiàn),zuul可以成功地調(diào)用eureka上對應(yīng)的服務(wù),并將結(jié)果正確返回:

springboot基于過濾器實(shí)現(xiàn)接口請求耗時(shí)統(tǒng)計(jì)操作

以上這篇springboot基于過濾器實(shí)現(xiàn)接口請求耗時(shí)統(tǒng)計(jì)操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 国产精品免费_区二区三区观看 | 久久九九视频 | 香蕉香蕉国产片一级一级毛片 | 高清在线一区二区 | 91久久青草精品38国产 | 成人在线视频免费看 | 国产色视频一区二区三区 | 91影视做在线观看免费 | 香港一级特黄高清免费 | 操美女大逼视频 | 久久精品免费全国观看国产 | 成人亲子乱子伦视频 | 亚洲一区二区三区免费在线观看 | 欧美成人精品第一区 | 黄色三级国产 | 成人免费网站 | 91成人爽a毛片一区二区 | 在线观看毛片视频 | 久久精品国产精品亚洲 | 亚洲免费视频在线观看 | 久久久久综合一本久道 | 在线亚州| 久久精品国产精品青草 | 一级特黄aa大片欧美网站 | 91精品视频播放 | 一及黄色| 欧美日韩国产高清一区二区三区 | 久久欧美精品欧美九久欧美 | 国内自拍在线视频高清 | 成人在线网 | 免费在线视频成人 | 黄色毛片子 | 天堂在线视频网站 | 国产成人性色视频 | 一区二区三区免费高清视频 | 亚洲欧美一区二区三区四区 | 午夜影院a级片 | 欧美特级大片 | 国产亚洲精品久久久久91网站 | 国产成人精品高清免费 | 欧美影院网站视频观看 |