Springboot應用部署

參考官網:

Deploying Spring Boot Applications

Deploying Spring Boot Applications
部署Spring Boot 應用程序

Spring Boot’s flexible packaging options provide a great deal of choice when it comes to deploying your application. You can deploy Spring Boot applications to a variety of cloud platforms, to container images (such as Docker), or to virtual/real machines.

在部署應用程序時,Spring Boot的靈活打包選項提供了大量的選擇。
您可以將Spring引導應用程序部署到各種雲平臺、容器鏡像(如Docker)或虛擬/真實機器上。

This section covers some of the more common deployment scenarios.

本節介紹一些更常見的部署場景。

  1. Deploying to Containers
  2. Deploying to the Cloud
  3. Installing Spring Boot Applications
  4. What to Read Next

Deploying to Containers
部署到容器

If you are running your application from a container, you can use an executable jar, but it is also often an advantage to explode it and run it in a different way. Certain PaaS implementations may also choose to unpack archives before they run. For example, Cloud Foundry operates this way. The simplest way to run an unpacked archive is by starting the appropriate launcher, as follows:

如果您正在從容器中運行應用程序,那麼您可以使用可執行jar,但是將其分解並以不同的方式運行通常也是一種優勢。某些PaaS實現也可以選擇在運行之前解包存檔。
例如,Cloud Foundry就是這樣操作的。運行一個未打包的檔案最簡單的方法是啓動適當的啓動程序,如下:

$ jar -xf myapp.jar
$ java org.springframework.boot.loader.JarLauncher

This is actually slightly faster on startup (depending on the size of the jar) than running from an unexploded archive. At runtime you shouldn’t expect any differences.

實際上在啓動時(取決於jar的大小)比在未分解的歸檔中運行要快一些。在運行時,您不應該期望任何差異。

Once you have unpacked the jar file, you can also get an extra boost to startup time by running the app with its "natural" main method instead of the JarLauncher. For example:

一旦你解壓縮了jar文件,你還可以通過運行應用程序的“natural”主方法來獲得啓動時間的額外增加,而不是通過JarLauncher。例如:

