MethodInterceptor;方法调用拦截器;使用:打印操作日志,阿里sentinal接入用于服务降级

Interface MethodInterceptor

  • org.aopalliance.intercept.MethodInterceptor

    • invoke(MethodInvocation invocation)
  • 这是一个函数接口,因此可以用作lambda表达式或方法引用的赋值目标。

  • 拦截在到达目标的过程中对接口的调用。它们嵌套在目标(target)的“顶部”。

  • 用户应该实现invoke(MethodInvocation)方法来修改原始行为。例如:以下的类实现了一个追踪拦截器(追踪所有对拦截方法的调用)

invoke(MethodInvocation invocation)

  • 实现此方法以在调用的方法之前和之后执行额外的处理。
  • invocation:方法调用连接点Joinpoint
  • 简洁的实现当然希望调用Joinpoint.proceed()。
  • 调用Joinpoint.proceed()的结果;可能被拦截机拦截

使用场景

  1. 追踪拦截器
    场景一打印操作日志,可扩展:‘添加调用链’
class TracingInterceptor implements MethodInterceptor {
   Object invoke(MethodInvocation i) throws Throwable {
     System.out.println("method "+i.getMethod()+" is called on "+
                        i.getThis()+" with args "+i.getArguments());
     Object ret=i.proceed();
     System.out.println("method "+i.getMethod()+" returns "+ret);
     return ret;
   }
 }
  1. 收集方法调用情况,用于服务降级。(当服务出现大概率失败时,停止对外提供服务)
    场景二阿里sentinal接入时,可作为一种可选方案
import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.EntryType;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.context.ContextUtil;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.extern.slf4j.Slf4j;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

/**
 * feign接入阿里sentinal,简化后代码
 *
 * @Author JQ.wang
 * @Date 2020/4/7
 */
@Slf4j
public class InvokeResultCollectInterceptor implements MethodInterceptor {

    @Override
    public Object invoke(MethodInvocation mi) throws Throwable {
        log.debug("around 拦截到了{}方法...", mi);

        Object response;
        Entry entry = null;
        // 降级是否开启
        Boolean switchFlag = Boolean.TRUE;
        // 用于记录方法调用耗时
        long elapsedTime = 0L;
        // sentinal 中的resource
        String resourceName = "";
        try {
            if (switchFlag) {
                // todo set resourceName
                // ... resourceName = sentinelUtil.getResuorceName(mi.getMethod());
                ContextUtil.enter(resourceName);
                entry = SphU.entry(resourceName, EntryType.OUT, 1, mi.getArguments());
            }
            long startTime = System.currentTimeMillis();
            // 方法调用
            response = mi.proceed();
            // 方法耗时
            elapsedTime = System.currentTimeMillis() - startTime;
        } catch (Exception e) {
            // sentinel:筛选异常并统计
            if (e instanceof BlockException) {
                log.warn("url:" + resourceName + ",it is blocked!");
                // throw new BizException(code,msg);
                throw e;
            }
            // todo 抛出业务异常
            throw e;
        } finally {
            // 释放资源
            if (switchFlag) {
                if (entry != null) {
                    entry.exit();
                }
                ContextUtil.exit();
            }
        }
        return response;
    }
}
发布了8 篇原创文章 · 获赞 1 · 访问量 136

猜你喜欢

转载自blog.csdn.net/C_Wangjq/article/details/105376613