(window.webpackJsonp=window.webpackJsonp||[]).push([[48],{476:function(e,t,a){"use strict";a.r(t);var o=a(56),n=Object(o.a)({},(function(){var e=this,t=e.$createElement,a=e._self._c||t;return a("ContentSlotsDistributor",{attrs:{"slot-key":e.$parent.slotKey}},[a("h1",{attrs:{id:"deploying-spring-boot-applications"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#deploying-spring-boot-applications"}},[e._v("#")]),e._v(" Deploying Spring Boot Applications")]),e._v(" "),a("p",[e._v("Spring Boot’s flexible packaging options provide a great deal of choice when it comes to deploying your application.\nYou can deploy Spring Boot applications to a variety of cloud platforms, to virtual/real machines, or make them fully executable for Unix systems.")]),e._v(" "),a("p",[e._v("This section covers some of the more common deployment scenarios.")]),e._v(" "),a("h2",{attrs:{id:"_1-deploying-to-the-cloud"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_1-deploying-to-the-cloud"}},[e._v("#")]),e._v(" 1. Deploying to the Cloud")]),e._v(" "),a("p",[e._v("Spring Boot’s executable jars are ready-made for most popular cloud PaaS (Platform-as-a-Service) providers.\nThese providers tend to require that you “bring your own container”.\nThey manage application processes (not Java applications specifically), so they need an intermediary layer that adapts "),a("em",[e._v("your")]),e._v(" application to the "),a("em",[e._v("cloud’s")]),e._v(" notion of a running process.")]),e._v(" "),a("p",[e._v("Two popular cloud providers, Heroku and Cloud Foundry, employ a “buildpack” approach.\nThe buildpack wraps your deployed code in whatever is needed to "),a("em",[e._v("start")]),e._v(" your application.\nIt might be a JDK and a call to "),a("code",[e._v("java")]),e._v(", an embedded web server, or a full-fledged application server.\nA buildpack is pluggable, but ideally you should be able to get by with as few customizations to it as possible.\nThis reduces the footprint of functionality that is not under your control.\nIt minimizes divergence between development and production environments.")]),e._v(" "),a("p",[e._v("Ideally, your application, like a Spring Boot executable jar, has everything that it needs to run packaged within it.")]),e._v(" "),a("p",[e._v("In this section, we look at what it takes to get the "),a("RouterLink",{attrs:{to:"/en/spring-boot/getting-started.html#getting-started.first-application"}},[e._v("application that we developed")]),e._v(" in the “Getting Started” section up and running in the Cloud.")],1),e._v(" "),a("h3",{attrs:{id:"_1-1-cloud-foundry"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_1-1-cloud-foundry"}},[e._v("#")]),e._v(" 1.1. Cloud Foundry")]),e._v(" "),a("p",[e._v("Cloud Foundry provides default buildpacks that come into play if no other buildpack is specified.\nThe Cloud Foundry "),a("a",{attrs:{href:"https://github.com/cloudfoundry/java-buildpack",target:"_blank",rel:"noopener noreferrer"}},[e._v("Java buildpack"),a("OutboundLink")],1),e._v(" has excellent support for Spring applications, including Spring Boot.\nYou can deploy stand-alone executable jar applications as well as traditional "),a("code",[e._v(".war")]),e._v(" packaged applications.")]),e._v(" "),a("p",[e._v("Once you have built your application (by using, for example, "),a("code",[e._v("mvn clean package")]),e._v(") and have "),a("a",{attrs:{href:"https://docs.cloudfoundry.org/cf-cli/install-go-cli.html",target:"_blank",rel:"noopener noreferrer"}},[e._v("installed the "),a("code",[e._v("cf")]),e._v(" command line tool"),a("OutboundLink")],1),e._v(", deploy your application by using the "),a("code",[e._v("cf push")]),e._v(" command, substituting the path to your compiled "),a("code",[e._v(".jar")]),e._v(".\nBe sure to have "),a("a",{attrs:{href:"https://docs.cloudfoundry.org/cf-cli/getting-started.html#login",target:"_blank",rel:"noopener noreferrer"}},[e._v("logged in with your "),a("code",[e._v("cf")]),e._v(" command line client"),a("OutboundLink")],1),e._v(" before pushing an application.\nThe following line shows using the "),a("code",[e._v("cf push")]),e._v(" command to deploy an application:")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("$ cf push acloudyspringtime -p target/demo-0.0.1-SNAPSHOT.jar\n")])])]),a("table",[a("thead",[a("tr",[a("th"),e._v(" "),a("th",[e._v("In the preceding example, we substitute "),a("code",[e._v("acloudyspringtime")]),e._v(" for whatever value you give "),a("code",[e._v("cf")]),e._v(" as the name of your application.")])])]),e._v(" "),a("tbody")]),e._v(" "),a("p",[e._v("See the "),a("a",{attrs:{href:"https://docs.cloudfoundry.org/cf-cli/getting-started.html#push",target:"_blank",rel:"noopener noreferrer"}},[a("code",[e._v("cf push")]),e._v(" documentation"),a("OutboundLink")],1),e._v(" for more options.\nIf there is a Cloud Foundry "),a("a",{attrs:{href:"https://docs.cloudfoundry.org/devguide/deploy-apps/manifest.html",target:"_blank",rel:"noopener noreferrer"}},[a("code",[e._v("manifest.yml")]),a("OutboundLink")],1),e._v(" file present in the same directory, it is considered.")]),e._v(" "),a("p",[e._v("At this point, "),a("code",[e._v("cf")]),e._v(" starts uploading your application, producing output similar to the following example:")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("Uploading acloudyspringtime... OK\nPreparing to start acloudyspringtime... OK\n-----\x3e Downloaded app package (8.9M)\n-----\x3e Java Buildpack Version: v3.12 (offline) | https://github.com/cloudfoundry/java-buildpack.git#6f25b7e\n-----\x3e 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)\n Expanding Open Jdk JRE to .java-buildpack/open_jdk_jre (1.6s)\n-----\x3e 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)\n Memory Settings: -Xss349K -Xmx681574K -XX:MaxMetaspaceSize=104857K -Xms681574K -XX:MetaspaceSize=104857K\n-----\x3e 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)\n Adding certificates to .java-buildpack/container_certificate_trust_store/truststore.jks (0.6s)\n-----\x3e 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)\nChecking status of app 'acloudyspringtime'...\n 0 of 1 instances running (1 starting)\n ...\n 0 of 1 instances running (1 starting)\n ...\n 0 of 1 instances running (1 starting)\n ...\n 1 of 1 instances running (1 running)\n\nApp started\n")])])]),a("p",[e._v("Congratulations! The application is now live!")]),e._v(" "),a("p",[e._v("Once your application is live, you can verify the status of the deployed application by using the "),a("code",[e._v("cf apps")]),e._v(" command, as shown in the following example:")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("$ cf apps\nGetting applications in ...\nOK\n\nname requested state instances memory disk urls\n...\nacloudyspringtime started 1/1 512M 1G acloudyspringtime.cfapps.io\n...\n")])])]),a("p",[e._v("Once Cloud Foundry acknowledges that your application has been deployed, you should be able to find the application at the URI given.\nIn the preceding example, you could find it at "),a("code",[e._v("https://acloudyspringtime.cfapps.io/")]),e._v(".")]),e._v(" "),a("h4",{attrs:{id:"_1-1-1-binding-to-services"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_1-1-1-binding-to-services"}},[e._v("#")]),e._v(" 1.1.1. Binding to Services")]),e._v(" "),a("p",[e._v("By default, metadata about the running application as well as service connection information is exposed to the application as environment variables (for example: "),a("code",[e._v("$VCAP_SERVICES")]),e._v(").\nThis architecture decision is due to Cloud Foundry’s polyglot (any language and platform can be supported as a buildpack) nature.\nProcess-scoped environment variables are language agnostic.")]),e._v(" "),a("p",[e._v("Environment variables do not always make for the easiest API, so Spring Boot automatically extracts them and flattens the data into properties that can be accessed through Spring’s "),a("code",[e._v("Environment")]),e._v(" abstraction, as shown in the following example:")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v('import org.springframework.context.EnvironmentAware;\nimport org.springframework.core.env.Environment;\nimport org.springframework.stereotype.Component;\n\n@Component\npublic class MyBean implements EnvironmentAware {\n\n private String instanceId;\n\n @Override\n public void setEnvironment(Environment environment) {\n this.instanceId = environment.getProperty("vcap.application.instance_id");\n }\n\n // ...\n\n}\n\n')])])]),a("p",[e._v("All Cloud Foundry properties are prefixed with "),a("code",[e._v("vcap")]),e._v(".\nYou can use "),a("code",[e._v("vcap")]),e._v(" properties to access application information (such as the public URL of the application) and service information (such as database credentials).\nSee the "),a("a",{attrs:{href:"https://docs.spring.io/spring-boot/docs/2.6.4/api/org/springframework/boot/cloud/CloudFoundryVcapEnvironmentPostProcessor.html",target:"_blank",rel:"noopener noreferrer"}},[e._v("‘CloudFoundryVcapEnvironmentPostProcessor’"),a("OutboundLink")],1),e._v(" Javadoc for complete details.")]),e._v(" "),a("table",[a("thead",[a("tr",[a("th"),e._v(" "),a("th",[e._v("The "),a("a",{attrs:{href:"https://github.com/pivotal-cf/java-cfenv/",target:"_blank",rel:"noopener noreferrer"}},[e._v("Java CFEnv"),a("OutboundLink")],1),e._v(" project is a better fit for tasks such as configuring a DataSource.")])])]),e._v(" "),a("tbody")]),e._v(" "),a("h3",{attrs:{id:"_1-2-kubernetes"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_1-2-kubernetes"}},[e._v("#")]),e._v(" 1.2. Kubernetes")]),e._v(" "),a("p",[e._v("Spring Boot auto-detects Kubernetes deployment environments by checking the environment for "),a("code",[e._v('"*_SERVICE_HOST"')]),e._v(" and "),a("code",[e._v('"*_SERVICE_PORT"')]),e._v(" variables.\nYou can override this detection with the "),a("code",[e._v("spring.main.cloud-platform")]),e._v(" configuration property.")]),e._v(" "),a("p",[e._v("Spring Boot helps you to "),a("RouterLink",{attrs:{to:"/en/spring-boot/features.html#features.spring-application.application-availability"}},[e._v("manage the state of your application")]),e._v(" and export it with "),a("RouterLink",{attrs:{to:"/en/spring-boot/actuator.html#actuator.endpoints.kubernetes-probes"}},[e._v("HTTP Kubernetes Probes using Actuator")]),e._v(".")],1),e._v(" "),a("h4",{attrs:{id:"_1-2-1-kubernetes-container-lifecycle"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_1-2-1-kubernetes-container-lifecycle"}},[e._v("#")]),e._v(" 1.2.1. Kubernetes Container Lifecycle")]),e._v(" "),a("p",[e._v("When Kubernetes deletes an application instance, the shutdown process involves several subsystems concurrently: shutdown hooks, unregistering the service, removing the instance from the load-balancer…​\nBecause this shutdown processing happens in parallel (and due to the nature of distributed systems), there is a window during which traffic can be routed to a pod that has also begun its shutdown processing.")]),e._v(" "),a("p",[e._v("You can configure a sleep execution in a preStop handler to avoid requests being routed to a pod that has already begun shutting down.\nThis sleep should be long enough for new requests to stop being routed to the pod and its duration will vary from deployment to deployment.\nThe preStop handler can be configured by using the PodSpec in the pod’s configuration file as follows:")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v('spec:\n containers:\n - name: "example-container"\n image: "example-image"\n lifecycle:\n preStop:\n exec:\n command: ["sh", "-c", "sleep 10"]\n')])])]),a("p",[e._v("Once the pre-stop hook has completed, SIGTERM will be sent to the container and "),a("RouterLink",{attrs:{to:"/en/spring-boot/web.html#web.graceful-shutdown"}},[e._v("graceful shutdown")]),e._v(" will begin, allowing any remaining in-flight requests to complete.")],1),e._v(" "),a("table",[a("thead",[a("tr",[a("th"),e._v(" "),a("th",[e._v("When Kubernetes sends a SIGTERM signal to the pod, it waits for a specified time called the termination grace period (the default for which is 30 seconds)."),a("br"),e._v("If the containers are still running after the grace period, they are sent the SIGKILL signal and forcibly removed."),a("br"),e._v("If the pod takes longer than 30 seconds to shut down, which could be because you have increased "),a("code",[e._v("spring.lifecycle.timeout-per-shutdown-phase")]),e._v(", make sure to increase the termination grace period by setting the "),a("code",[e._v("terminationGracePeriodSeconds")]),e._v(" option in the Pod YAML.")])])]),e._v(" "),a("tbody")]),e._v(" "),a("h3",{attrs:{id:"_1-3-heroku"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_1-3-heroku"}},[e._v("#")]),e._v(" 1.3. Heroku")]),e._v(" "),a("p",[e._v("Heroku is another popular PaaS platform.\nTo customize Heroku builds, you provide a "),a("code",[e._v("Procfile")]),e._v(", which provides the incantation required to deploy an application.\nHeroku assigns a "),a("code",[e._v("port")]),e._v(" for the Java application to use and then ensures that routing to the external URI works.")]),e._v(" "),a("p",[e._v("You must configure your application to listen on the correct port.\nThe following example shows the "),a("code",[e._v("Procfile")]),e._v(" for our starter REST application:")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("web: java -Dserver.port=$PORT -jar target/demo-0.0.1-SNAPSHOT.jar\n")])])]),a("p",[e._v("Spring Boot makes "),a("code",[e._v("-D")]),e._v(" arguments available as properties accessible from a Spring "),a("code",[e._v("Environment")]),e._v(" instance.\nThe "),a("code",[e._v("server.port")]),e._v(" configuration property is fed to the embedded Tomcat, Jetty, or Undertow instance, which then uses the port when it starts up.\nThe "),a("code",[e._v("$PORT")]),e._v(" environment variable is assigned to us by the Heroku PaaS.")]),e._v(" "),a("p",[e._v("This should be everything you need.\nThe most common deployment workflow for Heroku deployments is to "),a("code",[e._v("git push")]),e._v(" the code to production, as shown in the following example:")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("$ git push heroku main\n")])])]),a("p",[e._v("Which will result in the following:")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("Initializing repository, done.\nCounting objects: 95, done.\nDelta compression using up to 8 threads.\nCompressing objects: 100% (78/78), done.\nWriting objects: 100% (95/95), 8.66 MiB | 606.00 KiB/s, done.\nTotal 95 (delta 31), reused 0 (delta 0)\n\n-----\x3e Java app detected\n-----\x3e Installing OpenJDK 1.8... done\n-----\x3e Installing Maven 3.3.1... done\n-----\x3e Installing settings.xml... done\n-----\x3e Executing: mvn -B -DskipTests=true clean install\n\n [INFO] Scanning for projects...\n Downloading: https://repo.spring.io/...\n Downloaded: https://repo.spring.io/... (818 B at 1.8 KB/sec)\n ....\n Downloaded: https://s3pository.heroku.com/jvm/... (152 KB at 595.3 KB/sec)\n [INFO] Installing /tmp/build_0c35a5d2-a067-4abc-a232-14b1fb7a8229/target/...\n [INFO] Installing /tmp/build_0c35a5d2-a067-4abc-a232-14b1fb7a8229/pom.xml ...\n [INFO] ------------------------------------------------------------------------\n [INFO] BUILD SUCCESS\n [INFO] ------------------------------------------------------------------------\n [INFO] Total time: 59.358s\n [INFO] Finished at: Fri Mar 07 07:28:25 UTC 2014\n [INFO] Final Memory: 20M/493M\n [INFO] ------------------------------------------------------------------------\n\n-----\x3e Discovering process types\n Procfile declares types -> web\n\n-----\x3e Compressing... done, 70.4MB\n-----\x3e Launching... done, v6\n https://agile-sierra-1405.herokuapp.com/ deployed to Heroku\n\nTo [email protected]:agile-sierra-1405.git\n * [new branch] main -> main\n")])])]),a("p",[e._v("Your application should now be up and running on Heroku.\nFor more details, see "),a("a",{attrs:{href:"https://devcenter.heroku.com/articles/deploying-spring-boot-apps-to-heroku",target:"_blank",rel:"noopener noreferrer"}},[e._v("Deploying Spring Boot Applications to Heroku"),a("OutboundLink")],1),e._v(".")]),e._v(" "),a("h3",{attrs:{id:"_1-4-openshift"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_1-4-openshift"}},[e._v("#")]),e._v(" 1.4. OpenShift")]),e._v(" "),a("p",[a("a",{attrs:{href:"https://www.openshift.com/",target:"_blank",rel:"noopener noreferrer"}},[e._v("OpenShift"),a("OutboundLink")],1),e._v(" has many resources describing how to deploy Spring Boot applications, including:")]),e._v(" "),a("ul",[a("li",[a("p",[a("a",{attrs:{href:"https://blog.openshift.com/using-openshift-enterprise-grade-spring-boot-deployments/",target:"_blank",rel:"noopener noreferrer"}},[e._v("Using the S2I builder"),a("OutboundLink")],1)])]),e._v(" "),a("li",[a("p",[a("a",{attrs:{href:"https://access.redhat.com/documentation/en-us/reference_architectures/2017/html-single/spring_boot_microservices_on_red_hat_openshift_container_platform_3/",target:"_blank",rel:"noopener noreferrer"}},[e._v("Architecture guide"),a("OutboundLink")],1)])]),e._v(" "),a("li",[a("p",[a("a",{attrs:{href:"https://blog.openshift.com/using-spring-boot-on-openshift/",target:"_blank",rel:"noopener noreferrer"}},[e._v("Running as a traditional web application on Wildfly"),a("OutboundLink")],1)])]),e._v(" "),a("li",[a("p",[a("a",{attrs:{href:"https://blog.openshift.com/openshift-commons-briefing-96-cloud-native-applications-spring-rhoar/",target:"_blank",rel:"noopener noreferrer"}},[e._v("OpenShift Commons Briefing"),a("OutboundLink")],1)])])]),e._v(" "),a("h3",{attrs:{id:"_1-5-amazon-web-services-aws"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_1-5-amazon-web-services-aws"}},[e._v("#")]),e._v(" 1.5. Amazon Web Services (AWS)")]),e._v(" "),a("p",[e._v("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.\nThe options include:")]),e._v(" "),a("ul",[a("li",[a("p",[e._v("AWS Elastic Beanstalk")])]),e._v(" "),a("li",[a("p",[e._v("AWS Code Deploy")])]),e._v(" "),a("li",[a("p",[e._v("AWS OPS Works")])]),e._v(" "),a("li",[a("p",[e._v("AWS Cloud Formation")])]),e._v(" "),a("li",[a("p",[e._v("AWS Container Registry")])])]),e._v(" "),a("p",[e._v("Each has different features and pricing models.\nIn this document, we describe to approach using AWS Elastic Beanstalk.")]),e._v(" "),a("h4",{attrs:{id:"_1-5-1-aws-elastic-beanstalk"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_1-5-1-aws-elastic-beanstalk"}},[e._v("#")]),e._v(" 1.5.1. AWS Elastic Beanstalk")]),e._v(" "),a("p",[e._v("As described in the official "),a("a",{attrs:{href:"https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_Java.html",target:"_blank",rel:"noopener noreferrer"}},[e._v("Elastic Beanstalk Java guide"),a("OutboundLink")],1),e._v(", there are two main options to deploy a Java application.\nYou can either use the “Tomcat Platform” or the “Java SE platform”.")]),e._v(" "),a("h5",{attrs:{id:"using-the-tomcat-platform"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#using-the-tomcat-platform"}},[e._v("#")]),e._v(" Using the Tomcat Platform")]),e._v(" "),a("p",[e._v("This option applies to Spring Boot projects that produce a war file.\nNo special configuration is required.\nYou need only follow the official guide.")]),e._v(" "),a("h5",{attrs:{id:"using-the-java-se-platform"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#using-the-java-se-platform"}},[e._v("#")]),e._v(" Using the Java SE Platform")]),e._v(" "),a("p",[e._v("This option applies to Spring Boot projects that produce a jar file and run an embedded web container.\nElastic Beanstalk environments run an nginx instance on port 80 to proxy the actual application, running on port 5000.\nTo configure it, add the following line to your "),a("code",[e._v("application.properties")]),e._v(" file:")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("server.port=5000\n")])])]),a("table",[a("thead",[a("tr",[a("th"),e._v(" "),a("th",[e._v("Upload binaries instead of sources"),a("br"),a("br"),e._v("By default, Elastic Beanstalk uploads sources and compiles them in AWS."),a("br"),e._v("However, it is best to upload the binaries instead."),a("br"),e._v("To do so, add lines similar to the following to your "),a("code",[e._v(".elasticbeanstalk/config.yml")]),e._v(" file:"),a("br"),a("br"),a("code",[e._v("
deploy:
artifact: target/demo-0.0.1-SNAPSHOT.jar
")])])])]),e._v(" "),a("tbody")]),e._v(" "),a("table",[a("thead",[a("tr",[a("th"),e._v(" "),a("th",[e._v("Reduce costs by setting the environment type"),a("br"),a("br"),e._v("By default an Elastic Beanstalk environment is load balanced."),a("br"),e._v("The load balancer has a significant cost."),a("br"),e._v("To avoid that cost, set the environment type to “Single instance”, as described in "),a("a",{attrs:{href:"https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/environments-create-wizard.html#environments-create-wizard-capacity",target:"_blank",rel:"noopener noreferrer"}},[e._v("the Amazon documentation"),a("OutboundLink")],1),e._v("."),a("br"),e._v("You can also create single instance environments by using the CLI and the following command:"),a("br"),a("br"),a("code",[e._v("
eb create -s
")])])])]),e._v(" "),a("tbody")]),e._v(" "),a("h4",{attrs:{id:"_1-5-2-summary"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_1-5-2-summary"}},[e._v("#")]),e._v(" 1.5.2. Summary")]),e._v(" "),a("p",[e._v("This is one of the easiest ways to get to AWS, but there are more things to cover, such as how to integrate Elastic Beanstalk into any CI / CD tool, use the Elastic Beanstalk Maven plugin instead of the CLI, and others.\nThere is a "),a("a",{attrs:{href:"https://exampledriven.wordpress.com/2017/01/09/spring-boot-aws-elastic-beanstalk-example/",target:"_blank",rel:"noopener noreferrer"}},[e._v("blog post"),a("OutboundLink")],1),e._v(" covering these topics more in detail.")]),e._v(" "),a("h3",{attrs:{id:"_1-6-cloudcaptain-and-amazon-web-services"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_1-6-cloudcaptain-and-amazon-web-services"}},[e._v("#")]),e._v(" 1.6. CloudCaptain and Amazon Web Services")]),e._v(" "),a("p",[a("a",{attrs:{href:"https://cloudcaptain.sh/",target:"_blank",rel:"noopener noreferrer"}},[e._v("CloudCaptain"),a("OutboundLink")],1),e._v(" 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.\nCloudCaptain comes with deep integration for Spring Boot and uses the information from your Spring Boot configuration file to automatically configure ports and health check URLs.\nCloudCaptain leverages this information both for the images it produces as well as for all the resources it provisions (instances, security groups, elastic load balancers, and so on).")]),e._v(" "),a("p",[e._v("Once you have created a "),a("a",{attrs:{href:"https://console.cloudcaptain.sh",target:"_blank",rel:"noopener noreferrer"}},[e._v("CloudCaptain account"),a("OutboundLink")],1),e._v(", connected it to your AWS account, installed the latest version of the CloudCaptain Client, and ensured that the application has been built by Maven or Gradle (by using, for example, "),a("code",[e._v("mvn clean package")]),e._v("), you can deploy your Spring Boot application to AWS with a command similar to the following:")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("$ boxfuse run myapp-1.0.jar -env=prod\n")])])]),a("p",[e._v("See the "),a("a",{attrs:{href:"https://cloudcaptain.sh/docs/commandline/run.html",target:"_blank",rel:"noopener noreferrer"}},[a("code",[e._v("boxfuse run")]),e._v(" documentation"),a("OutboundLink")],1),e._v(" for more options.\nIf there is a "),a("a",{attrs:{href:"https://cloudcaptain.sh/docs/commandline/#configuration",target:"_blank",rel:"noopener noreferrer"}},[a("code",[e._v("boxfuse.conf")]),a("OutboundLink")],1),e._v(" file present in the current directory, it is considered.")]),e._v(" "),a("table",[a("thead",[a("tr",[a("th"),e._v(" "),a("th",[e._v("By default, CloudCaptain activates a Spring profile named "),a("code",[e._v("boxfuse")]),e._v(" on startup."),a("br"),e._v("If your executable jar or war contains an "),a("a",{attrs:{href:"https://cloudcaptain.sh/docs/payloads/springboot.html#configuration",target:"_blank",rel:"noopener noreferrer"}},[a("code",[e._v("application-boxfuse.properties")]),a("OutboundLink")],1),e._v(" file, CloudCaptain bases its configuration on the properties it contains.")])])]),e._v(" "),a("tbody")]),e._v(" "),a("p",[e._v("At this point, CloudCaptain creates an image for your application, uploads it, and configures and starts the necessary resources on AWS, resulting in output similar to the following example:")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("Fusing Image for myapp-1.0.jar ...\nImage fused in 00:06.838s (53937 K) -> axelfontaine/myapp:1.0\nCreating axelfontaine/myapp ...\nPushing axelfontaine/myapp:1.0 ...\nVerifying axelfontaine/myapp:1.0 ...\nCreating Elastic IP ...\nMapping myapp-axelfontaine.boxfuse.io to 52.28.233.167 ...\nWaiting for AWS to create an AMI for axelfontaine/myapp:1.0 in eu-central-1 (this may take up to 50 seconds) ...\nAMI created in 00:23.557s -> ami-d23f38cf\nCreating security group boxfuse-sg_axelfontaine/myapp:1.0 ...\nLaunching t2.micro instance of axelfontaine/myapp:1.0 (ami-d23f38cf) in eu-central-1 ...\nInstance launched in 00:30.306s -> i-92ef9f53\nWaiting for AWS to boot Instance i-92ef9f53 and Payload to start at https://52.28.235.61/ ...\nPayload started in 00:29.266s -> https://52.28.235.61/\nRemapping Elastic IP 52.28.233.167 to i-92ef9f53 ...\nWaiting 15s for AWS to complete Elastic IP Zero Downtime transition ...\nDeployment completed successfully. axelfontaine/myapp:1.0 is up and running at https://myapp-axelfontaine.boxfuse.io/\n")])])]),a("p",[e._v("Your application should now be up and running on AWS.")]),e._v(" "),a("p",[e._v("See the blog post on "),a("a",{attrs:{href:"https://cloudcaptain.sh/blog/spring-boot-ec2.html",target:"_blank",rel:"noopener noreferrer"}},[e._v("deploying Spring Boot apps on EC2"),a("OutboundLink")],1),e._v(" as well as the "),a("a",{attrs:{href:"https://cloudcaptain.sh/docs/payloads/springboot.html",target:"_blank",rel:"noopener noreferrer"}},[e._v("documentation for the CloudCaptain Spring Boot integration"),a("OutboundLink")],1),e._v(" to get started with a Maven build to run the app.")]),e._v(" "),a("h3",{attrs:{id:"_1-7-azure"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_1-7-azure"}},[e._v("#")]),e._v(" 1.7. Azure")]),e._v(" "),a("p",[e._v("This "),a("a",{attrs:{href:"https://spring.io/guides/gs/spring-boot-for-azure/",target:"_blank",rel:"noopener noreferrer"}},[e._v("Getting Started guide"),a("OutboundLink")],1),e._v(" walks you through deploying your Spring Boot application to either "),a("a",{attrs:{href:"https://azure.microsoft.com/en-us/services/spring-cloud/",target:"_blank",rel:"noopener noreferrer"}},[e._v("Azure Spring Cloud"),a("OutboundLink")],1),e._v(" or "),a("a",{attrs:{href:"https://docs.microsoft.com/en-us/azure/app-service/overview",target:"_blank",rel:"noopener noreferrer"}},[e._v("Azure App Service"),a("OutboundLink")],1),e._v(".")]),e._v(" "),a("h3",{attrs:{id:"_1-8-google-cloud"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_1-8-google-cloud"}},[e._v("#")]),e._v(" 1.8. Google Cloud")]),e._v(" "),a("p",[e._v("Google Cloud has several options that can be used to launch Spring Boot applications.\nThe 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 with Compute Engine.")]),e._v(" "),a("p",[e._v("To run in App Engine, you can create a project in the UI first, which sets up a unique identifier for you and also sets up HTTP routes.\nAdd a Java app to the project and leave it empty and then use the "),a("a",{attrs:{href:"https://cloud.google.com/sdk/install",target:"_blank",rel:"noopener noreferrer"}},[e._v("Google Cloud SDK"),a("OutboundLink")],1),e._v(" to push your Spring Boot app into that slot from the command line or CI build.")]),e._v(" "),a("p",[e._v("App Engine Standard requires you to use WAR packaging.\nFollow "),a("a",{attrs:{href:"https://github.com/GoogleCloudPlatform/java-docs-samples/tree/master/appengine-java8/springboot-helloworld/README.md",target:"_blank",rel:"noopener noreferrer"}},[e._v("these steps"),a("OutboundLink")],1),e._v(" to deploy App Engine Standard application to Google Cloud.")]),e._v(" "),a("p",[e._v("Alternatively, App Engine Flex requires you to create an "),a("code",[e._v("app.yaml")]),e._v(" file to describe the resources your app requires.\nNormally, you put this file in "),a("code",[e._v("src/main/appengine")]),e._v(", and it should resemble the following file:")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v('service: "default"\n\nruntime: "java"\nenv: "flex"\n\nruntime_config:\n jdk: "openjdk8"\n\nhandlers:\n- url: "/.*"\n script: "this field is required, but ignored"\n\nmanual_scaling:\n instances: 1\n\nhealth_check:\n enable_health_check: false\n\nenv_variables:\n ENCRYPT_KEY: "your_encryption_key_here"\n')])])]),a("p",[e._v("You can deploy the app (for example, with a Maven plugin) by adding the project ID to the build configuration, as shown in the following example:")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("\n com.google.cloud.tools\n appengine-maven-plugin\n 1.3.0\n \n myproject\n \n\n")])])]),a("p",[e._v("Then deploy with "),a("code",[e._v("mvn appengine:deploy")]),e._v(" (if you need to authenticate first, the build fails).")]),e._v(" "),a("h2",{attrs:{id:"_2-installing-spring-boot-applications"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_2-installing-spring-boot-applications"}},[e._v("#")]),e._v(" 2. Installing Spring Boot Applications")]),e._v(" "),a("p",[e._v("In addition to running Spring Boot applications by using "),a("code",[e._v("java -jar")]),e._v(", it is also possible to make fully executable applications for Unix systems.\nA fully executable jar can be executed like any other executable binary or it can be "),a("a",{attrs:{href:"#deployment.installing.nix-services"}},[e._v("registered with "),a("code",[e._v("init.d")]),e._v(" or "),a("code",[e._v("systemd")])]),e._v(".\nThis helps when installing and managing Spring Boot applications in common production environments.")]),e._v(" "),a("table",[a("thead",[a("tr",[a("th"),e._v(" "),a("th",[e._v("Fully executable jars work by embedding an extra script at the front of the file."),a("br"),e._v("Currently, some tools do not accept this format, so you may not always be able to use this technique."),a("br"),e._v("For example, "),a("code",[e._v("jar -xf")]),e._v(" may silently fail to extract a jar or war that has been made fully executable."),a("br"),e._v("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 "),a("code",[e._v("java -jar")]),e._v(" or deploying it to a servlet container.")])])]),e._v(" "),a("tbody")]),e._v(" "),a("table",[a("thead",[a("tr",[a("th"),e._v(" "),a("th",[e._v("A zip64-format jar file cannot be made fully executable."),a("br"),e._v("Attempting to do so will result in a jar file that is reported as corrupt when executed directly or with "),a("code",[e._v("java -jar")]),e._v("."),a("br"),e._v("A standard-format jar file that contains one or more zip64-format nested jars can be fully executable.")])])]),e._v(" "),a("tbody")]),e._v(" "),a("p",[e._v("To create a ‘fully executable’ jar with Maven, use the following plugin configuration:")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("\n org.springframework.boot\n spring-boot-maven-plugin\n \n true\n \n\n")])])]),a("p",[e._v("The following example shows the equivalent Gradle configuration:")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("bootJar {\n launchScript()\n}\n")])])]),a("p",[e._v("You can then run your application by typing "),a("code",[e._v("./my-application.jar")]),e._v(" (where "),a("code",[e._v("my-application")]),e._v(" is the name of your artifact).\nThe directory containing the jar is used as your application’s working directory.")]),e._v(" "),a("h3",{attrs:{id:"_2-1-supported-operating-systems"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_2-1-supported-operating-systems"}},[e._v("#")]),e._v(" 2.1. Supported Operating Systems")]),e._v(" "),a("p",[e._v("The default script supports most Linux distributions and is tested on CentOS and Ubuntu.\nOther platforms, such as OS X and FreeBSD, require the use of a custom "),a("code",[e._v("embeddedLaunchScript")]),e._v(".")]),e._v(" "),a("h3",{attrs:{id:"_2-2-unix-linux-services"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_2-2-unix-linux-services"}},[e._v("#")]),e._v(" 2.2. Unix/Linux Services")]),e._v(" "),a("p",[e._v("Spring Boot application can be easily started as Unix/Linux services by using either "),a("code",[e._v("init.d")]),e._v(" or "),a("code",[e._v("systemd")]),e._v(".")]),e._v(" "),a("h4",{attrs:{id:"_2-2-1-installation-as-an-init-d-service-system-v"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_2-2-1-installation-as-an-init-d-service-system-v"}},[e._v("#")]),e._v(" 2.2.1. Installation as an init.d Service (System V)")]),e._v(" "),a("p",[e._v("If you configured Spring Boot’s Maven or Gradle plugin to generate a "),a("a",{attrs:{href:"#deployment.installing"}},[e._v("fully executable jar")]),e._v(", and you do not use a custom "),a("code",[e._v("embeddedLaunchScript")]),e._v(", your application can be used as an "),a("code",[e._v("init.d")]),e._v(" service.\nTo do so, symlink the jar to "),a("code",[e._v("init.d")]),e._v(" to support the standard "),a("code",[e._v("start")]),e._v(", "),a("code",[e._v("stop")]),e._v(", "),a("code",[e._v("restart")]),e._v(", and "),a("code",[e._v("status")]),e._v(" commands.")]),e._v(" "),a("p",[e._v("The script supports the following features:")]),e._v(" "),a("ul",[a("li",[a("p",[e._v("Starts the services as the user that owns the jar file")])]),e._v(" "),a("li",[a("p",[e._v("Tracks the application’s PID by using "),a("code",[e._v("/var/run//.pid")])])]),e._v(" "),a("li",[a("p",[e._v("Writes console logs to "),a("code",[e._v("/var/log/.log")])])])]),e._v(" "),a("p",[e._v("Assuming that you have a Spring Boot application installed in "),a("code",[e._v("/var/myapp")]),e._v(", to install a Spring Boot application as an "),a("code",[e._v("init.d")]),e._v(" service, create a symlink, as follows:")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("$ sudo ln -s /var/myapp/myapp.jar /etc/init.d/myapp\n")])])]),a("p",[e._v("Once installed, you can start and stop the service in the usual way.\nFor example, on a Debian-based system, you could start it with the following command:")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("$ service myapp start\n")])])]),a("table",[a("thead",[a("tr",[a("th"),e._v(" "),a("th",[e._v("If your application fails to start, check the log file written to "),a("code",[e._v("/var/log/.log")]),e._v(" for errors.")])])]),e._v(" "),a("tbody")]),e._v(" "),a("p",[e._v("You can also flag the application to start automatically by using your standard operating system tools.\nFor example, on Debian, you could use the following command:")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("$ update-rc.d myapp defaults \n")])])]),a("h5",{attrs:{id:"securing-an-init-d-service"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#securing-an-init-d-service"}},[e._v("#")]),e._v(" Securing an init.d Service")]),e._v(" "),a("table",[a("thead",[a("tr",[a("th"),e._v(" "),a("th",[e._v("The following is a set of guidelines on how to secure a Spring Boot application that runs as an init.d service."),a("br"),e._v("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.")])])]),e._v(" "),a("tbody")]),e._v(" "),a("p",[e._v("When executed as root, as is the case when root is being used to start an init.d service, the default executable script runs the application as the user specified in the "),a("code",[e._v("RUN_AS_USER")]),e._v(" environment variable.\nWhen the environment variable is not set, the user who owns the jar file is used instead.\nYou should never run a Spring Boot application as "),a("code",[e._v("root")]),e._v(", so "),a("code",[e._v("RUN_AS_USER")]),e._v(" should never be root and your application’s jar file should never be owned by root.\nInstead, create a specific user to run your application and set the "),a("code",[e._v("RUN_AS_USER")]),e._v(" environment variable or use "),a("code",[e._v("chown")]),e._v(" to make it the owner of the jar file, as shown in the following example:")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("$ chown bootapp:bootapp your-app.jar\n")])])]),a("p",[e._v("In this case, the default executable script runs the application as the "),a("code",[e._v("bootapp")]),e._v(" user.")]),e._v(" "),a("table",[a("thead",[a("tr",[a("th"),e._v(" "),a("th",[e._v("To reduce the chances of the application’s user account being compromised, you should consider preventing it from using a login shell."),a("br"),e._v("For example, you can set the account’s shell to "),a("code",[e._v("/usr/sbin/nologin")]),e._v(".")])])]),e._v(" "),a("tbody")]),e._v(" "),a("p",[e._v("You should also take steps to prevent the modification of your application’s jar file.\nFirstly, configure its permissions so that it cannot be written and can only be read or executed by its owner, as shown in the following example:")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("$ chmod 500 your-app.jar\n")])])]),a("p",[e._v("Second, you should also take steps to limit the damage if your application or the account that is running it is compromised.\nIf an attacker does gain access, they could make the jar file writable and change its contents.\nOne way to protect against this is to make it immutable by using "),a("code",[e._v("chattr")]),e._v(", as shown in the following example:")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("$ sudo chattr +i your-app.jar\n")])])]),a("p",[e._v("This will prevent any user, including root, from modifying the jar.")]),e._v(" "),a("p",[e._v("If root is used to control the application’s service and you "),a("a",{attrs:{href:"#deployment.installing.nix-services.script-customization.when-running.conf-file"}},[e._v("use a "),a("code",[e._v(".conf")]),e._v(" file")]),e._v(" to customize its startup, the "),a("code",[e._v(".conf")]),e._v(" file is read and evaluated by the root user.\nIt should be secured accordingly.\nUse "),a("code",[e._v("chmod")]),e._v(" so that the file can only be read by the owner and use "),a("code",[e._v("chown")]),e._v(" to make root the owner, as shown in the following example:")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("$ chmod 400 your-app.conf\n$ sudo chown root:root your-app.conf\n")])])]),a("h4",{attrs:{id:"_2-2-2-installation-as-a-systemd-service"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_2-2-2-installation-as-a-systemd-service"}},[e._v("#")]),e._v(" 2.2.2. Installation as a systemd Service")]),e._v(" "),a("p",[a("code",[e._v("systemd")]),e._v(" is the successor of the System V init system and is now being used by many modern Linux distributions.\nAlthough you can continue to use "),a("code",[e._v("init.d")]),e._v(" scripts with "),a("code",[e._v("systemd")]),e._v(", it is also possible to launch Spring Boot applications by using "),a("code",[e._v("systemd")]),e._v(" ‘service’ scripts.")]),e._v(" "),a("p",[e._v("Assuming that you have a Spring Boot application installed in "),a("code",[e._v("/var/myapp")]),e._v(", to install a Spring Boot application as a "),a("code",[e._v("systemd")]),e._v(" service, create a script named "),a("code",[e._v("myapp.service")]),e._v(" and place it in "),a("code",[e._v("/etc/systemd/system")]),e._v(" directory.\nThe following script offers an example:")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("[Unit]\nDescription=myapp\nAfter=syslog.target\n\n[Service]\nUser=myapp\nExecStart=/var/myapp/myapp.jar\nSuccessExitStatus=143\n\n[Install]\nWantedBy=multi-user.target\n")])])]),a("table",[a("thead",[a("tr",[a("th"),e._v(" "),a("th",[e._v("Remember to change the "),a("code",[e._v("Description")]),e._v(", "),a("code",[e._v("User")]),e._v(", and "),a("code",[e._v("ExecStart")]),e._v(" fields for your application.")])])]),e._v(" "),a("tbody")]),e._v(" "),a("table",[a("thead",[a("tr",[a("th"),e._v(" "),a("th",[e._v("The "),a("code",[e._v("ExecStart")]),e._v(" field does not declare the script action command, which means that the "),a("code",[e._v("run")]),e._v(" command is used by default.")])])]),e._v(" "),a("tbody")]),e._v(" "),a("p",[e._v("Note that, unlike when running as an "),a("code",[e._v("init.d")]),e._v(" service, the user that runs the application, the PID file, and the console log file are managed by "),a("code",[e._v("systemd")]),e._v(" itself and therefore must be configured by using appropriate fields in the ‘service’ script.\nConsult the "),a("a",{attrs:{href:"https://www.freedesktop.org/software/systemd/man/systemd.service.html",target:"_blank",rel:"noopener noreferrer"}},[e._v("service unit configuration man page"),a("OutboundLink")],1),e._v(" for more details.")]),e._v(" "),a("p",[e._v("To flag the application to start automatically on system boot, use the following command:")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("$ systemctl enable myapp.service\n")])])]),a("p",[e._v("Run "),a("code",[e._v("man systemctl")]),e._v(" for more details.")]),e._v(" "),a("h4",{attrs:{id:"_2-2-3-customizing-the-startup-script"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_2-2-3-customizing-the-startup-script"}},[e._v("#")]),e._v(" 2.2.3. Customizing the Startup Script")]),e._v(" "),a("p",[e._v("The default embedded startup script written by the Maven or Gradle plugin can be customized in a number of ways.\nFor most people, using the default script along with a few customizations is usually enough.\nIf you find you cannot customize something that you need to, use the "),a("code",[e._v("embeddedLaunchScript")]),e._v(" option to write your own file entirely.")]),e._v(" "),a("h5",{attrs:{id:"customizing-the-start-script-when-it-is-written"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#customizing-the-start-script-when-it-is-written"}},[e._v("#")]),e._v(" Customizing the Start Script When It Is Written")]),e._v(" "),a("p",[e._v("It often makes sense to customize elements of the start script as it is written into the jar file.\nFor example, init.d scripts can provide a “description”.\nSince you know the description up front (and it need not change), you may as well provide it when the jar is generated.")]),e._v(" "),a("p",[e._v("To customize written elements, use the "),a("code",[e._v("embeddedLaunchScriptProperties")]),e._v(" option of the Spring Boot Maven plugin or the "),a("a",{attrs:{href:"https://docs.spring.io/spring-boot/docs/2.6.4/gradle-plugin/reference/htmlsingle/#packaging-executable-configuring-launch-script",target:"_blank",rel:"noopener noreferrer"}},[a("code",[e._v("properties")]),e._v(" property of the Spring Boot Gradle plugin’s "),a("code",[e._v("launchScript")]),a("OutboundLink")],1),e._v(".")]),e._v(" "),a("p",[e._v("The following property substitutions are supported with the default script:")]),e._v(" "),a("table",[a("thead",[a("tr",[a("th",[e._v("Name")]),e._v(" "),a("th",[e._v("Description")]),e._v(" "),a("th",[e._v("Gradle default")]),e._v(" "),a("th",[e._v("Maven default")])])]),e._v(" "),a("tbody",[a("tr",[a("td",[a("code",[e._v("mode")])]),e._v(" "),a("td",[e._v("The script mode.")]),e._v(" "),a("td",[a("code",[e._v("auto")])]),e._v(" "),a("td",[a("code",[e._v("auto")])])]),e._v(" "),a("tr",[a("td",[a("code",[e._v("initInfoProvides")])]),e._v(" "),a("td",[e._v("The "),a("code",[e._v("Provides")]),e._v(" section of “INIT INFO”")]),e._v(" "),a("td",[a("code",[e._v("${task.baseName}")])]),e._v(" "),a("td",[a("code",[e._v("${project.artifactId}")])])]),e._v(" "),a("tr",[a("td",[a("code",[e._v("initInfoRequiredStart")])]),e._v(" "),a("td",[a("code",[e._v("Required-Start")]),e._v(" section of “INIT INFO”.")]),e._v(" "),a("td",[a("code",[e._v("$remote_fs $syslog $network")])]),e._v(" "),a("td",[a("code",[e._v("$remote_fs $syslog $network")])])]),e._v(" "),a("tr",[a("td",[a("code",[e._v("initInfoRequiredStop")])]),e._v(" "),a("td",[a("code",[e._v("Required-Stop")]),e._v(" section of “INIT INFO”.")]),e._v(" "),a("td",[a("code",[e._v("$remote_fs $syslog $network")])]),e._v(" "),a("td",[a("code",[e._v("$remote_fs $syslog $network")])])]),e._v(" "),a("tr",[a("td",[a("code",[e._v("initInfoDefaultStart")])]),e._v(" "),a("td",[a("code",[e._v("Default-Start")]),e._v(" section of “INIT INFO”.")]),e._v(" "),a("td",[a("code",[e._v("2 3 4 5")])]),e._v(" "),a("td",[a("code",[e._v("2 3 4 5")])])]),e._v(" "),a("tr",[a("td",[a("code",[e._v("initInfoDefaultStop")])]),e._v(" "),a("td",[a("code",[e._v("Default-Stop")]),e._v(" section of “INIT INFO”.")]),e._v(" "),a("td",[a("code",[e._v("0 1 6")])]),e._v(" "),a("td",[a("code",[e._v("0 1 6")])])]),e._v(" "),a("tr",[a("td",[a("code",[e._v("initInfoShortDescription")])]),e._v(" "),a("td",[a("code",[e._v("Short-Description")]),e._v(" section of “INIT INFO”.")]),e._v(" "),a("td",[e._v("Single-line version of "),a("code",[e._v("${project.description}")]),e._v(" (falling back to "),a("code",[e._v("${task.baseName}")]),e._v(")")]),e._v(" "),a("td",[a("code",[e._v("${project.name}")])])]),e._v(" "),a("tr",[a("td",[a("code",[e._v("initInfoDescription")])]),e._v(" "),a("td",[a("code",[e._v("Description")]),e._v(" section of “INIT INFO”.")]),e._v(" "),a("td",[a("code",[e._v("${project.description}")]),e._v(" (falling back to "),a("code",[e._v("${task.baseName}")]),e._v(")")]),e._v(" "),a("td",[a("code",[e._v("${project.description}")]),e._v(" (falling back to "),a("code",[e._v("${project.name}")]),e._v(")")])]),e._v(" "),a("tr",[a("td",[a("code",[e._v("initInfoChkconfig")])]),e._v(" "),a("td",[a("code",[e._v("chkconfig")]),e._v(" section of “INIT INFO”")]),e._v(" "),a("td",[a("code",[e._v("2345 99 01")])]),e._v(" "),a("td",[a("code",[e._v("2345 99 01")])])]),e._v(" "),a("tr",[a("td",[a("code",[e._v("confFolder")])]),e._v(" "),a("td",[e._v("The default value for "),a("code",[e._v("CONF_FOLDER")])]),e._v(" "),a("td",[e._v("Folder containing the jar")]),e._v(" "),a("td",[e._v("Folder containing the jar")])]),e._v(" "),a("tr",[a("td",[a("code",[e._v("inlinedConfScript")])]),e._v(" "),a("td",[e._v("Reference to a file script that should be inlined in the default launch script."),a("br"),e._v("This can be used to set environmental variables such as "),a("code",[e._v("JAVA_OPTS")]),e._v(" before any external config files are loaded")]),e._v(" "),a("td"),e._v(" "),a("td")]),e._v(" "),a("tr",[a("td",[a("code",[e._v("logFolder")])]),e._v(" "),a("td",[e._v("Default value for "),a("code",[e._v("LOG_FOLDER")]),e._v("."),a("br"),e._v("Only valid for an "),a("code",[e._v("init.d")]),e._v(" service")]),e._v(" "),a("td"),e._v(" "),a("td")]),e._v(" "),a("tr",[a("td",[a("code",[e._v("logFilename")])]),e._v(" "),a("td",[e._v("Default value for "),a("code",[e._v("LOG_FILENAME")]),e._v("."),a("br"),e._v("Only valid for an "),a("code",[e._v("init.d")]),e._v(" service")]),e._v(" "),a("td"),e._v(" "),a("td")]),e._v(" "),a("tr",[a("td",[a("code",[e._v("pidFolder")])]),e._v(" "),a("td",[e._v("Default value for "),a("code",[e._v("PID_FOLDER")]),e._v("."),a("br"),e._v("Only valid for an "),a("code",[e._v("init.d")]),e._v(" service")]),e._v(" "),a("td"),e._v(" "),a("td")]),e._v(" "),a("tr",[a("td",[a("code",[e._v("pidFilename")])]),e._v(" "),a("td",[e._v("Default value for the name of the PID file in "),a("code",[e._v("PID_FOLDER")]),e._v("."),a("br"),e._v("Only valid for an "),a("code",[e._v("init.d")]),e._v(" service")]),e._v(" "),a("td"),e._v(" "),a("td")]),e._v(" "),a("tr",[a("td",[a("code",[e._v("useStartStopDaemon")])]),e._v(" "),a("td",[e._v("Whether the "),a("code",[e._v("start-stop-daemon")]),e._v(" command, when it is available, should be used to control the process")]),e._v(" "),a("td",[a("code",[e._v("true")])]),e._v(" "),a("td",[a("code",[e._v("true")])])]),e._v(" "),a("tr",[a("td",[a("code",[e._v("stopWaitTime")])]),e._v(" "),a("td",[e._v("Default value for "),a("code",[e._v("STOP_WAIT_TIME")]),e._v(" in seconds."),a("br"),e._v("Only valid for an "),a("code",[e._v("init.d")]),e._v(" service")]),e._v(" "),a("td",[e._v("60")]),e._v(" "),a("td",[e._v("60")])])])]),e._v(" "),a("h5",{attrs:{id:"customizing-a-script-when-it-runs"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#customizing-a-script-when-it-runs"}},[e._v("#")]),e._v(" Customizing a Script When It Runs")]),e._v(" "),a("p",[e._v("For items of the script that need to be customized "),a("em",[e._v("after")]),e._v(" the jar has been written, you can use environment variables or a "),a("a",{attrs:{href:"#deployment.installing.nix-services.script-customization.when-running.conf-file"}},[e._v("config file")]),e._v(".")]),e._v(" "),a("p",[e._v("The following environment properties are supported with the default script:")]),e._v(" "),a("table",[a("thead",[a("tr",[a("th",[e._v("Variable")]),e._v(" "),a("th",[e._v("Description")])])]),e._v(" "),a("tbody",[a("tr",[a("td",[a("code",[e._v("MODE")])]),e._v(" "),a("td",[e._v("The “mode” of operation."),a("br"),e._v("The default depends on the way the jar was built but is usually "),a("code",[e._v("auto")]),e._v(" (meaning it tries to guess if it is an init script by checking if it is a symlink in a directory called "),a("code",[e._v("init.d")]),e._v(")."),a("br"),e._v("You can explicitly set it to "),a("code",[e._v("service")]),e._v(" so that the "),a("code",[e._v("stop|start|status|restart")]),e._v(" commands work or to "),a("code",[e._v("run")]),e._v(" if you want to run the script in the foreground.")])]),e._v(" "),a("tr",[a("td",[a("code",[e._v("RUN_AS_USER")])]),e._v(" "),a("td",[e._v("The user that will be used to run the application."),a("br"),e._v("When not set, the user that owns the jar file will be used.")])]),e._v(" "),a("tr",[a("td",[a("code",[e._v("USE_START_STOP_DAEMON")])]),e._v(" "),a("td",[e._v("Whether the "),a("code",[e._v("start-stop-daemon")]),e._v(" command, when it is available, should be used to control the process."),a("br"),e._v("Defaults to "),a("code",[e._v("true")]),e._v(".")])]),e._v(" "),a("tr",[a("td",[a("code",[e._v("PID_FOLDER")])]),e._v(" "),a("td",[e._v("The root name of the pid folder ("),a("code",[e._v("/var/run")]),e._v(" by default).")])]),e._v(" "),a("tr",[a("td",[a("code",[e._v("LOG_FOLDER")])]),e._v(" "),a("td",[e._v("The name of the folder in which to put log files ("),a("code",[e._v("/var/log")]),e._v(" by default).")])]),e._v(" "),a("tr",[a("td",[a("code",[e._v("CONF_FOLDER")])]),e._v(" "),a("td",[e._v("The name of the folder from which to read .conf files (same folder as jar-file by default).")])]),e._v(" "),a("tr",[a("td",[a("code",[e._v("LOG_FILENAME")])]),e._v(" "),a("td",[e._v("The name of the log file in the "),a("code",[e._v("LOG_FOLDER")]),e._v(" ("),a("code",[e._v(".log")]),e._v(" by default).")])]),e._v(" "),a("tr",[a("td",[a("code",[e._v("APP_NAME")])]),e._v(" "),a("td",[e._v("The name of the app."),a("br"),e._v("If the jar is run from a symlink, the script guesses the app name."),a("br"),e._v("If it is not a symlink or you want to explicitly set the app name, this can be useful.")])]),e._v(" "),a("tr",[a("td",[a("code",[e._v("RUN_ARGS")])]),e._v(" "),a("td",[e._v("The arguments to pass to the program (the Spring Boot app).")])]),e._v(" "),a("tr",[a("td",[a("code",[e._v("JAVA_HOME")])]),e._v(" "),a("td",[e._v("The location of the "),a("code",[e._v("java")]),e._v(" executable is discovered by using the "),a("code",[e._v("PATH")]),e._v(" by default, but you can set it explicitly if there is an executable file at "),a("code",[e._v("$JAVA_HOME/bin/java")]),e._v(".")])]),e._v(" "),a("tr",[a("td",[a("code",[e._v("JAVA_OPTS")])]),e._v(" "),a("td",[e._v("Options that are passed to the JVM when it is launched.")])]),e._v(" "),a("tr",[a("td",[a("code",[e._v("JARFILE")])]),e._v(" "),a("td",[e._v("The explicit location of the jar file, in case the script is being used to launch a jar that it is not actually embedded.")])]),e._v(" "),a("tr",[a("td",[a("code",[e._v("DEBUG")])]),e._v(" "),a("td",[e._v("If not empty, sets the "),a("code",[e._v("-x")]),e._v(" flag on the shell process, allowing you to see the logic in the script.")])]),e._v(" "),a("tr",[a("td",[a("code",[e._v("STOP_WAIT_TIME")])]),e._v(" "),a("td",[e._v("The time in seconds to wait when stopping the application before forcing a shutdown ("),a("code",[e._v("60")]),e._v(" by default).")])])])]),e._v(" "),a("table",[a("thead",[a("tr",[a("th"),e._v(" "),a("th",[e._v("The "),a("code",[e._v("PID_FOLDER")]),e._v(", "),a("code",[e._v("LOG_FOLDER")]),e._v(", and "),a("code",[e._v("LOG_FILENAME")]),e._v(" variables are only valid for an "),a("code",[e._v("init.d")]),e._v(" service."),a("br"),e._v("For "),a("code",[e._v("systemd")]),e._v(", the equivalent customizations are made by using the ‘service’ script."),a("br"),e._v("See the "),a("a",{attrs:{href:"https://www.freedesktop.org/software/systemd/man/systemd.service.html",target:"_blank",rel:"noopener noreferrer"}},[e._v("service unit configuration man page"),a("OutboundLink")],1),e._v(" for more details.")])])]),e._v(" "),a("tbody")]),e._v(" "),a("p",[e._v("With the exception of "),a("code",[e._v("JARFILE")]),e._v(" and "),a("code",[e._v("APP_NAME")]),e._v(", the settings listed in the preceding section can be configured by using a "),a("code",[e._v(".conf")]),e._v(" file.\nThe file is expected to be next to the jar file and have the same name but suffixed with "),a("code",[e._v(".conf")]),e._v(" rather than "),a("code",[e._v(".jar")]),e._v(".\nFor example, a jar named "),a("code",[e._v("/var/myapp/myapp.jar")]),e._v(" uses the configuration file named "),a("code",[e._v("/var/myapp/myapp.conf")]),e._v(", as shown in the following example:")]),e._v(" "),a("p",[e._v("myapp.conf")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("JAVA_OPTS=-Xmx1024M\nLOG_FOLDER=/custom/log/folder\n")])])]),a("table",[a("thead",[a("tr",[a("th"),e._v(" "),a("th",[e._v("If you do not like having the config file next to the jar file, you can set a "),a("code",[e._v("CONF_FOLDER")]),e._v(" environment variable to customize the location of the config file.")])])]),e._v(" "),a("tbody")]),e._v(" "),a("p",[e._v("To learn about securing this file appropriately, see "),a("a",{attrs:{href:"#deployment.installing.nix-services.init-d.securing"}},[e._v("the guidelines for securing an init.d service")]),e._v(".")]),e._v(" "),a("h3",{attrs:{id:"_2-3-microsoft-windows-services"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_2-3-microsoft-windows-services"}},[e._v("#")]),e._v(" 2.3. Microsoft Windows Services")]),e._v(" "),a("p",[e._v("A Spring Boot application can be started as a Windows service by using "),a("a",{attrs:{href:"https://github.com/kohsuke/winsw",target:"_blank",rel:"noopener noreferrer"}},[a("code",[e._v("winsw")]),a("OutboundLink")],1),e._v(".")]),e._v(" "),a("p",[e._v("A ("),a("a",{attrs:{href:"https://github.com/snicoll/spring-boot-daemon",target:"_blank",rel:"noopener noreferrer"}},[e._v("separately maintained sample"),a("OutboundLink")],1),e._v(") describes step-by-step how you can create a Windows service for your Spring Boot application.")]),e._v(" "),a("h2",{attrs:{id:"_3-what-to-read-next"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_3-what-to-read-next"}},[e._v("#")]),e._v(" 3. What to Read Next")]),e._v(" "),a("p",[e._v("See the "),a("a",{attrs:{href:"https://www.cloudfoundry.org/",target:"_blank",rel:"noopener noreferrer"}},[e._v("Cloud Foundry"),a("OutboundLink")],1),e._v(", "),a("a",{attrs:{href:"https://www.heroku.com/",target:"_blank",rel:"noopener noreferrer"}},[e._v("Heroku"),a("OutboundLink")],1),e._v(", "),a("a",{attrs:{href:"https://www.openshift.com",target:"_blank",rel:"noopener noreferrer"}},[e._v("OpenShift"),a("OutboundLink")],1),e._v(", and "),a("a",{attrs:{href:"https://boxfuse.com",target:"_blank",rel:"noopener noreferrer"}},[e._v("Boxfuse"),a("OutboundLink")],1),e._v(" web sites for more information about the kinds of features that a PaaS can offer.\nThese are just four of the most popular Java PaaS providers.\nSince Spring Boot is so amenable to cloud-based deployment, you can freely consider other providers as well.")]),e._v(" "),a("p",[e._v("The next section goes on to cover the "),a("em",[a("RouterLink",{attrs:{to:"/en/spring-boot/cli.html#cli"}},[e._v("Spring Boot CLI")])],1),e._v(", or you can jump ahead to read about "),a("em",[a("RouterLink",{attrs:{to:"/en/spring-boot/build-tool-plugins.html#build-tool-plugins"}},[e._v("build tool plugins")])],1),e._v(".")])])}),[],!1,null,null,null);t.default=n.exports}}]);