Deploying a Spring Boot Application to Heroku

This post is the second part of a series of how to deploy a Spring Boot application to the cloud. We'll deploy to Heroku today.

The Sample App

As the sample application, we are going to use the comment-store app from my first Spring Boot book.

It is a simple microservice for handling CRUD operations for storing comments. The comments are stored in a SQL DB and the CRUD operations are partly offered in an REST-like API.

We'll use Postgres for the tutorial.

Deploy to Heroku

First, we'll deploy using the git repository push way and second, using the Heroku Maven Plugin.

Step 1

Install the Heroku CLI. You can download it here

Step 2

Create an account on Heroku.

Step 3

Open a terminal window and change into the directory of the sample application. For the deployment, the application must be inside a git repository.

And then we can log in the first time

heroku login

Step 4

Now, we can create our commentstore application and link it:

heroku create <app-name>

This will create a git repository on Heroku for the commentstore application and link it as a remote in our repository under the name heroku. The app name is optional and if you do not specify one, Heroku generates one.

Step 5

Next, we must define for Heroku what type of application we have and how to start it.

Create a file named Procfile in the root of the git repository.

web: java -Dserver.port=$PORT -jar comment-store/target/sbb-comment-app.jar

It defines a web dyno running the java command and we specify the jar file to run and set the port used by Heroku.

In simple cases, i.e. we only have one maven project in the git repository, Heroku automatically determines what to run.

Step 6

Create the Postgres service:

heroku addons:create heroku-postgresql:hobby-dev

It creates a Postgres service with the free hobby-dev tier.

Step 7

Next, we need to declare in the application.properties which database to use:

spring.datasource.url=${JDBC_DATABASE_URL}

The environment variable JDBC_DATABASE_URL is provided by Heroku and contains the full JDBC string for connecting to the database.

Step 8a - Git Deploy

Now we are ready for deploying our application for the first time.

Your changes have to be commited.

git push heroku master

This will push our master branch to Heroku. Heroku will now build our application, creating some artifacts for further rollout and finally deploys it to their infrastructure.

Step 8b - Deploy using the Heroku Maven Plugin

The git deploy variant forces you to push your source code to Heroku. It's not the best way for everyone. As an alternative, Heroku provides a way for binary deployments and offers a Maven plugin for the Java world .

Add the plugin to the plugins section in your pom.

<plugin>
    <groupId>com.heroku.sdk</groupId>
    <artifactId>heroku-maven-plugin</artifactId>
    <version>1.1.3</version>
    <configuration>

        <processTypes>
            <web>java $JAVA_OPTS -Dserver.port=$PORT -cp
                target/classes:target/dependency/*
                de.codeboje.springbootbook.commentstore.CommentStoreApp</web>
        </processTypes>
    </configuration>
</plugin>

Next, we need to configure the type of the application and what to run using the processTypes parameter. The difference to our Procfile version is that we do not specify the final jar file here, but rather add two directories. The first is target/classes which contains our compiled application classes (regular Maven folder) and second, target/dependency/ which contains all dependencies of our application and is created by the Heroku plugin.

The Heroku plugin will use the app name in the git repository. If your project is not in a git repo, you must specify the application name in the configuration above like:

<configuration>
    <appName>commentstore</appName>
</configuration>

However, we use git, so let's just deploy using Maven:

mvn clean heroku:deploy

This builds the application on your machine using Maven and then deploy the binary artifacts to Heroku.

Make a Request

When done you could access your application.

heroku open

This command opens your application in the browser. Call any of the actuators like application/health and you should get a valid response.

Use Postman and the collection and adjust the requests to the endpoint of your application.

Remove the App again

When you are done, you can remove the app again by running:

heroku apps:destroy <app-name>

You don't want to use free dyno time for a tutorial app, don't you?

Conclusion

It's easy to deploy your Spring Boot application to Heroku and your application is ready in a few minutes.