Table of Contents |
---|
...
The Micronaut framework is a modern full-stack toolkit backed by the Micronaut Foundation™. This microservice Framework is designed for building modular, easily testable microservice applications. The Micronaut framework utilizes innovative techniques at compile time to preconfigure much of the application's initialization logic, dramatically decreasing startup time and runtime memory requirements. This is one of the reasons that Micronaut services can be a great fit for edge devices with constrained environments.
The Micronaut framework was developed by the creators of the Grails® framework and takes inspiration from lessons learned over the years building real-world applications from monoliths to microservices using Spring, Spring Boot, and the Grails framework.
The Micronaut framework aims to provide all the tools necessary to build full-featured microservice applications, including:
- Dependency injection (DI) and inversion of control (IoC)
- Sensible defaults and auto-configuration
- Configuration and configuration sharing
- Service discovery
- HTTP routing
- HTTP client with client-side load-balancing
At the same time, the Framework aims to avoid the downsides of frameworks like Spring, Spring Boot, and Grails by providing:
- Fast startup time
- Reduced memory footprint
- Minimal use of reflection
- Minimal use of runtime proxies
- Easy testing
Historically, frameworks such as Spring and Grails were not designed to run in scenarios such as serverless functions, Android apps, or low memory footprint microservices. In contrast, the Micronaut framework is designed to be suitable for all of these scenarios.
This goal is achieved through the use of Java’s annotation processors, which are usable on any JVM language that supports them, as well as an HTTP Server and Client built on Netty. In order to provide a similar programming model to Spring and Grails, these annotation processors precompile the necessary metadata in order to perform DI, define aspect-oriented programming (AOP) proxies, and configure your application to run in a microservices environment.
Many of the Micronaut APIs are heavily inspired by the Spring and Grails frameworks. This is by design and aids in bringing developers up to speed quickly.
Compare Micronaut to Quarkus, Spring Boot
...
https://microservices.io/patterns/data/event-sourcing.html
problem
How to reliably/atomically update the database and publish messages/events?
forces
2PC - 2 phase commit not an option
solution
A good solution to this problem is to use event sourcing. Event sourcing persists the state of a business entity such an Order or a Customer as a sequence of state-changing events. Whenever the state of a business entity changes, a new event is appended to the list of events. Since saving an event is a single operation, it is inherently atomic. The application reconstructs an entity’s current state by replaying the events.
...
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
public class Order extends ReflectiveMutableCommandProcessingAggregate<Order, OrderCommand> { private OrderState state; private String customerId; public OrderState getState() { return state; } public List<Event> process(CreateOrderCommand cmd) { return EventUtil.events(new OrderCreatedEvent(cmd.getCustomerId(), cmd.getOrderTotal())); } public List<Event> process(ApproveOrderCommand cmd) { return EventUtil.events(new OrderApprovedEvent(customerId)); } public List<Event> process(RejectOrderCommand cmd) { return EventUtil.events(new OrderRejectedEvent(customerId)); } public void apply(OrderCreatedEvent event) { this.state = OrderState.CREATED; this.customerId = event.getCustomerId(); } public void apply(OrderApprovedEvent event) { this.state = OrderState.APPROVED; } public void apply(OrderRejectedEvent event) { this.state = OrderState.REJECTED; } |
Here is an example of an event handler in the CustomerService
that subscribes to Order
events:
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
@EventSubscriber(id = "customerWorkflow") public class CustomerWorkflow { @EventHandlerMethod public CompletableFuture<EntityWithIdAndVersion<Customer>> reserveCredit( EventHandlerContext<OrderCreatedEvent> ctx) { OrderCreatedEvent event = ctx.getEvent(); Money orderTotal = event.getOrderTotal(); String customerId = event.getCustomerId(); String orderId = ctx.getEntityId(); return ctx.update(Customer.class, customerId, new ReserveCreditCommand(orderTotal, orderId)); } } |
...
- Highly maintainable and testable
- Loosely coupled
- Independently deployable
- Organized around business capabilities
- Owned by a small team
The microservice architecture enables the rapid, frequent and reliable delivery of large, complex applications. It also enables an organization to evolve its technology stack.
Potential Value Opportunities
...
Potential Challenges
Candidate Solutions
Simple Test Environment for the Tutorials above
- Local Linux VM or MACOS
- or
- AWS Linux EC2 instance
- open JDK17
- Tomcat
- Data Store - H2, MYSQL, CouchDB, Postgres
Candidate solutions to test
- REST API - HelloName async
- GRPC - HelloName async
- Message API - send, receive async from a message broker store
- GraphQL test
- Groovy Data Science test
Step-by-step guide for Example
...