jvm sandbox实现字节码增强

1.安装jvm-sandbox
2.引入依赖

<parent>
    <groupId>com.alibaba.jvm.sandbox</groupId>
    <artifactId>sandbox-module-starter</artifactId>
    <version>1.2.0</version>
</parent>

3.编写脚本

@MetaInfServices( Module.class )
@Information( id = "run-function-agent" )// 模块名
public class BrokenClockTinkerModule implements Module{

	@Resource
	private ModuleEventWatcher moduleEventWatcher;

	@Command( "runFunctionAgent" )// 模块命令名
	public void runFunctionAgent(){

		new EventWatchBuilder( moduleEventWatcher ).onClass( "com.bj58.sandbox.Base" )// 想要对 Base 这个类进行切面
				.onBehavior( "run" )
				.onWatch( new AdviceListener(){

					@Override
					protected void before( Advice advice ) throws Throwable{

						System.out.println( "run() start" );
						super.before( advice );
					}

					@Override
					protected void afterReturning( Advice advice ) throws Throwable{
						System.out.println( "run() end" );
						super.afterReturning( advice );
					}
				} );

	}

}
  1. 编译打包
mvn clean package
  1. 将jar包复制到用户目录
cp target/sandbox-base-jar-with-dependencies.jar ~/.sandbox-module/
  1. 进入sandbox安装目录,执行命令
#将沙箱挂载到进程号为 15631 的 JVM 进程上,#运行指定模块, 模块功能生效 
./sandbox.sh -p 15631 -d 'run-function-agent/runFunctionAgent'
  1. 卸载沙箱
./sandbox.sh -p 15631 -S  

执行结果:
在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章