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

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,602评论 6 481
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,442评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 152,878评论 0 344
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,306评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,330评论 5 373
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,071评论 1 285
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,382评论 3 400
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,006评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,512评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,965评论 2 325
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,094评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,732评论 4 323
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,283评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,286评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,512评论 1 262
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,536评论 2 354
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,828评论 2 345

推荐阅读更多精彩内容