Introduction
Deploying a Spring Boot 3.1 application on Heroku with Tomcat 10.1 might seem tricky at first, but with the right steps, you can get your application running smoothly on a cloud-based platform. In this guide, we’ll walk through the entire process, ensuring a seamless deployment.
Why Deploy Spring Boot on Heroku?
Heroku provides a simple and scalable cloud platform for deploying applications. Some key advantages include:
- Free tier available: Great for development and testing.
- Simplified deployment: Just push your code to Git, and Heroku handles the rest.
- Scalability: Scale your application with ease as traffic grows.
- Built-in support for Java: Heroku natively supports Java, making it easier to deploy Spring Boot apps.
Step 1: Prepare Your Spring Boot Application
Before deploying, ensure that your Spring Boot 3.1 application meets these requirements:
- Uses Tomcat 10.1 as the embedded server.
- Configured with PostgreSQL or any other database compatible with Heroku.
- Includes a
Procfile
for Heroku execution.
Add Dependencies
Update your pom.xml
with the necessary dependencies for Spring Boot and Tomcat:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>10.1.0</version>
</dependency>
Step 2: Configure Heroku for Deployment
1. Install the Heroku CLI
If you haven’t already, install the Heroku CLI:
curl https://cli-assets.heroku.com/install.sh | sh
2. Login to Heroku
Run the following command:
heroku login
This opens a browser window for authentication.
3. Create a New Heroku App
In your project root directory, initialize a new Heroku app:
heroku create your-app-name
Replace your-app-name
with a unique name.
Step 3: Configure the Procfile
Heroku requires a Procfile
to specify the process type. Create a file named Procfile
in your project root and add:
web: java -jar target/your-app.jar
Replace your-app.jar
with the actual name of your built JAR file.
Step 4: Configure Application Properties
Heroku provides environment variables for configurations. Update your application.properties
or application.yml
:
server.port=${PORT}
spring.datasource.url=${JDBC_DATABASE_URL}
spring.datasource.username=${JDBC_DATABASE_USERNAME}
spring.datasource.password=${JDBC_DATABASE_PASSWORD}
Step 5: Deploy to Heroku
1. Initialize Git
If your project is not already a Git repository, initialize it:
git init
git add .
git commit -m "Initial commit"
2. Deploy to Heroku
git push heroku main
After deployment, Heroku assigns a URL to your app. You can check the status with:
heroku logs --tail
3. Scale the App
Heroku automatically assigns a free-tier dyno to your application. If needed, scale it:
heroku ps:scale web=1
Step 6: Open Your Application
Once deployed, open your application in a browser:
heroku open
This should load your Spring Boot 3.1 application running on Tomcat 10.1!
Conclusion
Deploying a Spring Boot 3.1 application on Heroku with Tomcat 10.1 is straightforward. By following these steps, you can leverage Heroku’s cloud infrastructure to run your applications efficiently with minimal setup.
🚀 Next Steps: Explore Heroku’s PostgreSQL integration, auto-scaling, and log monitoring for a more robust deployment!
Got questions or need help? Drop a comment below and let's discuss! 🚀