无法访问gcloud appengine devserver的端口

时间:2019-10-31 12:20:07

标签: bash gcloud devserver

为了进行测试,我尝试在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 &

要检查devserver是否已成功启动,我使用以下脚本:

#!/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.

我无法确定启动devserver或查询端口是否有任何问题。 有人有想法或解决方案吗? 谢谢!

1 个答案:

答案 0 :(得分:0)

默认情况下,仅接受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(以确认它可用于本地开发服务器)。

然后使用Google的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.

HTH!