springboot项目部署
1.轻量化部署
1.1原理
在打包好的jar包中依赖占用空间很大,讲jar包解压后有如下文件
在BOOT-INF文件夹中有classes和lib文件夹 classes中是当前项目编译好的代码 项目中所依赖的jar包在lib文件夹中
项目在迭代稳定后依赖不在变动,将这部分依赖提前放在服务器中后续项目更新只用上传代码就可以 这样的打包方法无法使用java -jar命令启动 在启动时使用-Dloader.path指定lib路径具体指令如下
java -Dloader.path=./lib -jar xxx.jar
1.2 使用
1.2.1 在pom文件中添加配置
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<!-- 使用spring-boot-maven-plugin插件来创建一个可执行的Spring Boot应用程序。这个插件会创建一个包含所有需要的依赖(包括项目的和Spring Boot的)的ZIP文件。-->
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!--表示创建可执行文件-->
<executable>true</executable>
<!--输出格式为ZIP-->
<layout>ZIP</layout>
<!--这里是填写需要包含进去的jar,必须项目中的某些模块,会经常变动,那么就应该将其坐标写进来如果没有则nothing ,表示不打包依赖 -->
<includes>
<include>
<groupId>nothing</groupId>
<artifactId>nothing</artifactId>
</include>
<!-- 在include中引入的依赖不会被打包到外部lib中
<include>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
</include>-->
</includes>
</configuration>
</plugin>
<!--使用maven-dependency-plugin插件将所有的依赖复制到JAR文件外面的lib目录中-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<!--指定复制依赖到的路径-->
<outputDirectory>
${project.build.directory}/lib
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
1.2.2 配置启动文件
nohup java -jar ./home/app/demo.jar > -Dspring.config.location=tmp/springboottmp/test.yml --spring.profiles.active=test -Dserver.port=8080 -Dserver.address=127.0.0.1 > logs/demo.log 2>&1 &
nohup | 不挂断运行命令,当账户退出或终端关闭时,程序仍然运行 |
---|---|
java -jar | 运行jar包 |
-Dserver.port | 指定端口号 |
-Dserver.address | 指定IP |
-Dloader.path | 指定类加载器搜索类文件的路径允许指定一个或多个目录路径,这些路径将按顺序被用于搜索类文件。当Java应用程序尝试加载一个类时,类加载器将首先在这些指定的路径中查找类文件 |
1.2.3 部分命令
//查询进程
ps -ef | grep demo
//查看端口被占用
netstat -tln | grep 80
//不挂断运行命令
nohup java -jar demo.jar &
//cat:以纯文本形式显示整个文件的内容
cat filename
//more和less:逐页显示文件内容,可以按空格键向下翻页,按q键退出。
more filename
less filename
//head和tail:分别显示文件的头几行和尾几行。
head filename #显示前10行
tail filename #显示最后10行
//vi或vim:一个强大的文本编辑器,可以用来查看和编辑文件。
vi filename
//grep:查找文件中是否包含指定的字符串。
grep 'search_string' filename #搜索包含'search_string'的行
2. windows部署(winsw)
WinSW-x64.exe 文件下载https://github.com/winsw/winsw/releases 或在本页面直接下载
2.1配置 winsw 部署 jar 包
新建xml文件内容如下 将jar包与xml文件 和 WinSW-x64.exe放在同一文件夹下 并将名称均修改为同一名称
<configuration>
<!-- 唯一ID -->
<id>localUtilsId</id>
<!-- 服务名称,启动服务时使用 -->
<name>localUtilsService</name>
<description>This is local utils</description>
<executable>java</executable>
<arguments>-jar local-utils-0.0.1.jar</arguments>
<!-- 开机启动 -->
<startmode>Automatic</startmode>
<!-- 日志配置 不使用 %Base% 相对路径会导致 jar 包的日志不输出 -->
<logpath>%BASE%\logs</logpath>
<logmode>rotate</logmode>
</configuration>
2.2 安装/卸载服务
使用管理员运行PowerShell并进入到jar包所在目录
# 安装服务
./XXX.exe install
# 卸载服务(需要先停止服务
./XXX.exe uninstall
2.3启动或停止服务
# 启动服务
net start XXX
# 停止服务
net stop XXX
# winsw 命令行帮助手册
.\XXX.exe /?
或者在Windows打开服务就可找到刚刚安装的服务
windows查看进程
netstat -ano | findstr 8002
根据进程号(PID)寻找进程名称
tasklist | findstr 25812
终止进程
taskkill /F /PID 25812