Linux 部署项目

1.软件安装

1.1 Redis 安装

https://blog.csdn.net/weixin_43412762/article/details/133688635

1.2 MySQL 安装

https://blog.csdn.net/weixin_49080355/article/details/119215545

1.3 jdk 安装

https://blog.csdn.net/m0_50217781/article/details/112414571

1.4 nginx安装(可不进行安装)

https://blog.csdn.net/shallow72/article/details/123878716

2.软件启动

https://blog.csdn.net/wiwiwiwiiwo/article/details/134697693?spm=1001.2014.3001.5501

3.打包 jar 包,并实现后台运行

3.1 maven命令

mvn clean package -Dmaven.test.skip=true

clean 为清除 原 target 目录
package 为重新打包

3.2 本地上传至服务器

scp fileName.jar root@47.99.89.193:filePath/

fileName 为 jar 包名称;
root 为服务器用户名;
47.99.89.193为服务器IP;
filePath 为 jar 包服务器存放位置

3.2 后台运行

nohup java -jar fileName.jar >temp.txt 2>&1 &

nohup 表示后台运行,当前窗口关闭进程不会中断;
temp.txt 表示标准重定向输出,日志信息输出到 temp.txt 文件中,不会在终端显示;
2>&1 表示将标准输出和错误输出均输出到temp.txt 文件中
& 将命令置于后台运行,使得程序在后台持续运行,而不会阻塞当前终端会话。

3.3 部署优化

当前部署方式缺点
1.每次上传新的 jar 包需要暂停服务器当前 jar 包进程
2.每次运行的命令过长
3.jar 包内存大,从本地上传的速度慢

jar 包目前所占内存过大的原因:
jar 包内存占用:
在这里插入图片描述
解压 jar 包,发现 lib 包占内存过多,
在这里插入图片描述
lib 包 存放的内容为依赖,代码更新时一般不更新,所以可以把依赖放到服务器中
在这里插入图片描述
在服务器新建 lib 包,将 lib 包内内容上传至 lib 文件夹下,修改 pom 配置

<build>
        <finalName>community</finalName>
        <plugins>
            <!-- 解决jar中没有主清单属性问题 -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.nowcoder.community.CommunityApplication</mainClass>
                    <!-- jar包不携带依赖配置开始-->
                    <layout>ZIP</layout>
                    <includes>
                        <include>
                            <groupId>nothing</groupId>
                            <artifactId>nothing</artifactId>
                        </include>
                    </includes>
                    <!-- jar包不携带依赖部署配置结束-->
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

再次打包上传,现在 jar 包仅占 641K,上传速度也有原来的 39 s 变为 不到 1s

在这里插入图片描述
针对1、2缺点,我们把所有的执行命令写入到 shell 文件中

vim start.sh

shell 文件内容

# 关闭程序
fileName=community.jar
pid=$(ps -ef | grep $fileName| grep -v "grep" | awk '{print $2}')
kill -9 $pid

# 启动项目
nohup java -jar -Dloader.path=./lib  $fileName > community.log 2>&1 &

启动命令

sh start.sh

4.浏览器运行

  • 9
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值