You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Vertex AI上用FastAPI创建端点时遭遇/health接口404问题求助

健康检查404问题的修复方案

1. 修正代码缩进错误

Python对缩进敏感,原代码中startup函数内的代码存在缩进混乱,会导致语法错误或路由无法正确注册。修正后的startup函数如下:

@app.on_event("startup")
def startup():
    path = os.environ["AIP_STORAGE_URI"]
    path = os.path.join(path, model_file)
    logger.info(f"Loading model state from: {path}")

    if path.startswith("gs://"):
        destination_file_name = f"/tmp/{model_file}"
        storage_client = storage.Client()
        with open(destination_file_name, "wb") as file:
            # Download file from GCP
            storage_client.download_blob_to_file(path, file)
    else:
        logger.info("Local path...")
        destination_file_name = path
    
    model_files = joblib.load(destination_file_name)
    global_items["model"] = model_files

    logger.info("State successfully retrieved from GCP")

2. 修正主模块入口判断

原代码中if __name__ == "__name__"是错误的,会导致uvicorn启动逻辑无法触发,需改为:

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8080, reload=False, log_level="info")

3. 确认健康检查端点的正确性

  • 确保/health路由是全局级别的定义,没有被错误缩进包裹在其他函数内部。
  • 检查是否导入了必要的依赖模块:
from fastapi import FastAPI, Response, status
import os
import storage
import joblib
import uvicorn
from logging import getLogger

logger = getLogger(__name__)
app = FastAPI()
global_items = {}
model_file = "your_model_filename.joblib"  # 替换为实际模型文件名

4. 验证Vertex AI部署配置

  • Vertex AI默认使用GET /health进行健康检查,无需额外配置,确保服务监听端口为8080(代码中已设置)。
  • 本地测试:运行代码后执行curl http://localhost:8080/health,确认返回200状态码后再重新构建镜像部署。

内容的提问来源于stack exchange,提问作者Moustafa Ashraf

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 06:35:19