Over a million developers have joined DZone.

Spring Boot Akka Event Sourcing Starter (Part 3)

DZone 's Guide to

Spring Boot Akka Event Sourcing Starter (Part 3)

Part 3 of this event sourcing starter shows a working example of the creation in action, from configuration and implementation to serialization.

· Java Zone ·
Free Resource

Now I will share a working service example of how to use the event sourcing toolkit starter in practice. This example will show the following:

  1. How to configure and use the event sourcing starter with Spring Boot web applications.
  2. How to implement your aggregate entity using the API of the toolkit.
  3. How to define your entity flow using the execution flow API.
  4. How to configure your entity.
  5. How to configure your Akka system with Spring Boot.
  6. How to call your aggregates from your service and connect that to your DDD service's REST API.
  7. How to use Google Protobuf to serialize your events instead of Java serialization.
  8. The usage of Apache Ignite as your persistence event store with Akka Persistence.
  9. In Part 4, we will cover the summary and possible designs plus some special remarks.

Configure and Use the Event Sourcing Starter

In your Spring Boot app, add the event souring toolkit Maven dependency: https://gist.github.com/Romeh/15eef42a3556861eb5d288109ede6fbd

Implement Your Aggregate Entity (OrderManager)

Your order aggregate flow (OrderManager) implementation will be as follows:

Screen Shot 2018-04-26 at 11.56.43

The order manager aggregate class will extend the toolkit's persistent entity class and define the flow logic for command and event handlers inside your custom entity using the flow execution DSL exposed to you from the persistent entity. The flow will be as follows:

Untitled Diagram(2)

The code of the order manager class (with enough documentation for the flow DSL) is on GitHub: OrderManager Java Code.

Configure Your Persistent Aggregate Entity via the Toolkit API

As explained before, you just need to implement the interface PersistentEntityProperties, and the toolkit will auto-discover it for your entity cluster sharding and persistence configuration. The code reference for the config in the working sample is the following: The entity configuration.

How to Configure Your Akka System With Spring Boot

For this, you just need to add a reference for your Akka system config file in your Spring Boot app config file (application.yml) with the proper properties names. The toolkit will pick it up: https://gist.github.com/Romeh/155f9bd0bb8e310270559acd4c2b849f

Call Your Aggregates From Your Service and Connect Them to Your DDD Service's REST API

  • First, implement your order broker service that will have a reference for PersistentEntityBroker, which is provided by the toolkit to abstract the cluster sharding lookup for your entity. The Order broker is here: Order Broker.
  • Then, you use the non-blocking asynchronous PatternsCS ask to call the target entity using PersistentEntityBroker. The code snippet is here: https://gist.github.com/Romeh/21e9b76d2dd68171232dfbe83110bfc4
  • Then, from your REST API resource, you can call your broker to invoke the target command or query in an async, non-blocking way as well. A small code snapshot to show how it is done is here: https://gist.github.com/Romeh/228a22a83893e22d2c47ef783097e3f4.
  • When you run the app , there is a run-time Swagger for the different applications' REST APIs' documentation and testing on http://localhost:9595/swagger-ui.html, where you can test the order.

Use Protobuf to Serialize Your Events

As we know, Java serialization is not optimal for performance optimization, so here, I shared how you can use the Protobuf protocol to do the serialization.

  • You need to implement SerializerWithStringManifest from Akka Persistence, which, in our application, is OrderManagerSerializer, which will use the generated Protobuf builder class from the file below.
  • The Protobuf definition for the event classes is in the proto folder of the project: EventsAndCommands.proto.
  • Add the needed Maven build plugin that generates the needed code based on the schema definition above. The plugin configuration will be as follows: https://gist.github.com/Romeh/c7571a38507b1767eba9b80ee6903689.

Using Apache Ignite as Your Persistence Event Store With Akka Persistence

Here, I am going to use a custom Akka Persistence plugin with Apache Ignite that I created before: https://github.com/Romeh/akka-persistance-ignite. You can check it out for more technical details about it.

So I just added the needed Maven dependency, plus the needed Apache Ignite grid configuration for Akka Persistence. That is it. Now, when you build and run the application, it will start the Apache Ignite server node as well which will be used to store the events and snapshots in its own journals

Now in Part 4, we will go through some remarks and different architectures using that toolkit for event sourcing and CQRS services.

For your reference, here is the GitHub toolkit with the working sample URL.

akka ,spring boot ,event sourcing ,domain driven design ,event driven architecture ,apache ignite ,java ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}