Spring Boot Application with Apache Cassandra running on separate containers — in 10 steps.
Let’s discuss about how we can create a Spring Boot application that can store data in Apache Cassandra. We will be running both the applications in separate Docker containers. Finally we will be performing a CI/CD with Travis CI and pushing the final Spring Boot application docker image to docker hub.
Step 1: Create a Spring Boot Application.
Let’s got to Spring Initializr website https://start.spring.io/ and create a Simple SB Application with the required dependencies as shown below.
Download the Spring boot application and import to your workspace.
Step 2: Preparing the pom.xml
Let’s see how the final pom.xml looks like.
Step 3: Create a Github Repository
Create a new Github repository. To merge the code from your local to the Github repository, run the following commands:
- Initialise the Github repository using the following commands
git init
git remote add origin <Repository SSH>
2. Perform a pull from the newly created repository.
git pull origin master
3. Make sure to add mvnw and mvnw.cmd to the .gitignore file
4. Commit and push the changes to master
git add .
git commit -m "Initial Commit"
git push origin master
Step 4: Introduce Cassandra Configurations
First step is to prepare the application.properties file which will contain all the Apache Cassandra related configurations
The above parameters include the keyspace and datacenter definition along with schema-action, base package and contact-points.
schema-action=NONE indicates that we do not want our database to be created or recreated on startup.
Note that since we are hosting Cassandra in a Docker Container, we have to provide the service/container name in docker-compose as the contact point.
Also, the basic configuration class looks something like this:
Step 5: Table Definition
Let’s create a table that contains the login details of employees. Remember, Cassandra mandates having at least one primary key in a table.
Step 6: Spring Data Repository management
The following is the repository management code constructed by spring data support.
You also have to create the Controllers to provide endpoints to access the above methods.
Step 7: Configuring docker in the Spring Boot app
The Dockerfile for the Spring Boot application will contain the following data.
The docker-compose.yml file will contain two images. One for the Spring Boot application while the other is for Apache Cassandra image. The follow is how the basic docker-compose.yml file should look like.
Few things to note here:
- Port 4070 is being used for running the Spring Boot application
- Whatever we provide as the container name here has to be provided in the contact-points field in the application.properties file.
- Apache Cassandra needs some time to fully load. But, the Spring Boot application needs Apache Cassandra to be up and running as it creates the keyspaces and tables during load. Thus, we provided restart: always such that the Spring Boot application will keep on restarting till the Apache Cassandra is fully loaded.
- The build context is provided as ‘.’ considering docker-compose.yml and the Dockerfile are in the same build path and jar file is at a relative path ./target/connectivity*.jar
Step 8: Configuring Travis-ci
Travis-CI is an excellent platform to perform build and test of your application and can be triggered when merged to github. It also provided support to push docker image to docker hub and also deploy the docker images to AWS.
Perform the following steps for the setup:
- Setup an login account in travis-ci.org. You have to connect your Github account at login.
- Enable your repository by going to settings and click on the switch beside your repository. If the repository does not comes in your settings page, click on the Sync account button before performing this step.
3. Once you enable the repository, the same would come in the travis-ci dashboard. Before performing a build, we have to do two things. One is to configure a .travis.yml file and push it to master. Second is optional if you are trying to create a docker image as well.
4. To create a docker image and push it to docker hub, travis-ci need to log in to the docker-cli using your docker hub credentials. The safest way is to save your credentials in the build settings such that no one else can access those data. To go to build settings, click on the repository on your dashboard and click on the ‘More options’ button and click on settings.
Once there, go to Environment variables and add your credentials. You can also specify which Git branch this credentials will be used.
Step 9: Creating docker repository
The next step would be to create a docker repository. Login to hub.docker.com and click on Create Repository. Note that in Docker Hub, you can create only 1 private repository and all other repositories would have public access.
Step 10: Configuring .travis.yml
The final step is to configure the .travis.yml file. The basic file should be something similar to below:
Note that you have to provide your Docker ID in the <dockerid> placeholders in the above file.
After configuring the .travis.yml file, we need to merge this file to master.
git add .travis.yml
git commit -m "Introducing travis-ci config"
git push origin master
After the merge, Travis-ci will automatically trigger a build and at the end of the build it will push the newly formed images to the Docker Hub. One thing to note here is you have to provide the name of the docker image same as the repository you created in step 9 or else it will create a new repository and save the image there.
And there we are. You are now able to Configure Apache Cassandra using Spring Boot and Docker and build and deploy the Spring Boot application to Docker Hub !
Happy Coding !