【问题标题】:How to deploy asp.net core (backend) and angular 8 (frontend) appplications together to Google App Engine?如何将 asp.net core(后端)和 angular 8(前端)应用程序一起部署到 Google App Engine?
【发布时间】:2020-07-09 12:01:11
【问题描述】:

我正在尝试将我的项目从共享主机迁移到 Google Cloud Platform。

我的应用程序由一个后端(asp.net core)和一个前端(angular 8)组成。它们仅通过 web api 调用进行通信。在我的本地环境和我当前的主机(共享 Windows 主机)上,它运行良好。我只需要通过 Visual Studio 中的 Web 部署发布我的 Web 项目,它会一键部署 .net 和 Angular 应用程序。

我的 Startup.cs 的一部分:

if (!Environment.IsDevelopment())
{
    // In production, the Angular files will be served from this directory
    services.AddSpaStaticFiles(configuration =>
    {
        configuration.RootPath = "MyAngularApp/dist";
    });
}

在其他可能的解决方案中,我找到了希望将应用程序部署到的 App Engine。我为后端和前端创建了 app.yaml 文件。

我可以一个一个地部署它们(“默认”服务的后端/“api”服务的前端),因此它们将被部署到单独的域。

我的解决方案不起作用,我想我的方法是错误的。这两个应用程序是否应该配置在一个 yaml 文件中?我正在寻找应该如何完成的最佳实践。

【问题讨论】:

    标签: angular asp.net-core google-app-engine google-cloud-platform


    【解决方案1】:

    特别是对于您的场景,由于 aspnet.core 和 angular 之间存在依赖关系,因此向 Google App Engine 的最平滑迁移是将您的应用程序容器化并使用带有 custom 运行时的 App Engine Flex。

    这将解决“它适用于开发但不适用于生产”的常见问题。否则,您在部署到其他 App Engine 产品(如 Standard 或 Flex)时可能会遇到一些错误,因为它们的运行时可能不包含您项目的所有依赖项。

    总之,您需要:

    1. 创建一个容器化您的 aspdotnet/angular 应用程序的 Dockerfile。我找到了this 很好的例子来说明如何将 dotnet 应用程序容器化。跟随后者可能还不够,因为它不包含所有 docker 语句,但您也可以使用 this 另一个来帮助您填充缺少的 docker 角度语句。

    2. 使用以下配置创建一个app.yaml 文件。
      runtime: custom
      env: flex
      service: [SERVICE_NAME]

    3. 运行gcloud app deploy

    如您所见,上面列表中最复杂的部分是 docker,但由于使用 Google App Engine flex 自定义运行时进行部署很少。

    【讨论】:

      【解决方案2】:

      不用容器化一个app.yaml就可以解决:

      runtime: aspnetcore
      env: flex
      
      instance_class: F2
      automatic_scaling:
        min_num_instances: 1
        max_num_instances: 5
        cool_down_period_sec: 120
        cpu_utilization:
          target_utilization: 0.6
        target_concurrent_requests: 10
      
      resources:
        cpu: 1
        memory_gb: 0.5
        disk_size_gb: 10
      
      api_version: 1
      threadsafe: true
      handlers:
      - url: /
        static_files: AngularApp/dist/index.html
        upload: AngularApp/dist/index.html
      - url: /
        static_dir: AngularApp/dist
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-09-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-15
        • 1970-01-01
        • 1970-01-01
        • 2017-02-08
        相关资源
        最近更新 更多