参考官网:
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.
本节介绍一些更常见的部署场景。
- Deploying to Containers
- Deploying to the Cloud
- Installing Spring Boot Applications
- 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
部署到云上
省略。。。
- 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
微软服务
- 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 -jar
or 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