Now, let's create our first microservice using Spring Boot.
All the code is in the spring-k8s-cassandra-microservices directory.
cd spring-k8s-cassandra-microservices
NOTE: You can check out any of the project files in the editor on the right. To open a file just click on its name. But, be aware that changes to the file save automatically, so be careful when reviewing files not to make inadvertent changes.
Let's start by compiling the code.
mvn package
While we are waiting for the compilation, let's check out the code for this microservice.
You can see the structure of the entity for this service in the Product.java file.
Open spring-k8s-cassandra-microservices/microservice-spring-boot/src/main/java/com/datastax/examples/product/Product.java
You can see that a Product has id
, name
, description
, price
and lastUpdated
fields.
This service will provide the following endpoints:
- /api/products/add: Add a new product
- /api/products/search/{name}: Search products by name
- /api/products/search/{name}/{id}: Search products by name and id
- /api/products/delete/{name}: Delete products by name
- /api/products/delete/{name}/{id}: Delete product by name and id
You can check out the code for accessing Cassandra in ProductDao.java.
Open spring-k8s-cassandra-microservices/microservice-spring-boot/src/main/java/com/datastax/examples/product/ProductDao.java
Let's build the Docker image for this service.
cd microservice-spring-boot
docker build -t example/spring-boot-service:1.0.0-SNAPSHOT .
Normally, we would store this image in a Docker repo, but for this example using KinD, let's just load it onto the KinD cluster nodes.
Loading the images may take a minute or so to complete.
Please be patient.
kind load docker-image example/spring-boot-service:1.0.0-SNAPSHOT --name spring-boot-cluster
cd ..
Let's create a namespace for this service to keep it organized and decoupled from the other services.
kubectl create ns spring-boot-service
We need to set up the credentials for accessing the Astra database.
Astra provides a secure connection bundle you will want to download.
Here's the command, but notice you will need to replace <paste link here> with a link to the secure bundle.
Copy this command and paste it in the terminal window, but DO NOT EXECUTE THE COMMAND YET!!!!!!
curl -L "<paste link here>" > creds.zip
To get the link to the secure connection bundle, on the Astra dashboard page, click on the CONNECT button.

Now, select the Driver Connection Method.
Then, right-click on the Download Secure Connection button and select Copy Link Address.
This places the link to the secure connection bundle on the clipboard, which you will now paste into the curl command.
Note that this link is time-sensitive, so you need to complete the next couple of steps without delay.

Now, go back to the terminal tab.
Paste the link (the one you just copied) into the curl
command as indicated - be sure to include the double quotes.
The curl command with the link will look something like this.

Once you have pasted the link, execute the curl
command.
Let's create a couple of variables to hold the database username and password to make the next couple of commands easy.
If you already set up these variables in the previous step, you don't need to do it again.
Otherwise, copy the following command.
Paste it into the terminal.
Then, replace <place username here>
with the username you supplied when you created the database.
DB_USER="<place username here>"
Now, if you have not already set up the variable for the password, use the following to set the password variable.
DB_PASSWORD="<place password here>"
Next, we'll store the database credentials as Kubernetes secrets so our service can use them.
kubectl -n spring-boot-service create secret generic db-secret --from-literal=username=$DB_USER --from-literal=password=$DB_PASSWORD
kubectl -n spring-boot-service create secret generic astracreds --from-file=secure-connect-bundle=creds.zip
Now, we are ready to deploy the service.
kubectl -n spring-boot-service apply -f deploy/spring-boot
We'll create another variable with the pod ID for later reference.
BOOT_SERVICE_POD=$(kubectl -n spring-boot-service get pods | tail -n 1 | cut -f 1 -d ' ')
At this point, we would like to test our service.
In a normal Kubernetes cluster, we would use port-forwarding to allow us to access the service from our local host.
But using kind, we can set up a proxy that will allow us to access the service from outside the cluster.
Start the proxy in second terminal by clicking on the following.
kubectl proxy
We'll leave the proxy running in terminal 2 and switch back to terminal 1 by clicking on the following.
echo "Welcome back to Terminal 1!"
The proxy gives us a base URL to the service.
We'll save this base URL in a variable so we can use it for several different accesses.
BOOT_BASE_URL=http://localhost:8001/api/v1/namespaces/spring-boot-service/pods/$BOOT_SERVICE_POD/proxy
NOTE: In a regular Kubernetes cluster (not running on KinD or proxy), you could set the base URL as
BOOT_BASE_URL=http://localhost:8083
.
You would also need to forward the port:
kubectl -n spring-boot-service port-forward $BOOT_SERVICE_POD 8083:8083
Let's make sure the pod is up and running.
Click the following to start watching the state of the pod.
watch kubectl get pods -n spring-boot-service
Wait for the STATUS to be RUNNING.
Then, you can exit the watch loop by clicking the following, which sends a Ctrl-C to the terminal.
echo "Exiting watch loop"
Now we are ready to try the service.
Let's start by adding a couple of products.
curl -X POST -H "Content-Type: application/json" -d '{"name": "mobile", "id":"123e4567-e89b-12d3-a456-556642440000", "description":"iPhone", "price":"500.00"}' $BOOT_BASE_URL/api/products/add
curl -X POST -H "Content-Type: application/json" -d '{"name": "mobile", "id":"123e4567-e89b-12d3-a456-556642440001", "description":"Android", "price":"600.00"}' $BOOT_BASE_URL/api/products/add
We can search for a product by name.
curl $BOOT_BASE_URL/api/products/search/mobile | jq
We can also search by name and ID.
curl $BOOT_BASE_URL/api/products/search/mobile/123e4567-e89b-12d3-a456-556642440001 | jq
We can even use the API to delete a product.
curl -X DELETE $BOOT_BASE_URL/api/products/delete/mobile/123e4567-e89b-12d3-a456-556642440001
Excellent! You have a running Spring Boot microservice talking to Cassandra!