Basic Example of Spring Boot Microservice using Istio as Service Mesh

Arup Mishra
3 min readApr 20, 2020

--

I searched a lot for some basic examples of Istio service mesh with Spring Boot, but was of no avail. So, in this blog have tried to cover it.

What does a service mesh do?

A service mesh, like the open source project Istio, is a way to control how different parts of an application share data with one another. Unlike other systems for managing this communication, a service mesh is a dedicated infrastructure layer built right into an app.

What is Istio?

Istio makes it easy to create a network of deployed services with load balancing, service-to-service authentication, monitoring, and more, with few or no code changes in service code. You add Istio support to services by deploying a special sidecar proxy throughout your environment that intercepts all network communication between microservices, then configure and manage Istio using its control plane functionality.

Istio Architecture -:

Now, we know some basics about Istio, let us try implementing it in Spring Boot.

Prerequisites -:

  1. Docker
  2. Kubernetes
  3. Spring Boot
  4. Maven
  5. Istio

Creating the Micro-services using Spring Boot

Generate the project using Spring Initialzr or the CLI and add Spring-web-starter as dependency.

We will create 2 micro-services to be deployed separately. Both the services will have 2 endpoints each, one to call each other and another for gateway calls

Folder Structure
Folder Structure
Microservice-1 Code
Microservice-2 code

Now, we will containerize it and deploy in our kubernetes cluster.

I used the command to containerize my services and deploy it in docker -hub

mvn com.google.cloud.tools:jib-maven-plugin:build -Dimage=<{docker-id}/{image-name}>

Once, it is deployed we can now go ahead and deploy the application in kubernetes.

First we will apply the manifest and set the profile in demo mode for istio.

Open a terminal in the istio installation path and use the below command -:

istioctl manifest apply --set profile=demo

Now, we will create a label namespace and instruct istio to deploy envoy proxies when application starts

kubectl label namespace default istio-injection=enabled

Now, lets deploy the applications using the yaml files -:

We will create a folder and name it kube and keep the yaml files in it for easy deployment.

Microservice-1 Deployment yaml
Microservice-2 Deployment yaml
Gateway for Microservice communication

We created a gateway for incoming traffic coming outside of the mesh.

Now lets deploy all of them by using —

kubectl apply -f kube

This will deploy all the microservices and create gateway for incoming traffic.

We can check if all pods are running by kubectl get pods.

If the pods are in running state, determine the ip and the port using the commands -:

Determine the IP using -:

(kubectl -n istio-system get service istio-ingressgateway -o jsonpath=’{jsonpath=’{.status.loadBalancer.ingress[0].hostname}’)

Determine the http port -:

(kubectl -n istio-system get service istio-ingressgateway -o jsonpath=’{jsonpath=’{.spec.ports}’)

This will give all the ports. Check for http2 and use the target port.

Now, just use the ip and port to access the application like -

http://localhost/service-1 or http://localhost:service-2

The full source code is present in here .

Hope, I was able to help you. If, any questions or suggestions please drop a comment as this is my first article.

--

--

No responses yet