You are here
Home > Heroku >

How To Deploy Spring Boot Application To Heroku

How To Deploy Spring Boot Application To HerokuThe development of a software application goes through multiple stages like: requirement analysis, designing, coding, testing. Once testing is completed successfully, we need to deploy the application somewhere in a server so that users can access it from anywhere. Generally, we deploy the application in the Cloud environment. Heroku is the cloud environment that provides us deployable infrastructure. So, we are going to discuss ‘How To Deploy Spring Boot Application to Heroku’? in this article.

Spring Boot’s flexible packaging options provide a great deal of choice when it comes to deploying your application. We can deploy Spring Boot applications to a variety of cloud platforms, to virtual/real machines, or make them fully executable for Unix systems. Here in this article we will discuss about the process of deployment in the Heroku cloud platform. Let’s discuss our topic ‘How To Deploy Spring Boot Application to Heroku’ and its related concepts.

Where can we deploy a Spring Boot Application?

We have a variety of options to deploy our Spring Boot Application. Here are the most commonly used platforms:

1) Kubernetes

2) Cloud Foundry

3) Heroku

4) OpenShift

5) Azure

6) Amazon Web Services (AWS)

7) CloudCaptain and Amazon Web Services

8) Google Cloud

What is Heroku?

Heroku is a platform as a service (PaaS) that offers developers to build, run, and operate applications entirely in the cloud. Typically, Heroku is useful in deploying, managing, and scaling the application in the Cloud. It supports several programming languages and also has a very straightforward and a suitable deployment model.

Heroku offers both free and paid services to accommodate your application in the cloud. If you run your production application on Heroku, you should use a production-grade Heroku paid services.

What is Platform-as-a-Service (PaaS)?

Platform-as-a-Service (PaaS) is a category of Service-oriented architecture. The most noteworthy categories of Service-oriented architecture are Infrastructure-as-a-Service (IaaS), Platform-as-a-Service (PaaS) and Software-as-a-Service (SaaS).

PaaS architecture typically includes operating systems, programming-language execution environments, databases, libraries, web servers, and connectivity to some platforms.

Users of PaaS services are able to deploy client-created or acquired applications onto the cloud infrastructure. Clients using PaaS have control over the deployed applications, but have limited access to configuration settings for the application-hosting environment. They also have limited access to the configuration settings of resources provided by the PaaS.

Some of the examples of PaaS offerings are Google App Engine, Apache Stratos, and the AWS Elastic Beanstalk. Each service simplifies developers’ work as they develop, test, and deploy their application with ease.

Prerequisites To Work With Heroku

Below is the list of prerequisites to deploy your Spring Boot Application to Heroku.

1) A Heroku Account

2) Heroku CLI

3) Git

What are the steps to deploy Spring Boot Application to Heroku?

1) Register yourself in Heroku : Link to Resister in Heroku

2) Download Heroku CLI installer as per your Operating System and Machine Configuration: Link to download Heroku CLI  Installer

3) Double Click on the downloaded Heroku CLI Installer and follow the instructions to install it in your System.

4) Verify the successful installation of Heroku CLI: Open command prompt,  just type ‘heroku’ as shown below and press enter. If you see some details of Heroku without any exception, it is successfully installed.

C:\Users\XYZ> heroku

5) Develop a sample Spring Boot Application if not done earlier

6) Deploy your Spring Boot Application in Heroku

How to develop a Sample Spring Boot Application?

If not done before, let’s develop a sample Spring Boot Application in order to deploy it to Heroku Cloud. Since our primary focus of this article is to understand the process of deployment to Heroku Cloud, we will create a simple web application only. Let’s follow step by step process as shown below:

Step#1 : Create a Spring Boot Project using STS(Spring Tool Suite)

Here, we will use STS(Spring Tool Suite) to create our Spring Boot Project. If you are new to Spring Boot, visit Internal Link on How to create a sample project in spring boot using STS. While creating a project in STS, add starters ‘Spring Web’, and ‘Thymeleaf’ in order to get the features of simple web application.

Step#2 : Create a Controller

We will just create a controller as ‘DisplayController.java’ and write a simple method to display a message on the browser as below:

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class DisplayController {

   @GetMapping("/")
   public String displayPage() {
      return "displayPage";
   }
}

Step#3 : Create a Page for View

Let’s create a simple page as ‘displayPage.html’ in order to display a message in the browser when we access the application as below. Please keep this page under folder ‘src/main/java/resources/template’.

<!DOCTYPE html>
<html>
  <head>
    <meta charset="ISO-8859-1">
    <title>Insert title here</title>
  </head>
  <body>
    <h2>Welcome To Spring Boot Heroku Deployment</h2>
  </body>
