Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
matrix-sample
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
mall
arch
matrix-sample
Commits
6951be07
Commit
6951be07
authored
Sep 02, 2019
by
QIANGLU
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add
parent
3c2ea562
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
134 additions
and
8 deletions
+134
-8
pom.xml
spring-boot-agent/pom.xml
+27
-0
AgentStartup.java
spring-boot-agent/src/main/java/com/qianglu/agent/AgentStartup.java
+48
-6
TimeInterceptor.java
spring-boot-agent/src/main/java/com/qianglu/agent/config/TimeInterceptor.java
+28
-0
PureStartup.java
spring-boot-pure/src/main/java/com/matrix/pure/PureStartup.java
+5
-2
Test.java
spring-boot-pure/src/main/java/com/matrix/pure/Test.java
+26
-0
No files found.
spring-boot-agent/pom.xml
View file @
6951be07
...
@@ -25,11 +25,36 @@
...
@@ -25,11 +25,36 @@
<artifactId>
javassist
</artifactId>
<artifactId>
javassist
</artifactId>
<version>
3.11.0.GA
</version>
<version>
3.11.0.GA
</version>
</dependency>
</dependency>
<dependency>
<groupId>
net.bytebuddy
</groupId>
<artifactId>
byte-buddy
</artifactId>
<version>
1.10.1
</version>
</dependency>
<dependency>
<groupId>
net.bytebuddy
</groupId>
<artifactId>
byte-buddy-agent
</artifactId>
<version>
1.10.1
</version>
</dependency>
</dependencies>
</dependencies>
<build>
<build>
<pluginManagement>
<pluginManagement>
<plugins>
<plugins>
<plugin>
<groupId>
org.apache.maven.plugins
</groupId>
<artifactId>
maven-jar-plugin
</artifactId>
<configuration>
<archive>
<manifestEntries>
<Premain-Class>
com.qianglu.agent.AgentStartup
</Premain-Class>
<Agent-Class>
com.qianglu.agent.AgentStartup
</Agent-Class>
<Can-Redefine-Classes>
true
</Can-Redefine-Classes>
<Can-Retransform-Classes>
true
</Can-Retransform-Classes>
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<plugin>
<groupId>
org.apache.maven.plugins
</groupId>
<groupId>
org.apache.maven.plugins
</groupId>
...
@@ -63,6 +88,8 @@
...
@@ -63,6 +88,8 @@
<artifactSet>
<artifactSet>
<includes>
<includes>
<include>
javassist:javassist:jar:
</include>
<include>
javassist:javassist:jar:
</include>
<include>
net.bytebuddy:byte-buddy:jar:
</include>
<include>
net.bytebuddy:byte-buddy-agent:jar:
</include>
</includes>
</includes>
</artifactSet>
</artifactSet>
</configuration>
</configuration>
...
...
spring-boot-agent/src/main/java/com/qianglu/agent/AgentStartup.java
View file @
6951be07
package
com
.
qianglu
.
agent
;
package
com
.
qianglu
.
agent
;
import
com.qianglu.agent.service.DumpClassesService
;
import
com.qianglu.agent.config.TimeInterceptor
;
import
com.qianglu.agent.service.PerformMonitorTransformer
;
import
net.bytebuddy.agent.builder.AgentBuilder
;
import
net.bytebuddy.description.type.TypeDescription
;
import
net.bytebuddy.dynamic.DynamicType
;
import
net.bytebuddy.implementation.MethodDelegation
;
import
net.bytebuddy.matcher.ElementMatchers
;
import
net.bytebuddy.utility.JavaModule
;
import
java.lang.instrument.Instrumentation
;
import
java.lang.instrument.Instrumentation
;
...
@@ -11,8 +16,45 @@ import java.lang.instrument.Instrumentation;
...
@@ -11,8 +16,45 @@ import java.lang.instrument.Instrumentation;
*/
*/
public
class
AgentStartup
{
public
class
AgentStartup
{
public
static
void
premain
(
String
arg
,
Instrumentation
instrumentation
)
{
public
static
void
premain
(
String
arg
,
Instrumentation
instrumentation
)
{
System
.
err
.
println
(
"agent startup , args is "
+
arg
);
System
.
err
.
println
(
"agent startup , args is "
+
arg
);
// 注册我们的文件下载函数
// instrumentation.addTransformer(new PerformMonitorTransformer());
instrumentation
.
addTransformer
(
new
PerformMonitorTransformer
());
}
AgentBuilder
.
Transformer
transformer
=
new
AgentBuilder
.
Transformer
()
{
@Override
public
DynamicType
.
Builder
<?>
transform
(
DynamicType
.
Builder
<?>
builder
,
TypeDescription
typeDescription
,
ClassLoader
classLoader
,
JavaModule
javaModule
)
{
return
builder
.
method
(
ElementMatchers
.
any
())
// 拦截任意方法
.
intercept
(
MethodDelegation
.
to
(
TimeInterceptor
.
class
));
}
};
AgentBuilder
.
Listener
listener
=
new
AgentBuilder
.
Listener
()
{
@Override
public
void
onDiscovery
(
String
s
,
ClassLoader
classLoader
,
JavaModule
javaModule
,
boolean
b
)
{
}
@Override
public
void
onTransformation
(
TypeDescription
typeDescription
,
ClassLoader
classLoader
,
JavaModule
javaModule
,
boolean
b
,
DynamicType
dynamicType
)
{
}
@Override
public
void
onIgnored
(
TypeDescription
typeDescription
,
ClassLoader
classLoader
,
JavaModule
javaModule
,
boolean
b
)
{
}
@Override
public
void
onError
(
String
s
,
ClassLoader
classLoader
,
JavaModule
javaModule
,
boolean
b
,
Throwable
throwable
)
{
}
@Override
public
void
onComplete
(
String
s
,
ClassLoader
classLoader
,
JavaModule
javaModule
,
boolean
b
)
{
}
};
new
AgentBuilder
.
Default
().
type
(
ElementMatchers
.
nameStartsWith
(
"com.matrix.pure"
)).
transform
(
transformer
).
with
(
listener
).
installOn
(
instrumentation
);
}
}
}
spring-boot-agent/src/main/java/com/qianglu/agent/config/TimeInterceptor.java
0 → 100644
View file @
6951be07
package
com
.
qianglu
.
agent
.
config
;
import
net.bytebuddy.implementation.bind.annotation.Origin
;
import
net.bytebuddy.implementation.bind.annotation.RuntimeType
;
import
net.bytebuddy.implementation.bind.annotation.SuperCall
;
import
java.lang.reflect.Method
;
import
java.util.concurrent.Callable
;
/**
* @author QIANGLU on 2019-09-02
*/
public
class
TimeInterceptor
{
@RuntimeType
public
static
Object
intercept
(
@Origin
Method
method
,
@SuperCall
Callable
<?>
callable
)
throws
Exception
{
long
start
=
System
.
currentTimeMillis
();
try
{
// 原有函数执行
return
callable
.
call
();
}
finally
{
System
.
out
.
println
(
method
+
": took "
+
(
System
.
currentTimeMillis
()
-
start
)
+
"ms"
);
}
}
}
\ No newline at end of file
spring-boot-pure/src/main/java/com/matrix/pure/PureStartup.java
View file @
6951be07
...
@@ -7,10 +7,13 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
...
@@ -7,10 +7,13 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
/**
* @author qianglu
* @author qianglu
*/
*/
@SpringBootApplication
//
@SpringBootApplication
public
class
PureStartup
{
public
class
PureStartup
{
public
static
void
main
(
String
[]
args
)
{
public
static
void
main
(
String
[]
args
)
{
SpringApplication
.
run
(
PureStartup
.
class
,
args
);
//
// SpringApplication.run(PureStartup.class, args);
}
}
}
}
spring-boot-pure/src/main/java/com/matrix/pure/Test.java
0 → 100644
View file @
6951be07
package
com
.
matrix
.
pure
;
/**
* @author QIANGLU on 2019-09-02
*/
public
class
Test
{
private
void
fun1
()
throws
Exception
{
System
.
out
.
println
(
"this is fun 1."
);
Thread
.
sleep
(
500
);
}
private
void
fun2
()
throws
Exception
{
System
.
out
.
println
(
"this is fun 2."
);
Thread
.
sleep
(
500
);
}
public
static
void
main
(
String
[]
args
)
throws
Exception
{
Test
test
=
new
Test
();
test
.
fun1
();
test
.
fun2
();
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment