✨ 方法耗时
This commit is contained in:
parent
8d349af5a2
commit
fc8834c4c5
32
pom.xml
32
pom.xml
|
@ -17,6 +17,12 @@
|
|||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/net.bytebuddy/byte-buddy -->
|
||||
<byte-buddy.version>1.14.12</byte-buddy.version>
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
|
||||
<log4j.version>2.23.0</log4j.version>
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/io.spring.javaformat/spring-javaformat-maven-plugin -->
|
||||
<!-- https://github.com/spring-io/spring-javaformat/ -->
|
||||
<!-- 高版本支持 JDK 8:https://github.com/spring-io/spring-javaformat/#java-8-support -->
|
||||
|
@ -30,6 +36,18 @@
|
|||
</properties>
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>net.bytebuddy</groupId>
|
||||
<artifactId>byte-buddy</artifactId>
|
||||
<version>${byte-buddy.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-core</artifactId>
|
||||
<version>${log4j.version}</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<distributionManagement>
|
||||
|
@ -90,9 +108,12 @@
|
|||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<artifactId>maven-assembly-plugin</artifactId>
|
||||
<version>3.3.0</version>
|
||||
<configuration>
|
||||
<descriptorRefs>
|
||||
<descriptorRef>jar-with-dependencies</descriptorRef>
|
||||
</descriptorRefs>
|
||||
<archive>
|
||||
<manifest>
|
||||
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
|
||||
|
@ -104,6 +125,15 @@
|
|||
</manifestEntries>
|
||||
</archive>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>make-assembly</id>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>single</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
|
|
|
@ -1,5 +1,12 @@
|
|||
package cn.com.xuxiaowei.boot.sentinel;
|
||||
|
||||
import cn.com.xuxiaowei.boot.sentinel.interceptor.ExecutionTimeInterceptor;
|
||||
import net.bytebuddy.agent.builder.AgentBuilder;
|
||||
import net.bytebuddy.implementation.MethodDelegation;
|
||||
import net.bytebuddy.matcher.ElementMatchers;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import java.lang.instrument.Instrumentation;
|
||||
|
||||
/**
|
||||
|
@ -10,8 +17,18 @@ import java.lang.instrument.Instrumentation;
|
|||
*/
|
||||
public class BootSentinelAgent {
|
||||
|
||||
private static final Logger logger = LogManager.getLogger(BootSentinelAgent.class);
|
||||
|
||||
public static void premain(String agentArgs, Instrumentation inst) {
|
||||
System.out.println("BootSentinelAgent is running.");
|
||||
logger.info("BootSentinelAgent 已启动");
|
||||
|
||||
AgentBuilder.Transformer transformer = (builder, typeDescription, classLoader, javaModule,
|
||||
protectionDomain) -> builder.method(ElementMatchers.any())
|
||||
.intercept(MethodDelegation.to(ExecutionTimeInterceptor.class));
|
||||
|
||||
new AgentBuilder.Default().type(ElementMatchers.nameStartsWith("com.example.demo.controller"))
|
||||
.transform(transformer)
|
||||
.installOn(inst);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,42 @@
|
|||
package cn.com.xuxiaowei.boot.sentinel.interceptor;
|
||||
|
||||
import net.bytebuddy.implementation.bind.annotation.Origin;
|
||||
import net.bytebuddy.implementation.bind.annotation.RuntimeType;
|
||||
import net.bytebuddy.implementation.bind.annotation.SuperCall;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
/**
|
||||
* @author xuxiaowei
|
||||
* @since 0.0.1
|
||||
*/
|
||||
public class ExecutionTimeInterceptor {
|
||||
|
||||
private static final Logger logger = LogManager.getLogger(ExecutionTimeInterceptor.class);
|
||||
|
||||
@RuntimeType
|
||||
public static Object intercept(@Origin Method method, @SuperCall Callable<?> callable) throws Exception {
|
||||
long start = System.currentTimeMillis();
|
||||
Object result = callable.call();
|
||||
|
||||
Class<?> declaringClass = method.getDeclaringClass();
|
||||
String declaringClassName = declaringClass.getName();
|
||||
|
||||
String methodName = method.getName();
|
||||
|
||||
StringBuilder parameterTypeNameBuilder = new StringBuilder();
|
||||
Class<?>[] parameterTypes = method.getParameterTypes();
|
||||
for (Class<?> parameterType : parameterTypes) {
|
||||
parameterTypeNameBuilder.append(parameterType.getTypeName());
|
||||
}
|
||||
|
||||
logger.info("{}#{}({}) 执行耗时 {} ms", declaringClassName, methodName, parameterTypeNameBuilder.toString(),
|
||||
(System.currentTimeMillis() - start));
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
<Configuration status="info">
|
||||
<Properties>
|
||||
<Property name="log-path">/logs</Property>
|
||||
</Properties>
|
||||
<Appenders>
|
||||
<RollingFile name="RollingFile" fileName="${log-path}/boot-sentinel.log"
|
||||
filePattern="${log-path}/boot-sentinel-%d{yyyy-MM-dd}-%i.log">
|
||||
<PatternLayout>
|
||||
<pattern>%d - %c - %L [%t] %-5p %x - %m%n</pattern>
|
||||
</PatternLayout>
|
||||
<Policies>
|
||||
<SizeBasedTriggeringPolicy size="102400KB"/>
|
||||
</Policies>
|
||||
<DefaultRolloverStrategy max="30"/>
|
||||
</RollingFile>
|
||||
<Console name="Console" target="SYSTEM_OUT">
|
||||
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
|
||||
</Console>
|
||||
</Appenders>
|
||||
<Loggers>
|
||||
<Root level="info">
|
||||
<AppenderRef ref="RollingFile"/>
|
||||
<AppenderRef ref="Console"/>
|
||||
</Root>
|
||||
<Logger name="cn.com.xuxiaowei.boot.sentinel" level="info"/>
|
||||
</Loggers>
|
||||
</Configuration>
|
Loading…
Reference in New Issue