关于构建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
        
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!对于使用Spring Boot部署Linux上的Docker环境,您可以按照以下步骤进行操作: 1. 首先,确保您已经在Linux服务器上安装了Docker,并且Docker服务已经启动。 2. 将您的Spring Boot项目打包成一个可执行的JAR文件。您可以使用Maven或者Gradle来完成这个步骤。 3. 创建一个Dockerfile,用于构建Docker镜像。在项目根目录下创建一个名为`Dockerfile`的文件,并添加以下内容: ``` FROM adoptopenjdk:11-jre-hotspot WORKDIR /app COPY target/your-project.jar app.jar EXPOSE 8080 ENTRYPOINT ["java", "-jar", "app.jar"] ``` 这个Dockerfile使用了AdoptOpenJDK提供的Java 11运行时环境,并将可执行的JAR文件复制到容器的`/app`目录下。 4. 使用Docker命令构建镜像。在项目根目录下执行以下命令: ``` docker build -t your-image-name . ``` 这个命令会根据Dockerfile构建一个名为`your-image-name`的镜像。注意命令最后的`.`表示Dockerfile所在的当前目录。 5. 运行Docker容器。执行以下命令: ``` docker run -d -p 8080:8080 your-image-name ``` 这个命令会在后台运行一个名为`your-image-name`的容器,并将容器的8080端口映射到宿主机的8080端口上。 现在,您的Spring Boot项目已经通过Docker部署Linux上了。您可以通过访问服务器的IP地址加上端口号来访问您的应用程序,例如:`http://your-server-ip:8080`。 希望对您有所帮助!如有任何问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值