spring boot 打包并部署到线上服务

一:打包前置准备

在pom.xml增加打包插件

复制代码
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

上面的配置是将所有依赖打包进jar包中,如果想要将依赖单独放到一个目录下(分离式打包),可以参照下面的配置

复制代码
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <!-- 生成的jar中,不要包含pom.xml和pom.properties这两个文件 -->
                    <addMavenDescriptor>false</addMavenDescriptor>
                    <manifest>
                        <!-- 是否要把第三方jar加入到类构建路径 -->
                        <addClasspath>true</addClasspath>
                        <!-- 外部依赖jar包的最终位置 -->
                        <classpathPrefix>lib/</classpathPrefix>
                        <!-- 项目启动类 -->
                        <mainClass>com.example.demo.DemoApplication</mainClass>
                    </manifest>
                    <!-- 额外手动追加一个本地包 GmSSLJNI-3.1.1.jar 到ClassPath -->
                    <!--<manifestEntries>
                        <Class-Path>lib/GmSSLJNI-3.1.1.jar</Class-Path>
                    </manifestEntries>-->
                </archive>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy-compile-lib</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>target/lib</outputDirectory>
                        <excludeTransitive>false</excludeTransitive>
                        <stripVersion>false</stripVersion>
                        <includeScope>compile</includeScope>
                    </configuration>
                </execution>
                <execution>
                    <id>copy-runtime-lib</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>target/lib</outputDirectory>
                        <excludeTransitive>false</excludeTransitive>
                        <stripVersion>false</stripVersion>
                        <includeScope>runtime</includeScope>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

二:maven打包

复制代码
mvn clean package -DskipTests

执行上面打包命令对项目进行打包操作,打包成功后早target目录下会生成打包的jar包,如果使用第二个配置,会在target目录ixa多一个lib的目录,目录里放的是说需要的依赖包

三:启动 Spring Boot 打包应用

复制代码
java -Xms512M -Xmx512M -jar .\demo-0.0.1-SNAPSHOT.jar --server.port=6290 --spring.profiles.active=prod > app.log 2>&1

参数说明

  • -Xmx512M:JVM 最大堆内存:最大可用 512MB
  • -Xms512M:JVM 初始堆内存:启动就分配 512MB
  • --server.port:服务监听端口
  • --spring.profiles.active=prod:启用 prod 生产配置
  • > app.log 2>&1:日志输出到 app.log

四:nginx配置

1:打包服务启动

将打包后的jar包上传到服务器上,然后运行打包项目

复制代码
nohup java -Xms512M -Xmx512M -jar .\demo-0.0.1-SNAPSHOT.jar --server.port=6290 --spring.profiles.active=prod > app.log 2>&1 &

上面命令的缺点是进程挂了后不会自动重启,正式环境建议使用systemd托管实现开机自启、进程崩溃自动重启

在/etc/systemd/system目录下创建demo.service文件

复制代码
[Unit]
# 服务描述
Description=demo SpringBoot Service
# 在网络就绪之后再启动
After=network.target
# 依赖网络
Wants=network.target

[Service]
# 执行用户,宝塔一般用www;如果是其他用户改成root
User=www
Group=www

# JAVA_HOME 环境变量,指定你的jdk
Environment="JAVA_HOME=/www/server/java/jdk1.8.0_371"

# 核心启动命令,JVM参数在-jar前面,应用参数放jar后面
ExecStart=/www/server/java/jdk1.8.0_371/bin/java \
-Xms512M \
-Xmx512M \
-XX:MetaspaceSize=128M \
-XX:MaxMetaspaceSize=256M \
-XX:+HeapDumpOnOutOfMemoryError \
-XX:HeapDumpPath=/www/log/demo-heap.hprof \
-Duser.timezone=Asia/Shanghai \
-Dfile.encoding=UTF-8 \
-jar /www/wwwroot/demo/demo-0.0.1-SNAPSHOT.jar \
--server.port=6290 \
--spring.profiles.active=prod

# ==== 自动重启配置 ====
# 退出时自动重启
Restart=on-failure
# 重启条件:非0异常退出码才重启(正常stop不重启)
RestartSec=5
# 5秒后重试启动

# 最大文件打开数(防止高并发too many open files)
LimitNOFILE=65535

# 日志输出
StandardOutput=journal+console
StandardError=journal+console

[Install]
# 开机自启,多用户模式
WantedBy=multi-user.target

systemctl相关命令参考

复制代码
# 1. 重新加载systemd,识别新服务
systemctl daemon-reload

# 2. 设置开机自启
systemctl enable demo.service

# 3. 启动服务
systemctl start demo

# 查看状态
systemctl status demo

# 查看实时日志
journalctl -u demo -f

# 停止服务
systemctl stop demo

# 重启服务(更新jar包后用)
systemctl restart demo

2:nginx配置

复制代码
server {
    listen 80;
    server_name demo.com; # 替换为你的域名或IP

    # 静态资源缓存优化(可选)
    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
        expires 30d;
        add_header Cache-Control "public, immutable";
    }

    # 反向代理到 Spring Boot
    location / {
        proxy_pass http://127.0.0.1:6290;
        
        # 传递真实客户端信息
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        
        # WebSocket 支持(如果应用需要)
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        
        # 超时设置,避免大文件或慢接口报错
        proxy_connect_timeout 60s;
        proxy_send_timeout 60s;
        proxy_read_timeout 60s;
    }
}

重启nginx生效

复制代码
nginx -t
nginx -s reload

根据如上我们就可以实现spring boot项目打包上线

相关推荐
土司大王1 小时前
LeetCode hot100——实现 Trie (前缀树)
java·算法·leetcode
lhldsg1 小时前
从零构建智慧场馆解决方案小程序:开发全流程实战
java·前端·小程序
明志数科1 小时前
具身智能数据工程观察:从集中式数采工厂到真实场景采集,数据供给路线正在转向
大数据·数据库·人工智能
小七在进步1 小时前
类和对象(一)
java·数据结构·算法
天行健,君子而铎1 小时前
数据分类分级的范式转换:从规则匹配到模型持续优化规模化前瞻算法
大数据·网络·数据库·安全·分类
云雀衔光1 小时前
多个 MCP Server 怎么编排:数据库 / Redis / Git / 飞书一把梭
java·数据库·人工智能·redis·git·语言模型·飞书
慧都小妮子1 小时前
GcExcel 服务端 Excel 组件:无需安装 Office 的 Java/.NET 报表方案
java·.net·excel·gcexcel·服务端excel组件
卓怡学长1 小时前
w236基于springboot应用型本科院校共享在线考试系统
java·spring boot·spring·intellij-idea
bksczm1 小时前
MySQL基础之数据类型
数据库·mysql