2015-04-10 01:57:40 +08:00
[[deployment]]
2015-07-07 08:55:53 +08:00
= Deploying Spring Boot applications
2014-03-14 04:25:33 +08:00
[partintro]
--
2015-04-10 01:57:40 +08:00
Spring Boot's flexible packaging options provide a great deal of choice when it comes to
deploying your application. You can easily deploy Spring Boot applications to a variety
of cloud platforms, to a container images (such as Docker) or to virtual/real machines.
This section covers some of the more common deployment scenarios.
--
[[cloud-deployment]]
== Deploying to the cloud
2014-03-14 04:25:33 +08:00
Spring Boot's executable jars are ready-made for most popular cloud PaaS
(platform-as-a-service) providers. These providers tend to require that you
2014-10-28 18:53:47 +08:00
"`bring your own container`"; they manage application processes (not Java applications
2014-03-14 04:25:33 +08:00
specifically), so they need some intermediary layer that adapts _your_ application to the
_cloud's_ notion of a running process.
2014-10-28 18:53:47 +08:00
Two popular cloud providers, Heroku and Cloud Foundry, employ a "`buildpack`" approach.
2014-03-14 04:25:33 +08:00
The buildpack wraps your deployed code in whatever is needed to _start_ your
2015-11-11 04:15:27 +08:00
application: it might be a JDK and a call to `java`, it might be an embedded web server,
2014-10-13 03:47:19 +08:00
or it might be a full-fledged application server. A buildpack is pluggable, but ideally
2014-03-14 04:25:33 +08:00
you should be able to get by with as few customizations to it as possible.
This reduces the footprint of functionality that is not under your control. It minimizes
2015-04-16 21:10:36 +08:00
divergence between development and production environments.
2014-03-14 04:25:33 +08:00
Ideally, your application, like a Spring Boot executable jar, has everything that it needs
to run packaged within it.
In this section we'll look at what it takes to get the
<<getting-started.adoc#getting-started-first-application, simple application that we
2014-10-28 18:53:47 +08:00
developed>> in the "`Getting Started`" section up and running in the Cloud.
2014-03-14 04:25:33 +08:00
[[cloud-deployment-cloud-foundry]]
2015-04-10 01:57:40 +08:00
=== Cloud Foundry
2014-03-14 04:25:33 +08:00
Cloud Foundry provides default buildpacks that come into play if no other buildpack is
2014-04-23 23:41:20 +08:00
specified. The Cloud Foundry https://github.com/cloudfoundry/java-buildpack[Java buildpack]
2017-10-20 02:02:06 +08:00
has excellent support for Spring applications, including Spring Boot. You can deploy
2014-04-23 23:41:20 +08:00
stand-alone executable jar applications, as well as traditional `.war` packaged
applications.
2014-03-14 04:25:33 +08:00
2014-04-16 17:48:36 +08:00
Once you've built your application (using, for example, `mvn clean package`) and
2014-04-23 23:41:20 +08:00
http://docs.cloudfoundry.org/devguide/installcf/install-go-cli.html[installed the `cf`
command line tool], simply deploy your application using the `cf push` command as follows,
substituting the path to your compiled `.jar`. Be sure to have
http://docs.cloudfoundry.org/devguide/installcf/whats-new-v6.html#login[logged in with your
`cf` command line client] before pushing an application.
2014-03-14 04:25:33 +08:00
[indent=0,subs="verbatim,quotes,attributes"]
----
2014-04-23 23:41:20 +08:00
$ cf push acloudyspringtime -p target/demo-0.0.1-SNAPSHOT.jar
2014-04-01 11:02:54 +08:00
----
2014-04-23 23:41:20 +08:00
See the http://docs.cloudfoundry.org/devguide/installcf/whats-new-v6.html#push[`cf push`
documentation] for more options. If there is a Cloud Foundry
http://docs.cloudfoundry.org/devguide/deploy-apps/manifest.html[`manifest.yml`]
file present in the same directory, it will be consulted.
2014-04-01 11:02:54 +08:00
2014-04-23 23:41:20 +08:00
NOTE: Here we are substituting `acloudyspringtime` for whatever value you give `cf`
as the name of your application.
2014-04-01 11:02:54 +08:00
2014-03-14 04:25:33 +08:00
At this point `cf` will start uploading your application:
[indent=0,subs="verbatim,quotes,attributes"]
----
2014-04-01 11:02:54 +08:00
Uploading acloudyspringtime... *OK*
Preparing to start acloudyspringtime... *OK*
-----> Downloaded app package (*8.9M*)
2017-01-25 04:25:54 +08:00
-----> Java Buildpack Version: v3.12 (offline) | https://github.com/cloudfoundry/java-buildpack.git#6f25b7e
-----> Downloading Open Jdk JRE 1.8.0_121 from https://java-buildpack.cloudfoundry.org/openjdk/trusty/x86_64/openjdk-1.8.0_121.tar.gz (found in cache)
Expanding Open Jdk JRE to .java-buildpack/open_jdk_jre (1.6s)
-----> Downloading Open JDK Like Memory Calculator 2.0.2_RELEASE from https://java-buildpack.cloudfoundry.org/memory-calculator/trusty/x86_64/memory-calculator-2.0.2_RELEASE.tar.gz (found in cache)
Memory Settings: -Xss349K -Xmx681574K -XX:MaxMetaspaceSize=104857K -Xms681574K -XX:MetaspaceSize=104857K
-----> Downloading Container Certificate Trust Store 1.0.0_RELEASE from https://java-buildpack.cloudfoundry.org/container-certificate-trust-store/container-certificate-trust-store-1.0.0_RELEASE.jar (found in cache)
Adding certificates to .java-buildpack/container_certificate_trust_store/truststore.jks (0.6s)
-----> Downloading Spring Auto Reconfiguration 1.10.0_RELEASE from https://java-buildpack.cloudfoundry.org/auto-reconfiguration/auto-reconfiguration-1.10.0_RELEASE.jar (found in cache)
2014-04-01 11:02:54 +08:00
Checking status of app 'acloudyspringtime'...
0 of 1 instances running (1 starting)
...
2017-01-25 04:25:54 +08:00
0 of 1 instances running (1 starting)
2014-04-01 11:02:54 +08:00
...
0 of 1 instances running (1 starting)
...
1 of 1 instances running (1 running)
2014-04-23 23:41:20 +08:00
App started
2014-03-14 04:25:33 +08:00
----
2014-04-01 11:02:54 +08:00
Congratulations! The application is now live!
It's easy to then verify the status of the deployed application:
[indent=0,subs="verbatim,quotes,attributes"]
----
$ cf apps
2014-04-23 23:41:20 +08:00
Getting applications in ...
OK
2014-04-01 11:02:54 +08:00
2014-04-23 23:41:20 +08:00
name requested state instances memory disk urls
2014-04-01 11:02:54 +08:00
...
2014-04-23 23:41:20 +08:00
acloudyspringtime started 1/1 512M 1G acloudyspringtime.cfapps.io
2014-04-01 11:02:54 +08:00
...
----
2014-03-14 04:25:33 +08:00
Once Cloud Foundry acknowledges that your application has been deployed, you should be
2014-04-01 11:02:54 +08:00
able to hit the application at the URI given, in this case
2016-04-13 12:23:22 +08:00
`\http://acloudyspringtime.cfapps.io/`.
2014-03-14 04:25:33 +08:00
[[cloud-deployment-cloud-foundry-services]]
2015-04-10 01:57:40 +08:00
==== Binding to services
2014-10-13 03:47:19 +08:00
By default, metadata about the running application as well as service connection
2014-03-14 04:25:33 +08:00
information is exposed to the application as environment variables (for example:
`$VCAP_SERVICES`). This architecture decision is due to Cloud Foundry's polyglot
(any language and platform can be supported as a buildpack) nature; process-scoped
environment variables are language agnostic.
Environment variables don't always make for the easiest API so Spring Boot automatically
2014-03-18 02:36:56 +08:00
extracts them and flattens the data into properties that can be accessed through
2014-03-14 04:25:33 +08:00
Spring's `Environment` abstraction:
[source,java,indent=0]
----
@Component
class MyBean implements EnvironmentAware {
private String instanceId;
@Override
public void setEnvironment(Environment environment) {
this.instanceId = environment.getProperty("vcap.application.instance_id");
}
// ...
}
----
All Cloud Foundry properties are prefixed with `vcap`. You can use vcap properties to
access application information (such as the public URL of the application) and service
2016-05-02 20:17:48 +08:00
information (such as database credentials). See `CloudFoundryVcapEnvironmentPostProcessor`
Javadoc for complete details.
2014-03-14 04:25:33 +08:00
2014-12-10 11:52:56 +08:00
TIP: The http://cloud.spring.io/spring-cloud-connectors/[Spring Cloud Connectors] project
is a better fit for tasks such as configuring a DataSource. Spring Boot includes
2016-05-18 14:55:42 +08:00
auto-configuration support and a `spring-boot-starter-cloud-connectors` starter.
2014-03-14 04:25:33 +08:00
[[cloud-deployment-heroku]]
2015-04-10 01:57:40 +08:00
=== Heroku
2014-03-14 04:25:33 +08:00
Heroku is another popular PaaS platform. To customize Heroku builds, you provide a
`Procfile`, which provides the incantation required to deploy an application. Heroku
assigns a `port` for the Java application to use and then ensures that routing to the
external URI works.
2014-04-16 17:48:36 +08:00
You must configure your application to listen on the correct port. Here's the `Procfile`
for our starter REST application:
2014-03-14 04:25:33 +08:00
[indent=0]
----
web: java -Dserver.port=$PORT -jar target/demo-0.0.1-SNAPSHOT.jar
----
Spring Boot makes `-D` arguments available as properties accessible from a Spring
`Environment` instance. The `server.port` configuration property is fed to the embedded
2014-11-19 05:00:28 +08:00
Tomcat, Jetty or Undertow instance which then uses it when it starts up. The `$PORT`
environment variable is assigned to us by the Heroku PaaS.
2014-03-14 04:25:33 +08:00
This should be everything you need. The most common workflow for Heroku deployments is to
`git push` the code to production.
[indent=0,subs="verbatim,quotes,attributes"]
----
$ git push heroku master
Initializing repository, *done*.
Counting objects: 95, *done*.
Delta compression using up to 8 threads.
Compressing objects: 100% (78/78), *done*.
Writing objects: 100% (95/95), 8.66 MiB | 606.00 KiB/s, *done*.
Total 95 (delta 31), reused 0 (delta 0)
-----> Java app detected
2015-05-19 04:39:09 +08:00
-----> Installing OpenJDK 1.8... *done*
-----> Installing Maven 3.3.1... *done*
2014-03-14 04:25:33 +08:00
-----> Installing settings.xml... *done*
2015-05-19 04:39:09 +08:00
-----> Executing: mvn -B -DskipTests=true clean install
2014-03-14 04:25:33 +08:00
[INFO] Scanning for projects...
Downloading: http://repo.spring.io/...
Downloaded: http://repo.spring.io/... (818 B at 1.8 KB/sec)
....
Downloaded: http://s3pository.heroku.com/jvm/... (152 KB at 595.3 KB/sec)
[INFO] Installing /tmp/build_0c35a5d2-a067-4abc-a232-14b1fb7a8229/target/...
[INFO] Installing /tmp/build_0c35a5d2-a067-4abc-a232-14b1fb7a8229/pom.xml ...
[INFO] ------------------------------------------------------------------------
[INFO] *BUILD SUCCESS*
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 59.358s
[INFO] Finished at: Fri Mar 07 07:28:25 UTC 2014
[INFO] Final Memory: 20M/493M
[INFO] ------------------------------------------------------------------------
-----> Discovering process types
Procfile declares types -> *web*
-----> Compressing... *done*, 70.4MB
-----> Launching... *done*, v6
http://agile-sierra-1405.herokuapp.com/ *deployed to Heroku*
To git@heroku.com:agile-sierra-1405.git
* [new branch] master -> master
----
2014-04-16 17:48:36 +08:00
Your application should now be up and running on Heroku.
2014-03-14 04:25:33 +08:00
2014-06-03 21:56:27 +08:00
[[cloud-deployment-openshift]]
2015-11-11 04:15:27 +08:00
=== OpenShift
2017-10-12 09:58:33 +08:00
https://www.openshift.com/[OpenShift] is the Red Hat public (and enterprise) extension of the
Kubernetes container orchestration platform. Just as in Kubernetes, OpenShift has many
options for installing Spring Boot based applications.
2014-06-03 21:56:27 +08:00
2017-10-12 09:58:33 +08:00
OpenShift has many resources describing how to deploy Spring Boot applications, which include :
2014-06-07 13:13:25 +08:00
2017-10-12 09:58:33 +08:00
* https://blog.openshift.com/using-openshift-enterprise-grade-spring-boot-deployments/[Using the S2I builder]
* https://access.redhat.com/documentation/en-us/reference_architectures/2017/html-single/spring_boot_microservices_on_red_hat_openshift_container_platform_3/[Architecture guide]
* https://blog.openshift.com/using-spring-boot-on-openshift/[Running as traditional web application on Wildfly]
* https://blog.openshift.com/openshift-commons-briefing-96-cloud-native-applications-spring-rhoar/[OpenShift Commons Briefing]
2014-06-07 13:13:25 +08:00
2014-11-04 09:20:40 +08:00
2017-01-11 19:57:53 +08:00
[[cloud-deployment-aws]]
=== Amazon Web Services (AWS)
Amazon Web Services offers multiple ways to install Spring Boot based applications, either
as traditional web applications (war) or as executable jar files with an embedded web
server. Options include :
* AWS Elastic Beanstalk
* AWS Code Deploy
* AWS OPS Works
* AWS Cloud Formation
* AWS Container Registry
Each has different features and pricing model, here we will describe only the simplest
option : AWS Elastic Beanstalk.
==== AWS Elastic Beanstalk
As described in the official http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_Java.html[Elastic
Beanstalk Java guide], there are two main options to deploy a Java application; You can
2017-02-27 06:18:48 +08:00
either use the "`Tomcat Platform`" or the "`Java SE platform`".
2017-01-11 19:57:53 +08:00
===== Using the Tomcat platform
This option applies to Spring Boot projects producing a war file. There is no any special
configuration required, just follow the official guide.
===== Using the Java SE platform
This option applies to Spring Boot projects producing a jar file and running an embedded
web container. Elastic Beanstalk environments run an nginx instance on port 80 to proxy
the actual application, running on port 5000. To configure it, add the following to your
`application.properties`:
[indent=0]
----
server.port=5000
----
===== Best practices
====== Uploading binaries instead of sources
2017-07-25 16:02:03 +08:00
By default Elastic Beanstalk uploads sources and compiles them in AWS. To upload the
2017-01-11 19:57:53 +08:00
binaries instead, add the following to your `.elasticbeanstalk/config.yml` file:
[source,xml,indent=0,subs="verbatim,quotes,attributes"]
----
deploy:
artifact: target/demo-0.0.1-SNAPSHOT.jar
----
====== Reduce costs by setting the environment type
By default an Elastic Beanstalk environment is load balanced. The load balancer has a cost
perspective, to avoid it, set the environment type to "`Single instance`" as described
http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/environments-create-wizard.html#environments-create-wizard-capacity[in the Amazon documentation].
Single instance environments can be created using the CLI as well using the following
command:
[indent=0]
----
eb create -s
----
==== Summary
2017-07-25 16:02:03 +08:00
This is one of the easiest ways to get to AWS, but there are more things
2017-01-11 19:57:53 +08:00
to cover, e.g.: how to integrate Elastic Beanstalk into any CI / CD tool, using the
Elastic Beanstalk maven plugin instead of the CLI, etc. There is a
https://exampledriven.wordpress.com/2017/01/09/spring-boot-aws-elastic-beanstalk-example/[blog]
covering these topics more in detail.
2015-08-13 21:41:18 +08:00
[[cloud-deployment-boxfuse]]
=== Boxfuse and Amazon Web Services
https://boxfuse.com/[Boxfuse] works by turning your Spring Boot executable jar or war
into a minimal VM image that can be deployed unchanged either on VirtualBox or on AWS.
Boxfuse comes with deep integration for Spring Boot and will use the information from your
2015-11-11 04:15:27 +08:00
Spring Boot configuration file to automatically configure ports and health check URLs.
2015-08-13 21:41:18 +08:00
Boxfuse leverages this information both for the images it produces as well as for all the
resources it provisions (instances, security groups, elastic load balancers, etc).
Once you have created a https://console.boxfuse.com[Boxfuse account], connected it to your
AWS account, and installed the latest version of the Boxfuse Client, you can deploy your
Spring Boot application to AWS as follows (ensure the application has been built by
Maven or Gradle first using, for example, `mvn clean package`):
[indent=0]
----
$ boxfuse run myapp-1.0.jar -env=prod
----
See the https://boxfuse.com/docs/commandline/run.html[`boxfuse run` documentation] for
more options. If there is a https://boxfuse.com/docs/commandline/#configuration
[`boxfuse.conf`] file present in the current directory, it will be consulted.
TIP: By default Boxfuse will activate a Spring profile named `boxfuse` on startup and if
your executable jar or war contains an
https://boxfuse.com/docs/payloads/springboot.html#configuration
[`application-boxfuse.properties`]
file, Boxfuse will base its configuration based on the properties it contains.
2015-12-04 19:18:03 +08:00
At this point `boxfuse` will create an image for your application, upload it,
2015-08-13 21:41:18 +08:00
and then configure and start the necessary resources on AWS:
[indent=0,subs="verbatim,quotes,attributes"]
----
Fusing Image for myapp-1.0.jar ...
Image fused in 00:06.838s (53937 K) -> axelfontaine/myapp:1.0
Creating axelfontaine/myapp ...
Pushing axelfontaine/myapp:1.0 ...
Verifying axelfontaine/myapp:1.0 ...
Creating Elastic IP ...
Mapping myapp-axelfontaine.boxfuse.io to 52.28.233.167 ...
Waiting for AWS to create an AMI for axelfontaine/myapp:1.0 in eu-central-1 (this may take up to 50 seconds) ...
AMI created in 00:23.557s -> ami-d23f38cf
Creating security group boxfuse-sg_axelfontaine/myapp:1.0 ...
Launching t2.micro instance of axelfontaine/myapp:1.0 (ami-d23f38cf) in eu-central-1 ...
Instance launched in 00:30.306s -> i-92ef9f53
Waiting for AWS to boot Instance i-92ef9f53 and Payload to start at http://52.28.235.61/ ...
Payload started in 00:29.266s -> http://52.28.235.61/
Remapping Elastic IP 52.28.233.167 to i-92ef9f53 ...
Waiting 15s for AWS to complete Elastic IP Zero Downtime transition ...
Deployment completed successfully. axelfontaine/myapp:1.0 is up and running at http://myapp-axelfontaine.boxfuse.io/
----
Your application should now be up and running on AWS.
There's a blog on https://boxfuse.com/blog/spring-boot-ec2.html[deploying Spring Boot apps
2015-12-04 19:18:03 +08:00
on EC2] as well as https://boxfuse.com/docs/payloads/springboot.html[documentation
2015-08-13 21:41:18 +08:00
for the Boxfuse Spring Boot integration] on their website that will get you started with a
Maven build to run the app.
2014-11-04 01:04:57 +08:00
[[cloud-deployment-gae]]
2017-06-23 23:32:14 +08:00
=== Google Cloud
2017-07-07 07:52:53 +08:00
Google Cloud has several options that could be used to launch Spring Boot applications.
The easiest to get started with is probably App Engine, but you could also find ways to
run Spring Boot in a container with Container Engine, or on a virtual machine using
Compute Engine.
To run in App Engine you can create a project in the UI first, which sets up a unique
identifier for you and also HTTP routes. Add a Java app to the project and leave it empty,
then use the https://cloud.google.com/sdk/downloads[Google Cloud SDK] to push your
2017-06-23 23:32:14 +08:00
Spring Boot app into that slot from the command line or CI build.
2017-07-07 07:52:53 +08:00
App Engine needs you to create an `app.yaml` file to describe the resources your app
2017-08-02 22:48:20 +08:00
requires. Normally you put this in `src/main/appengine`, and it looks something like this:
2017-06-23 23:32:14 +08:00
[source,yaml,indent=0]
----
2017-07-07 07:52:53 +08:00
service: default
2017-06-23 23:32:14 +08:00
2017-07-07 07:52:53 +08:00
runtime: java
env: flex
2017-06-23 23:32:14 +08:00
2017-07-07 07:52:53 +08:00
runtime_config:
jdk: openjdk8
2017-06-23 23:32:14 +08:00
2017-07-07 07:52:53 +08:00
handlers:
- url: /.*
script: this field is required, but ignored
2017-06-23 23:32:14 +08:00
2017-07-07 07:52:53 +08:00
manual_scaling:
instances: 1
2017-06-23 23:32:14 +08:00
2017-07-07 07:52:53 +08:00
health_check:
enable_health_check: False
2017-06-23 23:32:14 +08:00
2017-07-07 07:52:53 +08:00
env_variables:
ENCRYPT_KEY: your_encryption_key_here
2017-06-23 23:32:14 +08:00
----
2017-07-07 07:52:53 +08:00
You can deploy the app, for example, with a Maven plugin by simply adding the project ID
to the build configuration:
2017-06-23 23:32:14 +08:00
[source,xml,indent=0,subs="verbatim,quotes,attributes"]
----
2017-07-07 07:52:53 +08:00
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>appengine-maven-plugin</artifactId>
<version>1.3.0</version>
<configuration>
<project>myproject</project>
</configuration>
</plugin>
2017-06-23 23:32:14 +08:00
----
2017-07-07 07:52:53 +08:00
Then deploy with `mvn appengine:deploy` (if you need to authenticate first the build will
fail).
2017-06-23 23:32:14 +08:00
2017-07-07 07:52:53 +08:00
NOTE: Google App Engine Classic is tied to the Servlet 2.5 API, so you can't deploy a
Spring Application there without some modifications. See the
<<howto.adoc#howto-servlet-2-5, Servlet 2.5 section>> of this guide.
2014-11-04 09:20:40 +08:00
2015-06-04 02:57:28 +08:00
2015-06-11 14:32:30 +08:00
[[deployment-install]]
2015-04-10 01:57:40 +08:00
== Installing Spring Boot applications
In additional to running Spring Boot applications using `java -jar` it is also possible
2017-06-06 17:42:30 +08:00
to make fully executable applications for Unix systems. A fully executable jar can be
executed like any other executable binary or it can be <<deployment-service,registered
with `init.d` or `systemd`>>. This makes it very easy to install and manage Spring Boot
applications in common production environments.
WARNING: 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 only make your jar or war
2017-06-09 13:53:49 +08:00
fully executable if you intend to execute it directly, rather than running it with
2017-06-06 17:42:30 +08:00
`java -jar` or deploying it to a servlet container.
2015-06-05 12:57:43 +08:00
To create a '`fully executable`' jar with Maven use the following plugin configuration:
[source,xml,indent=0,subs="verbatim,quotes,attributes"]
----
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
----
2017-06-06 17:42:30 +08:00
With Gradle, the equivalent configuration is:
2015-06-05 12:57:43 +08:00
[source,groovy,indent=0,subs="verbatim,quotes,attributes"]
----
2017-09-21 18:30:57 +08:00
bootJar {
launchScript()
2015-06-05 12:57:43 +08:00
}
----
2016-07-16 06:32:13 +08:00
You can then run your application by typing `./my-application.jar` (where
2017-06-06 17:42:30 +08:00
`my-application` is the name of your artifact). The directory containing the
jar will be used as your application's working directory.
2016-07-16 06:32:13 +08:00
2017-06-06 17:42:30 +08:00
[[deployment-install-supported-operating-systems]]
=== Supported operating systems
The default script supports most Linux distributions and is tested on CentOS and
2016-04-21 00:46:36 +08:00
Ubuntu. Other platforms, such as OS X and FreeBSD, will require the use of a custom
`embeddedLaunchScript`.
2015-04-10 01:57:40 +08:00
2015-06-11 14:32:30 +08:00
[[deployment-service]]
2015-04-10 01:57:40 +08:00
=== Unix/Linux services
Spring Boot application can be easily started as Unix/Linux services using either `init.d`
or `systemd`.
2015-06-11 14:32:30 +08:00
[[deployment-initd-service]]
2015-07-15 19:16:30 +08:00
==== Installation as an init.d service (System V)
2016-07-16 06:32:13 +08:00
If you've configured Spring Boot's Maven or Gradle plugin to generate a
<<deployment-install,fully executable jar>>, and you're not using a custom
`embeddedLaunchScript`, then your application can be used as an `init.d` service. Simply
symlink the jar to `init.d` to support the standard `start`, `stop`, `restart` and
`status` commands.
The script supports the following features:
2015-04-10 01:57:40 +08:00
* Starts the services as the user that owns the jar file
2016-01-15 23:36:42 +08:00
* Tracks application's PID using `/var/run/<appname>/<appname>.pid`
2015-04-10 01:57:40 +08:00
* 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 simply create a symlink:
[indent=0,subs="verbatim,quotes,attributes"]
----
2015-06-25 00:00:18 +08:00
$ sudo ln -s /var/myapp/myapp.jar /etc/init.d/myapp
2015-04-10 01:57:40 +08:00
----
2016-07-18 02:58:19 +08:00
Once installed, you can start and stop the service in the usual way. For example, on a
Debian based system:
2016-07-16 06:32:13 +08:00
[indent=0,subs="verbatim,quotes,attributes"]
----
2016-07-18 02:58:19 +08:00
$ service myapp start
2016-07-16 06:32:13 +08:00
----
2016-07-17 00:20:32 +08:00
TIP: If your application fails to start, check the log file written to
2016-07-16 06:32:13 +08:00
`/var/log/<appname>.log` for errors.
You can also flag the application to start automatically using your standard operating
2016-07-18 02:58:19 +08:00
system tools. For example, on Debian:
2015-04-10 01:57:40 +08:00
[indent=0,subs="verbatim,quotes,attributes"]
----
$ update-rc.d myapp defaults <priority>
----
2016-07-16 06:32:13 +08:00
2016-01-15 23:36:42 +08:00
[[deployment-initd-service-securing]]
===== Securing an init.d service
NOTE: The following is a set of guidelines on how to secure a Spring Boot application
that's being run as an init.d service. It is not intended to be an exhaustive list of
everything that should be done to harden an application and the environment in which it
runs.
When executed as root, as is the case when root is being used to start an init.d service,
the default executable script will run the application as the user which owns the jar
file. You should never run a Spring Boot application as `root` so your application's jar
file should never be owned by root. Instead, create a specific user to run your
application and use `chown` to make it the owner of the jar file. For example:
[indent=0,subs="verbatim,quotes,attributes"]
----
$ chown bootapp:bootapp your-app.jar
----
In this case, the default executable script will run the application as the `bootapp`
user.
TIP: To reduce the chances of the application's user account being compromised, you should
consider preventing it from using a login shell. Set the account's shell to
`/usr/sbin/nologin`, for example.
You should also take steps to prevent the modification of your application's jar file.
Firstly, configure its permissions so that it cannot be written and can only be read or
executed by its owner:
[indent=0,subs="verbatim,quotes,attributes"]
----
$ chmod 500 your-app.jar
----
Secondly, you should also take steps to limit the damage if your application or the
account that's running it is compromised. If an attacker does gain access, they could make
the jar file writable and change its contents. One way to protect against this is to make
it immutable using `chattr`:
[indent=0,subs="verbatim,quotes,attributes"]
----
$ sudo chattr +i your-app.jar
----
This will prevent any user, including root, from modifying the jar.
If root is used to control the application's service and you
<<deployment-script-customization-conf-file, use a `.conf` file>> to customize its
startup, the `.conf` file will be read and evaluated by the root user. It should be
secured accordingly. Use `chmod` so that the file can only be read by the owner and use
`chown` to make root the owner:
[indent=0,subs="verbatim,quotes,attributes"]
----
$ chmod 400 your-app.conf
$ sudo chown root:root your-app.conf
----
2015-06-04 02:57:28 +08:00
2015-06-11 14:32:30 +08:00
[[deployment-systemd-service]]
2015-06-05 12:57:43 +08:00
==== Installation as a systemd service
2016-01-28 05:40:38 +08:00
Systemd is the successor of the System V init system, and is now being used by many modern
Linux distributions. Although you can continue to use `init.d` scripts with `systemd`, it
is also possible to launch Spring Boot applications using `systemd` '`service`' scripts.
2015-06-05 12:57:43 +08:00
2016-01-28 05:40:38 +08:00
Assuming that you have a Spring Boot application installed in `/var/myapp`, to install a
Spring Boot application as a `systemd` service create a script named `myapp.service` using
the following example and place it in `/etc/systemd/system` directory:
2015-06-05 12:57:43 +08:00
[indent=0]
----
[Unit]
Description=myapp
After=syslog.target
[Service]
2015-10-24 07:44:34 +08:00
User=myapp
2015-06-05 12:57:43 +08:00
ExecStart=/var/myapp/myapp.jar
2015-10-29 05:50:35 +08:00
SuccessExitStatus=143
2015-06-05 12:57:43 +08:00
[Install]
WantedBy=multi-user.target
----
2016-01-28 05:40:38 +08:00
TIP: Remember to change the `Description`, `User` and `ExecStart` fields for your
application.
2016-09-20 13:58:46 +08:00
TIP: Note that `ExecStart` field does not declare the script action command, which means
that `run` command is used by default.
2016-01-28 05:40:38 +08:00
Note that unlike when running as an `init.d` service, user that runs the application, PID
2016-09-20 13:58:46 +08:00
file and console log file are managed by `systemd` itself and therefore must be configured
using appropriate fields in '`service`' script. Consult the
2016-01-28 05:40:38 +08:00
http://www.freedesktop.org/software/systemd/man/systemd.service.html[service unit
configuration man page] for more details.
To flag the application to start automatically on system boot use the following command:
[indent=0,subs="verbatim,quotes,attributes"]
----
$ systemctl enable myapp.service
----
Refer to `man systemctl` for more details.
2015-06-05 12:57:43 +08:00
2015-06-11 14:32:30 +08:00
[[deployment-script-customization]]
2015-06-05 12:57:43 +08:00
==== Customizing the startup script
2016-07-16 06:32:13 +08:00
The default embedded startup script written by the Maven or Gradle plugin can be
customized in a number of ways. For most people, using the default script along with
a few customizations is usually enough. If you find you can't customize something that
you need to, you can always use the `embeddedLaunchScript` option to write your own
file entirely.
[[deployment-script-customization-when-it-written]]
===== Customizing script when it's written
It often makes sense to customize elements of the start script as it's written into the
jar file. For example, init.d scripts can provide a "`description`" and, since you know
this up front (and it won't change), you may as well provide it when the jar is generated.
To customize written elements, use the `embeddedLaunchScriptProperties` option of the
Spring Boot Maven or Gradle plugins.
The following property substitutions are supported with the default script:
[cols="1,6"]
|===
|Name |Description
|`mode`
|The script mode. Defaults to `auto`.
|`initInfoProvides`
|The `Provides` section of "`INIT INFO`". Defaults to `spring-boot-application` for Gradle
and to `${project.artifactId}` for Maven.
2016-12-02 18:04:18 +08:00
|`initInfoRequiredStart`
|The `Required-Start` section of "`INIT INFO`". Defaults to `$remote_fs $syslog $network`.
|`initInfoRequiredStop`
|The `Required-Stop` section of "`INIT INFO`". Defaults to `$remote_fs $syslog $network`.
2017-01-07 23:27:41 +08:00
|`initInfoDefaultStart`
|The `Default-Start` section of "`INIT INFO`". Defaults to `2 3 4 5`.
|`initInfoDefaultStop`
|The `Default-Stop` section of "`INIT INFO`". Defaults to `0 1 6`.
2016-07-16 06:32:13 +08:00
|`initInfoShortDescription`
|The `Short-Description` section of "`INIT INFO`". Defaults to `Spring Boot Application`
for Gradle and to `${project.name}` for Maven.
|`initInfoDescription`
|The `Description` section of "`INIT INFO`". Defaults to `Spring Boot Application` for
Gradle and to `${project.description}` (falling back to `${project.name}`) for Maven.
|`initInfoChkconfig`
|The `chkconfig` section of "`INIT INFO`". Defaults to `2345 99 01`.
2016-08-12 03:25:59 +08:00
|`confFolder`
|The default value for `CONF_FOLDER`. Defaults to the folder containing the jar.
2017-06-12 03:42:07 +08:00
|`inlinedConfScript`
|Reference to a file script that should be inlined in the default launch script.
This can be used to set environmental variables such as `JAVA_OPTS` before
any external config files are loaded.
2016-07-16 06:32:13 +08:00
|`logFolder`
|The default value for `LOG_FOLDER`. Only valid for an `init.d` service.
2016-10-27 16:36:29 +08:00
|`logFilename`
|The default value for `LOG_FILENAME`. Only valid for an `init.d` service.
2016-07-16 06:32:13 +08:00
|`pidFolder`
|The default value for `PID_FOLDER`. Only valid for an `init.d` service.
2016-10-27 16:36:29 +08:00
|`pidFilename`
|The default value for the name of the pid file in `PID_FOLDER`. Only valid for an
`init.d` service.
2016-07-16 06:32:13 +08:00
|`useStartStopDaemon`
|If the `start-stop-daemon` command, when it's available, should be used to control the
process. Defaults to `true`.
2016-10-17 18:56:05 +08:00
|`stopWaitTime`
|The default value for `STOP_WAIT_TIME`. Only valid for an `init.d` service.
Defaults to 60 seconds.
2016-07-16 06:32:13 +08:00
|===
[[deployment-script-customization-when-it-runs]]
===== Customizing script when it runs
For items of the script that need to be customized _after_ the jar has been written you
can use environment variables or a
<<deployment-script-customization-conf-file, config file>>.
The following environment properties are supported with the default script:
2015-06-04 02:57:28 +08:00
[cols="1,6"]
|===
|Variable |Description
2015-05-12 17:23:44 +08:00
2015-06-04 02:57:28 +08:00
|`MODE`
2015-06-05 12:57:43 +08:00
|The "`mode`" of operation. The default depends on the way the jar was built, but will
usually be `auto` _(meaning it tries to guess if it is an init script by checking if it
is a symlink in a directory called `init.d`)_. You can explicitly set it to `service` so
that the `stop\|start\|status\|restart` commands work, or to `run` if you just want to
run the script in the foreground.
2015-05-12 17:23:44 +08:00
2016-01-21 22:39:39 +08:00
|`USE_START_STOP_DAEMON`
|If the `start-stop-daemon` command, when it's available, should be used to control the
process. Defaults to `true`.
2015-06-04 02:57:28 +08:00
|`PID_FOLDER`
|The root name of the pid folder (`/var/run` by default).
2015-05-12 17:23:44 +08:00
2015-06-04 02:57:28 +08:00
|`LOG_FOLDER`
|The name of the folder to put log files in (`/var/log` by default).
2015-05-12 17:23:44 +08:00
2016-02-24 02:13:12 +08:00
|`CONF_FOLDER`
|The name of the folder to read .conf files from (same folder as jar-file by default).
2015-10-15 19:30:23 +08:00
|`LOG_FILENAME`
|The name of the log file in the `LOG_FOLDER` (`<appname>.log` by default).
2015-06-04 02:57:28 +08:00
|`APP_NAME`
|The name of the app. If the jar is run from a symlink the script guesses the app name,
but if it is not a symlink, or you want to explicitly set the app name this can be
useful.
2015-05-12 17:23:44 +08:00
2015-08-27 00:54:20 +08:00
|`RUN_ARGS`
|The arguments to pass to the program (the Spring Boot app).
2015-06-04 02:57:28 +08:00
|`JAVA_HOME`
|The location of the `java` executable is discovered by using the `PATH` by default, but
you can set it explicitly if there is an executable file at `$JAVA_HOME/bin/java`.
2015-05-12 17:23:44 +08:00
2015-09-29 20:58:37 +08:00
|`JAVA_OPTS`
|Options that are passed to the JVM when it is launched.
2015-06-04 02:57:28 +08:00
|`JARFILE`
|The explicit location of the jar file, in case the script is being used to launch a jar
that it is not actually embedded in.
2015-05-12 17:23:44 +08:00
2015-06-04 02:57:28 +08:00
|`DEBUG`
|if not empty will set the `-x` flag on the shell process, making it easy to see the logic
in the script.
2016-10-17 18:56:05 +08:00
|`STOP_WAIT_TIME`
|The time in seconds to wait when stopping the application before forcing a shutdown
(`60` by default).
2015-06-04 02:57:28 +08:00
|===
2015-05-12 17:23:44 +08:00
2015-10-29 05:50:35 +08:00
NOTE: The `PID_FOLDER`, `LOG_FOLDER` and `LOG_FILENAME` variables are only valid for an
`init.d` service. With `systemd` the equivalent customizations are made using '`service`'
script. Check the
http://www.freedesktop.org/software/systemd/man/systemd.service.html[service unit
configuration man page] for more details.
2015-10-05 21:14:51 +08:00
[[deployment-script-customization-conf-file]]
With the exception of `JARFILE` and `APP_NAME`, the above settings can be configured using
2016-07-16 06:32:13 +08:00
a `.conf` file. The file is expected next to the jar file and have the same name but
suffixed with `.conf` rather than `.jar`. For example, a jar named `/var/myapp/myapp.jar`
will use the configuration file named `/var/myapp/myapp.conf`.
2015-10-05 21:14:51 +08:00
2016-07-16 06:32:13 +08:00
.myapp.conf
2015-10-05 21:14:51 +08:00
[indent=0,subs="verbatim,quotes,attributes"]
----
JAVA_OPTS=-Xmx1024M
LOG_FOLDER=/custom/log/folder
----
2016-07-16 06:32:13 +08:00
TIP: You can use a `CONF_FOLDER` environment variable to customize the location of the
config file if you don't like it living next to the jar.
2015-06-19 14:09:56 +08:00
2016-01-15 23:36:42 +08:00
To learn about securing this file appropriately, please refer to
<<deployment-initd-service-securing,the guidelines for securing an init.d service>>.
2015-04-10 01:57:40 +08:00
2015-10-09 22:37:58 +08:00
[[deployment-windows]]
2016-09-18 23:12:21 +08:00
=== Microsoft Windows services
2015-10-09 22:37:58 +08:00
Spring Boot application can be started as Windows service using
https://github.com/kohsuke/winsw[`winsw`].
A sample https://github.com/snicoll-scratches/spring-boot-daemon[maintained separately]
2016-01-15 23:36:42 +08:00
to the core of Spring Boot describes step-by-step how you can create a Windows service for
2015-10-09 22:37:58 +08:00
your Spring Boot application.
2015-04-10 01:57:40 +08:00
[[deployment-whats-next]]
2014-03-14 04:25:33 +08:00
== What to read next
2015-08-13 21:41:18 +08:00
Check out the http://www.cloudfoundry.com/[Cloud Foundry],
2015-11-11 04:15:27 +08:00
https://www.heroku.com/[Heroku], https://www.openshift.com[OpenShift] and
2015-08-13 21:41:18 +08:00
https://boxfuse.com[Boxfuse] web sites for more information about the kinds of features
that a PaaS can offer. These are just four of the most popular Java PaaS providers, since
Spring Boot is so amenable to cloud-based deployment you're free to consider other
providers as well.
2014-03-14 04:25:33 +08:00
2014-10-28 18:53:47 +08:00
The next section goes on to cover the _<<spring-boot-cli.adoc#cli, Spring Boot CLI>>_;
2014-03-14 04:25:33 +08:00
or you can jump ahead to read about
2014-10-28 18:53:47 +08:00
_<<build-tool-plugins.adoc#build-tool-plugins, build tool plugins>>_.
2014-03-14 04:25:33 +08:00