參考官網(wǎng):
Deploying Spring Boot Applications
部署Spring Boot 應(yīng)用程序
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.
在部署應(yīng)用程序時(shí)昧绣,Spring Boot的靈活打包選項(xiàng)提供了大量的選擇蜗字。
您可以將Spring引導(dǎo)應(yīng)用程序部署到各種云平臺(tái)逛球、容器鏡像(如Docker)或虛擬/真實(shí)機(jī)器上仲器。
This section covers some of the more common deployment scenarios.
本節(jié)介紹一些更常見(jiàn)的部署場(chǎng)景酥馍。
- 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:
如果您正在從容器中運(yùn)行應(yīng)用程序衡创,那么您可以使用可執(zhí)行jar俄烁,但是將其分解并以不同的方式運(yùn)行通常也是一種優(yōu)勢(shì)它呀。某些PaaS實(shí)現(xiàn)也可以選擇在運(yùn)行之前解包存檔苞也。
例如洛勉,Cloud Foundry就是這樣操作的。運(yùn)行一個(gè)未打包的檔案最簡(jiǎn)單的方法是啟動(dòng)適當(dāng)?shù)膯?dòng)程序如迟,如下:
$ 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.
實(shí)際上在啟動(dòng)時(shí)(取決于jar的大小)比在未分解的歸檔中運(yùn)行要快一些收毫。在運(yùn)行時(shí),您不應(yīng)該期望任何差異殷勘。
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文件此再,你還可以通過(guò)運(yùn)行應(yīng)用程序的“natural”主方法來(lái)獲得啟動(dòng)時(shí)間的額外增加,而不是通過(guò)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:
通過(guò)將依賴(lài)項(xiàng)作為獨(dú)立于應(yīng)用程序類(lèi)和資源的層復(fù)制到映像输拇,還可以創(chuàng)建更高效的容器映像(應(yīng)用程序類(lèi)和資源通常更改得更頻繁)。實(shí)現(xiàn)分層的方法不止一種贤斜。例如策吠,使用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:
假設(shè)以上Dockerfile是當(dāng)前目錄,則可以使用docker build .來(lái)構(gòu)建您的docker鏡像瘩绒,也可以選擇指定應(yīng)用程序jar的路徑猴抹,如下例所示:
docker build --build-arg APPJAR=path/to/myapp.jar .
Deploying to the Cloud
部署到云上
省略。锁荔。蟀给。
- Installing Spring Boot Applications
安裝SpringBoot應(yīng)用程序
3.1. Supported Operating Systems
支持的操作系統(tǒng)
3.2. Unix/Linux Services
Unix/Linux 服務(wù)
3.2.1. Installation as an init.d Service (System V)
作為一個(gè)init.d 服務(wù)安裝
3.2.2. Installation as a systemd Service
作為一個(gè)systemd服務(wù)安裝
3.2.3. Customizing the Startup Script
自定義啟動(dòng)腳本
Customizing the Start Script when It Is Written
當(dāng)被寫(xiě)的時(shí)候自定義啟動(dòng)腳本
Customizing a Script When It Runs
當(dāng)運(yùn)行的時(shí)候自定義一個(gè)腳本
3.3. Microsoft Windows Services
微軟服務(wù)
- Installing Spring Boot Applications
安裝SpringBoot應(yīng)用程序
3.1. Supported Operating Systems
支持的操作系統(tǒng)
3.2. Unix/Linux Services
Unix/Linux 服務(wù)
3.2.1. Installation as an init.d Service (System V)
作為一個(gè)init.d 服務(wù)安裝
3.2.2. Installation as a systemd Service
作為一個(gè)systemd服務(wù)安裝
3.2.3. Customizing the Startup Script
自定義啟動(dòng)腳本
Customizing the Start Script when It Is Written
當(dāng)被寫(xiě)的時(shí)候自定義啟動(dòng)腳本
Customizing a Script When It Runs
當(dāng)運(yùn)行的時(shí)候自定義一個(gè)腳本
3.3. Microsoft Windows Services
微軟服務(wù)
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運(yùn)行Spring引導(dǎo)應(yīng)用程序之外,還可以為Unix系統(tǒng)生成完全可執(zhí)行的應(yīng)用程序阳堕。完全可執(zhí)行的jar可以像任何其他可執(zhí)行的二進(jìn)制文件一樣執(zhí)行跋理,也可以用init.d或systemd 注冊(cè)。這使得在常見(jiàn)的生產(chǎn)環(huán)境中很容易安裝和管理Spring Boot應(yīng)用程序恬总。
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.
完全可執(zhí)行的jar通過(guò)在文件的前面嵌入一個(gè)額外的腳本來(lái)工作前普。目前,一些工具不接受這種格式越驻,因此您可能不能總是使用這種技術(shù)汁政。例如道偷,jar -xf可能無(wú)法提取完全可執(zhí)行的jar或war。建議您只在打算直接執(zhí)行jar或war時(shí)才使其完全可執(zhí)行记劈,而不是使用“java -jar”運(yùn)行它或?qū)⑵洳渴鸬絪ervlet容器中勺鸦。
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文件不能完全執(zhí)行。嘗試這樣做將導(dǎo)致在直接執(zhí)行或使用java -jar時(shí)報(bào)告為損壞的jar文件目木。包含一個(gè)或多個(gè)zip64格式嵌套jar的標(biāo)準(zhǔn)格式j(luò)ar文件可以完全執(zhí)行换途。
To create a ‘fully executable’ jar with Maven, use the following plugin configuration:
要使用Maven創(chuàng)建一個(gè)“完全可執(zhí)行的”jar,請(qǐng)使用以下插件配置:
<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.
然后您可以通過(guò)鍵入./my-application.jar(其中my-application是artifact的名稱(chēng))來(lái)運(yùn)行您的應(yīng)用程序刽射。包含jar的目錄用作應(yīng)用程序的工作目錄军拟。
3.1. Supported Operating Systems
受支持的操作系統(tǒng)
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.
默認(rèn)腳本支持大多數(shù)Linux發(fā)行版,并在CentOS和Ubuntu上測(cè)試誓禁。
其他平臺(tái)懈息,如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插件來(lái)生成一個(gè)完全可執(zhí)行的jar辫继,并且您沒(méi)有使用定制的embeddedLaunchScript,那么您的應(yīng)用程序可以用作init俗慈。d服務(wù)姑宽。為此,將jar符號(hào)鏈接到init闺阱。支持標(biāo)準(zhǔn)的啟動(dòng)炮车、停止、重啟和狀態(tài)命令酣溃。
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