我有一个集成测试,该测试调用使用drop-wizard框架实现的微服务(abc-service)。我的集成测试调用了微服务内部的资源端点。 此abc服务依赖于2个模块,我在abc服务pom.xml中有此
a。方面模块
b。 Xyz-module->此模块对外部服务(不在我们的域中)进行http调用。 这个xyz模块有15个管理器类,每个类都有一个公共静态方法,该方法使http调用到外部服务,我想计算所有http调用的处理时间,即我想应用 @Around建议所有公共静态方法上的“ strong>”,这会在所有管理器类中进行http调用。 xyz-module中的所有类都是非Spring托管的bean
Aspect模块中的代码
package com.company.abc.operation.aspect
@Aspect
public class AppPerformanceMetricsAspect {
//@Around("execution(* com.company.product.abc.manager..*(..))") -- did'nt work
//@Around("execution(* com.company.product.abc.manager.*.*(..))") -- did'nt work
//@Pointcut("execution(* com.company.product.abc.manager.StoreDocumentManager.helloWorld(..))") -- did'nt work
// @Pointcut("within(om.company.product.abc.manager.*)") -- did'nt work
@Around("execution(* com.company.product.abc.manager.StoreDocumentManager.helloWorld())") //-- did'nt work
public Object getVaultManagerPerformanceMetrics(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.out.println("\n\n\n ########### I am here aaaaaaaaaaaa");
String packageName = proceedingJoinPoint.getSignature().getDeclaringTypeName();
String methodName = proceedingJoinPoint.getSignature().getName();
long start = System.currentTimeMillis();
Object output = proceedingJoinPoint.proceed();
long elapsedTime = System.currentTimeMillis() - start;
System.out.println("Exiting method [" + packageName + "." + methodName + "]; exec time (ms): " + elapsedTime);
return output;
}
}
微服务中的代码(abc-microservice中的配置类),该代码加载了启动微服务时的方面,我看到方面( AppPerformanceMetricsAspect )已注册到Spring容器中,捕获了日志在abc服务(微服务)启动时
INFO [2018-09-28 13:54:28,809] org.springframework.beans.factory.support.DefaultListableBeanFactory: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@14b99c84: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,springConfiguration,signingStrategyFactory,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor,supportCompensatingOperationsAspect,org.springframework.context.annotation.LoadTimeWeavingConfiguration,loadTimeWeaver,org.springframework.context.annotation.aspectj.SpringConfiguredConfiguration,org.springframework.context.config.internalBeanConfigurerAspect, AppPerformanceMetricsAspect]; root of factory hierarchy
建议在文章上进行一些堆叠,删除@EnableAspectJAutoProxy并使用@EnableLoadTimeWeaving。 为了使EnableLoadTimeWeaving正常工作,我使用了
VM选项:-javaagent:C:/dev/selenium/aspectj-weaver.jar -javaagent:C:/dev/selenium/spring-instrument-4.3.3.RELEASE.jar
@EnableLoadTimeWeaving(aspectjWeaving = EnableLoadTimeWeaving.AspectJWeaving.ENABLED) – didn’t work
@EnableSpringConfigured – didn’t work
//@EnableAspectJAutoProxy(proxyTargetClass = true) – this didn’t work
@ComponentScan(basePackages="com.company.abc.operation.aspect")
// path to the package where my aspect is defined AppPerformanceMetricsAspect
@Configuration
public class SpringConfiguration {
@Bean
public VaultManagerPerformanceMetricsAspect vaultManagerPerformanceMetricsAspect() {
return new VaultManagerPerformanceMetricsAspect();
} // even this didn’t work
}
================================================ ============================== 尝试1 :我正在尝试将您的解决方案集成到我的项目中,但无法正常工作。 当我使用
的VM选项启动微服务时-javaagent:C:/dev/selenium/aspectj-weaver.jar -javaagent:C:/dev/selenium/spring-instrument-4.3.3.RELEASE.jar
这就是我在日志中看到的内容
我看到包(com.abc.test.xyz.manager)中的所有类都被编织了
`[AppClassLoader@561279c8] debug weaving 'com.abc.test.xyz.manager.TestManager'
[AppClassLoader@561279c8] weaveinfo Join point 'method-execution(void'com.abc.test.xyz.manager.TestManager.execute())' in Type ''com.abc.test.xyz.manager.TestManager ' (TestManager.java:23) advised by around advice from 'com.test.compensating.operation.aspect.PerformanceMetricsAspect' (PerformanceMetricsAspect.java)'
但是当我尝试运行测试时
'ERROR [2018-11-30 19:15:14,435] com.abc.dropwizard.exceptionmappers.GenericExceptionMapper: Unhandled Service Error
! java.lang.NoSuchMethodError: com.test.compensating.operation.aspect.PerformanceMetricsAspect.aspectOf()Lcom/test/compensating/operation/aspect/PerformanceMetricsAspect;`
我使用Java 1.7运行我的微服务 确保我的微服务项目(类似于我在上一篇文章中的github中创建的tasklist-service项目)在pom.xml`
中具有此项<build>
…
…
<plugin>
<groupId>com.jcabi</groupId>
<artifactId>jcabi-maven-plugin</artifactId>
<version>0.14.1</version>
<executions>
<execution>
<goals>
<goal>ajc</goal>
</goals>
</execution>
</executions>
</plugin>
</build>`
我的方面是在其他模块中定义的,即 abc-compensating-operations模块,这类似于我在上一篇文章中的github中创建的calculate-metrics项目 abc-compensating-operations模块在pom.xml中具有以下依赖项
`<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.1</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.1</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>1.9.1</version>
</dependency>`
答案 0 :(得分:1)
我检查了您的GitHub存储库。首先,我解压缩并删除了所有ZIP文件(要提交它们非常丑陋,尤其是包括 target 目录内容),并添加了父POM,以便能够正确地构建整个项目。然后我发现有几处错误:
如果您想通过LTW使用Aspect模块,则需要根据AspectJ LTW documentation提供正确配置的 aop.xml 文件,例如像这样:
<aspectj>
<aspects>
<aspect name="com.test.compensating.operation.aspect.PerformanceMetricsAspect"/>
</aspects>
<weaver options="-verbose -showWeaveInfo">
<include within="com.test..*"/>
</weaver>
</aspectj>
此外,您的切入点是错误的:
@Around("execution(* com.test.manager.performOperation())")
它应该是(请注意..
表示法):
@Around("execution(* com.test.manager..performOperation())")
现在,您可以从IDE运行主类,并在启动过程中看到以下日志输出:
[AppClassLoader@18b4aac2] info AspectJ Weaver Version 1.9.1 built on Friday Apr 20, 2018 at 16:47:33 GMT
[AppClassLoader@18b4aac2] info register classloader sun.misc.Launcher$AppClassLoader@18b4aac2
[AppClassLoader@18b4aac2] info using configuration /C:/Users/alexa/Documents/java-src/SO_AJ_Spring_ApplyOnNonSpring/calculate-metrics/target/classes/META-INF/aop.xml
[AppClassLoader@18b4aac2] info register aspect com.test.compensating.operation.aspect.PerformanceMetricsAspect
[AppClassLoader@18b4aac2] warning javax.* types are not being woven because the weaver option '-Xset:weaveJavaxPackages=true' has not been specified
(...)
INFO [2018-11-05 11:23:03,317] org.eclipse.jetty.server.ServerConnector: Started application@7249dadf{HTTP/1.1}{0.0.0.0:8080}
INFO [2018-11-05 11:23:03,321] org.eclipse.jetty.server.ServerConnector: Started admin@4362d7df{HTTP/1.1}{0.0.0.0:8081}
INFO [2018-11-05 11:23:03,322] org.eclipse.jetty.server.Server: Started @4168ms
如果您访问http://localhost:8080/task-list,还将看到该方面有效:
[AppClassLoader@18b4aac2] weaveinfo Join point 'method-execution(void com.test.manager.AdditionManager.performOperation())' in Type 'com.test.manager.AdditionManager' (AdditionManager.java:16) advised by around advice from 'com.test.compensating.operation.aspect.PerformanceMetricsAspect' (PerformanceMetricsAspect.java)
########### I am here aaaaaaaaaaaa
Adding 2 numbers 1 2 = 12
Exiting method [com.test.manager.AdditionManager.performOperation]; exec time (ms): 0
0:0:0:0:0:0:0:1 - - [05/Nov/2018:11:23:12 +0000] "GET /task-list HTTP/1.1" 200 23 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36" 251
此外,您的uber JAR也配置不正确,即,从uber JAR启动时,应用程序引发了错误。从未使用过Dropwizard,我用Google搜索然后修复了like this。
我为您创建了一个forked repo branch和一个pull request。享受吧!