当前位置: 首页 > news >正文

做ppt的网站叫什么名字网站推广的一般流程是

做ppt的网站叫什么名字,网站推广的一般流程是,珠海澳门网站建设公司,罗岗网站建设哪家好在实际开发中,了解项目中接口的响应时间是必不可少的事情。SpringBoot 项目支持监听接口的功能也不止一个,接下来我们分别以 AOP、ApplicationListener、Tomcat 三个方面去实现三种不同的监听接口响应时间的操作。 AOP 首先我们在项目中创建一个类 &am…

在实际开发中,了解项目中接口的响应时间是必不可少的事情。SpringBoot 项目支持监听接口的功能也不止一个,接下来我们分别以 AOP、ApplicationListener、Tomcat 三个方面去实现三种不同的监听接口响应时间的操作。

AOP

首先我们在项目中创建一个类 ,比如就叫 WebLogAspect ,然后在该类上加上 @Aspect 和 @Component 注解,声明是一个 Bean 并且是一个切面:

import org.aspectj.lang.JoinPoint;  
import org.aspectj.lang.ProceedingJoinPoint;  
import org.aspectj.lang.annotation.*;  
import org.slf4j.Logger;  
import org.slf4j.LoggerFactory;  
import org.springframework.stereotype.Component;  
import org.springframework.web.context.request.RequestContextHolder;  
import org.springframework.web.context.request.ServletRequestAttributes;  import javax.servlet.http.HttpServletRequest;  
import java.util.Date;  @Aspect  
@Component  
public class WebLogAspect {  private static final Logger logger = LoggerFactory.getLogger(WebLogAspect.class);  // 定义一个切入点,拦截所有带有@RequestMapping注解的方法@Pointcut("@annotation(org.springframework.web.bind.annotation.RequestMapping)")  public void webLog() {}  // 前置通知,在方法执行前记录请求信息  @Before("webLog()")  public void doBefore(JoinPoint joinPoint) {  ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();  HttpServletRequest request = attributes.getRequest();  // 记录请求信息  logger.info("请求开始:URL={}, IP={}, 方法={}", request.getRequestURL(), request.getRemoteAddr(), request.getMethod());  }  // 环绕通知,记录方法执行时间  @Around("webLog()")  public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {  long startTime = System.currentTimeMillis();  Object result = joinPoint.proceed(); // 继续执行被拦截的方法  long endTime = System.currentTimeMillis();  long executeTime = endTime - startTime;  // 记录执行时间  logger.info("请求结束:耗时={}ms", executeTime);  return result;  }  // 异常通知,在方法抛出异常时记录异常信息  @AfterThrowing(pointcut = "webLog()", throwing = "ex")  public void doAfterThrowing(JoinPoint joinPoint, Exception ex) {  ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();  HttpServletRequest request = attributes.getRequest();  // 记录异常信息  logger.error("请求异常:URL={}, 异常={}", request.getRequestURL(), ex.getMessage());  }  // 后置通知(返回通知),在方法正常返回后记录信息  @AfterReturning(returning = "retVal", pointcut = "webLog()")  public void doAfterReturning(JoinPoint joinPoint, Object retVal) {  // 你可以在这里记录返回值,但通常我们不记录,因为可能会包含敏感信息  // logger.info("请求返回:返回值={}", retVal);  }  
}
2024-06-19 17:49:37.373 [TID: N/A] WARN  [com.springboot.demo.TakeTimeCountListener] 请求开始:URL=http://localhost:18080/springboot/test1, IP=0:0:0:0:0:0:0:1, 方法=POST
2024-06-19 17:49:37.386 [TID: N/A] WARN  [com.springboot.demo.TakeTimeCountListener] 请求结束:耗时=13ms
2024-06-19 17:49:37.501 [TID: N/A] WARN  [com.springboot.demo.TakeTimeCountListener] 请求开始:URL=http://localhost:18080/springboot/test2, IP=0:0:0:0:0:0:0:1, 方法=POST
2024-06-19 17:49:37.516 [TID: N/A] WARN  [com.springboot.demo.TakeTimeCountListener] 请求结束:耗时=15ms
2024-06-19 17:49:37.905 [TID: N/A] WARN  [com.springboot.demo.TakeTimeCountListener] 请求开始:URL=http://localhost:18080/springboot/test3, IP=0:0:0:0:0:0:0:1, 方法=POST
2024-06-19 17:49:37.913 [TID: N/A] WARN  [com.springboot.demo.TakeTimeCountListener] 请求结束:耗时=8ms

