如何为 App Engine 中的同级模块获取 url

How to get url for sibling modules in app engine

我有一个具有多个独立模块的 App Engine 应用程序。

deployed 时,这些模块在 http://<module>.<app_id>.appspot.com 可用,当使用 dev_appserver.py mod1.yaml mod2.yaml --port-9000 在本地测试时,mod1http://localhost:9000mod2http://localhost:9001 中运行。所有模块都在同一个项目中 运行。到目前为止一切顺利。

让我们假设 mod1 需要与 mod2 交谈。有没有办法动态地为 mod1 内的 mod2 获取 url?

# In mod1's code
import google.some.magic
url_for_mod2 = magic.get_url_for_module('mod2') # http://localhost:9001 or http://mod2.id.appspot.com

据我所知是不可能的。由于您有 2 项服务,将来可能会更多,因此您似乎正在使用分布式微服务架构,因此您需要注册您的服务,以便它们知道其他服务在哪里。为此我推荐 etcd 它真的很容易使用。 好心人还写了一个python-client给它!

可以使用 modules.get_hostname() API 构建同时在 GAE 和开发服务器上工作的跨模块 url。您可以在此答案中找到示例: