Spring profiles with H2, CockroachDB and Docker

Luiz Gustavo De O. Costa
8 min readMay 27, 2022

--

Many years ago, to run the application in different env (environments) we had put a lot of effort into achieving it, and in some situations changed the jar (shame šŸ˜œ) manually to make the Java application run in upstream/desired env..

In this article Iā€™ll cover how to use Spring Profiles, connect to a Database using Docker and how to put it on a docker compose šŸ˜.

By the way, all the code is available on my GitHub.

Hulk, Strange Doctor, and Peter Quill

1. What are Spring profiles?

Spring Profiles provide a way to segregate parts of your application configuration and make it only available in certain environments (Spring documentation)

2. Assumptions

  • Youā€™re using Docker to run the containers,
  • Security will not be covered in this article,
  • We donā€™t want to control the database data using Liquibase or Flyway,
  • If you want to follow step by step, you can use this code on my GitHub repo, this version doesnā€™t have Spring profiles apart from the profile default.

3. Why?

The company came up with a new idea (me šŸ˜ƒ), add an option to run other Database. Well, weā€™ve a lot of options on the market, but the company (me again)decided to use CockroachDB.

And now, should we change all project šŸ˜” ? Nope, since weā€™re using JPA/Hibernate, Spring we just need to add a new Profile and the requirement will be accomplished.

In order to achieve the goal above, instead of installing the database locally we are going to run it through Docker.

It is possible to run locally, via Docker or on the cloud.

3.1. AS-IS

Nowadays the application has your own database, an in-memory H2 instance. The lack of this approach loses the data every time the application is closed.

Application as-is

3.2. TO-BE

Now, using a persistent database, we can close the application or even allow other applications to query the data via api/query.

And the Spring Profiles will help us to tackle this requirement and enable the application to the new world šŸ˜® or continue using H2.

Below is a representation of the desired deployment.

Application to-be

4. Alternatives

Forget it, letā€™s take advantage of Spring Profiles.

5. Hands on

Before we go directly to hands on, letā€™s review some rules to use profiles.

5.1. Rules šŸ—’

  • Your Spring Boot application can use as many profiles as you want, for instance
application.properties        #Default and common configurations
application-dev.properties #To run on development env
application-test.properties #To run on test env
application-h2.properties #To run H2
application-mysql.properties #To run MY-SQL
calling via CLI
java -jar -Dspring.profiles.active=default,dev,test,mysql myJar.jar
  • The profile could be declared inside the same application.properties(yml) file separated by dashes
database.user: doctor_strange
---
spring.profiles: dev
database.password: peter_quill
---
spring.profiles: !dev
spring.profiles.include: local
app.security: true
calling via CLI
java -jar -Dspring.profiles.active=default,dev,uat myJar.jar
  • You can create new application configuration files, ie, application-<profile>.properties and replace the <profile> by your desired. In our case the profile name is cockroach, as was mentioned on the first Rule item.

5.2. Getting started

The current project has the structure below, pretty straightforward. Has only the Profile default, represented by application.properties file.

Current project on IntelliJ

5.3. Changes

In the picture below, we can identify the changes in blue.

Expected project on IntelliJ

For it, letā€™s create the files below šŸ‘‡

āœ”ļø Create a new application file for in-memory for H2 database, application-in-memory.properties.

application-in-memory.properties

āœ”ļø Create a new application file for CockroachDB, application-cockroach.properties

application-cockroach.properties

āœ”ļø Move the common configuration sections to application.properties file.

application.properties

āœ”ļø Add the dependency of PostgreSQL driver (yes CockroachDB uses PostgreSQL jar), inside the dependencies section.

āœ”ļø Change the class LoadOnStartup to be called only when the in-memory profile is activated.

āœ”ļø Add the DDL(Data Definition Language)/DML(Data Manipulation Language) script for CockroachDB.

SQL script DDL/DML

āœ”ļø Build the application and create a new image. This image will be used later.

chmod +x gradlew && ./gradlew clean build
docker build -t 16bits/zero2hero-be:0.0.2 .

āœ”ļø Push the image to Docker Hub, after the tests šŸ˜‰.

docker push 16bits/zero2hero-be:0.0.2# LOG
The push refers to repository [docker.io/16bits/zero2hero-be]
231cd9b2af5b: Pushed
83b767b06655: Layer already exists
14fbd8039ba4: Layer already exists
da55b45d310b: Layer already exists
0.0.2: digest: sha256:be3d26471df14d4607a71b73bce7553d9d1a11c1cb6995e0e613a047735e0add size: 1165
# END LOG

