DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Keep Your Application Secrets Secret
  • Auto-Scaling a Spring Boot Native App With Nomad
  • Android Cloud Apps with Azure
  • Delivering Your Code to the Cloud With JFrog Artifactory and GitHub Actions

Trending

  • Start Coding With Google Cloud Workstations
  • The Cypress Edge: Next-Level Testing Strategies for React Developers
  • Ethical AI in Agile
  • A Deep Dive Into Firmware Over the Air for IoT Devices
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Tutorial: Deploy a Spring Boot Application to the Cloud

Tutorial: Deploy a Spring Boot Application to the Cloud

Spring Boot makes it easy to create stand-alone applications that are ready to use and run. Learn how to deploy to the cloud in this short post.

By 
Thiago Negri user avatar
Thiago Negri
·
Jan. 16, 20 · Tutorial
Likes (11)
Comment
Save
Tweet
Share
30.4K Views

Join the DZone community and get the full member experience.

Join For Free

Spring Boot makes it easy to create stand-alone applications that are ready to use and run. It’s also a great alternative if you are looking to accelerate the development cycle of your app. There are many ways to deploy your Spring Boot app.  In this tutorial, I’ll show you three different ways: 

  1. Azure
  2. Amazon Web Services
  3. Self-hosted

We will use a simple application to demonstrate deployment for each of the options.

Build Your Spring Boot Sample App

I’ve created a very simple Spring Boot application you can download from GitHub. The app consists of a single controller and a small form to submit. So, clone the repository, build, and run it:

Shell
 




x


 
1
$ git clone https://github.com/oktadeveloper/okta-spring-boot-deploy.git
2
$ cd okta-spring-boot-deploy
3
$ ./mvnw spring-boot:run



A small functional form will be available at http://localhost:8080/greeting.

Before we continue to deployment options, let’s make sure our app is secure.

Secure Your Spring Boot App With Okta

Okta has a specific library for Spring Boot to make it easy to add OAuth 2.0 authentication to your app.

First, register for a forever-free developer account today! When you’re finished, complete the steps below to create an app.

  1. Log in to your developer account at developer.okta.com
  2. Navigate to Applications and click on Add Application
  3. Select Web and click Next
  4. Give the application a name (e.g. Spring Boot Awesome App)
  5. Add the following as Login redirect URI:
    • http://localhost:8080/login/oauth2/code/okta
  6. Click Done

First, include Okta as a dependency in the pom.xml:

XML
 




x


 
1
<dependency>
2
    <groupId>org.springframework.boot</groupId>
3
    <artifactId>spring-boot-starter-security</artifactId>
4
</dependency>
5
<dependency>
6
    <groupId>com.okta.spring</groupId>
7
    <artifactId>okta-spring-boot-starter</artifactId>
8
    <version>1.3.0</version>
9
</dependency>



Now, create the file src/main/resources/application.properties with your app settings:

Properties files
 




xxxxxxxxxx
1


 
1
okta.oauth2.issuer=<your_org_url>/oauth2/default
2
okta.oauth2.clientId=<app_client_id>
3
okta.oauth2.clientSecret=<app_client_secret>



You can find your Client ID and Client Secret in the Okta dashboard, on the General tab of the app you just created. Your Org URL is displayed at the top left of the Dashboard.

And we are done! Simple as that, your app is now secured with OAuth 2.0.

Deploy Your Spring Boot App to Azure

Microsoft has been invested in Java lately; a partnership with Azul, and acquisition of jClarity, naturally deploying to Azure is not hard.

Make sure you have an active account by registering on Azure’s website. The first step is to install the Azure CLI on your computer. We will use it to log in to Azure from a command-line terminal.

On the project, add the Azure plugin as part of the build on the pom.xml file. Try the example configuration below, and make sure you replace the appName with a unique name for your app. Also configure the region, to define where geographically your app will be deployed. The list of available regions is available in Azure Region Availability. Finally, the pricingTier determines what type of instances you will use and how much that will cost. The list of available pricing tiers is available in Azure App Service Pricing. The configured pricing tier on the example below is “F1”, which stands for their free tier, so don’t worry if you just want to try Azure out; you won’t get billed if you deploy to F1 tier.

XML
 




