文件

§部署您的應用程式

我們已了解如何在開發模式中執行 Play 應用程式,但不得使用 run 指令在生產模式中執行應用程式。使用 run 時,Play 會在每次要求時向 sbt 檢查是否有任何檔案已變更,這可能會對您的應用程式造成顯著的效能影響。

有數種方式可在生產模式中部署 Play 應用程式。讓我們從建議的方式開始,建立一個發行成品。

§應用程式密碼

在生產模式中執行應用程式之前,您需要產生一個應用程式密碼。若要深入了解如何執行此操作,請參閱設定應用程式密碼。在以下範例中,您將看到使用 -Dplay.http.secret.key=ad31779d4ee49d5ad5162bf1429c32e2e9933f3b。部署至生產環境時,您必須產生自己的密碼來使用。

§使用 JPA 部署 Play

如果您使用 JPA,您需要查看使用 JPA 部署 Play

§使用 dist 工作

dist 工作會建立應用程式的二進位版本,您可以將其部署到伺服器,而無需依賴 sbt,伺服器唯一需要的僅是 Java 安裝。

在 Play 主控台中,只需輸入 dist

[my-first-app] $ dist

您將會看到類似以下內容

$ sbt
[info] Loading global plugins from /Users/play-developer/.sbt/1.0/plugins
[info] Loading project definition from /Users/play-developer/my-first-app/project
[info] Set current project to my-first-app (in build file:/Users/play-developer/my-first-app/)
[my-first-app] $ dist
[info] Packaging /Users/play-developer/my-first-app/target/scala-2.13/my-first-app_2.13-1.0-SNAPSHOT-sources.jar ...
[info] Done packaging.
[info] Wrote /Users/play-developer/my-first-app/target/scala-2.13/my-first-app_2.13-1.0-SNAPSHOT.pom
[info] Main Scala API documentation to /Users/play-developer/my-first-app/target/scala-2.13/api...
[info] Packaging /Users/play-developer/my-first-app/target/scala-2.13/my-first-app_2.13-1.0-SNAPSHOT-web-assets.jar ...
[info] Done packaging.
[info] Packaging /Users/play-developer/my-first-app/target/scala-2.13/my-first-app_2.13-1.0-SNAPSHOT.jar ...
[info] Done packaging.
model contains 21 documentable templates
[info] Main Scala API documentation successful.
[info] Packaging /Users/play-developer/my-first-app/target/scala-2.13/my-first-app_2.13-1.0-SNAPSHOT-javadoc.jar ...
[info] Done packaging.
[info] Packaging /Users/play-developer/my-first-app/target/scala-2.13/my-first-app_2.13-1.0-SNAPSHOT-sans-externalized.jar ...
[info] Done packaging.
[info]
[info] Your package is ready in /Users/play-developer/my-first-app/target/universal/my-first-app-1.0-SNAPSHOT.zip
[info]
[success] Total time: 5 s, completed Feb 6, 2017 2:08:44 PM
[my-first-app] $

這會產生一個 ZIP 檔案,其中包含在應用程式的 target/universal 資料夾中執行應用程式所需的所有 JAR 檔案。

若要執行應用程式,請在目標伺服器上解壓縮檔案,然後執行 bin 目錄中的指令碼。指令碼的名稱是您的應用程式名稱,並提供兩個版本,一個是 bash shell 指令碼,另一個是 Windows .bat 指令碼。

$ unzip my-first-app-1.0.zip
$ my-first-app-1.0/bin/my-first-app -Dplay.http.secret.key=ad31779d4ee49d5ad5162bf1429c32e2e9933f3b

您也可以從命令列為生產環境指定不同的設定檔

$ my-first-app-1.0/bin/my-first-app -Dconfig.file=/full/path/to/conf/application-prod.conf

若要取得完整的用法說明,請使用 -h 選項呼叫啟動指令碼。

對於 Unix 使用者,zip 檔案不會保留 Unix 檔案權限,因此當檔案展開時,將需要將啟動指令碼設定為可執行檔

$ chmod +x /path/to/bin/<project-name>

或者,可以產生 tar.gz 檔案。Tar 檔案會保留權限。呼叫 Universal / packageZipTarball 任務,而不是 dist 任務

