首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在部署GCP云函数(Gen2)时修复缺少的端口问题?

如何在部署GCP云函数(Gen2)时修复缺少的端口问题?
EN

Stack Overflow用户
提问于 2022-11-23 04:36:32
回答 1查看 62关注 0票数 0

我试图在GCP中部署一个云函数(gen2),但是遇到相同的问题,并在每次部署云函数设置云运行时得到这个错误:

用户提供的容器无法在PORT=8080环境变量定义的端口上启动和侦听。

MAIN.PY

代码语言:javascript
运行
复制
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()

码头文件

代码语言:javascript
运行
复制
# 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、Main.py和Cloud在本地运行时,这一切似乎都在本地运行。但是,一旦我部署,我马上就会得到一个错误。

有人能指点我朝正确的方向吗?我需要在哪里编辑端口#,这样我的云功能才能成功部署?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-11-23 14:06:00

上面的错误可能是由侦听器端口的配置问题引起的,这可能是用户定义的值设置中的一些不匹配。您可以检查和验证以下指示,以了解错误的可能原因,并纠正这些指示,试图消除此问题:

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

代码语言:javascript
运行
复制
const port = parseInt(process.env.PORT) || 8080;
app.listen(port, () => {
  console.log(`helloworld: listening on port ${port}`);
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74541847

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档