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
分类:
spring
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?