当前位置:   article > 正文

linux部署java项目

linux部署java项目

一、部署war包的sh文件

#!/bin/bash
# ------------------------------ environment variable
# define script file name. When delete ./ files, don't delete self.
MY_NAME=middleware.sh
# 分支
branch_name=test
# define the Tomcat param
TOMCAT_HOME=/usr/local/tomcat/apache-tomcat-9.0.69
TOMCAT_PORT=8080
# project name, also is default context.
PROJandCONTEXT_NAME=middleware
# max try times. It's second.
max_trial_time=100
sleep_time=7

#GIT_DIR
GIT_DIR=/usr/local/tomcat/

# 启用下划线效果
OPEN_ULINE=\\033[4m
CLOSE_ULINE=\\033[24m

#拉取最新代码
echo " 拉取最新代码 "
cd /usr/local/tomcat/middleware
git pull
sleep 5
git checkout -b $branch_name origin/$branch_name
sleep 5
git pull

echo " [1] execute maven package. Command:mvn -U clean compile package -Ptest"
mvn -U clean compile package -Ptest

echo -e "\n$OPEN_ULINE [3] delete old package and deploy new package  $CLOSE_ULINE"
echo " [2]  delete all file or dir in $TOMCAT_HOME/webapps start with $PROJandCONTEXT_NAME."

TOMCAT_PID_INFO=`ps -ef | grep $TOMCAT_NAME`
PID_LIST=`ps -ef | grep $TOMCAT_NAME | grep -v "grep" | awk '{print $2}'`

# ------------------------------ environment variable end ---------------------------------
# ------------------------------ pre-check
echo -e "\n$OPEN_ULINE [3] pre-check(前置校验)  $CLOSE_ULINE"
echo " TOMCAT_HOME: $TOMCAT_HOME "
echo " MY_NAME: $MY_NAME "
echo " web context name: $PROJandCONTEXT_NAME "
# check directory is exist.
if [ -d $TOMCAT_HOME ]; then
    echo -e " [ OK ] Tomcat directory check "
else
    echo -e " [ FAIL ] Tomcat directory check "
    exit 0
fi
# check Maven
$(mvn --version >/dev/null 2>&1)
echo -e "check maven exist."
hasMaven=$?
if [ "$hasMaven" == "127" ]; then
        echo " [ERROR] maven is not exists!quit!"
        exit 0
else
        echo " [  OK ] you have install maven. continue..."
fi
# ------------------------------ pre-check end ---------------------------------
# ------------------------------ function define
function stopTomcatService () {
        # shutdown tomcat
        # use command netstat to find whether the port is closed.
        for (( i=$sleep_time; i<$max_trial_time; i=i+$sleep_time ))
        do
                if [ "$(ps -ef | grep apache-tomcat-9.0.63 | grep -v "grep" | awk '{print $2}')" == "" ]; then
					echo " Tomcat service not started or has been closed normally."
					break
				else
				kill -9 $(ps -ef | grep apache-tomcat-9.0.63 | grep -v "grep" | awk '{print $2}')
                fi
                sleep $sleep_time
        done
}
# ------------------------------ function define end ---------------------------------

echo "[4] Pre-check"
echo " Tomcat Home: $TOMCAT_HOME "
echo " Tomcat Name: $TOMCAT_NAME "
echo " TOMCAT_PID_INFO: ps -ef | grep $TOMCAT_NAME"
echo " TOMCAT_PID_INFO: $TOMCAT_PID_INFO"
echo " Tomcat PID_LIST: $PID_LIST"

echo "[5] Shutdown Tomcat normally."
if [ "$PID_LIST" != "" ]; then
	echo " [5.1] Shut down the tomcat server normally..."
	$TOMCAT_HOME/bin/shutdown.sh >/dev/null 2>&1
	echo " [5.2] The normal shutdown program ends. Sleep for 5 seconds..."
	sleep 5
fi

echo -e "\n$OPEN_ULINE [4] stop tomcat service  $CLOSE_ULINE"
stopTomcatService

echo "tomcat 已停止服务"

sleep 10

rm -rf $TOMCAT_HOME/webapps/$PROJandCONTEXT_NAME
rm -rf $TOMCAT_HOME/webapps/$PROJandCONTEXT_NAME.war

echo " [6] 删除旧包"

sleep 30

find $TOMCAT_HOME/webapps/ -maxdepth 1 -name $PROJandCONTEXT_NAME* -exec rm -r {} \;
echo " [7] copy package to tomcat webapps."
tmp_dir=$GIT_DIR/middleware/target/$PROJandCONTEXT_NAME.war
cp $GIT_DIR/middleware/target/$PROJandCONTEXT_NAME.war  $TOMCAT_HOME/webapps/
# cp -R ./target/$PROJandCONTEXT_NAME $TOMCAT_HOME/webapps/
echo -e "\n$OPEN_ULINE [6] start tomcat  $CLOSE_ULINE"
$TOMCAT_HOME/bin/startup.sh
echo " ========================================== "
echo " ***      mes-data-middleware SUCCESS               *** "
echo " ========================================== "

sleep 10

echo "tail -f $TOMCAT_HOME/logs/catalina.out"
# tail -f $TOMCAT_HOME/logs/catalina.out
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125

二、war包部署成功后,但访问不了接口

Springboot项目的入口类长这样:(这样的是以jar包的形式启动)

@SpringBootApplication
public class MiddlewareApplication {
	public static void main(String[] args) {
		SpringApplication.run(MiddlewareApplication.class, args);
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

war包的需要改成这样:(继承SpringBootServletInitializer类,并重写configure方法)

@SpringBootApplication
public class MiddlewareApplication extends SpringBootServletInitializer {

	public static void main(String[] args) {
		SpringApplication.run(MiddlewareApplication.class, args);
	}
	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
		return application.sources(MiddlewareApplication.class);
	}
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/693859
推荐阅读
相关标签
  

闽ICP备14008679号