在同一个应用的不同模块中使用Google端点

时间:2016-05-11 15:02:46

标签: python-2.7 google-app-engine google-cloud-endpoints

我对使用Google App引擎和云平台的其他Google服务进行开发非常陌生,我想创建一个使用不同模块的应用程序(因此他们可以有自己的生命周期)使用端点。

我正在努力使用api路径,因为我不知道如何将请求路由到好的模块。

我的目录树是这样的:

/myApp
  /module1
    __init__.py
    main.py
  /module2
    __init__.py
    main.py
  module1.yaml
  module2.yaml
  dispatch.yaml

module1.yaml

application: myapp
runtime: python27
threadsafe: true
module: module1
version: 0
api_version: 1

handlers:
# The endpoints handler must be mapped to /_ah/spi.
# Apps send requests to /_ah/api, but the endpoints service handles mapping
# those requests to /_ah/spi.
- url: /_ah/spi/.*
  script: module1.main.api

libraries:
- name: pycrypto
  version: 2.6
- name: endpoints
  version: 1.0

module2.yaml

application: myapp
runtime: python27
threadsafe: true
module: module2
version: 0
api_version: 1

handlers:
# The endpoints handler must be mapped to /_ah/spi.
# Apps send requests to /_ah/api, but the endpoints service handles mapping
# those requests to /_ah/spi.
- url: /_ah/spi/.*
  script: module2.main.api

libraries:
- name: pycrypto
  version: 2.6
- name: endpoints
  version: 1.0

dispatch.yaml

dispatch:
  - url: "*/_ah/spi/*"
    module: module1

  - url: "*/_ah/spi/.*"
    module: module2

所以我希望我的端点可以在某处调用相应模块的名称('_ah / api / module1'或'module1 / _ah / api')。我不知道在不同的.yaml文件中放什么。我甚至不知道我在做什么是正确的,或者是可能的。

感谢您的回答。

1 个答案:

答案 0 :(得分:1)

您可以在不同的模块上托管不同的端点(现在称为服务);正确解决它们的方法如下:

https://<service-name>-dot-<your-project-id>.appspot.com/_ah/api

现在,让我们假设你有 - 根据你的描述 - module1 module2 ,每个人都有不同的端点。您将通过点击:

来调用 module1 API

https://module1-dot-<your-project-id>.appspot.com/_ah/api

以类似的方式, module2 API:

https://module2-dot-<your-project-id>.appspot.com/_ah/api

如果您想深入了解此URL架构的工作原理(包括版本,这是此处等式的另一个重要部分),请阅读Addressing microservices以及紧随其后的Using API versions部分