fbpx

How to Deploy Spring Boot App On Heroku

In this article, you are going to see How to deploy Spring Boot app on Heroku with step by step guide. There are different ways to Deploy Spring boot app on Heroku and we are going to see a couple of convenient ways.

Introduction 

Most of the time while we develop spring based applications , we have to deploy and host in live running servers . But most of the time java based hosting are costlier and not meant for development . To solve this bottleneck most of the Cloud service providers provide a free tier to host applications , Heroku is one of them . 

Heroku is a PAAS cloud service provider which provides support for multiple programming languages to host your applications for free.

One of the biggest advantage of  deploying applications on Heroku cloud is it takes care of all the hassle of complex deployment process , infrastructure and scaling up your apps with multiple layers of security inbuilt .

 

Create a Simple Spring Boot App for Heroku

Let’s go ahead and create a simple spring boot application using spring initializr. Please follow the steps to create a simple spring boot app.

  1. Enter Group name as com.frugalis
  2. Enter artifact id as SpringHeroku.
  3. Click on Generate and import in your favorite IDE.

Now let’s go ahead and write a simple controller as below, we are now ready with a simple app to deploy spring boot app on Heroku. Please have a look at one of our blog post to Create Rest Services Using Spring Boot.




Deployment On Heroku Using Heroku-CLI and Git

First of all, you need to login to Heroku account and navigate to Heroku dashboard . In case you don’t have one, you can create it from Heroku’s Signup Page.We are going to use Heroku CLI and Git. Let’s follow the steps now to deploy.

1.1 Download And Install Heroku CLI:-

Heroku CLI is a command line tool that helps to manage Heroku apps using the terminal. Please Download Heroku CLI and follow the instructions to install it on your computer.

 1.2 Create Heroku App:-

Navigate to your Heroku dashboard by using the Dashboard link. Click on New button on right most corner and click on Create New App button.

Deploy Spring Boot App on heroku

You will get something like this below,

Deploy Spring Boot App On Heroku

Please enter the app name and click on create an app. Please note down the app name you entered. It will take you to Heroku app and the app page looks like below.

Spring Boot Heroku Deploy

1.3 Login to Heroku CLI:-

We are going to login to Heroku using Heroku CLI .If you haven’t already, log in to your Heroku account. Please open command prompt/terminal and enter the following command.

$ heroku login

You will be prompted to enter your Heroku account’s credentials either a page asking for Heroku Credentials will open or a prompt will open up asking for Id and password like below.

Enter your Heroku credentials:
Email: xyz@abcd.com
Password: *********
Logged in as xyz@abcd.com

1.4 Deploy Spring Boot App On Heroku using Heroku Git:-

Please navigate to your Spring Boot project and execute the following list of commands sequentially.

Deploy Spring Boot On Heroku

We have now deployed our app on Heroku. Here frugalisapp  is the app name. You can get these commands in  app page under deploy section. You can open deployed app using Herok by executing the command .

$ heroku open

Deploy Spring Boot App Using Jar File:-

Before going on to the details, Let’s look at few of the important plugins and install those . We are going to deal with two plugins Heroku Repo and Heroku CLI Deploy respectively.

Let’s clean the cache and reset the app using Heroku repo.

$ heroku plugins:install heroku-repo
$ heroku repo:gc -a appname
$ heroku repo:reset -a appname

Now let’s deploy our Spring Boot project using Heroku CLI Deploy plugin.

Open application.properties from src/main/java/resources/application.properties.

Please add following line below.

server.port=${PORT:8080}

Navigate to Spring project folder and run.

$mvn clean package

Now run deploy command from your project directory.

$ heroku deploy:jar target/SpringHeroku-0.0.1-SNAPSHOT.jar --app <APP-NAME>

Deploy Spring Boot App On Heroku Using Procfile

We are going to add a Procfile in our spring boot app like below . One of the biggest advantage of using a procfile in heroku is that it lets you customize and override the default configurations that we do in our application.properties . 

Heroku Procfile lets you run your own custom commands , lets say  you have a multi module spring boot application in that case you can specify the module from where your  spring boot jar should run and specify the command in Procfile .

web: java -Dserver.port=$PORT $JAVA_OPTS -jar target/*.jar

Now execute the deploy plugin again as below:-

$ heroku deploy:jar target/SpringHeroku-0.0.1-SNAPSHOT.jar --app <APP-NAME>

How to Verify Logs of Deployed Spring boot application In Heroku 

Once you have deployed your application , now we should be verifying if my application has started successfully or not . In that case you can use heroku CLI to view logs 

heroku logs --tail

This command is similar to what we use in Linux as ” tail -f ” . You can also verify application build log like shown below .

heroku build logs

Drop us an Email With Subject “Help On Spring Boot heroku ” if you want our team to help you on deploying any Spring boot App on heroku !! Free !!!!!! 

Have a Look at Complete Video with Step by Step Guide 

Download Code

Related Links:-

CRUD Rest API With Spring Web-Flux Reactive Programming with Spring 5

Creating REST Service with Spring Boot

CRUD Rest Service With Spring Boot and JPA

Spring Boot Profiling – Configure for Different Environments




heroku logs --tail

1 thought on “How to Deploy Spring Boot App On Heroku”

Comments are closed.