Guided Compound Exercise - Spring Microservice Design Patterns - Banking System #172
Replies: 9 comments
|
Hi Akash, Attaching the compound exercise assignment. I'm facing compilation issues. Could you please check and help me out to resolve the issue. Error: main java class file is non-project file Thanks, |
|
Hi Akash, Please find attached the code base:
Code Base: Name: Arul |
|
Hi Akash. Here are the 4 services:- Gateway url to start : Using swagger: The details of test can be found under README.md of transaction-service. Thanks |
|
Hi Akash, Please find the codebase below: Swagger URLs: Thanks, |
|
Hi @akash-coded , Sharing my code in zip file for Guided Compound Exercise - Spring Microservice Design Patterns - Banking System Thanks, |
|
Hi Akash, Here is the saga orchestrator pattern demo. Orchestrator will be connecting via feign and gettin response on topic. https://github.com/samik2023/saga-orc-orchestrator-service Iniiating order Regards |
|
Hi @akash-coded , Here is the saga implementation. https://github.com/obulix/MicSrv-Saga |
|
Hi Akash, Please find the attached project Thanks, |
|
hi @akash-coded , please find attached compound exercise solution . Please let me know if anything else is missing in it . I will update the code. |
Uh oh!
There was an error while loading. Please reload this page.
Let’s choose a banking domain focusing on a few primary operations:
Scenario Overview:
Imagine a banking system with 3 microservices:
Main Functionality Workflow:
Part 1: API Gateway, Proxy Pattern & Synchronous Communication
1. API Gateway using Spring Cloud Gateway:
Include dependencies in
pom.xml:application.ymlfor API Gateway:2. Proxy Pattern:
Implement Feign Clients in services for synchronous communications.
Part 2: Event-Driven Architecture (EDA) & Asynchronous Communication
3. Asynchronous Communication:
@Asyncannotations andThreadPoolTaskExecutorto execute methods in the background.Part 3: Implementing Saga Pattern
4. Saga Pattern:
a. TransactionService:
b. AccountService:
c. NotificationService:
Implementation Snippets:
TransactionService:
Initiate transaction and emit an event:
AccountService:
Handle transaction events, update account balance, and emit events:
NotificationService:
Listen for transaction success/failure events and send notifications:
Part 4: Detailed Microservice Implementation
4.1 AccountService
Dependencies:
application.properties:
Model: Account
Repository: AccountRepository
4.2 TransactionService
Model: Transaction
Repository: TransactionRepository
4.3 NotificationService
Model: Notification
Part 5: SAGA Pattern & Asynchronous Communication
To manage data consistency across services in a microservices architecture, the SAGA Pattern is employed. In this context:
TransactionServicecreates a PENDING transaction and sends aStartTransactionEvent.AccountServicelistens for this event, processes the transaction, and sends either aTransactionSuccessEventorTransactionFailureEvent.NotificationServicelistens for these events and sends appropriate notifications.Event Publishing & Listening:
EventPublisher (Common to all services):
EventListeners:
Part 6: Asynchronous Communication
Methods annotated with
@Asyncmust be declared in beans, and Spring’s@EnableAsyncannotation should be enabled:Using
@Async("taskExecutor")on methods will execute them in a separate thread:Part 7: Building, Testing, and Deploying the Services
Each microservice should be tested independently, ensuring unit tests validate business logic and integration tests validate inter-service communication.
Testing can involve using
TestContainersto create isolated database instances, usingMockMvcfor API testing, and employing@Mockand@Spyfor unit testing.Repository Structure:
Each microservice will have its dedicated repository with a similar structure:
Part 8: Proxy Pattern and API Gateway Integration
8.1 Proxy Pattern Implementation
In the banking scenario, let's implement a
LoggingProxyto manage and log the transaction flow within services without intruding into business logic.In
TransactionService, a proxy can log transaction statuses and parameters:In real-world scenarios, we'd implement a dynamic proxy instead of a static one to manage multiple methods without creating a specific proxy method for each original method.
8.2 API Gateway
We will use Spring Cloud Gateway to implement the API Gateway pattern.
Dependencies:
application.yaml
In the configuration above, requests to
/api/transactionwill be routed to theTransactionService,/api/accountwill route to theAccountService, and/api/notificationwill route to theNotificationService.Part 9: Securing Microservices
Security is paramount, particularly in a domain like banking. We could secure our services using OAuth2.0 and OpenID Connect for Authentication and Authorization.
Implementing a robust security mechanism ensures safe, secure communication and data transfer between microservices, and protects sensitive user data.
Part 10: Handling Data Consistency and Compensation Transactions
When working with SAGA Patterns, particularly in scenarios like banking, where consistency is crucial, compensation transactions should be in place in case a process fails mid-transaction.
For instance:
Part 11: Monitoring and Observability
Incorporate Spring Boot Actuator, Prometheus, and Grafana to create a monitoring system for microservices.
Dependencies:
application.properties:
By integrating Prometheus and Grafana with Spring Boot, developers gain insights into microservices' health, performance, and errors, ensuring immediate attention to potential issues.
Conclusion
The integration of various patterns and technologies like the Proxy pattern, API Gateway, SAGA, EDA, and Asynchronous Communication within a complex banking domain demonstrates a powerful, resilient, and scalable microservices architecture using Spring Boot.
Each step is essential to understand how to build scalable, maintainable, and observable systems. Test each part thoroughly to understand the interactions and ensure system reliability and consistency.
All reactions