Introduction
When we start building things as developers, one of our key goals is to share what we've created with others. For frontend developers, this is often straightforward, thanks to great hosting services likeVercelandNetlifythat support frontend apps seamlessly. However, for backend developers, showcasing our work can be more challenging. We build APIs, work with databases, and while JSON might be powerful, it’s not as visually compelling as an animation built with CSS or Lottie.
That’s why finding an efficient and reliable way to deploy backend applications is crucial. Spring Boot, a popular Java-based framework, simplifies the process of building production-ready applications, but deploying them can still be a challenge. This is whereKoyebcomes in. Koyeb offers a powerful and easy-to-use platform that allows developers to deploy their Spring Boot applications quickly with minimal setup. In this guide, we’ll walk you through the entire process of deploying a Spring Boot application with a PostgreSQL database on Koyeb, from start to finish.
1. Prerequisites
Before we dive into the deployment process, ensure you have the following:
-
A basic Spring Boot application.If you don't have one, you can quickly generate a project usingSpring Initializrwith these dependencies:
- Spring Web
- Spring Data JPA
- PostgreSQL Driver
- A GitHub (or GitLab/Bitbucket) repository,where your Spring Boot project is hosted.
- A Neon account.Sign up atNeon
- A Koyeb account.Sign up atKoyeb'swebsite if you don’t have one.
- Maven or Gradle installed,depending on how your Spring Boot project is configured.
2. Setup database
- On Koyeb, instantiate aFREEPostgreSQL database, which will provide a database URLbut is limited to 50 hours per month.
-On Neon, instantiate aFREEPostgreSQL database, which will also provide a database URL.
3. Connect database with Spring boot
Inside the resources directory you are going to create a file calledenv.properties
,inside of that store all your environment variables, in this caseDB_URL
,DB_USERNAME
andDB_PASSWORD
.
NEVER COMMITthis file to the repository of your github.
env.properties
file:
DB_USERNAME=<Get this from the Neon or Koyeb dashbord>
DB_PASSWORD=<Get this from the Neon or Koyeb dashbord>
DB_URL=<Get this from the Neon or Koyeb dashbord>
Yourapplication.properties
file:
application.properties
file:
server.port=${PORT:8080}
spring.datasource.url=${DB_URL}
spring.datasource.username=${DB_USERNAME}
spring.datasource.password=${DB_PASSWORD}
spring.jpa.hibernate.ddl-auto=update
spring.config.import=classpath:env.properties
Explanation
- server.port- This is the port where your application will run. We set an environment variable PORT with a fallback of 8080 in case PORT is null.
- spring.datasource.url- This is an environment variable coming from the Neon or Koyeb dashboard.
- spring.datasource.username- This is an environment variable coming from the Neon or Koyeb dashboard.
- spring.datasource.password- This is an environment variable coming from the Neon or Koyeb dashboard.
- spring.config.import- This imports the file where you store your sensitive data.
4. Create a system.properties file
At the root of the project, create a system.properties file.
This file specifies the Java runtime version to use so that the Koyeb Java buildpack executes the project with the correct version.
Remember:Koyeb accepts major version values1.8
,11
,13
,15
,17
,19
,and20
.
If you do not specify a Java version,version 1.8will be used.
I’m usingJava 21.If you are using another version, change it accordingly.
system.properties
java.runtime.version=21
5. Create a controller
This controller will display theHello World
message at the/
route.
packagecom.example.demo.Modules.User.controller;
importorg.springframework.web.bind.annotation.RestController;
importorg.springframework.http.ResponseEntity;
importorg.springframework.web.bind.annotation.GetMapping;
@RestController
publicclassUserController{
@GetMapping("/")
publicStringhelloWorld(){
return"Hello World";
}
}
6. Push the code to Github
Create a public repository onGithuband push your code. Grab theURLof this repository.
7. Deploy on Koyeb
- Enter your koyeb account.
- Go to Services > Web Services > Create web services with Github.
- Connect to Github or paste the public repository URL.
- Wait for the project to build.
- If successful, a public URL will be generated, and everyone can access your Spring Boot application.
Conclusion
That’s it! You’ve created a Spring Boot application, connected it with a cloud database, and deployed everything on Koyeb. This can be the start of a project you want to showcase in your portfolio, allowing clients to see what you can do.
You can increment this application with aImage Uploader Article.
Thanks for reading!