xxxxxxxxxx
1
28


 
1
<plugin>
2
    <groupId>com.microsoft.azure</groupId>
3
    <artifactId>azure-webapp-maven-plugin</artifactId>
4
    <version>1.7.0</version>
5
    <configuration>
6
     <schemaVersion>v2</schemaVersion>
7
     <resourceGroup>maven-plugin</resourceGroup>
8
     <appName>Spring-App</appName>
9
     <region>East US</region>
10
     <runtime>
11
         <os>linux</os>
12
         <javaVersion>jre8</javaVersion>
13
         <webContainer></webContainer>
14
     </runtime>
15
     <deployment>
16
         <resources>
17
             <resource>
18
                 <directory>${project.basedir}/target</directory>
19
                 <includes>
20
                     <include>*.jar</include>
21
                 </includes>
22
             </resource>
23
         </resources>
24
     </deployment>
25
     <pricingTier>F1</pricingTier>
26
    </configuration>
27
</plugin>
28

          



Since Azure handles HTTPS for us and we will run behind their proxy, we also need to update our Spring settings to pick up the standard HTTP headers and correctly build redirect URLs. Just add this line to application.properties file:

Properties files
 




xxxxxxxxxx
1


 
1
server.forward-headers-strategy=FRAMEWORK



After you have configured the pom.xml and the application.properties file, the deployment consists of opening a command line terminal, logging into Azure, and running the Maven goal to do the deployment. To login into Azure, use the command az login. This will open your web browser for the login process, and then print your account information to the console. Next, run mvn package to make sure your app is built. After logging in, use the command mvn azure-webapp:deploy to deploy your app to the Azure cloud. So, the entire deployment process looks like this:

Shell
 




xxxxxxxxxx
1


 
1
$ az login
2
$ mvn package
3
$ mvn azure-webapp:deploy



After that, your app will be deployed to Azure.

Now, add your app login endpoint to the app you created in Okta, so you can log in to it. To do that, go to your org panel, click Applications on the top bar, select the app you created, click General tab, click Edit of General Settings, and the following URI to “Login redirect URIs” as follows (your app name is what you defined in pom.xml file for the tag appName):

Plain Text
 




xxxxxxxxxx
1


 
1
https://<your_app_name>.azurewebsites.net/login/oauth2/code/okta



Head over to https://<your_app_name>.azurewebsites.net/greeting to see your app in action, deployed to Azure’s cloud.

Deploy Your Spring Boot App to Amazon Web Services



Amazon AWS is still the de-facto choice for public clouds with about half the market share. Java applications can be deployed to AWS Elastic Beanstalk with a few clicks of a button.

Since AWS will handle the app traffic for us and we will run behind their proxy, we need to update Spring to accept the standard HTTP headers and correctly build redirect URLs. Just add this line to application.properties file:

Properties files
 




xxxxxxxxxx
1


 
1
server.forward-headers-strategy=FRAMEWORK



After configuring the application.properties file, make sure your app is built by doing an mvn package. We will also need the generated JAR file. To deploy to Amazon Web Services, we will go to AWS Management Console and follow the deployment guide step-by-step:

  1. Search for and click on Elastic Beanstalk.
  2. This will take you to a step-by-step wizard to deploy the app.
  3. Click Get Started.
  4. Enter your application name.
  5. Select Java as the platform.
  6. Select Upload your code.
  7. Click Upload.
  8. Select Local file.
  9. Browse and select your app’s JAR file, located at <your_app_path>/target/demo-0.0.1-SNAPSHOT.jar
  10. You may leave the Version label as it is, but you will need to use unique labels every time you deploy a newer version of your app.
  11. Click Upload.
  12. Click Configure more options
  13. Under Software Click Modify
  14. Under Environment properties, create a new property with Name SERVER_PORT and Value 5000.
  15. Click Save.
  16. Click Create app.

You will land on a page that shows the progress of creating the app. Wait until it finishes. When it’s complete, the URL of your app should show at the top bar of the screen. Now add the login endpoint to your Okta app (same as we did for Azure, detailed above):

Plain Text
 




xxxxxxxxxx
1


 
1
http://<your_app_name>-env.<app_id>.<region>.elasticbeanstalk.com/login/oauth2/code/okta



