Developers today are spoiled for choice when it comes to platforms on which to deploy their applications. The cloud has become the de facto standard for new application deployments, with the three major providers — Amazon Web Services (AWS), Microsoft Azure, and Google Cloud Platform (GCP) — all offering robust services.
In this article, we'll focus on deploying Spring Boot applications to the cloud. Spring Boot is a popular Java-based framework that makes it easy to create stand-alone, production-grade Spring-based applications that you can "just run."
There are multiple ways to deploy a Spring Boot application to the cloud. We'll cover two of the most popular approaches:
A platform as a service (PaaS) is a cloud computing model in which a third-party provider delivers platform and infrastructure tools, usually in the form of a platform-as-a-service stack, to users over the internet.
PaaS provides a complete development and deployment environment in the cloud, allowing developers to focus on writing code, rather than worrying about the underlying infrastructure.
One popular PaaS provider is Heroku, which offers a platform that is optimized for hosting Spring Boot applications.
Heroku's appeal lies in its simplicity. To deploy a Spring Boot application to Heroku, you simply need to create a Procfile
in the root of your project that contains the following line:
web: java -jar target/<your-application-name>.jar
You also need to add the following dependencies to your pom.xml
file:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
Once you've done that, you can deploy your application to Heroku using the heroku
command-line tool. First, you need to create a new Heroku application:
heroku create
Then, you can deploy your application to Heroku:
git push heroku master
Your application will now be up and running at https://<your-app-name>.herokuapp.com
.
Cloud Foundry is another popular PaaS provider that is worth considering for deploying Spring Boot applications.
Like Heroku, Cloud Foundry makes it easy to deploy Spring Boot applications. To deploy a Spring Boot application to Cloud Foundry, you first need to create a manifest.yml
file in the root of your project. This file contains information about your application, such as the name, memory limit, and hostname.
Here's an example manifest.yml
file:
---
applications:
- name: my-app
memory: 512M
disk_quota: 1024M
instances: 1
path: build/libs/my-app.jar
env:
JAVA_OPTS: -Djava.security.egd=file:///dev/urandom
buildpack: https://github.com/cloudfoundry/java-buildpack
You can then deploy your application to Cloud Foundry using the cf
command-line tool:
cf push
Your application will now be up and running at https://<your-app-name>.cfapps.io
.
An infrastructure as a service (IaaS) is a cloud computing model in which a third-party provider delivers infrastructure — typically, servers, storage, and networking — to users over the internet.
IaaS provides developers with much more control over the underlying infrastructure than PaaS, but also requires more work to set up and maintain.
Amazon Web Services (AWS) is the most popular IaaS provider, and offers a wide range of services that can be used to host Spring Boot applications. In this section, we'll focus on using Elastic Beanstalk to deploy a Spring Boot application to AWS.
Elastic Beanstalk is a service that makes it easy to deploy, manage, and scale web applications and services developed with Java, .NET, PHP, Node.js, Python, and Ruby on familiar servers such as Apache, Nginx, Passenger, and IIS.
To deploy a Spring Boot application to Elastic Beanstalk, you first need to create an application.properties
file in the src/main/resources
directory of your project. This file contains information about your application, such as the name, version, and environment.
Here's an example application.properties
file:
name=my-app
version=1.0.0
environment=production
You also need to create an AWS Elastic Beanstalk
file in the root of your project. This file contains information about your application's environment, such as the region, instance type, and security groups.
Here's an example AWS Elastic Beanstalk
file:
---
AWSEBDebuggingMode: true
AWSEBEnvironmentVariables:
JAVA_OPTS: -Djava.security.egd=file:///dev/urandom
AWSEBServiceRole: null
AWSEBUseAmi64: true
AWSEluaticLoadBalancerScheme: internet-facing
AWSEBSecurityGroups:
- my-security-group
AWSEBSubnets:
- subnet-12345678
AWSEBIAMInstanceProfile: null
AWSEBRDSDatabaseInstance: null
AWSEBRDSDatabasePassword: null
AWSEBRDSDatabaseUsername: null
AWSEBRDSDatabaseEngine: null
AWSEBRDSDatabaseName: null
AWSEBRDSSnapshotIdentifier: null
AWSEBRDSAllocatedStorage: null
AWSEBRDSStorageType: null
AWSEBRDSMultiAZ: null
AWSEBRDSIops: null
AWSEBRDSEncrypted: null
AWSEBRDSKmsKeyId: null
AWSEBRDSDeletionPolicy: null
AWSEBS3Key: my-s3-key
AWSEBS3Bucket: my-s3-bucket
AWSEBS3ObjectKey: my-s3-object-key
AWSEBS3ETag: my-s3-etag
AWSEBS3VersionId: my-s3-version-id
AWSEBS3Region: us-east-1
AWSEBS3StorageClass: STANDARD
AWSEBNotificationEndpoint: null
AWSEBNotificationProtocol: null
AWSEBNotificationTopicARN: null
AWSEBManagedActionHistoryRetentionPeriod: 14
Once you've created these files, you can deploy your application to Elastic Beanstalk using the eb
command-line tool. First, you need to create a new Elastic Beanstalk application:
eb create
Then, you can deploy your application to Elastic Beanstalk:
eb deploy
Your application will now be up and running at http://<your-app-name>.elasticbeanstalk.com
.
Microsoft Azure is another popular IaaS provider that offers a number of services that can be used to host Spring Boot applications. In this section, we'll focus on using App Service to deploy a Spring Boot application to Azure.
App Service is a Microsoft Azure PaaS service that is optimized for hosting web applications and web services. To deploy a Spring Boot application to App Service, you first need to create an application.properties
file in the src/main/resources
directory of your project. This file contains information about your application, such as the name, version, and environment.
Here's an example application.properties
file:
name=my-app
version=1.0.0
environment=production
You also need to create a web.xml
file in the src/main/webapp/WEB-INF
directory of your project. This file contains information about your application's servlet, such as the servlet class and mapping.
Here's an example web.xml
file:
<web-app>
<servlet>
<servlet-name>my-app</servlet-name>
<servlet-class>org.springframework.boot.web.servlet.support.SpringBootServletInitializer</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>my-app</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Once you've created these files, you can deploy your application to App Service using the az
command-line tool. First, you need to create a new App Service application:
az appservice plan create --name my-app-service-plan --resource-group my-resource-group --sku S1
Then, you can deploy your application to App Service:
az webapp create --resource-group my-resource-group --plan my-app-service-plan --name my-app --deployment-container-image="mcr.microsoft.com/java/jdk:8u192-zulu-alpine"
Your application will now be up and running at http://my-app.azurewebsites.net
.
In this article, we've looked at two different ways to deploy Spring Boot applications to the cloud:
Each approach has its own advantages and disadvantages, so it's important to choose the right one for your application.
If you're looking for a quick and easy way to get your application up and running in the cloud, then PaaS is the way to go. Heroku and Cloud Foundry are two popular PaaS providers that are worth considering.
If you're looking for more control over the underlying infrastructure, then IaaS is the way to go. AWS and Azure are two popular IaaS providers that offer a wide range of services that can be used to host Spring Boot applications.