Virendra Oswal
Virendra Oswal's Blog

Virendra Oswal's Blog

Host Spring Boot Application on Heroku

Host Spring Boot Application on Heroku

Publish Spring Boot Application on Heroku within 5 minutes.

Virendra Oswal's photo
Virendra Oswal
·Jul 6, 2021·

4 min read

Subscribe to my newsletter and never miss my upcoming articles

Play this article

In this blog post, we will see how we can use Heroku a Platform-as-a-Service (PaaS) to deploy the Spring Boot application which is an open-source Java framework that can be used to create PRODUCTION-ready micro-service with very minimal configuration.

Pre-requisites

  • Maven.
  • JDK 8+.
  • Heroku Account. (Sign up if not done, no credit card or anything required during sign-up)
  • Minimal Spring Boot application.
  • Heroku CLI. (Helps for better administration.)

We will be using Spring Initializr to create a minimal Spring boot application to get the application ready for deployment and in PRODUCTION within minutes. spring-initializr.JPG

Spring Boot Heroku Deployment

We will just add the REST service to our project so we can have an access point once deployed to Heroku. We will add the below code to our SpringBootHerokuApplication class to keep One FIle Strategy.

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

@RestController
class RandomResource {
    @GetMapping(path = "/greet")
    public String greet() {
        return String.format("%s says Hello from %s!", "Spring Boot", "Heroku");
    }
}

The above code is quite self-explanatory, we are exposing one endpoint named /greet which returns a static String.

We will just run locally once to see we are able to access it. Always better to check even if its small change. #developertip locaohost-ggreet.JPG Given it's working fine, we will prepare for Heroku deployment for the same.

Heroku Deployment

Login to Heroku, Create a new application in Heroku with the name of your choice. heroku-dashboard.JPG Fill in details, select the region as per your target audience. We will keep default and just click Create App Create-app.JPG

Procfile Creation for spring boot

Heroku apps include a Procfile that specifies the commands that are executed by the app on startup.

We will add a simple basic Procfile for our spring boot as below:

web: java -Dserver.port=$PORT -jar target/spring-boot-heroku-0.0.1-SNAPSHOT.jar

You can other variables if you want, this is basically required where PORT comes from Heroku Environment setup. Though for us we don't have to worry about the same, we get a kind of DNS-based URL from Heroku on deployment.

Example of Procfile with Spring profile activation

web: java -Dspring.profiles.active=prod -Dserver.port=$PORT -jar target/spring-boot-heroku-0.0.1-SNAPSHOT.jar

Above spring-boot-heroku-0.0.1-SNAPSHOT.jar is the name of the package created as part of the build process. You can override name using <build> --> <finalName> if using maven for meaningful name, but we will as default.

Connecting locally build code to Heroku App created from UI via Heroku CLI

We are now ready to make Spring Boot LIVE in PRODUCTION or to the public as you say :)
When you create the Heroku app, it associates the application to version control (git) so on push CI/CD can be achieved. Under Deploy tab of your respective app, you will get steps as below. Deploy.JPG

We will just follow the above steps and which would do push based on identified build type (maven and Gradle supported for spring boot), do packaging, read Procfile and trigger startup process.
Once the last command git push Heroku master is performed, it will start the deployment process. You will see logs on your local terminal as well as on the Heroku dashboard for that app.
The below snapshot will show build was successful and the web process Type has been started. It will also specify the URL where the application will be accessible local_push.JPG

From the dashboard of that app also you can view app URL as well open-app-heroku.JPG

We can view logs, restart dynos (which in our terms we can call isolated environment/container) by using the below link: logs.JPG

Voila! Our Spring Boot Application is now accessible via Heroku to the Internet for consumption within minutes.

Heroku Free tier offers enough free dyno hours per month to work on your hobby project before we can move to priced plans based on the requirement. :)

Resources

Thank you for reading, If you have reached it so far, please like the article, It will encourage me to write more such articles. Do share your valuable suggestions, I appreciate your honest feedback and suggestions!

I would love to connect with you at Twitter | LinkedIn.

 
Share this