Browse to your app at http://<your_app_name>-env.<app_id>.<region>.elasticbeanstalk.com/greeting. There you have your app deployed to Amazon Web Services.

AWS does not provide a default certificate, so your app is not running on HTTPS. You should definitely move it to HTTPS before going into production. This is covered in their documentation on Configuring HTTPS for Your Elastic Beanstalk Environment.

NOTE: Once you get this working, you can automate it with the Beanstalk Maven Plugin.

Self-Host Your Spring Boot App

If you decide to host the app yourself, first, you should decide whether you want to run your app by itself as a JAR or deploy it in an application server like Tomcat as a WAR. I would recommend a JAR, so the versions of your application and the application server are managed together, which simplifies your deployment.

To run as a standalone JAR, simply build your app via mvn package to generate a JAR file in the target folder. Then, you can run it with java -jar <your_app_name>-<your_app_version>.jar.

If you want to use an existing Tomcat installation or prefer to use an application server, your build process should generate a WAR instead of a JAR and also hook into the application server. For that, you should remove the spring-boot-maven-plugin from your pom.xml file. Then, add the following line to it:

XML
 




xxxxxxxxxx
1


 
1
<packaging>war</packaging>



Make sure Tomcat is not embedded into your application, as you are going to run it within an external Tomcat. Add these lines to the dependencies section of your pom.xml file:

XML
 




xxxxxxxxxx
1


 
1
<dependency>
2
    <groupId>org.springframework.boot</groupId>
3
    <artifactId>spring-boot-starter-tomcat</artifactId>
4
    <scope>provided</scope>
5
</dependency>



Now, change the DemoApplication class to make it register the application in the server, make it extend SpringBootServletInitializer and override the configure method as follows:

Java
 




xxxxxxxxxx
1
19


 
1
package com.oktadeveloper.demo;
2

          
3
import org.springframework.boot.SpringApplication;
4
import org.springframework.boot.autoconfigure.SpringBootApplication;
5
import org.springframework.boot.builder.SpringApplicationBuilder;
6
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
7

          
8
@SpringBootApplication
9
public class DemoApplication extends SpringBootServletInitializer {
10

          
11
    public static void main(String[] args) {
12
        SpringApplication.run(DemoApplication.class, args);
13
    }
14

          
15
    @Override
16
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
17
        return builder.sources(DemoApplication.class);
18
    }
19
}



Now if you run mvn clean package you should get a WAR file in the target folder. Copy that WAR file into your Tomcat installation webapps/ROOT.war and remove the existing webapps/ROOT directory. Tomcat will pick it up automatically and deploy it. After deployment completes, your app should be available at an address like http://localhost:8080/greeting.

There are some new challenges when self-hosting. For example, if you want to run your app on multiple servers, you need to make sure each server is running the same version of the app, and you will want another server to handle the load balancing. You may use NGINX as a load balancer, just make sure to always update its configuration as you scale your servers. One way to make sure all of your servers are set up the same way is to use some automation solutions like Chef. Self-hosting means you have to do everything by yourself, like TLS certificate management, to make sure your servers listen on HTTPS. Azure and Amazon have specific offerings to manage the certificate for you.

Learn More About Spring Deployment

We covered Azure, AWS, and self-hosted Tomcat deployment on this article. You can also run your app as a Docker container, which is covered in Get Jibby With Java, Docker, and Spring Boot.

If you want to learn more about Java, Spring, and User Auth using Okta, check out these awesome articles:

  • Simple Authentication with Spring Security
  • Which Java SDK Should You Use?
  • Build a Web App with Spring Boot and Spring Security in 15 Minutes
  • Learning Java as a First Language

Questions? Requests for a future post? Drop them in the comments! And don’t forget to follow @oktadev on Twitter and subscribe on Youtube.

Deploy Your Spring Boot App the Right Way was originally published on the Okta Developer Blog on December 3, 2019. 

Spring Framework Spring Boot application app Amazon Web Services azure Cloud Spring Security

Published at DZone with permission of Thiago Negri. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Keep Your Application Secrets Secret
  • Auto-Scaling a Spring Boot Native App With Nomad
  • Android Cloud Apps with Azure
  • Delivering Your Code to the Cloud With JFrog Artifactory and GitHub Actions

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!