Java打包、上传服务器、部署springboot应用简单脚本

Java打包、上传服务器部署springboot应⽤简单脚本
⼀.应⽤场景:
本地开发java应⽤程序,当部署时,要打包java应⽤,上传jar包到远程服务器,登录远程服务器并执⾏指令进⾏重启java服务。
有时候需要频繁反复进⾏该操作,每次都这样⼀步⼀步的做,有些繁琐并且浪费时间,因此写了⼀份shell脚本,只需要执⾏⼀个脚本指令,便⾃动做了上述所有的事情,节省了许多事。
⼆.环境
1.springboot的应⽤,java -jar跑服务,linux服务器使⽤账号密码登录(使⽤私钥等只需要修改对应的scp,ssh命令即可)
2.shell脚本,需要shell运⾏环境⽀持。mac下默认⽀持,windows需要使⽤powershell,区别于cmd,该程序⽀持shell脚本运⾏。当使⽤密钥时,需要修改id_rsa⽂件的读写权限,设置为当前⽤户权限就好,具体百度即可。
三.思路
重现步骤,第⼀步,mvn打包java应⽤,第⼆步,上传jar包和对应的配置⽂件到服务器,第三步,登录服务器,执⾏部署脚本
四.实现
1.⾸先新建打包脚本 package.sh
#!/bin/bash
mvn clean package -st.skip=true
该脚本进⾏打包指令,需要依赖maven环境,具体需要配置MAVEN_HOME还有PATH,也不多说了
2.写好上传服务器脚本 scp.sh
#! /usr/bin/expect
#⽬标服务器ip,接收第⼀个参数
set target_ip [lindex $argv 0]
#⽬标服务器端⼝,接收第⼆个参数
set target_port [lindex $argv 1]
#⽬标服务器⽤户名,接收第三个参数
set scpUser [lindex $argv 2]
#⽬标服务器密码,接收第四个参数
set scpPwd [lindex $argv 3]
#传输到⽬标服务器的路径
set target_path [lindex $argv 4]
#打印接收到的参数
for {set i 0} {$i < $argc} {incr i} {
#打印参数
puts "arg $i: [lindex $argv $i]"
}
#从第六个参数开始,作为发送的⽂件参数,通配符时,会把匹配到的所有⽂件依次传进
for {set i 5} {$i < $argc} {incr i} {
#scp指令  -P是端⼝,默认ssh端⼝是22, -r是发送的⽂件路径, :后⾯是⽬标地址,如果使⽤私钥,可以⽤ scp -i xxx/id_rsa -P ,把-i放在最前⾯
spawn scp -P $target_port -r [lindex $argv $i] $scpUser@$target_ip:$target_path
#使⽤expect的功能,当交互出现,匹配到password时,帮我们输⼊密码值
expect "*password:"
send "$scpPwd\n"
#交互出现,如果没有interact,则⽂件传输过程会被打断
interact
}
该脚本是expect脚本,需要使⽤expect来执⾏,⽽不是sh
3.在服务器上运⾏的部署脚本deploy.sh
#当前路径,使⽤第⼀个参数
currentPath=$1
#如果第⼀个参数为空,则pwd(运⾏脚本的当前⽬录)作为当前路径
if [ ! -n "$currentPath" ]; then
currentPath=$PWD
fi
#打印当前路径
echo $currentPath
#存放pid的⽂件路径配置
pidFile="$currentPath/test.pid"
#jar包的路径配置
jarPath="$currentPath/test/target/test.jar"
#target jar包路径配置
targetPath="$currentPath/test.jar"
#当pid⽂件存在时,读出pid⽂件的内容,kill掉该pid的进程
if [ -f $pidFile ]; then
echo "pid file exists!"
PID=$(cat $pidFile)          # 将PID从⽂件中读取,并作为⼀个变量
#杀掉该进程
kill -9 $PID
echo "exec kill $PID success!"
fi
#判断jarPath的⽂件路径存不存在,如果存在,则⽤jarPath路径部署,不存在,则⽤默认的targetPath部署
if [ -f $jarPath ]; then
echo "jarPath exists"
targetPath=$jarPath
弯曲弹簧
fi
红薯清洗机echo "执⾏启动java程序指令"
#cd进去currentPath⽬录,在该⽬录下执⾏java -jar指令
cd $currentPath
#部署jar包的指令,nohup是指忽略挂起  > /dev/null是把nohup的输⼊丢到⼀个空设备,即直接丢弃的意思, 2>&1 即所有类型⽇志的输出 &表⽰后台运⾏,后⾯>表⽰#便于后续杀进程重启
nohup java -jar $targetPath --spring.profiles.active=qa > /dev/null 2>&1 & echo $! >"$currentPath/test.pid"
echo "执⾏启动java程序指令完成!"
4.远程ssh执⾏指令脚本
#! /usr/bin/expect
set sshIp [lindex $argv 0]
set sshPort [lindex $argv 1]
set sshUser [lindex $argv 2]
set sshPwd [lindex $argv 3]
set cmd [lindex $argv 4]
set currentPath [lindex $argv 5]
#打印所有参数
for {set i 0} {$i < $argc} {incr i} {
puts "arg $i: [lindex $argv $i]"
}
#ssh执⾏远程指令,如果使⽤私钥,使⽤ssh -i xxx/id_rsa
spawn ssh -p $sshPort $sshUser@$sshIp $cmd $currentPath
expect {
"yes/no" {send "yes\r";exp_continue}
"*password" {send "$sshPwd\r"}
}
#结束expect
expect eof
5.最终⼀键打包上传部署脚本auto.sh
#获取当前路径
currentPath=$PWD
echo "====================开始打包===================="
#调⽤前⾯的打包脚本package.sh,所有的脚本放在同级⽬录下,⽅便调⽤
sh $currentPath/package.sh
echo "====================打包成功!===================="
#配置远程服务器的账号
scpUser='test'
#远程服务器的密码裹尸袋
scpPwd="test123456"
#远程服务器的ip
target_ip="192.168.1.1"
#远程服务器的端⼝
target_port="22"
#发送的jar包路径
from_path="$currentPath/test/target/test.jar"
#服务器部署的⽬录,发送到服务器的⽬录,需要在服务器先建好,不会⾃动创建⽬录
target_path="/data/my_project"
#需要发送部署脚本,部署脚本deploy.sh的路径
from_deploy_path="$currentPath/deploy.sh"
#发送配置⽂件,配置⽂件application.properties的路径
from_properties_path="$currentPath/bggw/src/main/resources/application.properties"
#发送所有的配置⽂件,配置⽂件通配
from_all_properties_path="$currentPath/bggw/src/main/resources/*.properties"
#远程服务器存放配置⽂件的路径,需要在远程服务器先创建好
target_properties_path="$target_path/config"
电子煎药壶#如果配置⽂件路径application.properties存在,就把配置⽂件路径所有的properties发送到远程服务器(如果不需要发送配置⽂件,直接注释该段落即可)if [ -f $from_properties_path ]; then
echo "====================发送配置⽂件===================="
#调⽤我们写的scp.sh脚本⽂件,传输⽂件,expect脚本需要⽤expect调⽤
expect $currentPath/scp.sh $target_ip $target_port $scpUser $scpPwd $target_properties_path $from_all_properties_path
echo "====================发送配置⽂件完成===================="
fi
echo "====================发送配置⽂件到${target_ip}成功!===================="
echo "====================发送jar包到${target_ip}===================="
#调⽤我们写的scp.sh脚本,发送jar包到远程服务器
expect $currentPath/scp.sh $target_ip $target_port $scpUser $scpPwd $target_path $from_path
echo "====================发送jar包到${target_ip}成功!===================="
echo "====================发送部署脚本===================="
#调⽤我们写的scp.sh脚本,发送部署脚本deploy.sh到远程服务器
expect $currentPath/scp.sh $target_ip $target_port $scpUser $scpPwd $target_path $from_deploy_path
echo "====================发送部署脚本成功!===================="
舆情终端
#调⽤deploy.sh的指令配置
sshCmd="sh $target_path/deploy.sh"
水炮泥echo "====================ssh服务器执⾏脚本===================="
#调⽤我们写的ssh.sh脚本,远程执⾏ sshCmd指令,即调⽤deploy.sh,即让远程服务器调⽤部署脚本
expect $currentPath/ssh.sh $target_ip $target_port $scpUser $scpPwd "$sshCmd" "$target_path"
echo "====================ssh服务器执⾏脚本结束===================="
echo "脚本执⾏结束"
到此所有的脚本已经写完了,只需要在本地执⾏
sh ./auto.sh
即可,调⽤⾃动部署脚本,把所有的脚本串联起来,⾃动化部署。
五.最后
之所以拆分成这么多脚本,也是为了每个脚本都可以重新利⽤,不⽤重复写scp和ssh的那⼀串指令,最主要是实验觉得expect脚本和bash 脚本貌似不能在同个⽂件同时使⽤,会有问题。
如果⾃⼰有其他的需求,进⾏⾃⼰的定制即可,⾥⾯的⼀些参数需要⾃⼰修改,如远程服务器的信息,⽂件的路径信息等
java服务器部署⼀般不会如此简单,会提前配置JAVA_HOME等其他信息,还有JVM运⾏的⼀些参数,该脚本仅作为简单的⼀个部署脚本,不作为⽣产环境的使⽤,不过作为测试环境,或者⾃⼰的服务器玩玩,还是可以的,⽣产环境如果没有⽐较复杂的配置,当然也是没有任何问题

本文发布于:2024-09-25 19:18:06,感谢您对本站的认可!

本文链接:https://www.17tex.com/tex/2/289428.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:脚本   服务器   部署   配置   远程   需要   发送
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议