shell脚本启动springboot项目
参考网址:
https://www.cnblogs.com/alliswell2king/p/11753289.html
https://mp.weixin.qq.com/s/FMn4gbGxTh-bmnZ5cajBPA
文章目录
1.快速构建springboot项目
快速构建springboot项目建议使用aliyun java initializr
访问地址:
https://start.aliyun.com/bootstrap.html
优势:
- 可以快速构建springboot项目,比spring initalizr方便快速
- 在线浏览代码
- 直接使用git clone获取代码
- 选择相应依赖可以有相应的示例代码生成
2.上传项目到测试centos服务器
- 使用git clone获取项目
- 导入idea写一个springmvc测试方法
HelloController
package com.example.springbootdemo.demos.web;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
/**
* @ClassName: HelloController
* @Author: 邵明
* @Date: 2021/9/6 19:33
* @Description:
*/
@RestController
public class HelloController {
@Value("${spring.application.name}")
private String projectName;
@Value("${server.port}")
private String port;
@GetMapping("/hello")
public Map hello(){
HashMap<Object, Object> map = new HashMap<>();
map.put("port",port);
map.put("projectName",projectName);
System.out.println("该springboot项目端口号为:"+port);
System.out.println("该springboot项目项目名为::"+projectName);
return map;
}
}
3.设置gitee仓库
-
自己在自己的gitee上新建一个仓库
-
设置ssh key公钥到gitee
参考:
https://gitee.com/help/articles/4191
4.idea同步到远程仓库
5.推送代码到远程仓库
重点说明:
一定推送到gitee远程仓库,而不是origin
6.登录centos服务器拉去代码
前提条件
-
安装jdk
-
maven
参考网址:
http://www.shaoming.club/archives/linux%E5%AE%89%E8%A3%85java%E5%AD%A6%E4%B9%A0%E7%8E%AF%E5%A2%83%E6%9C%AA%E5%AE%8C%E5%BE%85%E7%BB%ADmd
- 安装了git
安装git命令
yum install git -y
- 拉去项目到服务器
git clone <ssh地址>
7.测试项目是否可以访问
# 进入项目目录
cd ./springboot-demo
# 关联远程仓库取别名
git remote add gitee git@gitee.com:shao_ming314/springboot-demo.git
# maven进行打包
mvn package clean
启动项目
java -jar target/springboot-demo-0.0.1-SNAPSHOT.jar
访问网址: ip:8080/hello
测试成功
8.根据上述步骤写shell脚本
新建一个shell文件 start-springboot-demo.sh
内容如下:
#!/bin/bash
# 进入项目目录
cd /opt/project/demo_project/springboot-demo/
# 从仓库拉去项目 gitee(远程仓库别名),master(需要拉取的分支)
# 关联远程仓库的命令 git remote add gitee git@gitee.com:xxxxxx/springboot-demo.git
git pull gitee master
#拉取项目可能需要时间,等2秒钟
sleep 2
# maven打包 --先clean在打包过程中跳过测试,节省时间
mvn clean package -Dmaven.skip.test=true
# 打包可能需要一点时间,等十秒钟
sleep 10
#jar包文件路径及名称(目录按照各自配置)
APP_NAME=/opt/project/demo_project/springboot-demo/target/springboot-demo-0.0.1-SNAPSHOT.jar
#日志文件路径及名称(目录按照各自配置)
LOG_FILE=/opt/project/demo_project/springboot-demo/target/springboot-demo.log
#查询进程,并杀掉当前jar/java程序
pid=`ps -ef|grep $APP_NAME | grep -v grep | awk '{print $2}'`
kill -9 $pid
echo "$pid进程终止成功"
sleep 2
#判断jar包文件是否存在,如果存在启动jar包,并时时查看启动日志
if test -e $APP_NAME
then
echo '文件存在,开始启动此程序...'
# 启动jar包,指向日志文件,2>&1 & 表示打开或指向同一个日志文件
nohup java -jar $APP_NAME > $LOG_FILE 2>&1 &
#实时查看启动日志(此处正在想办法启动成功后退出)
tail -f $LOG_FILE
#输出启动成功(上面的查看日志没有退出,所以执行不了,可以去掉)
#echo '$APP_NAME 启动成功...'
else
echo '$APP_NAME 文件不存在,请检查。'
fi
9.测试是否成功
执行脚本
第一种方式
sh start-springboot-demo.sh
第二种方式
# 赋予脚本执行权限(测试期间,给777权限即可)
chmod 777 start-springboot-demo.sh
# 执行脚本
./start-springboot-demo.sh
成功打印日志并且浏览器可以访问
成功
个人csdn博客网址:https://blog.csdn.net/shaoming314
![jam](https://i-blog.csdnimg.cn/blog_migrate/902f9ea4e6285ceba0d18af8e9857bb2.jpeg)
个人博客网址:www.shaoming.club
![halo](https://i-blog.csdnimg.cn/blog_migrate/c2c1dd5b4b71e544d16229a48274dce9.jpeg)
勇敢牛牛,不怕困难
![img](https://i-blog.csdnimg.cn/blog_migrate/54e9eca74795660a3cac4b42388b9e4d.jpeg)