CQRS foundation and application structure

Introduction

In this article, I will give an overview of the microservices patterns Event Sourcing and Command Query Responsibility Segregation (CQRS) patterns. Then I will show how to apply these concepts on a Spring Web Application using Axon Framework. You can check the final code on Github.

I suggest that you already have some basic concepts of Java and Spring Boot to better understand.

Event Sourcing

This pattern means that every application state change should start with an event, and it should be stored. For example, on an e-commerce website, after the user clicks the product to be added to the shopping cart, this operation should send a JSON event containing the product ID and quantity.

Then, the final application state will be defined by the sequence of previously processed events.

The main benefits of using an event source instead of just maintaining the state of the last application are:

  • Audit: You can search stored events and accurately verify which events led to the next state. Replay: If the event processing error (for example: the database is closed), you can trigger the failed event again. Replication: You can post events on a message broker (for example: Apache Kafka) and use them on another microservice.

CQRS

One mode, it is recommended to separate read operations from write or update operations. From creating separate classes to using different databases, this can be done in many ways.

The main benefit of CQRS is the logical division of the code, making it clearer.

Demo Application Overview

The application will include a Spring Boot, which can simulate an e-commerce checkout platform. Through the REST endpoint, you can add or edit products with the following attributes: ID, name and quantity.

Following the CQRS concept, there will be separate modules for commands and queries:

  • Instructor: will include POST and release endpoints, will generate their respective commands, which will be translated into memorabilia. The memorabilia will be stored into a MongoDb database in a raw format, while also being processed to generate the final application state And stored in a Postgres database. The query end: will contain the endpoint to get the latest snapshot of our e-commerce shopping cart.

Axon Framework provides us with a simple method of CQRS and event source implementation, which makes it easy to understand the data flow.

Diagram representing the data flow from REST endpoint to event generation and aggregate change

Dependencies

  • Spring boot starter spring boot starter-web spring boot starter-data-mongodb spring boot starter-data-jpaaxon-spring boot starter axon mongo PostgreSQL

We will be using version 4.1.2 of axon-spring-boot-starter, which requires a separate Axon Server running to start our Spring Boot Application. To simplify this tutorial, we won't make use of Axon Server, so we can remove its dependency by declaring on build.gradle file:

compile('org.axonframework:axon-spring-boot-starter:4.1.2') {
  exclude group: 'org.axonframework', module: 'axon-server-connector'
}

Command module configuration

We will start by creating the Command module. If you're using IntelliJ, you can do it by clicking File > New > Module, select Gradle, then Java. In ArtfactId type commandside then Finish. This step will create a folder called commandside in the root folder of your application containing a single build.gradle file. Check its configuration here on Github repository.

Next, you need to create the structure of a Spring Boot application inside commandside folder by creating a path internally containing your main package src/main/java/com/example/project/command. where it will be placed your @SpringBootApplication annotated class and all other package related classes. Check the final result here.

Inside src/main/resources, create an application.yml file to place your Postgres configuration. You can follow my example here.

The most important step now is to configure the event storage engine Axon, in our case MongoDb. To do this, create a class with a configuration like this:

@Configuration
public class AxonConfig {

    // The `MongoEventStorageEngine` stores each event in a separate MongoDB document
    @Bean
    public EventStorageEngine storageEngine(MongoClient client) {
        return MongoEventStorageEngine
                .builder()
                .mongoTemplate(DefaultMongoTemplate
                        .builder()
                        .mongoDatabase(client)
                        .build())
                .build();
    }
}

The interrogation module does not require any special configuration. This will be a simple Spring Boot web application that can get the view model from the Postgres database.

Now run your application to check if everything is working.

Conclusion

In the first part, we discussed the basic concepts of CQRS and built a demo application. In the next step, we will order the module.

from: https://dev.to//fabiothiroki/cqrs-basics-and-application-structure-2ac2

Published 0 original articles · liked 0 · visits 394

Guess you like

Origin blog.csdn.net/cunxiedian8614/article/details/105690308