spring boot 的两种部署方式
一、 jar包部署
依赖和代码分离 可以实现当依赖没有变更的时候 只用上传代码块即可,如果依赖有新增 可以仅仅上传新增的依赖即可 在pom文件中加入如下插件
<plugins>
<plugin>
<groupId>org.springframework.boot
</groupId>
<artifactId>spring-boot-maven-plugin
</artifactId>
<configuration>
<fork>true
</fork>
<layout>ZIP
</layout>
<includes>
<include>
<groupId>nothing
</groupId>
<artifactId>nothing
</artifactId>
</include>
</includes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins
</groupId>
<artifactId>maven-dependency-plugin
</artifactId>
<executions>
<execution>
<id>copy-dependencies
</id>
<phase>package
</phase>
<goals>
<goal>copy-dependencies
</goal>
</goals>
<configuration>
#依赖输出地址
<outputDirectory>${project.build.directory}/libs
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.mybatis.generator
</groupId>
<artifactId>mybatis-generator-maven-plugin
</artifactId>
<version>1.3.2
</version>
<configuration>
<verbose>true
</verbose>
<overwrite>true
</overwrite>
</configuration>
</plugin>
</plugins>
部署服务,在目录下建立对应文件夹 1)config --> 放对应的配置文件 2)logs --> 放日志数据 3)lib --> 放依赖 jar包可以放在与上述目录同一级,然后使用脚本启动服务,可以根据需求更改自己对应的脚本内容
JAVA_HOME=
"/usr/local/jdk1.8.0_171"
RUNNING_USER=dev
APP_HOME=$
(cd `dirname
$0`
; pwd)
JAR_PATH=
$APP_HOME/dps
-1
.0
.jar
JOBS_PATH=
$APP_HOME/libs
LOG_DIR=
$APP_HOME/logs
CLASSPATH=
$CLASSPATH:
.:
$APP_HOME/config:
$JOBS_PATH
APP_MAINCLASS=
$JAR_PATH
export LANG=
"en_US.UTF-8"
JAVA_OPTS=
"-Xmx4096m -Djava.awt.headless=true -XX:MaxPermSize=128m"
psid=0
checkpid
() {
javaps=`
$JAVA_HOME/bin
/jps
-l
| grep
$JAR_PATH`
if [ -n
"$javaps" ]; then
psid=`
echo $javaps | awk
'{print $1}'`
else
psid=0
fi
}
start() {
echo "start"
checkpid
if [ $psid -ne 0
]; then
echo "================================"
echo "warn: $APP_MAINCLASS already started! (pid=$psid)"
echo "================================"
else
echo -n
"Starting $APP_MAINCLASS ..."
nohup
/usr
/local
/jdk1
.8
.0_171
/bin
/java
-jar
-Xmx4096M
-Xms1024M
-Dloader
.path=
$JOBS_PATH -DLOG_DIR=
$LOG_DIR -Dspring
.profiles
.active=test
$JAR_PATH >out
.file 2>&1 &
checkpid
if [ $psid -ne 0
]; then
echo "(pid=$psid) [OK]"
else
echo "[Failed]"
fi
fi
}
stop
() {
checkpid
if [ $psid -ne 0
]; then
echo -n
"Stopping $APP_MAINCLASS ...(pid=$psid) "
kill -9
$psid
if [ $?
-eq 0
]; then
echo "[OK]"
else
echo "[Failed]"
fi
checkpid
if [ $psid -ne 0
]; then
stop
fi
else
echo "================================"
echo "warn: $APP_MAINCLASS is not running"
echo "================================"
fi
}
status
() {
checkpid
if [ $psid -ne 0
]; then
echo "$APP_MAINCLASS is running! (pid=$psid)"
else
echo "$APP_MAINCLASS is not running"
fi
}
info
() {
echo "System Information:"
echo "****************************"
echo `head
-n 1
/etc
/issue`
echo `uname
-a`
echo
echo "JAVA_HOME=$JAVA_HOME"
echo `
$JAVA_HOME/bin
/java
-version`
echo
echo "APP_HOME=$APP_HOME"
echo "APP_MAINCLASS=$APP_MAINCLASS"
echo "****************************"
}
case
"$1" in
'start')
start
;;
'stop')
stop
;;
'restart')
stop
start
;;
'status')
status
;;
'info')
info
;;
*)
echo "Usage: $0 {start|stop|restart|status|info}"
;;
esac
exit 1
二、war包部署
war包部署成功后 ,可以对需要更新的代码单个文件进行上传,直接替换 该目录下对应的文件即可,比如tomcat安装在/opt/tomcat目录下 则代码的地址为: /opt/tomcat/webapps/burylogs/WEB-INF/classes 本地打包需要上传编译过的代码,具体目录为target下的classes目录下
1.修改相关配置文件
在application.properties这个文件中加配置 server.contextPath=/load 【项目名】修改Application方法
import org
.springframework
.boot
.SpringApplication
;
import org
.springframework
.boot
.autoconfigure
.SpringBootApplication
;
import org
.springframework
.boot
.builder
.SpringApplicationBuilder
;
import org
.springframework
.boot
.web
.servlet
.support
.SpringBootServletInitializer
;
@SpringBootApplication
public class LoadApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder
configure(SpringApplicationBuilder application
) {
return application
.sources(LoadApplication
.class);
}
public static void main(String
[] args
) {
SpringApplication
.run(LoadApplication
.class, args
);
}
}
修改pom文件,移除spring-boot-starter-web里面的tomcat
<dependency>
<groupId>org.springframework.boot
</groupId>
<artifactId>spring-boot-starter-web
</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot
</groupId>
<artifactId>spring-boot-starter-tomcat
</artifactId>
</exclusion>
</exclusions>
</dependency>
再加载tomcat
<dependency>
<groupId>org.springframework.boot
</groupId>
<artifactId>spring-boot-starter-tomcat
</artifactId>
<scope>provided
</scope>
</dependency>
4)build中添加war包的名称
<build>
<finalName>test
</finalName>
</build>
上传文件进行部署 1) 上传文件 rz -y -e 上传war包 上传路径为【服务器为10.20.23.19 路径为/opt/tomcat/webapps】 2) 启动tomcat /opt/tomcat/bin/startup.sh
查看tomcat启动日志 tail -f /opt/tomcat/logs/catalina.out关闭tomcat 的两种方式 (1) 查找tomcat进程: ps -ef|grep tomcat 查看pid, kill -9 pid2 (2)在 /bin/中执行 ./shutdown.sh再执行 ./startup.sh --重启服务