使用Google Cloud App Engine将前端和后端部署为两个独立的应用程序

4

我有两个应用程序想要使用Google Cloud App Engine进行部署。

其中一个是React前端,我希望通过www.videoo.io进行服务。

第二个是后端,将通过api.videoo.io提供服务。

前端的YAML文件为react.yaml:

runtime: nodejs16

env: standard
handlers:
- url: /static
  static_dir: static
  secure: always

- url: www.videoo.io/*
  service: frontend
  script: auto
  secure: always%   

API yaml文件,api.yaml:

runtime: python37
entrypoint: gunicorn -b :$PORT videoo.wsgi

service: "videoo-api"
env: standard
handlers:

- url: api.videoo.io/*
  service: backend
  script: auto
  secure: always%   

这是实现这个的正确方式吗?
如何最好地为这两个分离的应用程序提供服务,这些应用程序将进行交互式通信(前端将调用API以获取存储在Django应用中的对象信息)?
以下是我在Google App Engine设置中的域名信息: enter image description here

这个回答解决了您的问题吗?Google App Engine - 前端和后端Web开发 - Alex
1个回答

4
  1. You are on the right path. You are using the microservices architecture which is basically deploying individual apps as parts (services) under a single project.

  2. Your frontend service seems to be your default so you don't need a service name for it. Every GAE App needs a default service.

  3. Rename react.yaml to app.yaml (since it will be your default service) and update the contents to

    runtime: nodejs16
    
    env: standard
    handlers:
    - url: /static
      static_dir: static
      secure: always
    
    - url: /.*
      script: auto
      secure: always   
    
  4. Also rename your api.yaml to backend.yaml since that is what you called your service (not sure if this is required but I do that to easily track of what is controlling my service). Update the contents of the file to

    service: backend
    runtime: python37
    entrypoint: gunicorn -b :$PORT videoo.wsgi
    env: standard
    
    handlers:
    - url: /.*
      script: auto
      secure: always   
    
  5. You'll need a dispatch.yaml file to route traffic to the different services. Something like

dispatch:
  # Send all api traffic to the backend service.
  - url: "api.videoo.io/*"
    service: backend

  # Send all other traffic to the default (frontend).
  - url: "*/*"
    service: default
  1. 最后一步是在部署时,您需要将这两项服务与您的dispatch.yaml文件一起部署。dispatch.yaml文件必须位于项目根目录中。
gcloud app deploy app.yaml dispatch.yaml <path_to_backend.yaml>

1
基本上,您需要一个 URL 模式,遵守此处 url 属性的规则 - https://cloud.google.com/appengine/docs/standard/python3/config/appref - NoCommandLine
@Jul - 请查看文档,了解您可以拥有的服务的最大数量。针对您的情况,请参考此github gist中的示例dispatch.yaml - NoCommandLine
@NoCommandLine,我花了一些时间才回答,但是你的解决方案会不会把所有其他流量都发送到默认服务?例如,如果我有一个用于测试的开发服务,它的地址类似于dev-dot-frontend.appspot.etc.,那么访问该网址的流量会被重定向到默认服务而不是开发服务吗? - undefined
@Jul 在示例的 github gist 中,我只为 consoleapi 设置了特定的路由。如果你需要其他的,比如 dev,请在 default 之前添加额外的路由。如果这仍然无法满足你的需求,你可以在这里(在Stackoverflow上)开始一个聊天,或者通过我们的网站联系我们,提供你的具体情况,我会看看能否帮上忙。 - undefined
@NoCommandLine 谢谢你的提议,非常感激!但是我不知道如何在这里开始聊天。不过我想我明白了,如果我们有16个不同的服务,每个服务都有自己的URL需要访问,那么我们需要在调度文件中添加16个条目吗?我希望只需输入那些需要指定自定义域名的条目,而其他的则保留默认映射,但似乎每个都需要单独映射。 - undefined
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接