The API Gateway Design Pattern of Microservices Architecture
Microservices architectures often involve multiple services that clients need to interact with. Directly managing these individual service endpoints can be complex for clients.
API Gateways aggregate multiple microservices into a single entry point, simplifying client interactions by providing a unified API.
- Java Development Kit (JDK 8 or above)
- Maven
- IDE like STS(Spring Tool Suite) or Eclipse
We will create 3 microservices :
- Account Service -> Manages Bank Account creation and fetching
- Fund Transfer Service -> Manages trasnfer of funds for accounts
- Banking Gateway Service -> This will be our API Gateway Service that will have both Account and Fund Transfer services registered with it.
-
Create Microservices : Create three separate Spring Boot projects (either via Spring Initializr or your preferred method).
-
Add Dependencies : For API Gateway Service, add following dependencies :
For Account and Fund Transfer service, add following dependencies :
-
Business Logic :
- Account Service - Create a REST API to get an account by account number.
- Fund Transfer Service - Create a REST API to get transaction details for specific account.
- Banking Gateway Service(API Gateway Service) - Create a basic API Gateway Service that will call above 2 services upon getting client request.
-
API Gateway Configuration : Configure the API Gateway service with URI and Header based routing for Account and Fund Transfer Service respectively.
-
Run the services : Start all 3 services i.e. Account Service, Fund Transfer Service and API Gateway Service.
-
Test and connect h2 DB connection : Connect to Account Service DB using jdbc:h2:mem:AccountService_DB on h2 console. Connect to Account Service DB using jdbc:h2:mem:FundTransfer_DB on h2 console.
-
For testing Gateway, make a few calls on both Account and Fund Transfer Services using following URLs that use port 8080(Default API Gateway port):
Account Service POST call - http://localhost:8080/banking/account/createAccount
Fund Transfer Service POST call - http://localhost:8080/banking/fundTransfer/newFundTransferRequest
Account Service GET call - http://localhost:8080/banking/account/getBankAccounts
Fund Transfer Service GET call - http://localhost:8080/banking/fundTransfer/getTransactions
Make sure to make use of Header along with Body while making a POST call on Banking Gateway Service.
Customize the behavior of services by editing the respective application.properties file. Adjust settings such as port, logging, and error handling.
Account Controller :
Account Model :
FundTransfer Controller :
FundTransfer model :
Contributions are welcome!
For questions or feedback, please email at tathoded@gmail.com OR dnyaneshsunilrao.tathode@genpact.com.