</html>

Step#3 : Test the Application

Open the browser, and hit the URL “http://localhost:8080” to test the application. It should display the message ‘Welcome To Spring Boot Heroku Deployment’.

How To deploy Spring Boot Application to Heroku?

If your application is ready and you have done the step#1 to step#4 from the section ‘What are the steps to deploy Spring Boot Application to Heroku?’, follow the below steps.

1) Open Command Prompt (By typing ‘cmd’)

2) Type ‘heroku login’ and press enter as below:

C:\Users\XYZ> heroku login

It will ask you to press any key to open up the browser to login. Press Any key or simply press enter.

C:\Users\XYZ> heroku login
Heroku: Press any key to open up the browser to login or q to exit:
Opening browser to https://cli-auth.heroku.com/auth/cli/browser/1d430096-f98f-408d-9f66-f1b170201ceb?requestor=SFMyNTY.g2gDbQAAAA4xMjIuMTYxLjUwLjE4OW4GAPidzomDAWIAAVGA.BPys56DqYVghjKnCXYhxWD3eSO3Z_bda92dQu7KeogM
heroku: Waiting for login... |

3) One you follow above steps, you will be redirected to a browser. It will ask you to ‘Login to Heroku CLI’. Press ‘Login’.

4) Once you press the login, browser will display a message ‘Logged In’.

5) Come back to command prompt. You will see below messages:

Logging In... done
'Logged In as <emailId>.

6) Switch over to your project location in Command prompt. For example:

D:\>cd D:\Workspace-Spring-Tool-Suite-4-4.6.2.RELEASE\SpringBootHerokuDeploy

7) At the above location, type below command and press enter.

> heroku create

It will create a link to access the application and also an internal git account at heroku. There will be a dummy link with dummy application name. In our case it is : morning-retreat-09421 as shown below:

Creating app... done, ⬢ morning-retreat-09421
https://morning-retreat-09421.herokuapp.com/ | https://git.heroku.com/morning-retreat-09421.git

8) If you want to rename the link type below command:

> heroku apps:rename --app morning-retreat-09421 springboot-app-deploy-heroku

Here ‘springboot-app-deploy-heroku’ is the new name and ‘morning-retreat-09421’ is the dummy name provided by heroku.

Renaming morning-retreat-09421 to springboot-app-deploy-heroku... done
https://springboot-app-deploy-heroku.herokuapp.com/ | https://git.heroku.com/springboot-app-deploy-heroku.git

9) Type git init command and press enter as below:

> git init

It will initialize a git repository at your project location as shown below:

D:\Workspace-Spring-Tool-Suite-4-4.6.2.RELEASE\SpringBootHerokuDeploy>git init
Initialized empty Git repository in D:/Workspace-Spring-Tool-Suite-4-4.6.2.RELEASE/SpringBootHerokuDeploy/.git/

10)  Type below command and press enter to add project files into git repository

> git add .

11) Type below command and press enter to commit all files into repository:

> git commit -m "First Commit"

On pressing enter, it will display below messages while committing the project files.

D:\Workspace-Spring-Tool-Suite-4-4.6.2.RELEASE\SpringBootHerokuDeploy>git commit -m "First Commit"
[master (root-commit) d9110c9] First Commit
11 files changed, 635 insertions(+)
create mode 100644 .gitignore
create mode 100644 .mvn/wrapper/maven-wrapper.jar
create mode 100644 .mvn/wrapper/maven-wrapper.properties
create mode 100644 mvnw
create mode 100644 mvnw.cmd
create mode 100644 pom.xml
create mode 100644 src/main/java/com/dev/springboot/SpringBootHerokuDeployApplication.java
create mode 100644 src/main/java/com/dev/springboot/controller/DisplayController.java
create mode 100644 src/main/resources/application.properties
create mode 100644 src/main/resources/templates/displayPage.html
create mode 100644 src/test/java/com/dev/springboot/SpringBootHerokuDeployApplicationTests.java

12) Set remote git to ‘springboot-app-deploy-heroku’ by typing below command:

> heroku git:remote -a springboot-app-deploy-heroku
D:\Workspace-Spring-Tool-Suite-4-4.6.2.RELEASE\SpringBootHerokuDeploy>heroku git:remote -a springboot-app-deploy-heroku
set git remote heroku to https://git.heroku.com/springboot-app-deploy-heroku.git

13) Push the changes by typing the following command and pressing enter:

> git push heroku master

How to test the deployed application?

Find the URL from the step#8 above, and hit in the browser. In our case it is : https://springboot-app-deploy-heroku.herokuapp.com/

You will see the expected results.

2 thoughts on “How To Deploy Spring Boot Application To Heroku

Leave a Reply


Top