MethodInterceptor; method call interceptor; use: print operation log, Ali sentinal access for service degradation

Interface MethodInterceptor

  • org.aopalliance.intercept.MethodInterceptor

    • invoke(MethodInvocation invocation)
  • This is a function of the interface, and therefore can be used as an assignment target lambda expression or referenced methods.

  • Intercept calls to the interface in the process of reaching the goals. They nest in the target (target) "top."

  • Users should implement invoke (MethodInvocation) method to modify the original behavior. For example: The following class implements a tracking interceptor (track all calls to intercept method)

invoke(MethodInvocation invocation)

  • This is implemented to invoke the method before and after performing additional processing.
  • invocation: Method Invocation connection point Joinpoint
  • Simple realization of course want to call Joinpoint.proceed ().
  • Call Joinpoint.proceed () results; interceptors could be intercepted

scenes to be used

  1. Tracking Blocker
    Scene One : 打印操作日志scalable: 'Add call chain'
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. Collection method calls the situation for service degradation. (When the service big probability of failure, stop the external services)
    Scene Two : 阿里sentinal接入时it can be used as an alternative program
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;
    }
}
Released eight original articles · won praise 1 · views 136

Guess you like

Origin blog.csdn.net/C_Wangjq/article/details/105376613