$ jar -xf myapp.jar
$ java -cp BOOT-INF/classes:BOOT-INF/lib/* com.example.MyApplication

More efficient container images can also be created by copying the dependencies to the image as a separate layer from the application classes and resources (which normally change more frequently). There is more than one way to achieve this layer separation. For example, using a Dockerfile you could express it in this form:

通過將依賴項作爲獨立於應用程序類和資源的層複製到映像,還可以創建更高效的容器映像(應用程序類和資源通常更改得更頻繁)。實現分層的方法不止一種。例如,使用Dockerfile可以這樣表示:

FROM openjdk:8-jdk-alpine AS builder
WORKDIR target/dependency
ARG APPJAR=target/*.jar
COPY ${APPJAR} app.jar
RUN jar -xf ./app.jar

FROM openjdk:8-jre-alpine
VOLUME /tmp
ARG DEPENDENCY=target/dependency
COPY --from=builder ${DEPENDENCY}/BOOT-INF/lib /app/lib
COPY --from=builder ${DEPENDENCY}/META-INF /app/META-INF
COPY --from=builder ${DEPENDENCY}/BOOT-INF/classes /app
ENTRYPOINT ["java","-cp","app:app/lib/*","com.example.MyApplication"]

Assuming the above Dockerfile is the current directory, your docker image can be built with docker build ., or optionally specifying the path to your application jar, as shown in the following example:

假設以上Dockerfile是當前目錄,則可以使用docker build .來構建您的docker鏡像,也可以選擇指定應用程序jar的路徑,如下例所示:

docker build --build-arg APPJAR=path/to/myapp.jar .

Deploying to the Cloud

部署到雲上

省略。。。

  1. Installing Spring Boot Applications
    安裝SpringBoot應用程序
    3.1. Supported Operating Systems
    支持的操作系統
    3.2. Unix/Linux Services
    Unix/Linux 服務
    3.2.1. Installation as an init.d Service (System V)
    作爲一個init.d 服務安裝
    3.2.2. Installation as a systemd Service
    作爲一個systemd服務安裝
    3.2.3. Customizing the Startup Script
    自定義啓動腳本
    Customizing the Start Script when It Is Written
    當被寫的時候自定義啓動腳本
    Customizing a Script When It Runs
    當運行的時候自定義一個腳本
    3.3. Microsoft Windows Services
    微軟服務
  1. Installing Spring Boot Applications
    安裝SpringBoot應用程序

3.1. Supported Operating Systems
支持的操作系統
3.2. Unix/Linux Services
Unix/Linux 服務
3.2.1. Installation as an init.d Service (System V)
作爲一個init.d 服務安裝
3.2.2. Installation as a systemd Service
作爲一個systemd服務安裝
3.2.3. Customizing the Startup Script
自定義啓動腳本
Customizing the Start Script when It Is Written
當被寫的時候自定義啓動腳本
Customizing a Script When It Runs
當運行的時候自定義一個腳本
3.3. Microsoft Windows Services
微軟服務

In addition to running Spring Boot applications by using java -jar, it is also possible to make fully executable applications for Unix systems. A fully executable jar can be executed like any other executable binary or it can be registered with init.d or systemd. This makes it very easy to install and manage Spring Boot applications in common production environments.

除了使用java -jar運行Spring引導應用程序之外,還可以爲Unix系統生成完全可執行的應用程序。完全可執行的jar可以像任何其他可執行的二進制文件一樣執行,也可以用init.d或systemd 註冊。這使得在常見的生產環境中很容易安裝和管理Spring Boot應用程序。

Fully executable jars work by embedding an extra script at the front of the file. Currently, some tools do not accept this format, so you may not always be able to use this technique. For example, jar -xf may silently fail to extract a jar or war that has been made fully executable. It is recommended that you make your jar or war fully executable only if you intend to execute it directly, rather than running it with java -jaror deploying it to a servlet container.

完全可執行的jar通過在文件的前面嵌入一個額外的腳本來工作。目前,一些工具不接受這種格式,因此您可能不能總是使用這種技術。例如,jar -xf可能無法提取完全可執行的jar或war。建議您只在打算直接執行jar或war時才使其完全可執行,而不是使用“java -jar”運行它或將其部署到servlet容器中。

A zip64-format jar file cannot be made fully executable. Attempting to do so will result in a jar file that is reported as corrupt when executed directly or with java -jar. A standard-format jar file that contains one or more zip64-format nested jars can be fully executable.

zip64格式的jar文件不能完全執行。嘗試這樣做將導致在直接執行或使用java -jar時報告爲損壞的jar文件。包含一個或多個zip64格式嵌套jar的標準格式jar文件可以完全執行。

To create a ‘fully executable’ jar with Maven, use the following plugin configuration:

要使用Maven創建一個“完全可執行的”jar,請使用以下插件配置:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <executable>true</executable>
    </configuration>
</plugin>

The following example shows the equivalent Gradle configuration:

下面的示例展示了等效的Gradle配置:

bootJar {
    launchScript()
}

You can then run your application by typing ./my-application.jar (where my-application is the name of your artifact). The directory containing the jar is used as your application’s working directory.

然後您可以通過鍵入./my-application.jar(其中my-application是artifact的名稱)來運行您的應用程序。包含jar的目錄用作應用程序的工作目錄。

3.1. Supported Operating Systems
受支持的操作系統

The default script supports most Linux distributions and is tested on CentOS and Ubuntu. Other platforms, such as OS X and FreeBSD, require the use of a custom embeddedLaunchScript.

默認腳本支持大多數Linux發行版,並在CentOS和Ubuntu上測試。
其他平臺,如OS X和FreeBSD,需要使用定製的embeddedLaunchScript。

3.2. Unix/Linux Services

Spring Boot application can be easily started as Unix/Linux services by using either init.d or systemd.

3.2.1. Installation as an init.d Service (System V)

If you configured Spring Boot’s Maven or Gradle plugin to generate a fully executable jar, and you do not use a custom embeddedLaunchScript, your application can be used as an init.d service. To do so, symlink the jar to init.d to support the standard start, stop, restart, and status commands.

如果您配置了Spring Boot的Maven或Gradle插件來生成一個完全可執行的jar,並且您沒有使用定製的embeddedLaunchScript,那麼您的應用程序可以用作init。d服務。爲此,將jar符號鏈接到init。支持標準的啓動、停止、重啓和狀態命令。

The script supports the following features:

  • Starts the services as the user that owns the jar file

  • Tracks the application’s PID by using /var/run/<appname>/<appname>.pid

  • Writes console logs to /var/log/<appname>.log

Assuming that you have a Spring Boot application installed in /var/myapp, to install a Spring Boot application as an init.d service, create a symlink, as follows:

$ sudo ln -s /var/myapp/myapp.jar /etc/init.d/myapp

Once installed, you can start and stop the service in the usual way. For example, on a Debian-based system, you could start it with the following command:

$ service myapp start

If your application fails to start, check the log file written to /var/log/<appname>.log for errors.

You can also flag the application to start automatically by using your standard operating system tools. For example, on Debian, you could use the following command:

update-rc.d myapp defaults <priority>

Securing an init.d Service

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章