"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Deploying Spring Boot Applications on Koyeb

Deploying Spring Boot Applications on Koyeb

Published on 2024-09-17
Browse:777

Deploying Spring Boot Applications on Koyeb

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 like Vercel and Netlify that 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 where Koyeb comes 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 using Spring Initializr with 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 at Neon
  • A Koyeb account. Sign up at Koyeb's website 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 a FREE PostgreSQL database, which will provide a database URL but is limited to 50 hours per month.

-On Neon, instantiate a FREE PostgreSQL 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 called env.properties, inside of that store all your environment variables, in this case DB_URL, DB_USERNAME and DB_PASSWORD.

NEVER COMMIT this file to the repository of your github.

env.properties file:

DB_USERNAME=
DB_PASSWORD=
DB_URL=

Your application.properties file:

application.propertiesfile:

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 values 1.8, 11, 13, 15, 17, 19, and 20.

If you do not specify a Java version, version 1.8 will be used.

I’m using Java 21. If you are using another version, change it accordingly.

system.properties

java.runtime.version=21

5. Create a controller

This controller will display the Hello World message at the / route.

package com.example.demo.Modules.User.controller;

import org.springframework.web.bind.annotation.RestController;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;

@RestController
public class UserController {

    @GetMapping("/")
    public String helloWorld() {
        return "Hello World";
    }

}

6. Push the code to Github

Create a public repository on Github and push your code. Grab the URL of 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 a Image Uploader Article.

Thanks for reading !


? Reference

  • Koyeb - Spring Boot deploy
  • Koyeb - Java Reference

? Talk to me

  • LinkedIn
  • Github
  • Portfolio
Release Statement This article is reproduced at: https://dev.to/mspilari/deploying-spring-boot-applications-on-koyeb-44kj?1 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3