sbt Universal / packageZipTarball

預設情況下,dist 任務會在產生的套件中包含 API 文件。如果不需要,請在 build.sbt 中新增這些行

Compile / doc / sources := Seq.empty

Compile / packageDoc / publishArtifact := false

對於有子專案的建置,上述陳述必須套用至所有子專案定義。

§原生封裝器

Play 使用 sbt 原生封裝器外掛程式。原生封裝器外掛程式宣告 dist 任務以建立 zip 檔案。呼叫 dist 任務直接等於呼叫下列內容

[my-first-app] $ Universal / packageBin

可以產生許多其他類型的檔案,包括

請參閱 原生封裝器外掛程式的文件,以取得更多資訊。

§建置伺服器發行版

sbt 原生封裝器外掛程式提供多個原型。Play 預設使用的原型稱為 Java 伺服器原型,它啟用下列功能

可以在 Java 伺服器應用程式原型文件 中找到更多資訊。

§最小 Debian 設定

將下列設定新增至您的建置

lazy val root = (project in file("."))
  .enablePlugins(PlayScala, DebianPlugin)

Linux / maintainer := "First Lastname <[email protected]>"

Linux / packageSummary := "My custom package summary"

packageDescription := "My longer package description"

然後使用下列指令建置您的套件

[my-first-app] $ Debian / packageBin

§最小 RPM 設定

將下列設定新增至您的建置

lazy val root = (project in file("."))
  .enablePlugins(PlayScala, RpmPlugin)

Linux / maintainer := "First Lastname <[email protected]>"

Linux / packageSummary := "My custom package summary"

packageDescription := "My longer package description"

rpmRelease := "1"

rpmVendor := "example.com"

rpmUrl := Some("http://github.com/example/server")

rpmLicense := Some("Apache v2")

然後使用下列指令建置您的套件

[my-first-app] $ Rpm / packageBin

會有一些錯誤記錄。這是因為 rpm 記錄到 stderr,而不是 stdout。

§在您的發行版中包含其他檔案

包含在專案的 dist 目錄中的任何內容都將包含在原生封裝器建置的發行版中。請注意,在 Play 中,dist 目錄等於原生封裝器自己的文件所提到的 src/universal 目錄。

§Play PID 組態

Play 管理自己的 PID,這在 生產組態 中有說明。

由於 Play 使用獨立的 pidfile,我們必須提供給它正確的路徑,這裡是 packageName.value。pid 檔案的名稱必須是 play.pid。為了告訴啟動指令碼將 PID 檔案放在哪裡,請在 dist/conf 資料夾中放置一個 application.ini 檔案,並加入以下內容

s"-Dpidfile.path=/var/run/${packageName.value}/play.pid",
# Add all other startup settings here, too

請參閱 sbt-native-packager 的 Play 頁面,以取得更多詳細資訊。

若要防止 Play 建立 PID,請將屬性設定為 /dev/null

-Dpidfile.path=/dev/null

若要取得替換項的完整清單,請仔細查看 自訂 Java 伺服器文件自訂 Java 應用程式文件

§發佈到 Maven (或 Ivy) 儲存庫

您也可以將您的應用程式發佈到 Maven 儲存庫。這會同時發佈包含您的應用程式的 JAR 檔案和對應的 POM 檔案。

您必須在 build.sbt 檔案中設定您要發佈到的儲存庫

publishTo := Some(
  "My resolver".at("https://mycompany.com/repo")
)

credentials += Credentials(
  "Repo",
  "https://mycompany.com/repo",
  "admin",
  "admin123"
)

然後在 Play 主控台中,使用 publish 任務

[my-first-app] $ publish

查看 sbt 文件 以取得更多關於解析器和認證定義的資訊。

§執行內部製作伺服器

在某些情況下,您可能不想建立完整的發行版,您可能實際上想從專案的來源目錄執行應用程式。這需要在伺服器上安裝 sbt,且可以使用 stage 任務來執行。

$ sbt clean stage

您將會看到類似這樣的內容

