HystrixCommandAspect 在普通 Java 应用程序中导致 NoSuchMethodError

HystrixCommandAspect Resulting into NoSuchMethodError in plain Java application

我正在尝试在我的 Java 应用程序中使用 Hystrix,它是一个 spring java 应用程序。

在 POM 中使用以下 Maven 依赖项来启用 Hystrix 命令:

  <dependency>
      <groupId>com.netflix.hystrix</groupId>
      <artifactId>hystrix-javanica</artifactId>
      <version>1.5.8</version>
    </dependency>

    <dependency>
      <groupId>com.netflix.hystrix</groupId>
      <artifactId>hystrix-core</artifactId>
      <version>1.5.12</version>
    </dependency>

    <dependency>
      <groupId>com.netflix.rxjava</groupId>
      <artifactId>rxjava-core</artifactId>
      <version>0.20.7</version>
    </dependency>

使用以下依赖项来启用 AspectJ:

<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjrt</artifactId>
  <version>1.8.7</version>
</dependency>
<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjweaver</artifactId>
  <version>1.8.7</version>
</dependency>

使用以下配置在 META-INF 中创建了 aop.xml:

<aspectj>

  <aspects>
    <aspect name="com.netflix.hystrix.contrib.javanica.aop.aspectj.HystrixCommandAspect" />
  </aspects>

  <weaver options="-verbose">
    <include within="*" />
  </weaver>
</aspectj>

在我的服务中使用了 Hystrix 命令 Class :

import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;

@Component
@Service
public class TestHystrix 

    @HystrixCommand(commandKey = "testHystrix", threadPoolKey = "testHystrix", commandProperties = {
            @HystrixProperty(name = "hystrix.command.testHystrix.execution.isolation.thread.timeoutInMilliseconds", value = "30") }, threadPoolProperties = {
                    @HystrixProperty(name = "hystrix.threadpool.testHystrix.maximumSize", value = "3") })
public void  testHystrix() {

添加了以下 JVM 参数:

-DWeavingMode=compile

但是在 Junit 测试和应用程序运行时,都会导致以下错误:

java.lang.NoSuchMethodError: com.netflix.hystrix.contrib.javanica.aop.aspectj.HystrixCommandAspect.aspectOf()Lcom/netflix/hystrix/contrib/javanica/aop/aspectj/HystrixCommandAspect;

请帮忙。

在提问之前,您应该先查阅您喜欢使用的任何工具的 manual。我只是在那里引用表格:

Aspect weaving

Javanica supports two weaving modes: compile and runtime. (...)

  • CTW. To use CTW mode you need to use specific jar version: hystrix-javanica-ctw-X.Y.Z. This jar is assembled with aspects compiled with using AJC compiler. If you will try to use regular hystrix-javanica-X.Y.Z with CTW then you get NoSuchMethodError aspectOf() at runtime from building with iajc. Also, you need to start your app with using java property: -DWeavingMode=compile. (...)

所以也许您想切换图书馆。

顺便说一句,如果您使用编译时织入 (CTW),则不需要 aop.xml 因为 AspectJ 仅将其用于加载时织入 (LTW)。

我能够通过使用以下 AspectJ 插件配置以及上述 Maven 依赖项来解决问题:

 <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>aspectj-maven-plugin</artifactId>
        <version>1.7</version>
        <configuration>
          <complianceLevel>1.8</complianceLevel>
          <source>1.8</source>
          <target>1.8</target>
         <!--  <showWeaveInfo>true</showWeaveInfo>
          <verbose>true</verbose>-->
          <Xlint>ignore</Xlint> 
          <encoding>UTF-8 </encoding>
          <!-- Provide the Source information.  -->          
          <!-- <aspectLibraries>
            <aspectLibrary>
              <groupId>com.netflix.hystrix</groupId>
              <artifactId>hystrix-javanica</artifactId>
            </aspectLibrary>
          </aspectLibraries> -->
          <!--Weaving already compiled JAR artifacts  -->
          <weaveDependencies>
            <weaveDependency>
              <groupId>com.netflix.hystrix</groupId>
              <artifactId>hystrix-javanica</artifactId>
            </weaveDependency>
          </weaveDependencies>
        </configuration>
        <executions>
          <execution>
            <goals>
              <!-- use this goal to weave all your main classes -->
              <goal>compile</goal>
              <!-- use this goal to weave all your test classes -->
              <goal>test-compile</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

这个插件会启用Post编译编织,更多细节参考一篇非常好的文章@http://www.baeldung.com/aspectj https://www.mojohaus.org/aspectj-maven-plugin/examples/weaveJars.html 使用此插件,也不需要 aop.xml 和 -DWeavingMode=compile