āœ”ļø Create a new docker-compose file on the project root folder, named it docker-compose-cockroach.yml. More details about this file will be discussed in section 6.2.3.

docker-compose-cockroach.yml

All set, so itā€™s time to test the system.

6. Running using the new Spring Profiles

To connect to the new database, we need to start the application using the profiles default, cockroach.

Wait āœ‹ How I know that name? This name should follow the application properties, ie, remove the application and the minus sign from the file and then this will be the profile name (Make it better)application-<PROFILE-NAME>.properties/yaml/yml . Look at the table below to understand better.

Database x Profiles

The picture below illustrates how the application will be deployed.

Spring Profiles default, cockroach

6.1. Database configuration

To configure the database is really a piece of cake, just start your Docker runtime and run the command below to create the container.

docker run -d --name=cockroachDB  -p 26257:26257 -p 8081:8080  -v "${PWD}/cockroach-data/roach1:/cockroach/cockroach-data"  cockroachdb/cockroach:v22.1.0 start-single-node --insecure

Thatā€™s it, your database should be accessible by the port 26257 and the Admin Page through the port 8081

Admin page

6.2. SpringBoot + CockroachDB

Database is up and running, then itā€™s time to start the application using the default, cockroach profiles.

6.2.1 Via IntelliJ

Open the Run/Debug Configurations and the profiles to the Active profiles section.

IntelliJ Run Configurations

And run, then the SpringBoot application will be start using both profiles

SpringBoot console

However if you want to use the in-memory just change the profile to default, in-memory.

6.2.2. Via CLI

Running over the CLI, we have to do some additional steps.

1 ā€” Create the artifact

chmod +x gradlew && ./gradlew clean build

2 ā€” Run the jar passing the arguments

java -jar -Dspring.profiles.active=default,cockroach build/libs/zero2hero-0.0.1-SNAPSHOT.jar
Logs first part
Logs second part

6.2.3. Via Docker Compose

The last change is in place. Duplicate the docker-compose in order to have one file to start using database in-memory and other to use CockroachDB.

Create a new docker-compose file, mentioned on the section 5.3, since the first one will use an in memory database.

āš ļø āš ļø Pay attention to the lines āš ļø āš ļø :

3 ā€” Creating the database section

13 ā€” Ask for our back-end application wait until the database starts

20 ā€” Overriding the application configuration. āš ļø Very important to connect to the database, otherwise youā€™ll receive a message ā€œConnect refusedā€.

docker-compose-cockroach.yml

Your environment should have the aspect below

Project on IntelliJ

Now is time to run šŸƒour dockerized application

Dockerized application diagram

6.2.3.1. Via IntelliJ

Just open the docker-compose-cockroach.yml file and click on the first play icon, and all containers will start.

IntelliJ docker-compose-cockroach.yml file

VoilĆ , the three containers are up and running, check on the bottom left.

Containers running

Just to confirm, letā€™s open the front-end (http://localhost:3000) and the Cockroach admin page (http://localhost:8081).

Front-end

Home screen

Using one of those users, copy and paste the Username and Password to the fields User and Password and hit the SIGN IN button

Home screen
Movie load from containers

6.2.3.2. Via CLI

Via CLI is straightforward.

docker-compose -f <docker-compose file> up -ddocker-compose -f /Users/luizcosta/workspace/git/16-bits-zero-to-hero/docker-compose-cockroach.yml up -d

7. Browse the data

It is useful for all modes, be it running as a single container or using docker-compose.

7.1. Via IntelliJ

After configure the connection,

Connection configuration

you can open the table or query the data using SQL command.

Data

Executing the query

SQL query command

7.2. Via Console

Using a command interface, such as iTerm2, type the command

docker exec -it <CONTAINER_NAME> ./cockroach sql --insecure
docker exec -it 16-bits-cockroach ./cockroach sql --insecure
Connected to Database screen

After the connection you can use the PostgreSQL commands and browse the data. First, I will ask to show me all tables from defaultb schema and then ask to bring all movies.

show tables;
select * from movies;
Show tables and select movies screen

7.3. Via DBeaver

--

--

Luiz Gustavo De O. Costa
Luiz Gustavo De O. Costa

Written by Luiz Gustavo De O. Costa

Hey friend!! Iā€™m Luiz Gustavo, a Java developer and Iā€™m here to learn and write about Java, tests and good practices

No responses yet