1
- [[cloud- deployment]]
2
- = Deploying to the cloud
1
+ [[deployment]]
2
+ == Deploying Spring Boot applications
3
3
4
4
[partintro]
5
5
--
6
+ Spring Boot's flexible packaging options provide a great deal of choice when it comes to
7
+ deploying your application. You can easily deploy Spring Boot applications to a variety
8
+ of cloud platforms, to a container images (such as Docker) or to virtual/real machines.
9
+
10
+ This section covers some of the more common deployment scenarios.
11
+ --
12
+
13
+
14
+
15
+ [[cloud-deployment]]
16
+ == Deploying to the cloud
17
+
6
18
Spring Boot's executable jars are ready-made for most popular cloud PaaS
7
19
(platform-as-a-service) providers. These providers tend to require that you
8
20
"`bring your own container`"; they manage application processes (not Java applications
@@ -23,12 +35,11 @@ to run packaged within it.
23
35
In this section we'll look at what it takes to get the
24
36
<<getting-started.adoc#getting-started-first-application, simple application that we
25
37
developed>> in the "`Getting Started`" section up and running in the Cloud.
26
- --
27
38
28
39
29
40
30
41
[[cloud-deployment-cloud-foundry]]
31
- == Cloud Foundry
42
+ === Cloud Foundry
32
43
Cloud Foundry provides default buildpacks that come into play if no other buildpack is
33
44
specified. The Cloud Foundry https://github.com/cloudfoundry/java-buildpack[Java buildpack]
34
45
has excellent support for Spring applications, including Spring Boot. You can deploy
@@ -102,7 +113,7 @@ able to hit the application at the URI given, in this case
102
113
103
114
104
115
[[cloud-deployment-cloud-foundry-services]]
105
- === Binding to services
116
+ ==== Binding to services
106
117
By default, metadata about the running application as well as service connection
107
118
information is exposed to the application as environment variables (for example:
108
119
`$VCAP_SERVICES`). This architecture decision is due to Cloud Foundry's polyglot
@@ -142,7 +153,7 @@ auto-configuration support and a `spring-boot-starter-cloud-connectors` starter
142
153
143
154
144
155
[[cloud-deployment-heroku]]
145
- == Heroku
156
+ === Heroku
146
157
Heroku is another popular PaaS platform. To customize Heroku builds, you provide a
147
158
`Procfile`, which provides the incantation required to deploy an application. Heroku
148
159
assigns a `port` for the Java application to use and then ensures that routing to the
@@ -225,7 +236,7 @@ Your application should now be up and running on Heroku.
225
236
226
237
227
238
[[cloud-deployment-openshift]]
228
- == Openshift
239
+ === Openshift
229
240
https://www.openshift.com/[Openshift] is the RedHat public (and enterprise) PaaS solution.
230
241
Like Heroku, it works by running scripts triggered by git commits, so you can script
231
242
the launching of a Spring Boot application in pretty much any way you like as long as the
@@ -288,14 +299,85 @@ run the app.
288
299
289
300
290
301
[[cloud-deployment-gae]]
291
- == Google App Engine
302
+ === Google App Engine
292
303
Google App Engine is tied to the Servlet 2.5 API, so you can't deploy a Spring Application
293
304
there without some modifications. See the <<howto.adoc#howto-servlet-2-5, Servlet 2.5 section>>
294
305
of this guide.
295
306
296
307
308
+ [[deployment-service]]
309
+ == Installing Spring Boot applications
310
+ In additional to running Spring Boot applications using `java -jar` it is also possible
311
+ to execute applications directly on Unix systems (Linux, OSX, FreeBSD etc). This makes it
312
+ very easy to install and manage Spring Boot applications in common production
313
+ environments. As long as you are generating '`fully executable`' jars from your build, and
314
+ you are not using a custom `embeddedLaunchScript`, the following techniques can be used.
315
+
316
+
317
+
318
+ === Unix/Linux services
319
+ Spring Boot application can be easily started as Unix/Linux services using either `init.d`
320
+ or `systemd`.
321
+
322
+
323
+
324
+ ==== Installation as a init.d (system v) service
325
+ The default executable script that is embedded into Spring Boot executable jars will act
326
+ as an `init.d` script when it is symlinked to `/etc/init.d`. The standard `start`, `stop`,
327
+ `restart` and `status` commands can be used. The script supports the following features:
328
+
329
+ * Starts the services as the user that owns the jar file
330
+ * Tracks application PIDs using `/var/run/<appname>.pid`
331
+ * Writes console logs to `/var/log/<appname>.log`
332
+
333
+ Assuming that you have a Spring Boot application installed in `/var/myapp`, to install a
334
+ Spring Boot application as an `init.d` service simply create a symlink:
335
+
336
+ [indent=0,subs="verbatim,quotes,attributes"]
337
+ ----
338
+ $ sudo link -s /var/myapp/myapp.jar /etc/init.d/myapp
339
+ ----
340
+
341
+ TIP: It is advisable to create a specific user account to run you application. Ensure
342
+ that you have set the owner of the jar file using `chown` before installing your service.
297
343
298
- [[cloud-deployment-whats-next]]
344
+ Once installed, you can start and stop the service in the usual way. You can also flag the
345
+ application to start automatically using your standard operating system tools. For example,
346
+ if you use Debian:
347
+
348
+ [indent=0,subs="verbatim,quotes,attributes"]
349
+ ----
350
+ $ update-rc.d myapp defaults <priority>
351
+ ----
352
+
353
+
354
+
355
+ ==== Installation as a systemd service
356
+ Systemd is the successor to `init.d` scripts, and now being used by many many modern Linux
357
+ distributions. Although you can continue to use `init.d` script with `systemd`, it is also
358
+ possible to launch Spring Boot applications using `systemd` '`service`' scripts.
359
+
360
+ For example, to run a Spring Boot application installed in `var/myapp` you can add the
361
+ following script in `/etc/systemd/system/myapp.service`:
362
+
363
+ [indent=0]
364
+ ----
365
+ [Unit]
366
+ Description=myapp
367
+ After=syslog.target
368
+
369
+ [Service]
370
+ ExecStart=/var/myapp/myapp.jar
371
+
372
+ [Install]
373
+ WantedBy=multi-user.target
374
+ ----
375
+
376
+ TIP: Remember to change the `Description` and `ExecStart` fields for your application.
377
+
378
+
379
+
380
+ [[deployment-whats-next]]
299
381
== What to read next
300
382
Check out the http://www.cloudfoundry.com/[Cloud Foundry], https://www.heroku.com/[Heroku]
301
383
and https://www.openshift.com[Openshift] web sites for more information about the kinds of
@@ -307,6 +389,3 @@ The next section goes on to cover the _<<spring-boot-cli.adoc#cli, Spring Boot C
307
389
or you can jump ahead to read about
308
390
_<<build-tool-plugins.adoc#build-tool-plugins, build tool plugins>>_.
309
391
310
-
311
-
312
-
0 commit comments