requests.get(endpoint/ping)
在CDK中,我定义了我的集成AS
# Health check route
@router.get("/ping", status_code=status.HTTP_200_OK)
async def ping():
logging.info(f"ping request received.")
# SageMaker will ping this to check if the endpoint is healthy
return JSONResponse({"status": "ok"})
我的理论是
需要VPC链接,并且侦听器对象不足。 需要一些参数,以确保API网关不会修改响应
其他解释
httpapihttpalBintegration
// Create the HTTP ALB Integration
const albIntegration = new apigwv2_integrations.HttpAlbIntegration(`AlbIntegration-${props.stageName}`,
listener,
);
// Create the HTTP API with the integration
this.httpApi = new apigwv2.HttpApi(this, `HttpApi-${props.stageName}`, {
defaultIntegration: albIntegration,
});
您的CDK部署成功了,请求对API网关端点的请求贯穿服务。
{"status": "ok"}
🔍根本原因
🔄HTTPALB健康检查默认行为
默认路由
时,API网关期望ALB的响应将为HTTP符合http code-,包括headers
和/ping
✅solution
"Healthy Connection"
HttpAlbIntegration
,更严格。处理RAW HTTP风格的响应(因为您的FastApi应用自然会返回),您想使用付款格式1.0,就像这样:
/ping
该版本假设您正在使用CDKv2。
🔧2。确保ALB健康检查不会干扰
检查您的ALB目标组的Health检查路径。如果碰到payloadFormatVersion: '1.0'
,请确保不会超越响应(某些框架可能会以不同的方式对待健康检查)。考虑将ALB健康检查路径与您的公共路线分开,例如使用:
2.0
然后将ALB健康检查配置为使用const albIntegration = new apigwv2_integrations.HttpAlbIntegration(`AlbIntegration-${props.stageName}`, listener, { parameterMapping: undefined, payloadFormatVersion: apigwv2.PayloadFormatVersion.VERSION_1_0,
});
而不是
/ping
。
3. 3. confirm fastapi响应适当
您已经返回了jsonresponse,这很好。但是确认标题和状态代码的设置正确:
/ping
@router.get("/healthcheck")
async def healthcheck():
return "OK"
/healthcheck
✅避免将ALB健康检查路由到面向用户的端点
✅确认您的ALB目标组健康(在控制台中检查)✅确保Fargate Service的安全组允许从Alb
🧪本地测试如果您直接击中
alb URL(绕过API网关),您会看到正确的JSON吗?如果是,那么问题可能是API网关如何解释ALB的响应的问题 - 这进一步支持设置
/ping
至from fastapi.responses import JSONResponse
from fastapi import status
@router.get("/ping", status_code=status.HTTP_200_OK)
async def ping():
return JSONResponse(status_code=status.HTTP_200_OK, content={"status": "ok"})
。