天天看點

Jvm-Sandbox-Repeater的部署

1.什麼是jvm-sandbox-repeater?

jvm-sandbox-repeater 是 JVM-Sandbox 生态體系下的重要子產品,它具備了JVM-Sandbox 的所有特點,插件式設計便于快速适配各種中間件,封裝請求錄制/回放基礎協定,也提供了通用可擴充的各種豐富API。jvm-sandbox 是指 JVM 沙箱容器,一種 JVM 的非侵入式運作期 AOP 解決方案。

2.它的核心能力是什麼?

2.1 通用錄制/回放能力

(1).無侵入式錄制 HTTP/Java/Dubbo 入參/傳回值錄制能力(業務系統無感覺)

(2).基于 TTL 提供多線程子調用追蹤,完整追蹤一次請求的調用路徑

(3).入口請求(HTTP/Dubbo/Java)流量回放、子調用(Java/Dubbo)傳回值 Mock 能力

2.2 快速可擴充API實作

(1).錄制/回放插件式架構

(2).提供标準接口,可通過配置/簡單編碼實作一類通用插件

2.3. standalone工作模式

無需依賴任何服務端/存儲,可以單機工作,提供錄制/回放能力

3.如何部署?

3.1 Console部署

setp1: 下載下傳源碼

git clone https://github.com/alibaba/jvm-sandbox-repeater
           

複制

step2: 配置資料庫資訊 application.properties 及初始化資料庫相關的初始化 sql 檔案位址為:

./jvm-sandbox-repeater/repeater-console/repeater-console-dal/src/main/resources/database.sql
           

複制

step3: 在伺服器上配置mvn指令

如果是配置好了yum 源,可以使用以下指令直接安裝maven

yum install maven
           

複制

step4: 編譯并啟動console指令

cd ./jvm-sandbox-repeater
mvn install -DskipTests && nohup java -jar repeater-console/repeater-console-start/target/repeater-console.jar > console.log 2>&1 &
           

複制

3.2 Repeater部署

setp1: 在上面的源碼下載下傳目錄下

cd ./jvm-sandbox-repeater/bin  && sh install-local.sh
           

複制

install-local.sh腳本内容如下:

