CQRS基础和应用程序结构

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.

我建议您已经具有一些Java和Spring Boot的基本概念,以更好地理解。

Event Sourcing

这种模式意味着每个应用程序状态更改都应由一个事件开始,并应将其存储。 例如,在一个电子商务网站上,用户单击要添加到购物车的产品后,此操作应发送一个包含产品ID和数量的JSON事件。

然后,将通过先前处理的事件的顺序来定义最终的应用程序状态。

使用事件源而不是仅维护最后一个应用程序状态的主要好处是:

  • 审核:您可以搜索存储的事件并准确验证哪些事件导致了下一个状态。重播:如果事件处理出错(例如:数据库已关闭),则可以再次触发失败的事件。复制:您可以在消息代理(例如:Apache Kafka)上发布事件,并在另一个微服务上使用。

CQRS

一种模式,建议将读取操作与写入或更新操作分开。 从创建单独的类到使用不同的数据库,这可以通过多种方式完成。

CQRS的主要好处是代码的逻辑划分,使其更加清晰。

Demo Application Overview

该应用程序将包含一个Spring Boot,它可以模拟电子商务结帐平台。 通过REST端点,您可以添加或编辑包含以下属性的产品:ID,名称和数量。

遵循CQRS概念,将有用于命令和查询的单独模块:

  • 指令方:将包含开机自检和放端点,将生成其各自的命令,这将被翻译成大事记. The 大事记 will be stored into a MongoDb database in a raw format, while also being processed to generate the final application state和stored in a Postgres database.查询端:将包含得到端点以获取我们电子商务购物车的最新快照。

Axon Framework为我们提供了有关CQRS和事件源实现的简单方法,从而易于理解数据流。

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

Dependencies

  • 弹簧启动启动器弹簧启动启动器-web弹簧启动启动器-data-mongodb弹簧启动启动器-data-jpaaxon-弹簧启动启动器轴突蒙哥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.

现在最重要的步骤是配置事件存储引擎Axon,在我们的示例中为MongoDb。 为此,请创建一个组态像这样的课:

@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();
    }
}

询问模块不需要任何特殊配置。 这将是一个简单的Spring Boot Web应用程序,可以从Postgres数据库获取视图模型。

现在运行您的应用程序,以检查是否一切正常。

Conclusion

在第一部分中,我们讨论了CQRS的基本概念并构建了演示应用程序。 在下一步中,我们将对命令模块。

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

发布了0 篇原创文章 · 获赞 0 · 访问量 394

猜你喜欢

转载自blog.csdn.net/cunxiedian8614/article/details/105690308
今日推荐