文件

§部署到 CloudFoundry / AppFog

§先決條件

註冊免費的 Cloud Foundry 帳戶,並使用下列指令安裝或更新 Cloud Foundry 命令列工具 VMC 至最新版本 (0.3.18 或更高版本)

gem install vmc

§建置您的應用程式

在 play 提示字元中輸入 dist 指令,封裝您的應用程式。

§部署您的應用程式

使用 VMC push 指令將建立的 zip 檔案部署到 Cloud Foundry。如果您選擇建立資料庫服務,Cloud Foundry 將在應用程式啟動時自動套用您的資料庫演進。

yourapp$ vmc push --path=dist/yourapp-1.0.zip
Application Name: yourapp
Detected a Play Framework Application, is this correct? [Yn]:
Application Deployed URL [yourapp.cloudfoundry.com]:
Memory reservation (128M, 256M, 512M, 1G, 2G) [256M]:
How many instances? [1]:
Create services to bind to 'yourapp'? [yN]: y
1: mongodb
2: mysql
3: postgresql
4: rabbitmq
5: redis
What kind of service?: 3
Specify the name of the service [postgresql-38199]: your-db
Create another? [yN]:
Would you like to save this configuration? [yN]: y
Manifest written to manifest.yml.
Creating Application: OK
Creating Service [your-db]: OK
Binding Service [your-db]: OK
Uploading Application:
  Checking for available resources: OK
  Processing resources: OK
  Packing application: OK
  Uploading (186K): OK
Push Status: OK
Staging Application 'yourapp': OK
Starting Application 'yourapp': OK

§使用服務

§自動重新設定

Cloud Foundry 使用稱為自動重新設定的機制,自動將您的 Play 應用程式連線到關聯式資料庫服務。如果在 Play 設定中找到單一資料庫設定(例如,default),且單一資料庫服務執行個體已繫結到應用程式,Cloud Foundry 將自動覆寫設定中的連線屬性,以指向繫結到應用程式的 PostgreSQL 或 MySQL 服務。

這是讓簡單的應用程式快速啟動並執行的絕佳方式。但是,您的應用程式很可能會包含特定於您所使用的資料庫類型的 SQL。在這些情況下,或者如果您的應用程式需要繫結到多個服務,您可能會選擇避免自動重新設定,並明確指定服務連線屬性。

§連線到 Cloud Foundry 服務

與以往相同,Cloud Foundry 會透過 VCAP_SERVICES 環境變數,以 JSON 格式提供所有服務連線資訊給您的應用程式。但是,連線資訊也可用作您可以在 Play 設定中使用的系列屬性。以下是從 application.conf 檔案連線到名為 tasks-db 的 PostgreSQL 服務的範例

db.default.driver=${?cloud.services.tasks-db.connection.driver}
db.default.url=${?cloud.services.tasks-db.connection.url}
db.default.password=${?cloud.services.tasks-db.connection.password}
db.default.username=${?cloud.services.tasks-db.connection.username}

這些資訊可供所有類型的服務使用,包括 NoSQL 和訊息傳遞服務。此外,如果只有一種單一服務(例如 postgresql),您可以僅透過類型來參考該服務,而不是特別透過名稱,如下例所示

db.default.driver=${?cloud.services.postgresql.connection.driver}
db.default.url=${?cloud.services.postgresql.connection.url}
db.default.password=${?cloud.services.postgresql.connection.password}
db.default.username=${?cloud.services.postgresql.connection.username}

我們建議將這些屬性保留在一個獨立的檔案中(例如 cloud.conf),然後僅在為 Cloud Foundry 建立發行版時才包含它們。您可以使用 -Dconfig.fileplay dist 指定替代設定檔。

§退出自動重新配置

如果您使用上面引用的屬性,您將會自動退出。若要明確退出,請在應用程式的 conf 目錄中包含一個名為「cloudfoundry.properties」的檔案,並新增項目 autoconfig=false

下一步:部署到 Clever Cloud


在這個文件檔中發現錯誤嗎?此頁面的原始程式碼可以在 這裡 找到。在閱讀 文件檔指南 後,請隨時提交拉取請求。有問題或建議要分享嗎?請前往 我們的社群論壇 與社群展開對話。