本地App Engine向Datastore注册实体时遇连接错误
Datastore模拟器连接失败:写入/查询实体时触发503错误
尝试向Datastore写入实体或执行查询时均触发错误,应用未连接到Datastore模拟器。
错误详情
调用<function make_call..rpc_call at 0x7fba88531510>时超出3次重试上限,最后异常为503 无法连接所有地址;最后错误:UNKNOWN: ipv4:127.0.0.1:8000: Trying to connect an http1.x server
运行方式:通过python3 main.py启动FastAPI应用,调用创建实体接口/test时触发错误。
期望:在本地Datastore模拟器中保存实体并创建对应的Kind。
相关代码
main.py
import uvicorn from fastapi import FastAPI from google.cloud import ndb app = FastAPI() ds_client = ndb.Client() class Neighborhood(ndb.Model): db_id = ndb.ComputedProperty(lambda self: self.key.id()) name = ndb.StringProperty(required=True) zone = ndb.StringProperty() active = ndb.BooleanProperty(default=True) @app.get("/test2") def fetch_neighborhood(): 'get most recent Neighborhood' with ds_client.context(): return Neighborhood.query().fetch() @app.post("/test") def store_neighborhood(): 'create new Neighborhood entity in Datastore' with ds_client.context(): namespace = 'client2' neighborhood = Neighborhood(id=123, namespace=namespace) neighborhood.name = "test" neighborhood.put() if __name__ == "__main__": uvicorn.run("main:app", host="0.0.0.0", port=8080, reload=True)
启动脚本(bash)
#!/bin/bash HOST=localhost PY_PATH=python27=/usr/bin/python2.7,python3=/usr/bin/python3 export APPLICATION_ID=dev~None export GOOGLE_APPLICATION_CREDENTIALS="./app_engine_service_account_dev.json" export DEPLOY_ENVIRONMENT=development export HTTP_HOST="localhost:8080" python3 ~/google-cloud-sdk/bin/dev_appserver.py \ --port=8080 \ --host=$HOST \ --support_datastore_emulator=true \ --runtime_python_path=$PY_PATH \ app-dev.yaml # --clear_datastore=yes \
app-dev.yaml
runtime: python310 entrypoint: gunicorn -w 4 -k uvicorn.workers.UvicornWorker main:app instance_class: F1 automatic_scaling: target_cpu_utilization: 0.7 min_instances: 1 max_instances: 1 min_pending_latency: 30ms # default value max_pending_latency: automatic max_concurrent_requests: 32 max_idle_instances: 0 skip_files: - ^(.*/)?#.*#$ - ^(.*/)?.*~$ - ^(.*/)?.*\.py[co]$ - ^(.*/)?.*/RCS/.*$ - ^(.*/)?\..*$ - ^(.*/)?.*\.bak$ - ^(.*/)?.*deploy.sh$ - ^(.*/)?docs/*$ - ^(.*/)?venv/*$ - ^(.*/)?downloads/*$ env_variables: DEPLOY_ENVIRONMENT: 'development' DATASTORE_EMULATOR_HOST: 'localhost:8000' DATASTORE_EMULATOR_HOST_PATH: 'localhost:8000/datastore' DATASTORE_HOST: 'http://localhost:8000' HTTP_HOST: 'localhost:8080' STORAGE_EMULATOR_HOST: 'http://localhost:9023'
解决方案
1. 确保Datastore模拟器已正常启动
- 单独启动模拟器:在终端执行以下命令启动Datastore模拟器
gcloud beta emulators datastore start --host-port=localhost:8000 - 若使用
dev_appserver.py启动,需检查启动日志,确认模拟器已成功启动(日志中会包含Datastore emulator running at localhost:8000相关信息)
2. 运行应用时加载必要环境变量
直接执行python3 main.py时,不会自动加载app-dev.yaml或bash脚本中的环境变量,需手动设置:
# 先加载模拟器环境变量(启动模拟器的终端执行后,复制输出的export命令到当前终端,或直接执行) $(gcloud beta emulators datastore env-init) # 设置项目ID(与脚本中一致) export APPLICATION_ID="dev~None" # 启动应用 python3 main.py
3. 代码优化:显式指定项目ID
修改main.py中的NDB客户端初始化,避免自动检测项目ID失败:
ds_client = ndb.Client(project="dev~None")
4. 检查端口占用
确认8000端口未被其他程序占用:
lsof -i :8000
若有占用进程,执行kill -9 <进程ID>杀掉后重启模拟器。
内容的提问来源于stack exchange,提问作者Matheus Theu
相关产品推荐
相关产品推荐

