【问题标题】:How to use AOP with Feign calls如何在 Feign 调用中使用 AOP
【发布时间】:2017-01-25 16:28:08
【问题描述】:

我对如何在 AOP 中使用 Feign 客户端很感兴趣。例如:

API:

public interface LoanClient {
    @RequestLine("GET /loans/{loanId}")
    @MeteredRemoteCall("loans")
    Loan getLoan(@Param("loanId") Long loanId);
}

配置:

@Aspect
@Component // Spring Component annotation
public class MetricAspect {

    @Around(value = "@annotation(annotation)", argNames = "joinPoint, annotation")
    public Object meterRemoteCall(ProceedingJoinPoint joinPoint, 
                        MeteredRemoteCall annotation) throws Throwable {
    // do something
  }
}

但我不知道如何“拦截” api 方法调用。我哪里做错了?

更新:

我的 Spring 类注解:

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface MeteredRemoteCall {

    String serviceName();
}

【问题讨论】:

  • @Component 注解从何而来?也许从春天开始?如果是这样,您使用 Spring AOP 还是 AspectJ?你如何编译代码? @MeteredRemoteCall 来自哪里?一个特殊的库还是你自己的注释?你能显示注释源代码吗?我想我知道你的问题的答案,但这取决于你对我的问题的回答。
  • @kriegaex 是 @Component 注释它的 Spring 类,用新类更新了我的问题

标签: java aop aspectj spring-aop feign


【解决方案1】:

您的情况有些复杂,因为您有几个问题:

  • 您使用 Spring AOP,一个基于动态代理(接口的 JDK 代理,类的 CGLIB 代理)的“AOP lite”框架。它仅适用于 Spring bean/组件,但据我所知,您的 LoanClient 不是 Spring @Component
  • 即使是 Spring 组件,Feign 也会通过反射创建自己的 JDK 动态代理。它们不在 Spring 的控制范围内。可能有一种方法可以通过编程或通过 XML 配置手动将它们连接到 Spring 中。但是我无法帮助你,因为我不使用 Spring。
  • Spring AOP 仅支持 AspectJ 切入点的子集。具体来说,它不支持call(),而只支持execution()。 IE。它只编织到执行方法的地方,而不是调用它的地方。
  • 但是执行发生在实现接口的方法中,并且接口方法上的注释(例如@MeteredRemoteCall)永远不会被它们的实现类继承。事实上,方法注解在 Java 中是从不继承的,只有从类(不是接口!)到相应子类的类级别注解。 IE。即使您的注释类有一个@Inherited 元注释,它对@Target({ElementType.METHOD}) 也无济于事,仅对@Target({ElementType.TYPE}) 有帮助。 更新:因为我之前已经多次回答过这个问题,所以我刚刚记录了这个问题以及Emulate annotation inheritance for interfaces and methods with AspectJ 中的解决方法。

那你能做什么?最好的选择是在 Spring 应用程序中使用 use full AspectJ via LTW(加载时编织)。这使您可以使用 call() 切入点,而不是 Spring AOP 隐式使用的 execution()。如果您在 AspectJ 中的方法上使用 @annotation() 切入点,它将匹配调用和执行,因为我将在一个独立示例中向您展示(没有 Spring,但效果与 AspectJ 在 Spring 中使用 LTW 相同):

标记注释:

package de.scrum_master.app;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MeteredRemoteCall {}

假装客户端:

此示例客户端将完整的 StackOverflow 问题页面(HTML 源代码)作为字符串抓取。

package de.scrum_master.app;

import feign.Param;
import feign.RequestLine;

public interface StackOverflowClient {
    @RequestLine("GET /questions/{questionId}")
    @MeteredRemoteCall
    String getQuestionPage(@Param("questionId") Long questionId);
}

驱动程序应用:

此应用程序以三种不同的方式使用 Feign 客户端界面进行演示:

  1. 没有 Feign,通过匿名子类手动实例化
  2. 与 #1 类似,但这次在实现方法中添加了额外的标记注释
  3. 通过 Feign 的规范用法
package de.scrum_master.app;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import feign.Feign;
import feign.codec.StringDecoder;

