Inter-Microservice Communication with OpenFeign using AWS AppMesh
Ed Legaspi
Posted on October 24, 2022
Learn how to use AWS AppMesh to facilitate inter-microservice communication with OpenFeign without using Spring Eureka. Instead, Service Discovery native to AWS will be used.
1. Introduction
Microservice architecture is becoming more and more popular nowadays. Thanks to the ever-changing requirements of software development. It allows cross-functional teams to develop, test, and deploy different services, which leads to faster release to the customer.
2. Microservice Pattern
One pattern in which a microservice architecture can be applied to a project is business capability. For instance, an e-commerce store can have a catalog service, price service, delivery service, etc. When presenting the catalog or product data to the client, it's usually accompanied by a price. Thus, catalog service must pull the cost from the price service. Or, if you have a backend for front-end service, you call and aggregate the result of catalog and price service data.
3. Spring Cloud
It would help to use a framework that offers a toolkit when developing a microservice architecture. Some of the most commonly known are Go, Quarkus, Micronaut, but my personal favorite is Spring Cloud.
Spring Cloud offers:
- Ribbon - load balancing
- Eureka - naming server
- Resilience4j - fault-tolerance
- API Gateway
- OpenFeign - API client
- Distributed-Tracing - Sleuth/Zipkin Spring Microservice Architecture
4. Service Discovery
Service Discovery is a way for a set of microservices to locate and communicate with each other on a network. It needs a central service where all microservices need to register. The microservices register with their name and network information such as IP. In the Spring Cloud ecosystem, it is the Eureka library that should run on its own service.
This presents a problem since Eureka server is am an essential part of the application. You need to have at least 2 instances to ensure that it's always available. Otherwise, your application will be broken. And extra service means additional maintenance and cost.
5. AWS App Mesh
Introducing the AWS AppMesh, built differently but solves the same problem of microservice networking. It gives end-to-end visibility and high availability for your application.
This service is free. You need to pay the underlying compute engine, either EC2 or Fargate.
6. OpenFeign
OpenFeign is a library that allows us to create HTTP clients with an annotated interface. No implementation code is required.
An example OpenFeign client:
@FeignClient(name = "applicant-services", url = "${app.ct.client.applicant.url}")
public interface ApplicantProxy {
@GetMapping("/applicants/profiles")
List<String> getApplicantsByJob();
@GetMapping("/applicants/top")
List<String> getTopApplicantsByJob();
}
If the services are registered in Eureka, we won't need the URL parameter. It should point to the Service discovery endpoint of the service [see 8.4].
7. Missing Service Discovery without Eureka?
Fortunately, AWS has a native alternative, Amazon ECS Service Discovery. It uses AWS Cloud Map API actions to manage HTTP and DNS namespaces for your Amazon ECS services.
This service is not free. Pricing computation https://docs.aws.amazon.com/AmazonECS/latest/developerguide/service-discovery.html.
8. AWS Setup
Since you're here I will assume that you are already familiar with the AWS services.
8.1 ECR
Create 2 new repositories:
- applicant-services
- job-services
8.2 Cloud Map
Add a new namespace: czetsuyatech.lab
8.3 App Mesh
Add two virtual nodes:
dev-vn-application-services
dev-vn-job-services
Add two virtual services:
- dev-vs-application-services
- dev-vs-job-services
Add a new App Mesh Lab
8.4 ECS
Create two task definitions using Fargate. Add the following containers:
- applicant/job services
- aws-xray-daemon
- envoy (we need to define the following container variables)
- ENVOY_LOG_LEVEL=trace
- ENABLE_ENVOY_XRAY_TRACING=1
- XRAY_DAEMON_PORT=2000
Set these environment variables.
Don't forget to enable AppMesh.
Discovery Endpoints:
- applicant-services - http://applicant-services.czetsuyatech.lab:8081
- job-services - not needed
9. Service URLs
Applicant Services
Job Services
10. Source Code
Posted on October 24, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.