【问题标题】:Cannot reach port of gcloud appengine devserver无法访问 gcloud appengine devserver 的端口
【发布时间】:2020-02-26 19:22:53
【问题描述】:

为了测试,我尝试在 docker 内运行 gcloud devserver 并附上该注释:

sudo /usr/local/gcloud/google-cloud-sdk/bin/java_dev_appserver.sh --disable_update_check --port=8888 --help /app/target/qdacity-war/ 2>&1 | sudo tee /app/logs/devserver.log > /dev/null &

为了检查开发服务器是否启动成功,我使用了这个脚本:

#!/bin/bash
# This script waits until the port 8888 is open.

SERVER=localhost
PORT=8888

TIMEOUT=180
TIME_INTERVAL=2

PORT_OPEN=1
PORT_CLOSED=0

time=0
isPortOpen=0

while [ $time -lt $TIMEOUT ] && [ $isPortOpen -eq $PORT_CLOSED ];
do 

    # Connect to the port
    (echo > /dev/tcp/$SERVER/$PORT) >/dev/null 2>&1
    if [ $? -ne 0 ]; then
        isPortOpen=$PORT_CLOSED
    else
        isPortOpen=$PORT_OPEN
    fi

    time=$(($time+$TIME_INTERVAL))
    sleep $TIME_INTERVAL
done

if [ $isPortOpen -eq $PORT_OPEN ]; then
    echo "Port is open after ${time} seconds."

    # Give the server more time to properly start
    sleep 10
else
    echo "Reached the timeout (${TIMEOUT} seconds). The port ${SERVER}:${PORT} is not available."

    exit 1
fi

跑完所有的测试,我才回来:

Reached the timeout (180 seconds). The port localhost:8888 is not available.

我无法确定启动开发服务器或查询端口是否有任何问题。 有没有人有想法或解决方案? 谢谢!

【问题讨论】:

  • lsof -i :8888 的输出是什么?这应该为您提供当前正在该端口上侦听的任何进程的进程 ID (PID)。
  • 您是否尝试过使用其他端口运行您的应用服务器?
  • @Gerb 谢谢,我使用了“sudo lsof -i :8888”,但没有得到任何输出,没有表格或错误:/
  • @AndresS 谢谢,但是更改端口会导致相同的结果。

标签: bash gcloud devserver


【解决方案1】:

默认情况下,仅接受 localhost|loopback 流量,您无法远程访问服务器。

请尝试添加--address=0.0.0.0: (link) 到你的java_dev_appserver.sh 命令。

示例

使用了 Google 的 HelloWorld 示例的变体。

使用mvn appengine:run 运行此程序(以确认其工作并构建 WAR)。

然后/path/to/bin/java_dev_appserver.sh ./target/hellofreddie-0.1(确认它与本地开发服务器一起工作)。

然后使用谷歌的Cloud SDK容器镜像(link),将之前生成的WAR目录挂载到其中,并在:9999上运行服务器:

docker run \
--interactive \
--tty \
--publish=9999:9999 \
--volume=${PWD}/target:/target \
google/cloud-sdk \
  /usr/lib/google-cloud-sdk/bin/java_dev_appserver.sh \
  --address=0.0.0.0 \
  --port=9999 \
  ./target/hellofreddie-0.1

能够卷曲端点:

curl \
--silent \
--location \
--write-out "%{http_code}" \
--output /dev/null \
localhost:9999

返回200

并且,运行使用PORT=9999 调整的脚本会返回:

Port is open after 2 seconds.

【讨论】:

  • 我使用了一个简单的 HelloFreddie 示例,生成了一个 WAR(目录)并在 docker run --interactive --tty --publish=9999:9999 --volume=${PWD}/target:/target google/cloud-sdk 中运行 java_dev_appserver.sh --address=0.0.0.0 --port=9999 ./target/hellofreddie-0.1,并且能够按预期卷曲端点。因此,对于在 docker 容器中运行应用程序的简单案例,这似乎对我有用。
猜你喜欢
  • 2021-12-28
  • 1970-01-01
  • 1970-01-01
  • 2013-03-18
  • 1970-01-01
  • 2015-01-12
  • 2019-06-01
  • 2021-07-07
  • 2021-02-09
相关资源
最近更新 更多