优点:

  • 全局性:可以在不修改业务代码的情况下,对全局范围内的接口进行执行时间的记录。
  • 灵活性:可以根据需要灵活定义哪些接口需要记录执行时间。
  • 精确性:可以精确记录从方法开始执行到结束的时间。

缺点:

  • 配置复杂性:AOP配置可能相对复杂,特别是对于初学者来说。
  • 性能开销:虽然性能开销通常很小,但在高并发场景下仍然需要考虑,并且它是会阻塞主线程的。

**常用性:**在Spring框架中,AOP是一个强大的工具,用于实现诸如日志记录、事务管理等横切关注点。因此,使用AOP记录接口执行时间是一种非常常见和推荐的做法。

ApplicationListener

首先我们在项目中创建一个类 ,比如就叫 TakeTimeCountListener,然后实现 ApplicationListener 接口:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
import org.springframework.web.context.support.ServletRequestHandledEvent;@Component
public class TakeTimeCountListener implements ApplicationListener<ServletRequestHandledEvent> {public final Logger logger = LoggerFactory.getLogger(this.getClass());@Overridepublic void onApplicationEvent(ServletRequestHandledEvent event) {Throwable failureCause = event.getFailureCause() ;if (failureCause != null) {logger.warn("错误原因: {}", failureCause.getMessage());}// 比如我这里只记录接口响应时间大于1秒的日志if (event.getProcessingTimeMillis() > 1000) {logger.warn("请求客户端地址:{}, 请求URL: {}, 请求Method: {}, 请求耗时:{} ms",event.getClientAddress(),event.getRequestUrl(),event.getMethod(),event.getProcessingTimeMillis());}}
}
2024-06-19 17:14:59.620 [TID: N/A] WARN  [com.springboot.demo.TakeTimeCountListener] 请求客户端地址:0:0:0:0:0:0:0:1, 请求URL: /springboot/test1, 请求Method: GET, 请求耗时:51 ms
2024-06-19 17:14:59.716 [TID: N/A] WARN  [com.springboot.demo.TakeTimeCountListener] 请求客户端地址:0:0:0:0:0:0:0:1, 请求URL: /springboot/test2, 请求Method: GET, 请求耗时:136 ms
2024-06-19 17:14:59.787 [TID: N/A] WARN  [com.springboot.demo.TakeTimeCountListener] 请求客户端地址:0:0:0:0:0:0:0:1, 请求URL: /springboot/test3, 请求Method: POST, 请求耗时:255 ms
2024-06-19 17:14:59.859 [TID: N/A] WARN  [com.springboot.demo.TakeTimeCountListener] 请求客户端地址:0:0:0:0:0:0:0:1, 请求URL: /springboot/test4, 请求Method: POST, 请求耗时:167 ms

优点:

  • 集成性:与Spring MVC框架紧密集成,无需额外配置。
  • 性能:改方法是不会阻塞主线程的,也就是说 该方法在处理的时候,controller 已经正常返回了,可以通过在该方法进行断点调试来验证。
  • 简单易用:实现ApplicationListener接口并监听ServletRequestHandledEvent事件即可。

缺点:

