docker 如何在部署GCP云功能(第2代)时修复缺少端口的问题?

edqdpe6u  于 2022-11-28  发布在  Docker
关注(0)|答案(1)|浏览(122)

我尝试在GCP中部署云功能(gen2),但遇到相同的问题,并且在云功能设置云运行时每次部署都出现此错误:
用户提供的容器无法启动并侦听PORT=8080环境变量所提供的定义端口。

主要年份

from google.cloud import pubsub_v1
from google.cloud import firestore
import requests
import json
from firebase_admin import firestore
import google.auth

credentials, project = google.auth.default()


# API INFO
Base_url = 'https://xxxxxxxx.net/v1/feeds/sportsbookv2'
Sport_id = 'xxxxxxxx'
AppID = 'xxxxxxxx'
AppKey = 'xxxxxxxx'
Country = 'en_AU'
Site = 'www.xxxxxxxx.com'

project_id = "xxxxxxxx"
subscription_id = "xxxxxxxx-basketball-nba-events"
timeout = 5.0

subscriber = pubsub_v1.SubscriberClient()
subscription_path = subscriber.subscription_path(project_id, subscription_id)

db = firestore.Client(project='xxxxxxxx')

def winodds(message: pubsub_v1.subscriber.message.Message) -> None:

        events = json.loads(message.data)
        event_ids = events['event_ids']

        url = f"{Base_url}/betoffer/event/{','.join(map(str, event_ids))}.json?app_id={AppID}&app_key={AppKey}&local={Country}&site={Site}"
        print(url)

        windata = requests.get(url).text
        windata = json.loads(windata)

        for odds_data in windata['betOffers']:
            if odds_data['betOfferType']['name'] == 'Head to Head' and 'MAIN' in odds_data['tags']:
                event_id = odds_data['eventId']
                home_team = odds_data['outcomes'][0]['participant']
                home_team_win_odds = odds_data['outcomes'][0]['odds']
                away_team = odds_data['outcomes'][1]['participant']
                away_team_win_odds = odds_data['outcomes'][1]['odds']

                print(f'{event_id} {home_team} {home_team_win_odds} {away_team} {away_team_win_odds}')

                # WRITE TO FIRESTORE
                doc_ref = db.collection(u'xxxxxxxx').document(u'basketball_nba').collection(u'win_odds').document(f'{event_id}')
                doc_ref.set({
                    u'event_id': event_id,
                    u'home_team': home_team,
                    u'home_team_win_odds': home_team_win_odds,
                    u'away_team': away_team,
                    u'away_team_win_odds': away_team_win_odds,
                    u'timestamp': firestore.SERVER_TIMESTAMP,
                })

streaming_pull_future = subscriber.subscribe(subscription_path, callback=winodds)
print(f"Listening for messages on {subscription_path}..\n")

# Wrap subscriber in a 'with' block to automatically call close() when done.
with subscriber:
    try:
        # When `timeout` is not set, result() will block indefinitely,
        # unless an exception is encountered first.
        streaming_pull_future.result()
    except TimeoutError:
        streaming_pull_future.cancel()  # Trigger the shutdown.
        streaming_pull_future.result()  # Block until the shutdown is complete.

if __name__ == "__main__":
    winodds()

对接文件

# Use the official Python image.
# https://hub.docker.com/_/python
FROM python:3.10

ENV APP_HOME /app
WORKDIR $APP_HOME
COPY . .

ENV GOOGLE_APPLICATION_CREDENTIALS /app/xxxxx-key.json

ENV PORT 8080

# Install production dependencies.
RUN pip install functions-framework
RUN pip install -r requirements.txt

# Run the web service on container startup.
CMD exec functions-framework --target=winodds --debug --port=$PORT

我正在使用PyCharm,当我通过Docker、www.example.com和Cloud Run在本地运行时,它似乎都在本地工作Main.py。但我一部署就立即得到一个错误。
有人能给我指出正确的方向吗?我需要在哪里编辑端口号,以便我的云功能能够成功部署?

btqmn9zl

btqmn9zl1#

上述错误可能是由监听程序端口的配置问题导致的,这可能是用户定义的值设置中的某些不匹配。您可以检查并验证以下指针,以了解错误的可能原因,并纠正这些错误以尝试消除问题:

  • 检查是否已将服务配置为侦听所有网络接口,这些接口0.0.0.0在故障排除问题中通常表示为www.example.com
  • 已按照Google最佳实践配置PORT
  • 已根据“Deploy a Python service to Cloud Run”指南配置应用程序中的端口。

您可以首先检查下面的简单示例,以检查它们是否正常工作。

const port = parseInt(process.env.PORT) || 8080;
app.listen(port, () => {
  console.log(`helloworld: listening on port ${port}`);
});

相关问题