Spring Boot is a popular framework for building cloud-native applications. Each Spring Boot application is stand-alone and self-contained, which makes them easy to deploy in a distributed fashion – whether to containers or on Kubernetes.
The example on this page shows how you can connect a Spring Boot application to YugabyteDB Managed, using a version of the Spring Boot PetClinic sample application that has been updated with a profile making it compatible with YugabyteDB.
The repository for the application is at https://github.com/yugabyte/spring-petclinic. Instructions for connecting this application to YugabyteDB are also provided in the petclinic_db_setup_yugabytedb.md
file in this repository.
In this walkthrough, you will:
- Download the Spring Boot PetClinic application
- Connect the application to YugabyteDB Managed
- Containerize the application in Docker
Prerequisites
- Java 8 or newer (full JDK)
- Git
- Docker
- YugabyteDB Managed cluster, with your computer IP address whitelisted in an IP allow list
Download and connect the PetClinic application
-
On your computer, clone the Spring Boot PetClinic sample application:
$ git clone https://github.com/yugabyte/spring-petclinic.git
Cloning into 'spring-petclinic'... remote: Enumerating objects: 8616, done. remote: Counting objects: 100% (18/18), done. remote: Compressing objects: 100% (18/18), done. remote: Total 8616 (delta 1), reused 13 (delta 0), pack-reused 8598 Receiving objects: 100% (8616/8616), 7.29 MiB | 19.03 MiB/s, done. Resolving deltas: 100% (3268/3268), done.
-
Go to the
spring-petclinic
directory.$ cd spring-petclinic
-
Open the file
spring-petclinic/src/main/resources/db/yugabytedb/user.sql
and copy the contents. -
Sign in to YugabyteDB Managed and select your cluster, and note the host and port details.
-
Click Connect and choose Launch Cloud Shell.
-
Paste the contents of the
user.sql
file into the Cloud Shell to create the application database and user.yugabyte=# DROP DATABASE IF EXISTS "petclinic"; yugabyte=# DROP USER IF EXISTS "petclinic"; yugabyte=# CREATE DATABASE "petclinic"; yugabyte=# CREATE USER "petclinic" WITH PASSWORD 'petclinic'; yugabyte=# GRANT ALL PRIVILEGES ON DATABASE "petclinic" to "petclinic";
NOTICE: database "petclinic" does not exist, skipping DROP DATABASE NOTICE: role "petclinic" does not exist, skipping DROP ROLE CREATE DATABASE CREATE ROLE GRANT
-
On your computer, run the PetClinic application using the following command:
$ ./mvnw spring-boot:run \ -Dspring-boot.run.profiles=yugabytedb \ -Dspring-boot.run.arguments="--YBDB_URL=jdbc:postgresql://[host]:[port]/petclinic?load-balance=true"
where
[host]
and[port]
are the host and port number of your YugabyteDB Managed cluster. Thespring-boot.run.profiles
parameter tells the application to use the YugabyteDB database configuration. Thespring-boot.run.arguments
parameter provides the application with the connection string to your YugabyteDB Managed cluster.[INFO] Scanning for projects... [INFO] [INFO] ------------< org.springframework.samples:spring-petclinic >------------ [INFO] Building petclinic 2.4.5 [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] >>> spring-boot-maven-plugin:2.4.5:run (default-cli) > test-compile @ spring-petclinic >>>
Note
This configuration automatically initializes your database, and will delete and reinitialize the database on every run. To not initialize the database after the first run, use the
--DB_INIT=never
argument:$ ./mvnw spring-boot:run \ Dspring-boot.run.profiles=yugabytedb \ -Dspring-boot.run.arguments="--YBDB_URL=jdbc:postgresql://[host]:[port]/petclinic?load-balance=true, \ --DB_INIT=never"
-
Go to http://localhost:8080.
The PetClinic application is now running locally and is connected to your YugabyteDB Managed cluster.
Containerize the application using Docker and run locally
-
Start Docker on your computer.
-
Containerize the PetClinic application:
$ ./mvnw spring-boot:build-image
-
Tag your image:
$ docker tag [image_id] spring-petclinic
You can find the image id by running
docker image ls
. -
Run the image as a container in Docker to make sure it’s working correctly:
$ docker run -d --name=spring-petclinic -p 8080:8080 -e \ JAVA_OPTS="-Dspring.profiles.active=yugabytedb \ -Dspring.datasource.url=jdbc:postgresql://[host]:[port]/petclinic?load-balance=true \ -Dspring.datasource.initialization-mode=never" spring-petclinic
where
[host]
and[port]
are the host and port number of your YugabyteDB Managed cluster. -
Go to http://localhost:8080.
The PetClinic sample application is now connected to your YugabyteDB Managed cluster and running locally on Docker.