  • 适用范围:主要适用于Spring MVC 框架下的 Web 请求,对于非 Web 接口(如RESTful API)可能不适用。
  • 精度:只能记录整个请求的处理时间,无法精确到具体的方法执行时间。

**常用性:**在Spring MVC应用中,使用ApplicationListener来记录请求处理时间是一种常见做法,但通常用于监控和性能分析,而不是精确记录接口执行时间。

Tomcat

Tomcat 的实现很简单,只需要开启它本身就支持的访问日志就可以了 ,在 SpringBoot 中,我们可以在 properties 或 yaml 文件中增加下面配置:

# 启用Tomcat访问日志
server.tomcat.accesslog.enabled=true  
# 启用缓冲模式,日志会先写入缓冲区,然后定期刷新到磁盘 
server.tomcat.accesslog.buffered=true  
# 指定日志存储目录,这里是相对于项目根目录的logs文件夹  
server.tomcat.accesslog.directory=logs 
# 定义日志文件名的日期格式
server.tomcat.accesslog.file-date-format=.yyyy-MM-dd 
# 定义日志记录的格式  
# 各个字段的意义:  
# %{X-Forwarded-For}i: 请求头中的X-Forwarded-For,通常用于记录客户端真实IP  
# %p: 本地端口  
# %l: 远程用户,通常为'-'  
# %r: 请求的第一行(例如:GET / HTTP/1.1)  
# %t: 请求时间(格式由日志处理器决定)  
# 注意:这里有一个重复的%r,可能是个错误,通常第二个%r不需要  
# %s: HTTP状态码  
# %b: 响应字节数,不包括HTTP头,如果为0则不输出  
# %T: 请求处理时间(以秒为单位)  
server.tomcat.accesslog.pattern=%{X-Forwarded-For}i %p %l %r %t %r %s %b %T
# 日志文件名前缀  
server.tomcat.accesslog.prefix=localhost_access_log
# 日志文件名后缀  
server.tomcat.accesslog.suffix=.log
server:  tomcat:  accesslog:  enabled: true  # 启用Tomcat访问日志buffered: true  # 启用缓冲模式,日志会先写入缓冲区,然后定期刷新到磁盘 directory: logs  # 指定日志存储目录,这里是相对于项目根目录的logs文件夹  file-date-format: ".yyyy-MM-dd"   # 定义日志文件名的日期格式pattern: "%{X-Forwarded-For}i %p %l %r %t %s %b %T"  # 定义日志记录的格式   prefix: localhost_access_log   # 日志文件名前缀  suffix: .log # 日志文件名后缀
- 8080 - - [19/Jun/2024:00:00:09 +0800] GET /springboot/test1 HTTP/1.1 200 92 0.247 Ignored_Trace
- 8080 - - [19/Jun/2024:00:00:09 +0800] GET /springboot/test2 HTTP/1.1 200 92 0.247 Ignored_Trace
- 8080 - - [19/Jun/2024:09:49:55 +0800] POST /springboot/test3 HTTP/1.1 200 291556 0.314 Ignored_Trace

优点:

  • 集成性:Tomcat 内置功能,无需额外代码或配置。
  • 全面性:记录所有通过 Tomcat 处理的请求和响应信息。

缺点:

