FeignAspect.java 2.2 KB
Newer Older
yanzg's avatar
yanzg committed
1 2 3 4 5
package com.yanzuoguang.cloud.aop;

import com.yanzuoguang.util.contants.ResultConstants;
import com.yanzuoguang.util.exception.CodeException;
import com.yanzuoguang.util.exception.ExceptionHelper;
yanzg's avatar
yanzg committed
6
import com.yanzuoguang.util.helper.StringHelper;
yanzg's avatar
yanzg committed
7 8 9 10 11
import com.yanzuoguang.util.vo.ResponseResult;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
yanzg's avatar
yanzg committed
12
import org.springframework.beans.factory.annotation.Value;
yanzg's avatar
yanzg committed
13 14 15 16
import org.springframework.stereotype.Component;

/**
 * 接口切面,用于验证接口回调返回参数
yanzg's avatar
yanzg committed
17
 *
yanzg's avatar
yanzg committed
18
 * @author 颜佐光
yanzg's avatar
yanzg committed
19
 */
yanzg's avatar
yanzg committed
20 21
@Aspect
@Component
yanzg's avatar
yanzg committed
22
public class FeignAspect extends BaseRequestAspect {
yanzg's avatar
yanzg committed
23

yanzg's avatar
yanzg committed
24
    private static final String TAG = FeignAspect.class.getSimpleName();
yanzg's avatar
yanzg committed
25

yanzg's avatar
yanzg committed
26 27 28 29

    @Value("${yzg.logFeign:false}")
    private boolean logFeign;

yanzg's avatar
yanzg committed
30 31 32
    /**
     * AOP的表达式
     */
yanzg's avatar
yanzg committed
33
    @Pointcut("execution(* *..feign..*Feign.*(..))")
yanzg's avatar
yanzg committed
34 35 36 37 38 39 40 41 42 43 44
    public void feignAspect() {
    }

    /**
     * 执行环形切面
     *
     * @param joinPoint
     * @return
     */
    @Around(value = "feignAspect()")
    public Object requestFeignAround(ProceedingJoinPoint joinPoint) throws Throwable {
yanzg's avatar
yanzg committed
45 46
        boolean clear = requestLogInit();
        long start = requestLog(TAG, joinPoint);
yanzg's avatar
yanzg committed
47
        Object result = null;
yanzg's avatar
yanzg committed
48 49
        Exception ex = null;

yanzg's avatar
yanzg committed
50 51 52 53 54 55 56
        try {
            result = joinPoint.proceed();

            // 假如是标准格式,则验证接口是否成功,不成功则抛出异常
            if (result instanceof ResponseResult) {
                ResponseResult responseResult = (ResponseResult) result;
                if (!ResultConstants.SUCCESS.equals(responseResult.getCode())) {
yanzg's avatar
yanzg committed
57
                    throw new CodeException(responseResult.getCode(), responseResult.getMessage(), responseResult.getTarget());
yanzg's avatar
yanzg committed
58 59
                }
            }
yanzg's avatar
yanzg committed
60

yanzg's avatar
yanzg committed
61
            return result;
yanzg's avatar
yanzg committed
62 63 64
        } catch (CodeException e) {
            ex = e;
            throw e;
yanzg's avatar
yanzg committed
65
        } catch (Exception e) {
yanzg's avatar
yanzg committed
66 67 68 69
            result = ExceptionHelper.getError(e);
            ex = e;
            throw e;
        } finally {
yanzg's avatar
yanzg committed
70
            responseLog(logFeign, clear, TAG, StringHelper.EMPTY, joinPoint, start, result, ex);
yanzg's avatar
yanzg committed
71 72 73 74 75
        }
    }


}