Canonical URL: Republished from munonye.com. Full microservices hub: munonye.com/microservices-tutorials/
What you'll build
-
Command side:
CreateOrderCommand→OrderAggregate→OrderCreatedEvent - Event store: Axon Server
-
Query side:
OrderProjection→ H2 read model → REST GET
Prerequisites: Java 17+, Spring Boot 3.x, Axon Server
Step 1 — Dependencies
<dependency>
<groupId>org.axonframework</groupId>
<artifactId>axon-spring-boot-starter</artifactId>
<version>4.9.3</version>
</dependency>
Step 2 — Configure Axon Server
axon.axonserver.servers=localhost:8124
spring.application.name=order-service
Run ./axonserver — UI at http://localhost:8024.
Step 3 — Aggregate
@Aggregate
public class OrderAggregate {
@AggregateIdentifier private String orderId;
@CommandHandler
public OrderAggregate(CreateOrderCommand cmd) {
AggregateLifecycle.apply(new OrderCreatedEvent(cmd.getOrderId(), cmd.getProductId()));
}
@EventSourcingHandler
public void on(OrderCreatedEvent event) {
this.orderId = event.getOrderId();
}
}
Step 4 — Query projection
@EventHandler
public void on(OrderCreatedEvent event) {
repo.save(new OrderView(event.getOrderId(), event.getProductId(), "CREATED"));
}
Step 5 — REST
-
POST
/orders→CommandGateway.send(CreateOrderCommand) -
GET
/orders/{id}→ read fromOrderViewRepository
Full code walkthrough with curl tests: read on munonye.com →
Top comments (0)