public class Application {
    public static void main(String[] args) {
        StackOverflowClient soClient;
        long questionId = 41856687L;

        soClient = new StackOverflowClient() {
            @Override
            public String getQuestionPage(Long loanId) {
                return "StackOverflowClient without Feign";
            }
        };
        System.out.println("  " + soClient.getQuestionPage(questionId));

        soClient = new StackOverflowClient() {
            @Override
            @MeteredRemoteCall
            public String getQuestionPage(Long loanId) {
                return "StackOverflowClient without Feign + extra annotation";
            }
        };
        System.out.println("  " + soClient.getQuestionPage(questionId));

        // Create StackOverflowClient via Feign
        String baseUrl = "http://stackoverflow.com";
        soClient = Feign
            .builder()
            .decoder(new StringDecoder())
            .target(StackOverflowClient.class, baseUrl);
        Matcher titleMatcher = Pattern
            .compile("<title>([^<]+)</title>", Pattern.CASE_INSENSITIVE)
            .matcher(soClient.getQuestionPage(questionId));
        titleMatcher.find();
        System.out.println("  " + titleMatcher.group(1));
    }
}

没有方面的控制台日志:

  StackOverflowClient without Feign
  StackOverflowClient without Feign + extra annotation
  java - How to use AOP with Feign calls - Stack Overflow

如您所见,在第 3 种情况下,它只打印这个 StackOverflow 问题的问题标题。 ;-) 我正在使用正则表达式匹配器从 HTML 代码中提取它,因为我不想打印完整的网页。

方面:

这基本上是您的附加连接点日志记录方面。

package de.scrum_master.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;

import de.scrum_master.app.MeteredRemoteCall;

@Aspect
public class MetricAspect {
    @Around(value = "@annotation(annotation)", argNames = "joinPoint, annotation")
    public Object meterRemoteCall(ProceedingJoinPoint joinPoint, MeteredRemoteCall annotation)
        throws Throwable
    {
        System.out.println(joinPoint);
        return joinPoint.proceed();
    }
}

带有方面的控制台日志:

call(String de.scrum_master.app.StackOverflowClient.getQuestionPage(Long))
  StackOverflowClient without Feign
call(String de.scrum_master.app.StackOverflowClient.getQuestionPage(Long))
execution(String de.scrum_master.app.Application.2.getQuestionPage(Long))
  StackOverflowClient without Feign + extra annotation
call(String de.scrum_master.app.StackOverflowClient.getQuestionPage(Long))
  java - How to use AOP with Feign calls - Stack Overflow

如您所见,对于这三种情况,以下连接点都会被截获:

  1. 只有call(),因为即使手动实例化,实现类也没有接口方法的注释。所以execution()无法匹配。
  2. call()execution() 都是因为我们手动将标记注释添加到实现类中。
  3. 只有call(),因为Feign创建的动态代理没有接口方法的注解。所以execution()无法匹配。

我希望这可以帮助您了解发生了什么以及为什么。

底线:使用完整的 AspectJ 以使您的切入点与 call() 连接点匹配。那么你的问题就解决了。

【讨论】:

  • 感谢详细解答!你能提供你使用的依赖项吗? aspectjr 和 aspectweaver ?
  • 可能是因为我使用的是 Spring Boot,所以我无法捕捉到 Feign 方法的调用?
  • 对于完整的 AspectJ,您需要使用 AspectJ 编译器 (Ajc) 从命令行或通过例如AspectJ Maven 插件反过来使用 aspectjtools.jar 进行编译。对于 LTW,您需要 aspectjweaver.jar 作为 Java 代理。我猜,我在答案中提供的链接解释了如何从 Spring 中准确使用它。 aspectjrt.jar 小于 aspectjweaver.jar 并且 LTW 不需要,因为运行时已作为子集包含在 weaver jar 中。编译时编织需要运行时 jar,这是我使用 AspectJ 的首选方法。
猜你喜欢
  • 2020-10-08
  • 2019-01-25
  • 1970-01-01
  • 1970-01-01
  • 2019-06-12
  • 2018-05-06
  • 1970-01-01
  • 1970-01-01
  • 2016-10-13
相关资源
最近更新 更多