Last Update: 27.06.2017. By Jens in Spring Boot
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.
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.
First, we’ll deploy using the git repository push way and second, using the Heroku Maven Plugin.
Install the Heroku CLI. You can download it here
Create an account on Heroku.
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
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.
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.
Create the Postgres service:
heroku addons:create heroku-postgresql:hobby-dev
It creates a Postgres service with the free hobby-dev tier.
Next, we need to declare in the application.properties which database to use:
The environment variable JDBC_DATABASE_URL is provided by Heroku and contains the full JDBC string for connecting to the database.
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.
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.
When done you could access your application.
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.
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?
It’s easy to deploy your Spring Boot application to Heroku and your application is ready in a few minutes.
Want content like this in your inbox each workday? No BS, spam or tricks... just useful content: