Springboot應(yīng)用部署

參考官網(wǎng):

Deploying Spring Boot Applications

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)景酥馍。

  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:

如果您正在從容器中運(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

部署到云上

省略。锁荔。蟀给。

  1. 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ù)
  1. 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 -jaror 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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末瘦穆,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子救拉,更是在濱河造成了極大的恐慌难审,老刑警劉巖瘫拣,帶你破解...
    沈念sama閱讀 219,270評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件亿絮,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡麸拄,警方通過(guò)查閱死者的電腦和手機(jī)派昧,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,489評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)拢切,“玉大人蒂萎,你說(shuō)我怎么就攤上這事』匆” “怎么了五慈?”我有些...
    開(kāi)封第一講書(shū)人閱讀 165,630評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵纳寂,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我泻拦,道長(zhǎng)毙芜,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,906評(píng)論 1 295
  • 正文 為了忘掉前任争拐,我火速辦了婚禮腋粥,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘架曹。我一直安慰自己隘冲,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,928評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布绑雄。 她就那樣靜靜地躺著展辞,像睡著了一般。 火紅的嫁衣襯著肌膚如雪万牺。 梳的紋絲不亂的頭發(fā)上纵竖,一...
    開(kāi)封第一講書(shū)人閱讀 51,718評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音杏愤,去河邊找鬼靡砌。 笑死,一個(gè)胖子當(dāng)著我的面吹牛珊楼,可吹牛的內(nèi)容都是我干的通殃。 我是一名探鬼主播,決...
    沈念sama閱讀 40,442評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼厕宗,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼画舌!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起已慢,我...
    開(kāi)封第一講書(shū)人閱讀 39,345評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤曲聂,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后佑惠,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體朋腋,經(jīng)...
    沈念sama閱讀 45,802評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,984評(píng)論 3 337
  • 正文 我和宋清朗相戀三年膜楷,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了旭咽。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,117評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡赌厅,死狀恐怖穷绵,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情特愿,我是刑警寧澤仲墨,帶...
    沈念sama閱讀 35,810評(píng)論 5 346
  • 正文 年R本政府宣布勾缭,位于F島的核電站,受9級(jí)特大地震影響目养,放射性物質(zhì)發(fā)生泄漏漫拭。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,462評(píng)論 3 331
  • 文/蒙蒙 一混稽、第九天 我趴在偏房一處隱蔽的房頂上張望采驻。 院中可真熱鬧,春花似錦匈勋、人聲如沸礼旅。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,011評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)痘系。三九已至,卻和暖如春饿自,著一層夾襖步出監(jiān)牢的瞬間汰翠,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,139評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工昭雌, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留复唤,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,377評(píng)論 3 373
  • 正文 我出身青樓烛卧,卻偏偏與公主長(zhǎng)得像佛纫,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子总放,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,060評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容