zoukankan      html  css  js  c++  java
  • org.springframework.cloud FeignInterceptor

    package org.rx.feign;
    
    import org.apache.commons.lang3.ArrayUtils;
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.Signature;
    import org.aspectj.lang.reflect.MethodSignature;
    import org.rx.bean.Tuple;
    import org.rx.util.LogInterceptor;
    import org.rx.util.StringBuilder;
    import org.springframework.cloud.netflix.feign.FeignClient;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    import java.lang.reflect.Method;
    import java.util.Arrays;
    import java.util.function.Function;
    import java.util.stream.Collectors;
    
    public class FeignInterceptor extends LogInterceptor {
        @Override
        protected Object onProcess(ProceedingJoinPoint joinPoint, StringBuilder msg) throws Throwable {
            Signature signature = joinPoint.getSignature();
            if (!(signature instanceof MethodSignature)) {
                return joinPoint.proceed();
            }
            Method method = ((MethodSignature) signature).getMethod();
            RequestMapping apiMapping = method.getAnnotation(RequestMapping.class);
            if (apiMapping == null) {
                return joinPoint.proceed();
            }
    
            String url = "";
            FeignClient feignClient = null;
            for (Class<?> pi : joinPoint.getTarget().getClass().getInterfaces()) {
                if ((feignClient = pi.getAnnotation(FeignClient.class)) != null) {
                    break;
                }
            }
            if (feignClient != null) {
                url += feignClient.url();
            }
            RequestMapping baseMapping = method.getDeclaringClass().getAnnotation(RequestMapping.class);
            Function<RequestMapping, String> pf = p -> String.join(",",
                    !ArrayUtils.isEmpty(p.value()) ? p.value() : p.path());
            if (baseMapping != null) {
                url += pf.apply(baseMapping);
            }
            url += pf.apply(apiMapping);
    
            String httpMethod = ArrayUtils.isEmpty(apiMapping.method()) ? "POST"
                    : String.join(",", Arrays.stream(apiMapping.method()).map(p -> p.name()).collect(Collectors.toList()));
            msg.appendLine().appendLine("%s		%s", httpMethod, resolveUrl(url, signature));
            return super.onProcess(joinPoint, msg);
        }
    
        protected String resolveUrl(String url, Signature signature) {
            return url;
        }
    
        @Override
        protected Tuple<String, String> getProcessFormat() {
            return Tuple.of("Request:	%s", "Response:	%s");
        }
    }
  • 相关阅读:
    android中statusbar高度的问题
    int和short做循环计数器时的效率问题
    解决Rectangle Packing问题【原创】
    10个android开源项目(转)
    自动编译.9.png文件
    通过wifi调试android程序
    HBase 性能优化笔记
    [转载]定制CentOS 6.3 自动安装盘
    region split时metascan出现regioninfo为空
    Google Dremel 原理 如何能3秒分析1PB
  • 原文地址:https://www.cnblogs.com/Googler/p/9283049.html
Copyright © 2011-2022 走看看