使用本教程的部署方案,可以简化应用部署及后期迭代流程
需要使用maven-antrun-plugin插件把jar包发布到release/lib目录下
build-app
org.springframework.boot
spring-boot-maven-plugin
maven-antrun-plugin
package
run
在应用根路径下面,执行以下命令
mvn clean package
一般我们把程序发布到生产环境,都需要使用外部配置文件,不然话的如果要修改配置,还要在本地改完配置文件在编译jar,然后再发布,太麻烦了.
在启动jar的时候,通过spring.location.config指定
完整命令如下,指定外部配置文件在应用的启动脚本中已经声明了
java -jar build-app.jar --spring.config.location=d:/application.yml
application.yml文件如下
#配置应用程序的名字,该名字会在logback配置文件中使用
spring:
application:
name: build-app
#指定logback配置文件所在路径,不配置默认在 resource 下面找
logging:
config: conf/spring-logback.xml
springboot项目默认用的是slf4j输出日志,
我这里使用的logback-spring.xml来配置日志输出info和error级别的日志信息
日志的输出的位置默认在应用发布目录下,也可以通关修改下面log.path属性进行修改
logback-spring.xml文件内容如下
<configuration scan="true" scanPeriod="10 seconds">
<springProperty scope="context" name="logName" source="spring.application.name" defaultValue="server"/>
<contextName>logbackcontextName>
<property name="log.path" value="logs" />
<property name="log.name" value="${logName}" />
<property name="saveDay" value="7" />
<property name="maxFileSize" value="100MB" />
<conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" />
<conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" />
<conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter" />
<property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>infolevel>
filter>
<encoder>
<Pattern>${CONSOLE_LOG_PATTERN}Pattern>
<charset>UTF-8charset>
encoder>
appender>
<appender name="INFO_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}/${log.name}-info.logfile>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] - %msg%npattern>
<charset>UTF-8charset>
encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log.path}/${log.name}-info-%d{yyyy-MM-dd}%i.logfileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>${maxFileSize}maxFileSize>
timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>${saveDay}maxHistory>
rollingPolicy>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>infolevel>
<onMatch>ACCEPTonMatch>
<onMismatch>DENYonMismatch>
filter>
appender>
<appender name="ERROR_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}/${log.name}-error.logfile>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] - %msg%npattern>
<charset>UTF-8charset>
encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log.path}/${log.name}-error-%d{yyyy-MM-dd}%i.logfileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>${maxFileSize}maxFileSize>
timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>${saveDay}maxHistory>
rollingPolicy>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERRORlevel>
<onMatch>ACCEPTonMatch>
<onMismatch>DENYonMismatch>
filter>
appender>
<root level="info">
<appender-ref ref="CONSOLE" />
<appender-ref ref="INFO_FILE" />
<appender-ref ref="ERROR_FILE" />
root>
configuration>
只需要修改SERVER_NAME属性即可使用脚本
server.sh脚本内容如下
#:!/bin/bash
BASE_DIR=$(cd $(dirname $0);cd ..; pwd)
echo "Welcome enter $BASE_DIR"
cd $BASE_DIR
#启动的jar存放路径(需要换成你jar包的位置)
SERVER_NAME=build-app.jar
SERVER_PATH="lib/${SERVER_NAME}"
#程序的外部配置文件路径,这里使用了相对路径
CONFIG=conf/application.yml
#日志文件的路径
LOG_CONFIG=conf/logback-spring.xml
#nohup命令的日志输出路径,
CONSOLE_LOG_OUT=logs/server-console.log
#设置最小堆内存 最大堆内存 年轻代堆内存 初始化元空间内存 最大元空间内存 (元空间是jdk1.8才有的,jdk1.8之前叫永久代,jvm知识请阅读方志明老师写的深入理解Java虚拟机)
JAVA_OPT='-Xms512m -Xmx512m -Xmn256m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=256m'
#设置内存溢出打印堆日志
JAVA_OPT="${JAVA_OPT} -XX:-OmitStackTraceInFastThrow -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=${BASE_DIR}/logs/heapdump.hprof"
#设置GC输出日志 ,测试环境可以打印,生产环境需要注释掉
JAVA_OPT="${JAVA_OPT} -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintGCDateStamps -Xloggc:${BASE_DIR}/logs/gc.log"
#设置启动程序需要指定的spring配置信息
JAVA_OPT="${JAVA_OPT} -jar ${SERVER_PATH} "
JAVA_OPT="${JAVA_OPT} --spring.config.location=${CONFIG}"
JAVA_OPT="${JAVA_OPT} --logging.config=${LOG_CONFIG}"
if [ ! -d "logs" ] ;
then
echo 'mkdir logs dir'
mkdir -p "logs"
fi
function start() {
PID=$(ps -ef | grep $SERVER_NAME | grep -v grep | awk '{ print $2 }')
if [ -z "$PID" ]
then
echo will start ...
else
echo "Start fail, app runing. at $CURRENT_DIR, pid=$PID"
exit 1
fi
echo '-----print JAVA_OPT------'
echo $JAVA_OPT
#把启动命令里的 >$CONSOLE_LOG_OUT 2>&1 & 换成 >dev/null & 可以让nohup不输出启动日志,改成>dev/null的话记得把 tail -f
nohup java $JAVA_OPT >$CONSOLE_LOG_OUT 2>&1 &
#查看实时的启动日志
tail -f $CONSOLE_LOG_OUT
}
function stop() {
PID=$(ps -ef | grep $SERVER_NAME | grep -v grep | awk '{ print $2 }')
if [ -z "$PID" ]
then
echo Application is already stopped
else
echo kill $PID
kill $PID
fi
}
#根据启动脚本的第1个参数决定调用哪个方法
# 启动 start,停止 stop, 重启 restart
case $1 in
start)
shift 1
start $@
;;
stop)
shift 1
stop
;;
restart)
shift 1
stop
sleep 3
start $@
;;
esac
#修改server.sh脚本,把SERVER_NAME改成你的jar包的名称
vi bin/server.sh
#给刚上传到服务器的脚本文件分配执行的权限
chmod 777 bin/server.sh
#启动程序
bin/server.sh start
bin/server.sh stop
重启命令相当于 stop和start 的组合命令
bin/server.sh restart
bin/startup.cmd
只需要修改SERVER_NAME属性,然后双击此脚本即可启动程序
@echo off
@rem -----------------------------------------------------------------------------
@rem Start script for the app Server
@rem -----------------------------------------------------------------------------
setlocal enabledelayedexpansion
set errorlevel=
title build-app server
set CONFIG=conf/application.yml
set LOG_CONFIG=conf/logback-spring.xml
rem 此处需要修改成你的jar包
set SERVER_NAME=build-app.jar
set SERVER_PATH=lib/%SERVER_NAME%
set JAVA_OPT=-Xms512m -Xmx512m -Xmn256m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m
set JAVA_OPT=%JAVA_OPT% -XX:-OmitStackTraceInFastThrow -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=logs/heapdump.hprof
set JAVA_OPT=%JAVA_OPT% -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintGCDateStamps -Xloggc:logs/gc.log
set JAVA_OPT=%JAVA_OPT% -jar %SERVER_PATH%
set JAVA_OPT=%JAVA_OPT% --spring.config.location=%CONFIG%
set JAVA_OPT=%JAVA_OPT% --logging.config=%BASE_DIR%%LOG_CONFIG%
cd /d "%~dp0"
cd ..
set BASE_DIR=%~dp0
rem Check if Java is correctly installed and set
java -version 1>nul 2>nul
if !errorlevel! neq 0 (
@echo Please install Java 1.8 or higher and make sure the Java is set correctly.
@echo You can execute command [ java -version ] to check if Java is correctly installed and set.
pause
goto:eof
)
@echo Please do not close the current window.
@echo -----print JAVA_OPT------
@echo %JAVA_OPT%
java %JAVA_OPT%
@echo Stopped %SERVER_JAR%
pause
goto:eof
shutdown.cmd
需要把脚本中的build-app换成你jar包的名称,然后双击此脚本既可以停止应用
@echo off
if not exist "%JAVA_HOME%\bin\jps.exe" echo Please set the JAVA_HOME variable in your environment, We need java(x64)! jdk8 or later is better! & EXIT /B 1
setlocal
set "PATH=%JAVA_HOME%\bin;%PATH%"
echo killing build-app server
for /f "tokens=1" %%i in ('jps -m ^| find "build-app"') do ( taskkill /F /PID %%i )
echo Done!
后续会更新这块的里面.
github地址
要是觉得我写的对你有点帮助的话,麻烦在github上帮我点 Star
【SpringBoot框架篇】其它文章如下,后续会继续更新。