【发布时间】:2016-07-13 09:16:43
【问题描述】:
我已尝试按照说明使放置向导 AspectJ 指标正常工作,请参阅 https://github.com/astefanutti/metrics-aspectj 但是,当我按照这些简单说明并通过 JConsole mbeans 查看指标时,没有为我的注释服务报告指标,只有资源是默认的放置向导行为。
我使用的是旧版本的放置向导,因此不会与 com.codahale.metrics artefacts 发生 jar 依赖冲突
在构建应用程序时,点击以下 url 会生成一个随机字符串 http://localhost:8080/string
这是我的简单示例代码:
pom.xml
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>test-project</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>test project</name>
<description>try and get aspect j metrics working in dropwizard following instructions on https://github.com/astefanutti/metrics-aspectj</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-core</artifactId>
<version>0.7.1</version>
</dependency>
<dependency>
<groupId>io.astefanutti.metrics.aspectj</groupId>
<artifactId>metrics-aspectj</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>com.codahale.metrics</groupId>
<artifactId>metrics-core</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>com.codahale.metrics</groupId>
<artifactId>metrics-annotation</artifactId>
<version>3.0.2</version>
</dependency>
<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>
-->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<configuration>
<aspectLibraries>
<aspectLibrary>
<groupId>io.astefanutti.metrics.aspectj</groupId>
<artifactId>metrics-aspectj</artifactId>
</aspectLibrary>
</aspectLibraries>
<complianceLevel>1.8</complianceLevel>
<source>1.8</source>
<target>1.8</target>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<configuration>
<createDependencyReducedPom>true</createDependencyReducedPom>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Premain-Class>org.aspectj.weaver.loadtime.Agent</Premain-Class>
<Main-Class>com.example.MyApplication</Main-Class>
</manifestEntries>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
MyApplication.java
package com.example;
import com.example.service.MyService;
import io.dropwizard.Application;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
public class MyApplication extends Application<MyConfiguration> {
public static void main(String[] args) throws Exception {
MyApplication app = new MyApplication();
app.run(args);
}
@Override
public void initialize(Bootstrap<MyConfiguration> arg0) {
}
@Override
public void run(MyConfiguration configuration, Environment environment) throws Exception {
environment.jersey().register(new MyResource(new MyService()));
}
}
MyConfiguration.java
package com.example;
import io.dropwizard.Configuration;
public class MyConfiguration extends Configuration {
}
MyResource.java
package com.example;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import com.codahale.metrics.annotation.Timed;
import com.example.service.MyService;
@Path("/string")
@Produces(MediaType.APPLICATION_JSON)
public class MyResource {
private final MyService service;
public MyResource(MyService service) {
this.service = service;
}
@GET
@Timed
public String getString() {
return "string is [" + this.service.constructString() + "]";
}
}
MyService.java
package com.example.service;
import java.util.Random;
import com.codahale.metrics.annotation.Timed;
import io.astefanutti.metrics.aspectj.Metrics;
@Metrics(registry = "myRegistry")
public class MyService {
private final Random random = new Random();
@Timed(name = "myTimedMethod")
public String constructString() {
byte[] bytes = new byte[20];
this.random.nextBytes(bytes);
return new String(bytes);
}
}
example.yml
logging:
level: INFO
我使用标准 maven 命令在命令行上构建阴影 jar:
mvn clean install
并使用以下命令运行 jar:
java -jar -javaagent:<path-to-maven-repo>/org/aspectj/aspectjweaver/1.8.7/aspectjweaver-1.8.7.jar target/test-project-0.0.1-SNAPSHOT.jar server example.yml
我不太确定自己错过了什么,但花了一天的时间搜索并没有产生任何有用的见解。
【问题讨论】:
标签: java aop aspectj dropwizard code-metrics