$ sbt
[info] Loading global plugins from /Users/play-developer/.sbt/1.0/plugins
[info] Loading project definition from /Users/play-developer/my-first-app/project
[info] Set current project to my-first-app (in build file:/Users/play-developer/my-first-app/)
[my-first-app] $ stage
[info] Updating {file:/Users/play-developer/my-first-app/}root...
[info] Packaging /Users/play-developer/my-first-app/target/scala-2.13/my-first-app_2.13-1.0-SNAPSHOT-sources.jar ...
[info] Done packaging.
[info] Wrote /Users/play-developer/my-first-app/target/scala-2.13/my-first-app_2.13-1.0-SNAPSHOT.pom
[info] Resolving jline#jline;2.12.2 ...
[info] Done updating.
[info] Main Scala API documentation to /Users/play-developer/my-first-app/target/scala-2.13/api...
[info] Compiling 8 Scala sources and 1 Java source to /Users/play-developer/my-first-app/target/scala-2.13/classes...
[info] Packaging /Users/play-developer/my-first-app/target/scala-2.13/my-first-app_2.13-1.0-SNAPSHOT-web-assets.jar ...
[info] Done packaging.
model contains 21 documentable templates
[info] Main Scala API documentation successful.
[info] Packaging /Users/play-developer/my-first-app/target/scala-2.13/my-first-app_2.13-1.0-SNAPSHOT-javadoc.jar ...
[info] Done packaging.
[info] Packaging /Users/play-developer/my-first-app/target/scala-2.13/my-first-app_2.13-1.0-SNAPSHOT.jar ...
[info] Done packaging.
[info] Packaging /Users/play-developer/my-first-app/target/scala-2.13/my-first-app_2.13-1.0-SNAPSHOT-sans-externalized.jar ...
[info] Done packaging.
[success] Total time: 8 s, completed Feb 6, 2017 2:11:10 PM
[my-first-app] $

這會清除並編譯您的應用程式,擷取所需的相依性並將它們複製到 target/universal/stage 目錄。它也會建立 bin/<start> 腳本,其中 <start> 是專案名稱。腳本會在 Unix 風格系統上執行 Play 伺服器,且也有對應的 Windows 用 bat 檔。

例如,要從專案資料夾啟動 my-first-app 專案的應用程式,您可以

$ target/universal/stage/bin/my-first-app -Dplay.http.secret.key=ad31779d4ee49d5ad5162bf1429c32e2e9933f3b

您也可以從命令列為生產環境指定不同的設定檔

$ target/universal/stage/bin/my-first-app -Dconfig.file=/full/path/to/conf/application-prod.conf

§執行測試執行個體

Play 提供了一個方便的工具程式,用於在製作模式中執行測試應用程式。

注意:這不適用於製作用途。

若要在製作模式中執行應用程式,請執行 runProd

[my-first-app] $ runProd

§使用 sbt 組合外掛程式

儘管未正式支援,但 sbt 組合外掛程式可用於封裝和執行 Play 應用程式。這將產生一個 jar 作為輸出成品,並允許您使用 java 指令直接執行它。

若要使用此功能,請在您的 project/plugins.sbt 檔中加入外掛程式的相依性

addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "2.1.4")

現在,將下列組態加入您的 build.sbt

assembly / mainClass := Some("play.core.server.ProdServerStart")
assembly / fullClasspath += Attributed.blank(PlayKeys.playPackageAssets.value)

assembly / assemblyMergeStrategy := {
  case manifest if manifest.contains("MANIFEST.MF") =>
    // We don't need manifest files since sbt-assembly will create
    // one with the given settings
    MergeStrategy.discard
  case referenceOverrides if referenceOverrides.contains("reference-overrides.conf") =>
    // Keep the content for all reference-overrides.conf files
    MergeStrategy.concat
  case x =>
    // For all the other files, use the default sbt-assembly merge strategy
    val oldStrategy = (assembly / assemblyMergeStrategy).value
    oldStrategy(x)
}

現在,您可以透過執行 sbt assembly 來建立成品,並透過執行來執行您的應用程式

$ java -Dplay.http.secret.key=ad31779d4ee49d5ad5162bf1429c32e2e9933f3b -jar target/scala-2.XX/<yourprojectname>-assembly-<version>.jar

當然,您需要代換為正確的專案名稱、版本和 Scala 版本。

下一步:製作組態


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