我正在努力解决一个奇怪的问题。我正在尝试使用AWS CodeBuild和CodeDeploy在ec2上部署基于tomcat的Java servlet应用程序。
[经过几节教程和视频之后,我成功设置了它,并且确实在ec2上部署了应用程序,但是我的服务没有响应所需的URL。我试图在端口8080上找到进程,但没有可用。
我的BuildSpec.yml如下图所示
version: 0.2
env:
variables:
JAVA_HOME: "/usr/lib/jvm/java-8-openjdk-amd64"
phases:
install:
commands:
- echo installing maven...
- apt-get update -y
- apt-get install -y maven
build:
commands:
- echo building Mudoku-webapp
- mvn install
artifacts:
files:
- target/*.war
- target/dependency/webapp-runner.jar
- scripts/*.sh
- appspec.yml
discard-paths: yes
cache:
paths:
- '/root/.m2/**/*'
我的appspec.yml如下所示:
version: 0.0
os: linux
files:
- source: /
destination: /home/ubuntu
hooks:
AfterInstall:
- location: fix_previleges.sh
timeout: 300
runas: root
ApplicationStop:
- location: stop_server.sh
timeout: 300
runas: root
ApplicationStart:
- location: start_server.sh
timeout: 300
runas: root
在部署期间执行的脚本为
fix_previleges.sh
:
#!/bin/bash
echo "Setting executable previliges for all the scripts and required jars"
chmod +x /home/ubuntu/*.jar
chmod +x /home/ubuntu/*.sh
echo "Done setting the previliges for needed scripts and jars"
start_server.sh
:
#!/bin/bash
echo "deploying Mudoku-webapp.war"
java -jar webapp-runner.jar *.war </dev/null &>/dev/null &
echo "done deploying Mudoku-webapp.war"
stop_server.sh
:
#!/bin/bash
echo "Killing process on port 8080"
kill -9 $(lsof -t -i:8080)
echo "Done killing the process on port 8080"
部署日志按预期显示
[2020-04-25 15:48:42.336] [d-OB5YU0VQ2]LifecycleEvent - ApplicationStop
[2020-04-25 15:48:42.336] [d-OB5YU0VQ2]Script - stop_server.sh
[2020-04-25 15:48:42.389] [d-OB5YU0VQ2][stdout]Killing process on port 8080
[2020-04-25 15:48:42.398] [d-OB5YU0VQ2][stderr]kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]
[2020-04-25 15:48:42.398] [d-OB5YU0VQ2][stdout]Done killing the process on port 8080
[2020-04-25 15:48:46.679] [d-OB5YU0VQ2]LifecycleEvent - AfterInstall
[2020-04-25 15:48:46.680] [d-OB5YU0VQ2]Script - fix_previleges.sh
[2020-04-25 15:48:46.736] [d-OB5YU0VQ2][stdout]Setting executable previliges for all the scripts and required jars
[2020-04-25 15:48:46.738] [d-OB5YU0VQ2][stdout]Done setting the previliges for needed scripts and jars
[2020-04-25 15:48:47.723] [d-OB5YU0VQ2]LifecycleEvent - ApplicationStart
[2020-04-25 15:48:47.723] [d-OB5YU0VQ2]Script - start_server.sh
[2020-04-25 15:48:47.775] [d-OB5YU0VQ2][stdout]deploying Mudoku-webapp.war
[2020-04-25 15:48:47.776] [d-OB5YU0VQ2][stdout]done deploying Mudoku-webapp.war
由于没有在端口8080上运行的进程,所以预料中会发生错误,但是我认为这不会影响进一步的执行。
日志中的最后两个语句显然暗示start_server.sh
已成功执行。
有趣的是,AWS CodePipeline复制了所有所需的构建工件,并按预期复制。
如果我使用命令在ec2实例上手动运行start_server.sh
ubuntu@ip-172-31-27-194:~$ ls
Mudoku-webapp.war appspec.yml fix_previleges.sh install start_server.sh stop_server.sh webapp-runner.jar
ubuntu@ip-172-31-27-194:~$ ./start_server.sh
deploying Mudoku-webapp.war
done deploying Mudoku-webapp.war
ubuntu@ip-172-31-27-194:~$
由于我已经花了两天时间来调试它而没有任何收获,所以高度赞赏任何回答和提示。
显然您的应用程序不是由CodeDeploy启动的。您看到两行的事实:
"deploying Mudoku-webapp.war"
&
"done deploying Mudoku-webapp.war"
什么也没有,有趣的输出来自:
java -jar webapp-runner.jar *.war </dev/null &>/dev/null &
重定向为null。您的应用程序是否需要交互式终端才能工作?您可以将Java执行的输出重定向到文件并检查是否有任何线索。