如何使用 AWS CodeDeploy 对自动部署进行故障排除 "Port 8080 was already in use"?

How do I troubleshoot "Port 8080 was already in use" with automated deployments with AWS CodeDeploy?

我正在练习 Spring 使用 Travis CIAWS S3AWS CodeDeploy 启动自动部署。目前,如果我这样做git push,自动部署正在进行中,但会出现以下问题。

***************************
APPLICATION FAILED TO START
***************************

Description:

Web server failed to start. Port 8080 was already in use.

Action:

Identify and stop the process that's listening on port 8080 or configure this application to listen on another port.

所以,我使用下面的命令来查看使用端口8080的进程。

sudo lsof -i TCP:8080

用户产生了 ec2-user。如果我用kill pid kill ec2-user 来使用端口8080,服务器就不能用了。如何解决?

[编辑]

appspec.yml

hooks:
  ApplicationStart:
    - location: deploy.sh
      timeout: 60
      runas: ec2-user

deploy.sh

#!/bin/bash

REPOSITORY=/home/ec2-user/app/travis
PROJECT_NAME=springboot-test

echo "> Copy Build File"

cp $REPOSITORY/zip/*.jar $REPOSITORY/

echo "> confirm current application pid"

CURRENT_PID=$(pgrep -fl $PROJECT_NAME | grep jar | awk '{print }')

echo "> pid: $CURRENT_PID"

if [ -z "$CURRENT_PID" ]; then
  echo "> No application"
else
  echo "> kill -15 $CURRENT_PID"
  kill -15 $CURRENT_PID
  sleep 5
fi

echo "> Deploy new application"

JAR_NAME=$(ls -tr $REPOSITORY/*.jar | tail -n 1)

echo "> JAR Name: $JAR_NAME"

echo "> Add +x in $JAR_NAME"

chmod +x $JAR_NAME

echo "> run $JAR_NAME"

nohup java -jar \
  -Dspring.config.location=/home/ec2-user/app/application.yml \
  -Dspring.profiles.active=real \
  $JAR_NAME > $REPOSITORY/nohup.out 2>&1 &

您应该将 ApplicationStop 挂钩添加到您的 appspec.yml 中。挂钩首先执行,您应该定义它以停止您的 Web 服务器。