栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

关于构建springboot应用流程(可 linux直接部署或docker运行) 篇章二 编译打包(运行脚本参考)

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

关于构建springboot应用流程(可 linux直接部署或docker运行) 篇章二 编译打包(运行脚本参考)

一.编译打包 1.建立运行文件
  • 需要运行项目,那么就得将 jar 包运行起来,运行需要启动文件
    • 在 resources 下新建 bin 目录
      • 新建 boot.sh 运行 jar ,后面再将它打包到镜像中
        - 参考链接,boot.sh 启动源码

        		 	    #!/bin/sh
        					#
        					#    .   ____          _            __ _ _
        					#   /\ / ___'_ __ _ _(_)_ __  __ _    
        					#  ( ( )___ | '_ | '_| | '_ / _` |    
        					#   \/  ___)| |_)| | | | | || (_| |  ) ) ) )
        					#    '  |____| .__|_| |_|_| |___, | / / / /
        					#   =========|_|==============|___/=/_/_/_/
        					#   :: Spring Boot Startup Script ::
        					#
        					
        					### BEGIN INIT INFO
        					# Provides:          {{initInfoProvides:spring-boot-application}}
        					# Required-Start:    {{initInfoRequiredStart:$remote_fs $syslog $network}}
        					# Required-Stop:     {{initInfoRequiredStop:$remote_fs $syslog $network}}
        					# Default-Start:     {{initInfoDefaultStart:2 3 4 5}}
        					# Default-Stop:      {{initInfoDefaultStop:0 1 6}}
        					# Short-Description: {{initInfoShortDescription:Spring Boot Application}}
        					# Description:       {{initInfoDescription:Spring Boot Application}}
        					# chkconfig:         {{initInfoChkconfig:2345 99 01}}
        					### END INIT INFO
        					
        					[ -n "$DEBUG" ] && set -x
        					
        					# Initialize variables that cannot be provided by a .conf file
        					
        					action="$1"
        					WORKING_DIR="$(pwd)"
        					# shellcheck disable=SC2153
        					[ -n "$JARFILE" ] && jarfile="$JARFILE"
        					[ -n "$APP_NAME" ] && identity="$APP_NAME"
        					
        					# Follow symlinks to find the real jar and detect init.d script
        					cd "$(dirname "$0")" || exit 1
        					[ -z "$jarfile" ] && jarfile=$(pwd)/$(basename "$0")
        					while [ -L "$jarfile" ]; do
        					  if [[ "$jarfile" =~ init.d ]]; then
        					    init_script=$(basename "$jarfile")
        					  else
        					    configfile="${jarfile%.*}.conf"
        					    [ -r ${configfile} ] && . "${configfile}"
        					  fi
        					  jarfile=$(readlink "$jarfile")
        					  cd "$(dirname "$jarfile")" || exit 1
        					  jarfile=$(pwd)/$(basename "$jarfile")
        					done
        					jarfolder="$( (cd "$(dirname "$jarfile")" && pwd -P) )"
        					cd "$WORKING_DIR" || exit 1
        					
        					# Inline script specified in build properties
        					#{{inlinedConfScript:}}
        					
        					# Source any config file
        					#configfile="$(basename "${jarfile%.*}.conf")"
        					configfile="jvm.conf"
        					# Initialize CONF_FOLDER location defaulting to jarfolder
        					[ -z "$CONF_FOLDER" ] && CONF_FOLDER="{{confFolder:${jarfolder}}}"
        					# shellcheck source=/dev/null
        					[ -r "${CONF_FOLDER}/${configfile}" ] && . "${CONF_FOLDER}/${configfile}"
        					# ANSI Colors
        					echoRed() { echo $'e[0;31m'"$1"$'e[0m'; }
        					echoGreen() { echo $'e[0;32m'"$1"$'e[0m'; }
        					echoYellow() { echo $'e[0;33m'"$1"$'e[0m'; }
        					
        					
        					# Create an identity for log/pid files
        					if [ -z "$identity" ]; then
        					  if [ -n "$init_script" ]; then
        					    identity="${init_script}"
        					  else
        					    identity=$(basename "${jarfile%.*}")_${jarfolder////}
        					  fi
        					fi
        					
        					# Initialize log file name if not provided by the config file
        					[ -z "$LOG_FILENAME" ] && LOG_FILENAME="{{logFilename:${identity}.log}}"
        					
        					# Initialize stop wait time if not provided by the config file
        					[ -z "$STOP_WAIT_TIME" ] && STOP_WAIT_TIME="{{stopWaitTime:60}}"
        					
        					# Utility functions
        					checkPermissions() {
        					  touch "$pid_file" &> /dev/null || { echoRed "Operation not permitted (cannot access pid file)"; return 4; }
        					  touch "$log_file" &> /dev/null || { echoRed "Operation not permitted (cannot access log file)"; return 4; }
        					}
        					
        					isRunning() {
        					  ps -p "$1" &> /dev/null
        					}
        					
        					await_file() {
        					  end=$(date +%s)
        					  let "end+=10"
        					  while [ ! -s "$1" ]
        					  do
        					    now=$(date +%s)
        					    if [ $now -ge $end ]; then
        					      break
        					    fi
        					    sleep 1
        					  done
        					}
        					
        					# Build the pid and log filenames
        					pid_file="$PID_FOLDER/{{pidFilename:${identity}.pid}}"
        					log_file="$LOG_FOLDER/$LOG_FILENAME"
        					
        					[ "$(id -u)" = "0" ] && run_user=$(ls -ld "$jarfile" | awk '{print $3}')
        					
        					
        					# Issue a warning if the application will run as root
        					[ "$(id -u ${run_user})" = "0" ] && { echoYellow "Application is running as root (UID 0). This is considered insecure."; }
        					
        					# Find Java
        					if [ -n "$JAVA_HOME" ] && [ -x "$JAVA_HOME/bin/java" ]; then
        					    javaexe="$JAVA_HOME/bin/java"
        					elif type -p java > /dev/null 2>&1; then
        					    javaexe=$(type -p java)
        					elif [ -x "/usr/bin/java" ];  then
        					    javaexe="/usr/bin/java"
        					else
        					    echo "Unable to find Java"
        					    exit 1
        					fi
        					
        					arguments="-Dsun.misc.URLClassPath.disableJarChecking=true $JAVA_OPTS -jar $jarfile $RUN_ARGS $*"
        					# Action functions
        					start() {
        					  if [ -f "$pid_file" ]; then
        					    pid=$(cat "$pid_file")
        					    isRunning "$pid" && { echoYellow "Already running [$pid]"; return 0; }
        					  fi
        					  do_start "$@"
        					}
        					
        					do_start() {
        					  working_dir=$(dirname "$jarfile")
        					  cd $working_dir
        					#  pushd "$working_dir" > /dev/null
        					  if [ ! -e "$PID_FOLDER" ]; then
        					    mkdir -p "$PID_FOLDER" &> /dev/null
        					  fi
        					  if [ ! -e "$log_file" ]; then
        					    touch "$log_file" &> /dev/null
        					  fi
        					  checkPermissions || return $?
        					  eval "$javaexe $arguments >> $log_file &"
        					  pid=$!
        					  echo "$pid" > "$pid_file"
        					  [ -z $pid ] && { echoRed "Failed to start"; return 1; }
        					  echoGreen "Started [$pid]"
        					}
        					
        					stop() {
        					  working_dir=$(dirname "$jarfile")
        					  cd $working_dir
        					  [ -f $pid_file ] || { echoYellow "Not running (pidfile not found)"; return 0; }
        					  pid=$(cat "$pid_file")
        					  isRunning "$pid" || { echoYellow "Not running (process ${pid}). Removing stale pid file."; rm -f "$pid_file"; return 0; }
        					  do_stop "$pid" "$pid_file"
        					}
        					
        					do_stop() {
        					  kill "$1" &> /dev/null || { echoRed "Unable to kill process $1"; return 1; }
        					  for i in `seq $STOP_WAIT_TIME`; do
        					    isRunning "$1" || { echoGreen "Stopped [$1]"; rm -f "$2"; return 0; }
        					    [ $i = STOP_WAIT_TIME/2 ] && kill "$1" &> /dev/null
        					    sleep 1
        					  done
        					  echoRed "Unable to kill process $1";
        					  return 1;
        					}
        					
        					restart() {
        					  stop && start
        					}
        					
        					
        					status() {
        					  [ -f "$pid_file" ] || { echoRed "Not running"; return 3; }
        					  pid=$(cat "$pid_file")
        					  isRunning "$pid" || { echoRed "Not running (process ${pid} not found)"; return 1; }
        					  echoGreen "Running [$pid]"
        					  return 0
        					}
        					
        					run() {
        					  oldFolder="$(dirname "$pwd")"
        					  cd "$(dirname "$jarfile")"
        					  eval "$javaexe $arguments"
        					  result=$?
        					  cd "$oldFolder"
        					  return "$result"
        					}
        					
        					# Call the appropriate action function
        					case "$action" in
        					start)
        					  start "$@"; exit $?;;
        					stop)
        					  stop "$@"; exit $?;;
        					restart)
        					  restart "$@"; exit $?;;
        					status)
        					  status "$@"; exit $?;;
        					debug)
        					  run "$@"; exit $?;;
        					run)
        					  run "$@"; exit $?;;
        					*)
        					  echo "Usage: $jarfile {start|stop|status|debug|run}"; exit 1;
        					esac
        					
        					exit 0
        
      • 新建 server.sh 用于调用 boot.sh,参考如下

        	#!/bin/sh
        	JAR_NAME="service.jar"
        	PID_NAME="service-pid"
        	COMMAND=" debug | run | start | stop | restart | status "
        	if [ $# -lt 1 ]; then
        	    echo " 必要参数:
        	       		debug   debug模式,前台打印日志,默认8002端口;
        	       		run     前台模式启动;
        	       		start   后台模式启动;
        	       		stop    停止;
        	       		restart 重启;
        	       		status  查看状态;
        		   重写SpringBoot支持的参数,以--开头。
        	       	example:   --server.port=9999  			  服务器端口改为9999
        	       			   --logging.level.root=debug     打印全部的debug日志
        	       			   --logging.level.cn.fs=debug    打印当前项目的debug日志
        	       			   --spring.profiles.active=prod  设置当前激活的配置文件为prod
        	        exp: sh server.sh  run --server.port=9999 --logging.level.root=debug
        	    "
        	    exit
        	fi
        	
        	ACTION=$(echo "$COMMAND" | grep -o " $1 ")
        	shift
        	if [ "$ACTION" = "" ]; then
        	    echo "缺少必要参数: {$COMMAND}"
        	    exit
        	fi
        	JVM_ARGS="$*"
        	#while getopts m:a:: opt
        	#do
        	#  case "$opt" in
        	#  m)
        	#  	 ACTION=$(echo "$COMMAND" | grep -o " $OPTARG ")
        	#  	 if [ "$ACTION" = "" ]; then
        	#  	 	echo "启动模式错误: $OPTARG"
        	#  	 	exit 1
        	#  	 fi
        	#  	;;
        	#  a) JVM_ARGS=$OPTARG ;;
        	#  *) echo "未知参数: $opt $OPTARG" && exit 1;;
        	#  esac
        	#done
        	#if [ "$ACTION" = "" ]; then
        	#    echo "缺少参数: -m"
        	#    exit 1
        	#fi
        	# shellcheck disable=SC2164
        	HOME="$(cd "$(dirname "${BASH_SOURCE-$0}")/.."; pwd)"
        	
        	EXEC_JAR="$HOME/bin/$JAR_NAME"
        	
        	#项目配置目录,主要是指定JVM参数的位置
        	CONF_DIR="$HOME/conf"
        	#指定项目日志目录
        	LOG_FOLDER="$HOME/logs"
        	#指定程序运行时的pid
        	PID_FOLDER=$LOG_FOLDER
        	#判断
        	[ ! -e "$LOG_FOLDER" ] &&  mkdir "$LOG_FOLDER"
        	
        	#LOG_FILENAME是shell的重定向日志名
        	
        	if [ -f "$EXEC_JAR" ]; then
        		eval "chmod +x $EXEC_JAR"
        		eval "APP_NAME=$PID_NAME  
        			  HOME=$HOME 
        			  CONF_FOLDER=$CONF_DIR 
        			  PID_FOLDER=$PID_FOLDER 
        			  LOG_FOLDER=$LOG_FOLDER 
        			  LOG_FILENAME=out.log 
        			  $EXEC_JAR $ACTION $JVM_ARGS"
        	    else
        	    	echo "可执行文件不存在."
        	fi
        	exit
        
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/944826.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号