zoukankan      html  css  js  c++  java
  • Is it possible to create @Around Aspect for feign.Client

    https://github.com/spring-cloud/spring-cloud-openfeign/issues/59

    看到github 有人问这个问题,做了个示例:

    import org.apache.commons.io.IOUtils;
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Pointcut;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.cloud.netflix.feign.ribbon.LoadBalancerFeignClient;
    import org.springframework.stereotype.Component;
    import feign.Request;
    import feign.Response;
    
    import java.io.StringWriter;
    
    @Aspect
    @Component
    public class FeignClientAspect {
    
        private static final Logger LOGGER = LoggerFactory.getLogger(FeignClientAspect.class);
    
        @Pointcut("execution(* org.springframework.cloud.netflix.feign.ribbon.LoadBalancerFeignClient.*(..))")
        public void feignClientPointcut() {
        }
    
        @org.aspectj.lang.annotation.Around("feignClientPointcut()")
        public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
    
            LOGGER.info("feignclient begin");
            long start = System.currentTimeMillis();
            Object object = joinPoint.getTarget();
    
            if (!(object instanceof LoadBalancerFeignClient)) {
                LOGGER.info("feignclient not LoadBalancerFeignClient");
                return joinPoint.proceed();
            }
            Object[] args = joinPoint.getArgs();
            for (Object obj : args) {
                if (obj instanceof Request) {
                    Request request = (Request) obj;
                    LOGGER.info("feignclient request url:{}", request.url());
                }
            }
            Object result = joinPoint.proceed();
            if (result instanceof Response) {
                Response response = (Response) result;
    
                StringWriter writer = new StringWriter();
                IOUtils.copy(response.body().asInputStream(), writer, "UTF-8");
                String theString = writer.toString();
    
                LOGGER.info("feignclient response body:{}", theString);
            }
            LOGGER.info("feignclient end ,耗时:{}", (System.currentTimeMillis() - start));
            return result;
        }
    }

    通过Aspect ,我们能拿到 FeignClient 请求和响应信息,可以做性能、异常上报。

    https://www.cnblogs.com/zhangjianbin/p/9245023.html

  • 相关阅读:
    LeetCode 141. Linked List Cycle(判断链表是否有环)
    LeetCode 680. Valid Palindrome II(双指针)
    >/dev/null 2>&1
    18个最佳代码编辑器
    vi和vim常用命令
    搞定Windows连Linux三大件:SecureCRT,FileZilla,NX
    define和typedef的区别
    C++ const,static成员
    C++虚函数练习题
    c++虚函数解析
  • 原文地址:https://www.cnblogs.com/zhangzhi19861216/p/10009377.html
Copyright © 2011-2022 走看看