Spring boot默认使用项目包内配置文件,如果需要使用外部配置文件参见如下优先级:
第一种是在jar包的同一目录下建一个config文件夹,然后把配置文件放到这个文件夹下;
第二种是直接把配置文件放到jar包的同级目录;
第三种在classpath下建一个config文件夹,然后把配置文件放进去;
第四种是在classpath下直接放配置文件。
这四种方式的优先级是从一到四一次降低的。
这里有一个坑:一二两种方案其实不是jar包的同级目录,而是(执行启动指令的)同级目录。为了便于理解我把上面的当前目录说成jar包的同级目录,这种说法并不对。
如此,如果使用脚本启动,则不能直接写全地址去启动,而是要先CD到相应目录再启动;
附配置文件:
APP_PATH=/usr/local/application/juheApi
APP_NAME=juheApi-0.0.1-SNAPSHOT.jar
#使用说明,用来提示输入参数
usage() {
echo "Usage: sh 执行脚本.sh [start appPath appName|stop appPath appName|restart appPath appName|status appPath appName]"
exit 1
}
#检查程序是否在运行
is_exist(){
#pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}' `
pid=`ps -ef|grep $APP_NAME|grep -v grep|grep -v /bin/bash|awk '{print $2}'`
#如果不存在返回1,存在返回0
if [ -z "${pid}" ]; then
return 1
else
return 0
fi
}
#启动方法
start(){
is_exist
if [ $? -eq "0" ]; then
echo "${APP_NAME} is already running. pid=${pid} ."
else
echo "${APP_NAME} is starting"
# nohup java -Xms3072m -Xmx3072m -jar -Duser.timezone=GMT+08 $APP_NAME > /dev/null 2>&1 &
# nohup java -jar -Duser.timezone=GMT+08 $APP_NAME > /dev/null 2>&1 &
source /etc/profile
if [ $? -eq 0 ]; then
echo "====Source java path is ok!===="
cd $APP_PATH
nohup java -jar -Duser.timezone=GMT+08 $APP_NAME > /dev/null 2>&1 &
java -version
else
echo "====Source java path is failed!===="
fi
fi
}
#停止方法
stop(){
is_exist
if [ $? -eq "0" ]; then
kill -9 $pid
else
echo "${APP_NAME} is not running"
fi
}
#输出运行状态
status(){
is_exist
if [ $? -eq "0" ]; then
echo "${APP_NAME} is running. Pid is ${pid}"
else
echo "${APP_NAME} is NOT running."
fi
}
#重启
restart(){
stop
start
}
if [ ! -n "$1" ];then
usage
exit 1
fi
if [ ! -n "$2" ];then
usage
exit 1
fi
if [ ! -n "$3" ];then
usage
exit 1
fi
APP_PATH="$2"
APP_NAME="$3"
#根据输入参数,选择执行对应方法,不输入则执行使用说明
case "$1" in
"start")
start
;;
"stop")
stop
;;
"status")
status
;;
"restart")
restart
;;
*)
usage
;;
esac