Heroku
Overview
Heroku is a cloud platform as a service (PaaS) that allows developers to build, run, and operate applications entirely in the cloud. It supports multiple programming languages and uses Git for version control. Heroku applications run in Dynos, which are lightweight Linux containers. The platform simplifies the deployment process by handling the complexities of building, packaging, and deploying applications.
Deploying a Spring Boot Application on Heroku
Heroku provides a straightforward process for deploying Spring Boot applications. The deployment involves several steps, including logging into Heroku, creating a Git repository, provisioning a Heroku application, and pushing the code to Heroku.
Step 1: Login to Heroku
First, you need to log in to Heroku from your terminal. This step is crucial as it allows you to execute subsequent commands for deployment. Use the following command to log in:
Listing 9.22 Login to Heroku
heroku login
heroku: Press any key to open up the browser to login or q to exit:
➥ Opening browser to https:/ /cli-
➥ auth.heroku.com/auth/cli/browser/d4da08df-3725-44b6-bf28-
➥ c0a78fbe54e3?requestor=SFMyNTY.g2gDbQAAAA8xMDMuMjE1LjIyNC4xNTFuBgDw-
➥ iCkewFiAAFRgA.6fS4ju_OBxvr9_YQNkSn5Z7UK68CQNULUhh9VEzCVxQ
Logging in... done
Logged in as *****@gmail.com
Step 2: Provision a Heroku Application
Next, you need to create a new Heroku application. This is done using the heroku create command, which also sets up a remote repository for your application.
Listing 9.24 Provisioning the Heroku application
heroku create
Creating app... done, ⬢ secure-journey-03985
➥ https:/ /secure-journey-03985.herokuapp.com/ |
➥ https:/ /git.heroku.com/secure-journey-03985.git
Heroku generates a random name for your application, such as secure-journey-03985.
Step 3: Attach a PostgreSQL Database
For applications that require a database, Heroku allows you to attach a PostgreSQL database. This is done using the heroku addons:create heroku-postgresql command. Once the add-on is created, Heroku automatically populates the necessary environment variables (SPRING_DATASOURCE_URL, SPRING_DATASOURCE_USERNAME, and SPRING_DATASOURCE_PASSWORD) to connect your application to the database.
Step 4: Deploy the Application
Finally, you can deploy your Spring Boot application by pushing the code to the Heroku master branch. This process uses Git for version control and the Maven wrapper (.mvnw) for building the application.
Listing 9.25 Deploying the Spring Boot application in Heroku
c:\sbip\repo\ch09\course-tracker-app-heroku>git push heroku master
Enumerating objects: 41, done.
Counting objects: 100% (41/41), done.
Delta compression using up to 8 threads
Compressing objects: 100% (30/30), done.
Writing objects: 100% (41/41), 64.32 KiB | 5.85 MiB/s, done.
Total 41 (delta 3), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Building on the Heroku-20 stack
remote: -----> Determining which buildpack to use for this app
remote: -----> Java app detected
remote: -----> Installing JDK 11... done
remote: -----> Executing Maven
remote: $ ./mvnw -DskipTests clean dependency:list install
...
...
remote: https:/ /secure-journey-03985.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploys... done.
To https:/ /git.heroku.com/secure-journey-03985.git
* [new branch] master -> master
Once the application is successfully built and deployed, it is accessible via a URL like https://secure-journey-03985.herokuapp.com. You can also use the heroku open command to open the application URL in a browser. To check the Spring Boot startup logs, use the heroku logs command.