Service discovery in spring cloud using netflix eureka in AWS Fargate.

Bhanupratap Singh
3 min readSep 8, 2018

--

Photo by Hennie Stander on Unsplash

Service discovery is an important aspect of microservices based architectures , that actually acts like an index of all other services running , and if system uses autoscaling feature provided by aws or any other cloud provider then service discovery becomes very important aspect of whole infrastructure.

Netflix did a wonderful job by opensourcing some of its core components one of which is eureka it basically solves middle tier load balancing and service discovery problem , which was initially not available with cloud providers like aws , now days aws provides ALB which can be used as middle tier load balancer.

My challange was to build an architecture for microservices which should not have cloud specific dependencies so selecting Netflix Eureka was an obvious choice for me , there were other options available but since Eureka is battle tested within Netfilx production deployment this gave me more confidence for choosing Eureka.

Our entire microservices architecture is based on spring boot and spring cloud and spring cloud provides eureka as a maven dependency so thats pretty easy to use and configure , while we were doing POC on our local machines things were running perfectly fine with a sample architecutre as shown below

All services will connect to Eureka on startup

We choose to use aws fargate as container orchestration on aws platform but during deployment we face an issue that business services were connected to eureka server but IP address and port which was given to eureka server by microservice was not correct and clients were not able to connect to underlying services via eureka.

Solution to this problem is read IP address form AWS metadata and send that to eureka during service registration with eureka on startup , and this can be achieved by two ways .

1.make services aws aware and read ip address from metadata , but problem with this approach is underlying microservice becomes infrastructure aware.

2. Read aws metadata form .sh file and then send that to eureka via config settings

I have decided to use second approach and changes are given below

Include entrypoint.sh file during Fargate setup and put below lines in that file

export ECS_INSTANCE_IP_ADDRESS=$(curl — retry 5 — connect-timeout 3 -s 169.254.169.254/latest/meta-data/local-ipv4)
export ECS_TASK_NON_SECURE_INSTANCE_PORT=$(cat ${ECS_CONTAINER_METADATA_FILE} | grep HostPort | awk -F “: “ ‘{print $2}’ | sed ‘s/,$//g’)

Above line will read IP address and Port information from aws metadata and set following properties in Dockerfile

COPY entrypoint.sh /usr/local/bin/

Set following properties in application.properties file in underlying microservice

eureka.instance.preferIpAddress=true
eureka.instance.ip-address=${ECS_INSTANCE_IP_ADDRESS}
eureka.instance.nonSecurePort=${ECS_TASK_NON_SECURE_INSTANCE_PORT}

This approach takes aws metadata reading out of spring boot and does that using bash file.

--

--

Bhanupratap Singh

Software Architect . Working on Holistic Identity, Artificial Intelligence. Love to code and build ....