Deploying spring boot application on Heroku cloud platform

Paweł Ćwik

Heroku is a cloud-based application platform and in this two-part article I will show you how, step by step, deploy Spring Boot application there.

Intro

First let’s take a deeper look into Heroku and how does it work. It advertises itself as “cloud platform based on a managed container system, with integrated data services and a powerful ecosystem, for deploying and running modern applications”. What does it mean in practical terms?

First of all Heroku works for applications written in Ruby, Node.js, Java, Python, Clojure, Scala, Go and PHP. So it’s quite versatile.  The source code for the application and the dependency file (like maven’s pom.xml for java-based applications) usually provides enough information for the Heroku platform to build and deploy the application. If we use some established frameworks we hardly have to make any changes in the code base to accommodate Heroku. It is able to simply figure out what part of our application is runnable. For example, in Ruby on Rails, it’s typically rails server and in Node.js it’s the main field in package.json. For other applications you need to explicitly declare what can be executed. It is done in a text file that accompanies your source code – a Procfile. In our case Heroku will ‘simply know’ how to deal with spring boot application so we will not dive into Procfiles.

We have our sources … now how to deliver it on Heroku? We simply use git. When you create an application on Heroku, it associates a new Git remote, typically named heroku, with the local Git repository for your application. All we have to do to deploy new code is just git push to heroku remote. Of course there are other ways to deploy our app – integration with Dropbox and GitHub is there for you to use as well as Heroku API.

After Heroku would get its’ hands on our code then, in case of Java application, it fetches binary library dependencies using Maven, compile the sources and creates an executable JAR file. All this as well as the language runtime and framework are bundled together into a thing called in Heroku nomenclature “slug”.

Then slug is taken and loaded into dyno – in Heroku’s dictionary it is an isolated, virtualized Unix container, that provides the environment required to run out an application. Then based on procfile or heroku know-how the command is run to start the application. We have control over how many dynos are running at any given time.

This should cover basics and now let us get some hands-on experience with Heroku.

Creating sample project

First of all we need something to deploy. Let’s create a skeleton for our spring-boot application using good, old spring initializr – spring project generator available at http://start.spring.io. We would need only two dependencies at the moment – web and thymeleaf.

Spring Initializr

Generate project, extract and open it in your IDE of choice. We already have main class generated for us – in this case it would be called HerokudemoApplication annotated as spring boot application.

@SpringBootApplication
public class HerokudemoApplication {

   public static void main(String[] args) {
      SpringApplication.run(HerokudemoApplication.class, args);
   }
}

Now create basic template called ‘hello’ in /src/resources/templates/hello.html 

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
  <head>
    <title>Clockwork Java Hello</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  </head>
  <body>
    <p>Hi there!</p>
  </body>
</html>

The last thing at this stage we need is a controller to serve hello template, it can be located in the same package as our ‘main’ class or any subpackage.

@Controller
public class HelloController {

    @RequestMapping("hi")
    public String hello() {
        return "hello";
    }
}

Don’t forget @Controller annotation – without it spring will not be able to find and initialize this class as a controller bean.

As I mentioned few paragraphs earlier Heroku will “just know” how to run spring-based applications and we do not need procfile. What we do need is to introduce this project into git version control.  So initialize git repository in the project directory (either using ‘git init’ command or IDE support), add the files and create an initial commit.

Done? Good, now we have everything that we need to deploy our application.

Deploying application on heroku

We are going to use HEROKU command line interface for deployment – download it from https://devcenter.heroku.com/articles/heroku-cli

First we have to log in into heroku, so if you did not register on heroku.com yet – do it now. After you are done, in terminal enter command:

heroku login

Use same credentials you have used during registration.

After successful login now we have to create new application on heroku platform – use

heroku create

If everything would go successfully (why wouldn’t it?) we should get back message like

Creating app… done, radiant-island-50498
https://radiant-island-50498.herokuapp.com/ | https://git.heroku.com/radiant-island-50498.git

radiant-island-50498 is the name of our application, created randomly by the Heroku. We can pass our custom name to heroku create command, but it has to be unique across whole herokuapp.com domain. For testing purposes its easer just to go with whatever platform will assign to us.

https://radinat-island-50498.herokuapp.com is (as you probably guessed) URL of our application

heroku app

But there’s nothing to see there at the moment, move along.

https://git.heroku.com/radiant-island-50498.git is URL of our remote, heroku-hosted git repository form which our slug will be created.

If we check our git remotes (git remote command) then you will notice that heroku create also added this address as remote repository called ‘heroku’. So now only one thing left to do

git push heroku master

And then a whole lot of logs would go through our screen – heroku is detecting sources as java and spring framework, will download dependencies, validate and build sources, reading our procfile (if any) and proceeds to launch the application.

Now when we visit https://radiant-island-50498.herokuapp.com/hi we will see hearty “Hi there!” in our browser.

And this is where we end first part. Go have some fun with your newly found heroku-power. In the second installment of this two-part article I will show you how to add PostgreSQL database support, how to take a peek into logs and metrics.

Poznaj mageek of j‑labs i daj się zadziwić, jak może wyglądać praca z j‑People!

Skontaktuj się z nami