Spring profiles with H2, CockroachDB and Docker
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.
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.
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.
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-SQLcalling 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: truecalling 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.
5.3. Changes
In the picture below, we can identify the changes in blue.
For it, letās create the files below š
āļø Create a new application file for in-memory for H2 database, application-in-memory.properties.
āļø Create a new application file for CockroachDB, application-cockroach.properties
āļø Move the common configuration sections to application.properties file.
āļø 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.
āļø 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.
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.
The picture below illustrates how the application will be deployed.
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
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.
And run, then the SpringBoot application will be start using both profiles
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
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ā.
Your environment should have the aspect below
Now is time to run šour dockerized application
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.
VoilĆ , the three containers are up and running, check on the bottom left.
Just to confirm, letās open the front-end (http://localhost:3000) and the Cockroach admin page (http://localhost:8081).
Front-end
Using one of those users, copy and paste the Username and Password to the fields User and Password and hit the SIGN IN button
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,
you can open the table or query the data using SQL command.
Executing the query
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
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;
7.3. Via DBeaver
8. Conclusion
Using profiles is a way to make the application configurable and ready to run in any environment.
If you like this article give your clap(s) šššš and review š
9. References
https://pixabay.com/photos/marvel-toys-heroes-superheroes-5364165/
https://gist.github.com/dbist/ebb1f39f580ad9d07c04c3a3377e2bff