[email protected] bin # more sh install-local.sh
#!/usr/bin/env bash
# repeater's target dir
REPEATER_TARGET_DIR=../target/repeater
# exit shell with err_code
# $1 : err_code
# $2 : err_msg
exit_on_err()
{
    [[ ! -z "${2}" ]] && echo "${2}" 1>&2
    exit ${1}
}
# package
sh ./package.sh || exit_on_err 1 "install failed cause package failed"  #此處調用到了package.sh 腳本,下面會單獨分析
# extract sandbox to ${HOME}
curl -s http://sandbox-ecological.oss-cn-hangzhou.aliyuncs.com/sandbox-1.2.1-bin.tar | tar xz -C ${HOME} || exit_on_err 1 "extract sandbox failed" #下載下傳并解壓sandbox檔案包
# copy module to ~/.sandbox-module
mkdir -p ${HOME}/.sandbox-module || exit_on_err 1 "permission denied, can not mkdir ~/.sandbox-module"  #建立目錄
cp -r ${REPEATER_TARGET_DIR}/* ${HOME}/.sandbox-module  || exit_on_err 1 "permission denied, can not copy module to ~/.sandbox-module" #複制../target/repeater目錄下的所有檔案到上一個指令建立的目錄下
           

複制

package.sh腳本内容如下:

[email protected] bin # more package.sh 
#!/usr/bin/env bash
# repeater's target dir
REPEATER_TARGET_DIR=../target/repeater  #定義變量
# exit shell with err_code
# $1 : err_code
# $2 : err_msg
exit_on_err()
{
    [[ ! -z "${2}" ]] && echo "${2}" 1>&2
    exit ${1}
}
# maven package the sandbox
mvn clean package -Dmaven.test.skip=true -f ../pom.xml || exit_on_err 1 "package repeater failed."   #編譯打包

mkdir -p ${REPEATER_TARGET_DIR}/plugins  #建立目錄
mkdir -p ${REPEATER_TARGET_DIR}/cfg #建立目錄
cp ./repeater-logback.xml ${REPEATER_TARGET_DIR}/cfg/repeater-logback.xml \  #拷貝檔案到repeater目錄
    && cp ./repeater.properties ${REPEATER_TARGET_DIR}/cfg/repeater.properties \
    && cp ./repeater-config.json ${REPEATER_TARGET_DIR}/cfg/repeater-config.json \
    && cp ../repeater-module/target/repeater-module-*-jar-with-dependencies.jar ${REPEATER_TARGET_DIR}/repeater-module.jar \
    && cp ../repeater-console/repeater-console-start/target/repeater-console.jar ${REPEATER_TARGET_DIR}/repeater-bootstrap.jar \
    && cp ../repeater-plugins/ibatis-plugin/target/ibatis-plugin-*-jar-with-dependencies.jar ${REPEATER_TARGET_DIR}/plugins/ibatis-plugin.jar \
    && cp ../repeater-plugins/java-plugin/target/java-plugin-*-jar-with-dependencies.jar ${REPEATER_TARGET_DIR}/plugins/java-plugin.jar \
    && cp ../repeater-plugins/mybatis-plugin/target/mybatis-plugin-*-jar-with-dependencies.jar ${REPEATER_TARGET_DIR}/plugins/mybatis-plugin.jar \
    && cp ../repeater-plugins/dubbo-plugin/target/dubbo-plugin-*-jar-with-dependencies.jar ${REPEATER_TARGET_DIR}/plugins/dubbo-plugin.jar \
    && cp ../repeater-plugins/redis-plugin/target/redis-plugin-*-jar-with-dependencies.jar ${REPEATER_TARGET_DIR}/plugins/redis-plugin.jar \
    && cp ../repeater-plugins/http-plugin/target/http-plugin-*-jar-with-dependencies.jar ${REPEATER_TARGET_DIR}/plugins/http-plugin.jar \
    && cp ../repeater-plugins/hibernate-plugin/target/hibernate-plugin-*-jar-with-dependencies.jar ${REPEATER_TARGET_DIR}/plugins/hibernate-plugin.jar \
    && cp ../repeater-plugins/spring-data-jpa-plugin/target/spring-data-jpa-plugin-*-jar-with-dependencies.jar ${REPEATER_TARGET_DIR}/plugins/spring-data-jpa-plugin.jar
           

複制

repeater 日志配置檔案如下:

repeater的日志路徑為:~/logs/sandbox/repeater/repeater.log --》這是由于logback.xml 配置決定的 repeater-logback.xml檔案内容如下:

[email protected] bin #more repeater-logback.xml 
<?xml version="1.0" encoding="UTF-8" ?>
<configuration scan="true" scanPeriod="10000">
    <appender name="REPEATER-FILE-APPENDER" class="ch.qos.logback.core.rolling.Ro
llingFileAppender">
        <file>${user.home}/logs/sandbox/repeater/repeater.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">        <FileNamePattern>${user.home}/logs/sandbox/repeater/repeater.log.%d{y
yyy-MM-dd}</FileNamePattern>
            <MaxHistory>30</MaxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss} %-5level %msg%n</pattern>
            <charset>UTF-8</charset>
        </encoder>
    </appender>
    <root level="info">
        <appender-ref ref="REPEATER-FILE-APPENDER"/>
    </root>
</configuration>
           

複制

3.3 Sandbox 部署

https://github.com/alibaba/jvm-sandbox
           

複制

  1. JVM-SANDBOX(沙箱)實作了一種在不重新開機、不侵入目标JVM應用的AOP解決方案
  2. 操作步驟:下載下傳最新版本的JVM-SANDBOX
wget http://ompc.oss-cn-hangzhou.aliyuncs.com/jvm-sandbox/release/sandbox-stable-bin.zip

# 解壓
unzip sandbox-stable-bin.zip

# 進入沙箱執行腳本
cd sandbox/bin
 
#在~/sandbox/bin/sandbox.sh 啟動的cfg目錄下有一個sandbox.properties檔案,目錄為/root/sandbox/cfg
sandbox.properties 檔案裡面指定了user_module=~/.sandbox-module

# 挂載目标JVM程序,這裡以console應用為示例來挂載,一條指令搞定:
sh ~/sandbox/bin/sandbox.sh -p `ps -ef | grep "console" | grep -v grep | awk '{print $2}'` -P 12250  【這個指令會将console JVM程序進行挂載】
           

複制

~/.sandbox-module目錄下可以看到:

[email protected] .sandbox-module # tree
.
├── cfg
│   ├── repeater-config.json
│   ├── repeater-logback.xml #這個repeater 啟動後的日志配置檔案
│   └── repeater.properties  #把repeater.properties裡面的ip and port 修改為為console 所在機器啟動的ip and port 
├── plugins
│   ├── dubbo-plugin.jar
│   ├── hibernate-plugin.jar
│   ├── http-plugin.jar
│   ├── ibatis-plugin.jar
│   ├── java-plugin.jar
│   ├── mybatis-plugin.jar
│   ├── redis-plugin.jar
│   └── spring-data-jpa-plugin.jar
├── repeater-bootstrap.jar
└── repeater-module.jar #repeater 子產品的啟動jar
           

複制

Sandbox 日志配置:

#more sandbox-logback.xml 
<?xml version="1.0" encoding="UTF-8" ?>
<configuration scan="true" scanPeriod="10000">
    <appender name="SANDBOX-FILE-APPENDER" class="ch.qos.logback.core.rolling.Rol
lingFileAppender">
        <file>${user.home}/logs/sandbox/sandbox.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"
>
<FileNamePattern>${user.home}/logs/sandbox/sandbox.log.%d{yyyy-MM-dd}
</FileNamePattern>
            <MaxHistory>30</MaxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss} %SANDBOX_NAMESPACE %-5level %msg%n</
pattern>
            <charset>UTF-8</charset>
        </encoder>
    </appender>

    <root level="info">
        <appender-ref ref="SANDBOX-FILE-APPENDER"/>
    </root>
</configuration>
           

複制

4.Console部署後的啟動頁面如下圖所示

Jvm-Sandbox-Repeater的部署

end