七. jenkins部署springboot项目(4)-linux环境--远程调试

 前提:linux环境,jenkins服务器和springboot服务器不在一台机器。

linux环境,jenkins这里就不说了,主要说下和windows环境的不同。

1. jenkins服务器连接springboot服务器通过秘钥连接,所以需要设置免密码登录,参照:https://www.cnblogs.com/gexiaoshan/p/9404424.html

2. 在jenkins上,系统管理-系统设置里配置秘钥路径,如下图:

Passphrase:密码(key的密码,如果你设置了,没有设置的话,这里不需要填)

Path to key:key文件(私钥)的路径

Key: 秘钥

我这里只要填Path to key: /root/.ssh/id_rsa

SSH Servers配置类似,就不详述了。

3. job的配置,主要如下:

linux上的shell脚本如下:

#!/bin/bash

echo "deploying $project_name..."
JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.161-0.b14.el7_4.x86_64
JAVA_HOME=/data/tools/jdk1.8
PATH=$JAVA_HOME/bin:$PATH
CLASSPATH=.:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar
export JAVA_HOME PATH CLASSPATH

DEPLOY_DIR=/data/services
JAR_FILE=$3
PROJECT_NAME=$1
BUILD_NUMBER=$2
BUILD_DATE=$(date +%Y%m%d)

DEPLOY_BACKUP_DIR=/data/backup

echo "start create backup folders..."
if [ ! -d $DEPLOY_BACKUP_DIR ];then
 mkdir -p $DEPLOY_BACKUP_DIR
 echo "create folder $DEPLOY_BACKUP_DIR"
fi

CURRENT_BUILD_BACKUP_DIR=$DEPLOY_BACKUP_DIR/$PROJECT_NAME/$BUILD_DATE/$BUILD_NUMBER
mkdir -p $CURRENT_BUILD_BACKUP_DIR
echo "create folder $CURRENT_BUILD_BACKUP_DIR"

cp $DEPLOY_DIR/$JAR_FILE $CURRENT_BUILD_BACKUP_DIR/$JAR_FILE
echo "create backup folders completed..."

cd $DEPLOY_DIR

mkdir -p $DEPLOY_DIR/$PROJECT_NAME
mv $DEPLOY_DIR/$JAR_FILE $DEPLOY_DIR/$PROJECT_NAME/$JAR_FILE
cd $DEPLOY_DIR/$PROJECT_NAME

echo "ps -ef | grep $JAR_FILE | grep -v grep | awk '{print $2}'"
pid=`ps -ef | grep $JAR_FILE | grep -v grep | grep -v $$ | awk '{print $2}'`
echo "current pid is $pid"
if [ -n "$pid" ]
then
  echo "kill current process.pid:[$pid]"
  kill -9 $pid
fi

#java -jar $JAR_FILE --spring.profiles.active=test > nohup.log &

java -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=0.0.0.0:$4,suspend=n -jar $JAR_FILE --spring.profiles.active=test -Xms256m -Xmx256m > nohup.log &
if [ $? = 0 ];then
  sleep 10
  tail -n 50 nohup.log
fi
 
echo "deploy $PROJECT_NAME is completed."

 

这里启动脚本:

java -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=0.0.0.0:$4,suspend=n -jar $JAR_FILE --spring.profiles.active=test -Xms256m -Xmx256m > nohup.log &

开启了一个debug端口($4:作为参数传进来的),用户测试环境上debug.

调试方式:

在idea上进行配置,如下:

Host:是springboot项目的服务器ip

Port:是上面说的debug端口号,并不是springboot项目本身的端口号。

配置好后就启动debug:

这时访问测试环境地址,就会进入本地debug。

 

欢迎扫码交流:

posted @ 2018-12-20 10:14  宅山仔  阅读(526)  评论(0编辑  收藏  举报