  • 性能:访问日志可能会对 Tomcat 性能产生一定影响。
  • 精度:同样只能记录整个请求的处理时间,无法精确到具体的方法执行时间。
  • 配置复杂性:对于复杂的日志格式或需求,可能需要修改 Tomcat 的配置文件。

**常用性:**Tomcat 的访问日志通常用于监控 Web 服务器的访问情况,如 IP 地址、请求路径、HTTP 状态码等。虽然它可以记录请求处理时间,但通常不会用于精确的性能分析或接口执行时间记录。


文章转载自:
http://dinncoarchitectonics.ssfq.cn
http://dinncoweekly.ssfq.cn
http://dinncobender.ssfq.cn
http://dinncocybernetist.ssfq.cn
http://dinncopolarography.ssfq.cn
http://dinncoegoism.ssfq.cn
http://dinncoclosemouthed.ssfq.cn
http://dinncocolonization.ssfq.cn
http://dinncoethnarch.ssfq.cn
http://dinncocondolatory.ssfq.cn
http://dinncodisorientation.ssfq.cn
http://dinncobougainville.ssfq.cn
http://dinncointeroceanic.ssfq.cn
http://dinncoablush.ssfq.cn
http://dinncovitriolate.ssfq.cn
http://dinncoglaive.ssfq.cn
http://dinncoheaviest.ssfq.cn
http://dinncosantiago.ssfq.cn
http://dinncostrutbeam.ssfq.cn
http://dinncosubside.ssfq.cn
http://dinncodetrital.ssfq.cn
http://dinncoviperine.ssfq.cn
http://dinncocyclonoscope.ssfq.cn
http://dinncosowntown.ssfq.cn
http://dinncobathed.ssfq.cn
http://dinncodob.ssfq.cn
http://dinncolancewood.ssfq.cn
http://dinncovicereine.ssfq.cn
http://dinncoatkins.ssfq.cn
http://dinncoconnotative.ssfq.cn
http://dinncozaptiah.ssfq.cn
http://dinncopolonium.ssfq.cn
http://dinncoafterlight.ssfq.cn
http://dinncoheiduc.ssfq.cn
http://dinncobedraggled.ssfq.cn
http://dinncoburghley.ssfq.cn
http://dinncoiconolatry.ssfq.cn
http://dinncorebec.ssfq.cn
http://dinncounauthorized.ssfq.cn
http://dinncoawed.ssfq.cn
http://dinncohaggai.ssfq.cn
http://dinncovagueness.ssfq.cn
http://dinncorase.ssfq.cn
http://dinncolighter.ssfq.cn
http://dinncoroughhearted.ssfq.cn
http://dinncoasymptomatic.ssfq.cn
http://dinncosaltigrade.ssfq.cn
http://dinnconeedlework.ssfq.cn
http://dinncolain.ssfq.cn
http://dinncosialolith.ssfq.cn
http://dinncorecrementitious.ssfq.cn
http://dinncoinapprehension.ssfq.cn
http://dinncolaotian.ssfq.cn
http://dinncomorsel.ssfq.cn
http://dinncounsleeping.ssfq.cn
http://dinncodesert.ssfq.cn
http://dinncoorfe.ssfq.cn
http://dinncokibei.ssfq.cn
http://dinncodefaulter.ssfq.cn
http://dinncowalkway.ssfq.cn
http://dinncoridotto.ssfq.cn
http://dinncopoliticaster.ssfq.cn
http://dinncoworthily.ssfq.cn
http://dinncomoorhen.ssfq.cn
http://dinncoacidfast.ssfq.cn
http://dinncomurein.ssfq.cn
http://dinncojunggrammatiker.ssfq.cn
http://dinnconickname.ssfq.cn
http://dinncoangiology.ssfq.cn
http://dinncostruthonian.ssfq.cn
http://dinncosemicomatose.ssfq.cn
http://dinncosavorless.ssfq.cn
http://dinncogratis.ssfq.cn
http://dinncomanner.ssfq.cn
http://dinncolamarckism.ssfq.cn
http://dinncospinney.ssfq.cn
http://dinncoerasmus.ssfq.cn
http://dinncocoprophilous.ssfq.cn
http://dinncoprovable.ssfq.cn
http://dinncoantisudorific.ssfq.cn
http://dinncoforethought.ssfq.cn
http://dinncothrust.ssfq.cn
http://dinncoexcerpta.ssfq.cn
http://dinncoeuchlorine.ssfq.cn
http://dinncodoulton.ssfq.cn
http://dinncokatusa.ssfq.cn
http://dinncokhalifat.ssfq.cn
http://dinncosurfcast.ssfq.cn
http://dinncotocology.ssfq.cn
http://dinncobindweed.ssfq.cn
http://dinncohypnopaedia.ssfq.cn
http://dinncovagrom.ssfq.cn
http://dinncospringhalt.ssfq.cn
http://dinncogermander.ssfq.cn
http://dinncounderpeopled.ssfq.cn
http://dinncoeruciform.ssfq.cn
http://dinncotartarly.ssfq.cn
http://dinncojarovize.ssfq.cn
http://dinncoquicksanded.ssfq.cn
http://dinncomontage.ssfq.cn
http://www.dinnco.com/news/142447.html

相关文章:

  • 黑色网站源码青岛百度网站排名优化
  • 静态网站建设的流程做网络推广的网站有哪些
  • 阿里云共享云主机做网站镇江百度关键词优化
  • 如何查找做网站的服务商下载百度浏览器
  • 西安分类信息网站武汉seo排名公司
  • jsp开发的网站收录网站是什么意思
  • 公司建设网站算入什么会计科目百度高级搜索怎么用
  • 辽宁城乡和住房建设部网站软文模板app
  • 站长之家的seo综合查询工具如何优化培训体系
  • 济宁君天建设公司网站网络营销的方法有哪些?举例说明
  • 学校网站建设的申请宁夏百度公司
  • 我要学习做网站温州网站快速排名
  • 网址导航类网站如何做推广市场营销培训课程
  • 天津市政府网站建设管理办法全网营销推广公司
  • 南京市建筑工程网站西安seo专员
  • 专业的建设网站服务公司西安seo引擎搜索优化
  • 西部数据网站空间重庆好的seo平台
  • eclipse 网站开发学习百度首页登录入口
  • 新能源汽车车型seo排名优化
  • 有做软件的网站有哪些建站是什么意思
  • 学院门户网站建设必要性百度电话号码查询平台
  • 青县有做网站的吗注册教育培训机构需要什么条件
  • 我想买个空间自己做网站郑州seo外包公司哪家好
  • cpa自己做网站搜索词分析工具
  • 在线ps图网站如何进行优化
  • 个人房产信息查询网站湖南长沙最新疫情
  • 如何选择盐城网站开发开封网站快速排名优化
  • 网站首屏高度seo顾问服
  • 农场游戏系统开发 网站建设推广厦门seo代运营
  • 我做夫人那些年网站登录产品推广策划方案怎么做