请求接口超时前的预警机制

编程入门 行业动态 更新时间:2024-10-25 16:26:37

请求<a href=https://www.elefans.com/category/jswz/34/1771365.html style=接口超时前的预警机制"/>

请求接口超时前的预警机制

请求接口超时前的预警机制

 【方案一】:调用接口处理时间过长,前端访问超时解决方案,改造为轮询查询程序执行结果。参考案例:调用接口处理时间过长,前端访问超时解决方案

1.后台接口改造为多线程执行,分两步:(1)创建线程执行接口内容;(2)提供接口查询功能

改造方法:

  • 自动排课功能所在的service类实现Runnable接口,将自动排课的实现逻辑写在run方法中。
  • 编写方法①创建并执行线程,执行run方法。
  • Controller层调用方法①实现自动排课功能。
  • 对于自动排课结果,可以放在redis中,接口①实时更新自动排课的状态(成功或者失败),可以通过接口②每间隔一段时间查询自动排课的结果。

 2. 前端大致分两次请求后台接口:

    第一次请求接口自动排课(线程或者mQ执行),这样在启动自动排课的时候就返回请求结果,告知用户正在进行排课。

    然后轮询调用第二接口,每隔几秒钟就去查询排课的结果。如果返回的状态为0代表排课成功,提示用户;如果返回的状态为1达标排课失败,提示失败原因;如果返回的状态为2代表排课正在执行中,继续轮询访问查询排课结果的接口

【缺陷】: 前端请求接口时间会拉长

【方案二】: 采用 AOP + Annotation 机制对接口响应超时监控

(1)自定义注解类:

package com.caox.TimeOutAop;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;/*** @author : nazi* @version : 1.0* @date : 2019/4/9 16:38*/
@Target(value = {ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface InterfaceProperty {/*** 接口超时时间,单位毫秒.默认值100毫秒* @return 设置的超时时间*/int timeout() default 400;/*** 当接口响应超时时,是否发送邮件.默认发送* @return         返回ture需要发送邮件*/boolean emailIfTimeout() default true;
}

(2)环绕Around增加切面类: 环绕Around增强用法参考文献:@Around环绕增强

package com.caox.TimeOutAop;import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;import java.lang.reflect.Method;
import java.util.UUID;/*** @author : nazi* @version : 1.0* @date : 2019/4/9 16:39*/
@Component
@Aspect
@Slf4j
public class SystemRequestAspect {/** 日志 */private static final Logger LOG = LoggerFactory.getLogger(SystemRequestAspect.class);/** 接口超时日志 */private static final Logger INTERFACE_TIMEOUT_LOG = LoggerFactory.getLogger("INTERFACE_TIMEOUT_LOG");@Around(value = "execution(* com.caox.service..*.*(..))", argNames="pjp")
//    @Around(value = "@@annotation(around)", argNames="pjp")public Object validator(ProceedingJoinPoint pjp) throws Throwable {// 扫描到的接口参数Object[] args = pjp.getArgs();/** 拦截的方法名称 */String methodName = pjp.getTarget().getClass().getSimpleName() + "." + pjp.getSignature().getName();Object obj = new Object();try {long start = System.currentTimeMillis();obj = pjp.proceed(args);long finish = System.currentTimeMillis();long useTime = finish - start;log.info("call 校验接口请求时间:{} {} {} {}",pjp.getTarget().getClass(), pjp.getSignature().getName() ,useTime,args);/** 接口响应时间监控 */interfaceUseTimeMonitor(pjp.getTarget().getClass(), pjp.getSignature().getName(), args, useTime);} catch (Throwable e) {//处理你的异常} finally {//处理其他}return obj;}/*** 接口响应时间监控** @param targetClass 接口实现class* @param methodName  接口方法* @param args        接口入参* @param useTime     调用接口实际使用时间*/private void interfaceUseTimeMonitor(Class targetClass, String methodName, Object[] args, long useTime) {/** 与接口注解最高用时做比较,符合条件发送邮件 */try {Class[] classArray = new Class[args.length];for(int i = 0; i < args.length ; ++i) {classArray[i] = args[i].getClass();}Method method = targetClass.getMethod(methodName, classArray);if(method.isAnnotationPresent(InterfaceProperty.class)) {InterfaceProperty interfaceProperty = method.getAnnotation(InterfaceProperty.class);if(useTime >= interfaceProperty.timeout()) {if(INTERFACE_TIMEOUT_LOG.isInfoEnabled()) {INTERFACE_TIMEOUT_LOG.info("接口超时,interface:[{}].useTime:[{}].settingUseTime:[{}].traceId:[{}]",new Object[]{targetClass.getSimpleName() + "." + methodName,useTime, interfaceProperty.timeout(), UUID.randomUUID().toString()});}}}} catch(Throwable e) {/** 监控逻辑处理错误什么都不做 */}}
}

(3)【主配置文件】: 

   <!--启用@AspectJ支持--><aop:aspectj-autoproxy/><!-- 使用annotation 自动注册bean --><context:component-scan base-package="com.caox"/>

 

 

更多推荐

请求接口超时前的预警机制

本文发布于:2024-02-11 05:52:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1679490.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:预警机制   接口

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!