由于jvm-sandbox
目前只提供了shell腳本來進行運行安裝部署,對于mac電腦比較方便,但是windows的方式的話比較痛苦,走了很多彎路,慢慢深入了解之后發現windows的方式也是可以去做的。
1. 下載安裝包
細看sandbox提供的一些腳本可以發現,其實腳本里的內容就是想把本地的項目結構打包成:
├── bin
│ ├── attach-pid.sh
│ ├── sandbox.sh
│ └── z-demo.sh
├── cfg
│ ├── repeater-config.json
│ ├── repeater-logback.xml
│ ├── repeater.properties
│ ├── sandbox-logback.xml
│ ├── sandbox.properties
│ └── version
├── example
│ └── sandbox-debug-module.jar
├── install-local.sh
├── lib
│ ├── sandbox-agent.jar
│ ├── sandbox-core.jar
│ └── sandbox-spy.jar
├── module
│ └── sandbox-mgr-module.jar
├── provider
│ └── sandbox-mgr-provider.jar
├── sandbox-module
│ ├── repeater-module-1.0.0-SNAPSHOT-jar-with-dependencies.jar
│ ├── sandbox-debug-module-1.3.3-jar-with-dependencies.jar
│ └── sandbox-manager-module-1.3.3-jar-with-dependencies.jar
├── sandbox-my-demo.jar
├── start.sh
└── z-demo-1.3.3.jar
如果你不想重寫那一套shell腳本的話,可以手動構建這些模塊,當然它也提供了一套已經打包好的方式,你可以下載
下載完成之后解壓到對應的目錄就行了。
2. 打包依賴
我們可以內部寫一些模塊,然后通過maven打包之后,安裝包目錄的lib下進行替換,比如:
├── lib
│ ├── sandbox-agent.jar
│ ├── sandbox-core.jar
│ └── sandbox-spy.jar
這三個包,在源碼模塊下的target模塊下找到如:
-
sandbox-spy-1.3.3-jar-with-dependencies.jar
替換成sandbox-spy.jar
-
sandbox-core-1.3.3-jar-with-dependencies.jar
替換成sandbox-core.jar
3. IDEA 通過 agent啟動
比如你有一個SpringBoot應用,那么在運行配置上加入如下配置:
-javaagent:E:\\study\sandbox\\lib\\sandbox-agent-1.3.3-jar-with-dependencies.jar
基本上沙箱環境已經差不多可以了.
4. 加入自定義的模塊
你可以將他的debug模塊放入安裝包解壓的sandbox-module
目錄下進行調試,在啟動的時候,agent的執行過程會經過sandbox-module
下找對應的插件包.
你只需要打斷點就行了.
還有如果啟動的時候打斷點,請在實現了*LoadCompleted*
接口的類中loadCompleted
方法去操作,因為該接口是沙箱環境啟動的時候會回調初始化的。
其他的像debug模塊的很多方法需要手動調用才會進入,初始化的時候不會進入。
需要注意的是,你每次寫完代碼并且編譯打包之后,需要移動該包到sandbox安裝包的sandbox-module的目錄下,不然會出現源碼斷點對應不上的問題。
提供一個自動拷貝jar的maven插件,在你每次打包時,會將對應的包打到安裝包目錄下更方便。
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<id>copy-lib-src-webapps</id>
<phase>package</phase>
<configuration>
<tasks>
<copy todir="E:\\study\\sandbox\\sandbox-module" overwrite="true"><!--執行復制操作,todir的值是將要復制jar包到的地方,overwrite是否重寫-->
<fileset dir="${project.build.directory}"><!--${project.build.directory}值是你的target目錄-->
<include name="sandbox-manager-module-1.3.3-jar-with-dependencies.jar"/><!--target目錄下的jar包-->
</fileset>
</copy>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
另外如果調試的時候發現任何問題切記一定要查看日志,日志一般不會打印在控制臺,在windows的
C:\Users\liukx\logs\sandbox
用戶目錄下。找到sandbox.log
日志瞄兩眼。
懵逼的時候切記多看看。
懵逼的時候切記多看看。
懵逼的時候切記多看看。