feign调用初步了解
开始aop demo测试
以自定义注解的方式,决定哪些接口要进行feign拦截
1.自定义注解 SendBankAnnotation,对使用改注解的feign接口进行拦截
@Target({ElemenType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SendBankAnnotation{
String value();
String notes() default "";
}
2.feign远程调用
@FeignClient(name="custorm",fallback=Hysitx.class)
public interface IRemoteCallService {
@RequestMapping(value="/custorm/getTest",method = RequestMethod.POST)
@SendBankAnnotation(value = "自定义接口名称",notes= “备注”)
List<String> test(BaseReq req);
}
3.aop拦截
@Aspect
@Component
public class sendFilter{
private Loggen logger = TSLogFactory.get(sendFilter.class);
@Pointcut("@annotation(com....自定义注解的路径)")
private void execute(){}
@around("executr()")
public Object around(ProceedingJoinPoint pjp){
long beginTime = System.currentTimeMillis();
Object result = null;
String edspTransCode = "";
try{
Object[] paramValues = pjp.getArgs();
Signature sig = pjp.getSignature();
MethodSignature msig = (MethodSignature )sig;
Method method = msig.getMethod();
SendBankAnnotation ann = method.getAnnotation(SendBankAnnotation.class);
edspTransCode = ann.value();
if("要过滤的接口名".equals(edspTransCode )){
BaseReq req = null;
Object arg0 = pjp.getArgs()[0];
if(arg0 instanceof BaseReq){
req = (BaseReq).arg0;
}
}
transformDictArgs(edspTransCode,pjp.getArgs());
logger.inf("接口变化:+edsptranscode+",参数"+JSON.tpJSONString(paramValues ));
result = pjp.preceed();
transformDictResult(edspTransCode,result);
}finally{
long costTime = System.currenTimeMillis()-beginTime;
logger.info("接口编号:"+edspTransCode +"耗时:"+costTime+",返回值:"+JSON.tpJSONString(result ));
}
}
}
|