是否可以启动两个 dev_appserver.py 连接到同一个 google 云数据存储模拟器?

Is it possible to start two dev_appserver.py connecting to the same google cloud datastore emulator?

用例:我正在 python 开发一个 appengine 标准应用程序,另一个正在开发。我希望将来自两个应用程序的实体放在同一个数据存储区中。这可能吗?

当我使用标志 --support_datastore_emulator=true 和特定的 --datastore_path 启动第一个 dev_appserver.py 时,gcloud beta emulators datastore env-init 命令失败并显示 ERROR: (gcloud.beta.emulators.datastore.env-init) Unable to find env.yaml in the data_dir [~/.config/gcloud/emulators/datastore]. Please ensure you have started the appropriate emulator.

是的,这是可能的,但你需要仔细设置它。

首先要记住的是,只有一个模拟进程(无论是模拟器本身还是开发服务器)应该处理某个数据存储模拟目录,运行同时处理多个模拟进程很可能会导致数据损坏,您可能已经观察到试图 运行 2 台开发服务器共享相同的 --datastore_path 配置。

所以您只需要一个数据存储模拟器进程来处理存储目录。您不希望开发服务器(它们也有能力或 运行 宁他们自己的数据存储模拟,但以独立的方式)也处理该存储目录,因此您不应该使用 --datastore_path 选项。开发服务器应该只与数据存储模拟器进程对话以获取所有数据存储访问。

当您启动数据存储模拟器进程时,您将在其日志中看到一行指定您需要传递给要共享该数据存储模拟实例的所有开发服务器的 DATASTORE_EMULATOR_HOST 环境变量:

[datastore] API endpoint: http://0.0.0.0:5555
[datastore] If you are using a library that supports the DATASTORE_EMULATOR_HOST environment variable, run:
[datastore] 
[datastore]   export DATASTORE_EMULATOR_HOST=0.0.0.0:5555
[datastore] 
[datastore] Dev App Server is now running.

因此,在要启动开发服务器的 shell(s) 中设置该环境变量,然后仅使用这些数据存储相关选项启动服务器:

export DATASTORE_EMULATOR_HOST=0.0.0.0:555
[.../]dev_appserver.py --support_datastore_emulator=true ...

这个 env var 是唯一需要的东西,在这里你不需要 运行 gcloud beta emulators datastore env-init, 在服务器的日志中,您应该看到这样一行:

WARNING 2018-06-14 13:54:41,238 api_server.py:581] Detected environment variable DATASTORE_EMULATOR_HOST=0.0.0.0:5555, dev_appserver will speak to the Cloud Datastore emulator running on this address. The datastore_path /some_path_you_may_have_used_before/datastore.db will be neglected. If you want datastore to store on /some_path_you_may_have_used_before/datastore.db, remove DATASTORE_EMULATOR_HOST from environment variables and restart dev_appserver

当开发服务器首次访问数据存储时,您会在数据存储模拟器日志中看到一些条目,例如:

[datastore] Jun 14, 2018 10:02:53 AM io.gapi.emulators.grpc.GrpcServer operationComplete
[datastore] INFO: Adding handler(s) to newly registered Channel.
[datastore] Jun 14, 2018 10:02:53 AM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
[datastore] INFO: Detected HTTP/2 connection.

如果 DATASTORE_EMULATOR_HOST 没有指向 运行ning 数据存储模拟器进程(或者与它的通信有问题),您将在开发服务器的日志中看到错误,可能与这些类似:

ERROR    2018-06-14 14:39:08,026 api_server.py:373] Exception while handling datastore_v3.Get()
Traceback (most recent call last):
  File "/home/usr_local/google-cloud-sdk-204.0.0/platform/google_appengine/google/appengine/tools/devappserver2/api_server.py", line 333, in _handle_POST
    response = service_stub.MakeSyncCallForRemoteApi(request)
  File "/home/usr_local/google-cloud-sdk-204.0.0/platform/google_appengine/google/appengine/tools/devappserver2/datastore_grpc_stub.py", line 190, in MakeSyncCallForRemoteApi
    request_pb, _TIMEOUT)
  File "/home/usr_local/google-cloud-sdk-204.0.0/platform/google_appengine/lib/grpcio-1.9.1/grpc/_channel.py", line 487, in __call__
    return _end_unary_response_blocking(state, call, False, deadline)
  File "/home/usr_local/google-cloud-sdk-204.0.0/platform/google_appengine/lib/grpcio-1.9.1/grpc/_channel.py", line 437, in _end_unary_response_blocking
    raise _Rendezvous(state, None, None, deadline)
_Rendezvous: <_Rendezvous of RPC that terminated with (StatusCode.UNAVAILABLE, Connect Failed)>

更多信息:

注意:(至少目前)只有 Google Cloud SDK 开发服务器(最新版本,我用 204.0.0 测试过)支持通话对于独立的数据存储模拟器,特定于 GAE 的 SDK 不会(或者至少我目前使用的 1.9.69 python 不会)。来自 Migrating to the Cloud Datastore Emulator:

Note: This migration requires the use of the Google Cloud SDK-based tooling.