Snapshotting Aggregates in Axon – 轴突中的聚集物快照

最后修改: 2021年 9月 27日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this article, we’ll be looking at how Axon supports aggregate snapshotting.

在这篇文章中,我们将探讨Axon如何支持聚合快照。

We consider this article to be an expansion of our main guide on Axon. As such, we’ll utilize both Axon Framework and Axon Server again. We’ll use the former in this article’s implementation, and the latter is the event store and message router.

我们认为这篇文章是我们关于Axon的主要指南的拓展。因此,我们将同时利用Axon FrameworkAxon Server再次。我们将在本文的实现中使用前者,而后者是事件存储和消息路由器。

2. Aggregate Snapshotting

2.聚合快照

Let’s start by understanding what snapshotting an aggregate means. When we start with Event Sourcing in an application, a natural question is how do I keep sourcing an aggregate performant in my application? Although there are several optimization options, the most straightforward is to introduce snapshotting.

让我们先来了解一下快照聚合的含义。当我们开始使用应用程序中的事件源时,一个自然的问题是如何在应用程序中保持聚合的性能?虽然有几个优化选项,但最直接的方法是引入快照。

Aggregate snapshotting is the process of storing a snapshot of the aggregate state to improve loading. When snapshotting is incorporated, loading an aggregate before command handling becomes a two-step process:

聚合快照是存储聚合状态的快照以改善加载的过程当快照被纳入时,在命令处理之前加载聚合变成了一个两步的过程:

  1. Retrieve the most recent snapshot, if any, and use it to source the aggregate. The snapshot carries a sequence number, defining up until which point it represents the aggregate’s state.
  2. Retrieve the remainder of events starting from the snapshot’s sequence, and source the rest of the aggregate.

If snapshotting should be enabled, a process that triggers the creation of snapshots is required. The snapshot creation process should ensure the snapshot resembles the entire aggregate state at its creation point. Lastly, the aggregate loading mechanism (read: the repository) should first load a snapshot, and then, any remaining events.

如果要启用快照,需要一个触发快照创建的过程。快照创建过程应确保快照与创建点的整个聚合状态相类似。最后,聚合加载机制(阅读:存储库)应首先加载快照,然后是任何剩余的事件。

3. Aggregate Snapshotting in Axon

3.轴突中的聚合快照

Axon Framework supports snapshotting of aggregates. For a complete overview of this process, check out this section of Axon’s reference guide.

Axon Framework支持对聚合体进行快照。关于这个过程的完整概述,请查看这个 Axon参考指南的章节。

Within the framework, the snapshotting process consists out of two main components:

在框架内,快照过程由两个主要部分组成:

The Snapshotter is the component that constructs the snapshot for an aggregate instance. By default, the framework will use the entire aggregate’s state as the snapshot.

Snapshotter是为聚合实例构建快照的组件默认情况下,框架将使用整个聚合的状态作为快照。

The SnapshotTriggerDefinition defines the trigger towards the Snapshotter to construct a snapshot. A trigger can be:

SnapshotTriggerDefinition定义了对Snapshotter的触发,以构建一个快照。一个触发器可以是:

  • after a set amount of events, or
  • once loading takes a certain amount, or
  • at set moments in time.

The storage and retrieval of snapshots reside with the event store and the aggregate’s Repository. To that end, the event store contains a distinct section to store the snapshots. In Axon Server, a separate snapshots file reflects this section.

快照的存储和检索由事件存储和聚合的存储库负责。为此,事件存储包含一个单独的部分来存储快照。在Axon服务器中,一个单独的快照文件反映了这个部分。

Snapshot loading is done by the repository, consulting the event store for this. As such, loading an aggregate, and incorporating a snapshot, are wholly taken care of by the framework.

快照加载是由资源库完成的,为此要咨询事件存储。因此,加载一个聚合,并纳入一个快照,是完全由框架来处理的。

4. Configuring Snapshotting

4.配置快照

We will be looking at the Order domain introduced in the previous article. Snapshot construction, storage, and loading are already taken care of by the Snapshotter, event store, and repository.

我们将着眼于订单域在前文中介绍的。快照的构建、存储和加载已经由Snapshotter、事件存储和资源库负责。

Hence, to introduce snapshotting to the OrderAggregate, we only have to configure the SnapshotTriggerDefinition.

因此,为了给OrderAggregate引入快照,我们只需要配置SnapshotTriggerDefinition

4.1. Defining a Snapshot Trigger

4.1.定义一个快照触发器

 Since the application uses Spring, we can add a SnapshotTriggerDefinition to the Application Context. To that end, we add a Configuration class:

由于应用程序使用Spring,我们可以在应用程序上下文中添加一个SnapshotTriggerDefinition。为此,我们添加了一个Configuration类:

@Configuration
public class OrderApplicationConfiguration {
    @Bean
    public SnapshotTriggerDefinition orderAggregateSnapshotTriggerDefinition(
      Snapshotter snapshotter,
      @Value("${axon.aggregate.order.snapshot-threshold:250}") int threshold) {
        return new EventCountSnapshotTriggerDefinition(snapshotter, threshold);
    }
}

In this case, we chose the EventCountSnapshotTriggerDefinitionThis definition triggers the creation of a snapshot once the event count for an aggregate matches the ‘threshold.’ Note that the threshold is configurable through a property.

在这种情况下,我们选择了EventCountSnapshotTriggerDefinition一旦聚合的事件计数符合 “阈值”,该定义就会触发快照的创建。请注意,该阈值可通过一个属性进行配置。

The definition also needs the Snapshotter, which Axon adds to the Application Context automatically. Hence, it can be wired as a parameter when constructing the trigger definition.

该定义还需要Snapshotter,Axon自动将其添加到应用上下文中。因此,在构建触发器定义时,它可以作为一个参数被连接起来。

Another implementation we could’ve used, is the AggregateLoadTimeSnapshotTriggerDefinition. This definition triggers the creation of a snapshot if loading the aggregate exceeds the loadTimeMillisThreshold. Lastly, since it’s a snapshot trigger, it also requires the Snapshotter to construct the snapshot.

另一个我们可以使用的实现是AggregateLoadTimeSnapshotTriggerDefinition。如果加载总量超过loadTimeMillisThreshold,该定义将触发快照的创建。最后,由于这是一个快照触发器,它还需要Snapshotter来构造快照。

4.2. Using the Snapshot Trigger

4.2.使用快照触发器[/span>

Now that the SnapshotTriggerDefinition is part of the application, we need to set it for the OrderAggregate. Axon’s Aggregate annotation allows us to specify the bean name of the snapshot trigger. 

现在,SnapshotTriggerDefinition是应用程序的一部分,我们需要为OrderAggregate设置它。Axon的Aggregate注解允许我们指定快照触发器的Bean名称。

Setting the bean name on the annotation will automatically configure the trigger definition for the aggregate:

在注解上设置Bean名称将自动配置聚合的触发器定义:

@Aggregate(snapshotTriggerDefinition = "orderAggregateSnapshotTriggerDefinition")
public class OrderAggregate {
    // state, command handlers and event sourcing handlers omitted
}

By setting the snapshotTriggerDefinition to equal the bean name of the constructed definition, we instruct the framework to configure it for this aggregate.

通过将snapshotTriggerDefinition设置为等于构建的定义的Bean名称,我们指示框架为这个聚合配置它。

5. Snapshotting in Action

5.行动中的快照

The configuration sets the trigger definition threshold to ‘250.’ This setting means that the framework constructs a snapshot after 250 events are published. Although this is a reasonable default for most applications, this prolongs our test.

该配置将触发器定义阈值设置为’250’。这一设置意味着框架在250个事件发布后构建一个快照尽管这对大多数应用来说是一个合理的默认值,但这延长了我们的测试时间。

So to perform a test, we will adjust the axon.aggregate.order.snapshot-threshold property to ‘5.’ Now, we can more easily test whether snapshotting works.

所以为了进行测试,我们将调整axon.aggregation.order.snapshot-threshold属性为’5’。现在,我们可以更容易地测试快照是否有效。

To that end, we start Axon Server and the Order application. After issuing sufficient commands to an OrderAggregate to generate five events, we can check if the application stored a snapshot by searching in the Axon Server Dashboard.

为此,我们启动Axon服务器和订单应用程序。在向一个OrderAggregate发出足够的命令以产生五个事件后,我们可以通过在Axon Server Dashboard中搜索来检查该应用程序是否存储了一个快照。

To search for snapshots, we need to click the ‘Search button in the left tab, select ‘Snapshots’ in the top left corner, and click the orange ‘Search’ button to the right. The table below should show a single entry like this:

为了搜索快照,我们需要点击左边标签的 “搜索 “按钮,在左上角选择 “快照”,然后点击右边的橙色 “搜索 “按钮。下面的表格应该显示一个这样的条目:

6. Conclusion

6.结语

In this article, we looked at what aggregate snapshotting is and how Axon Framework supports this concept.

在这篇文章中,我们探讨了什么是聚合快照以及Axon Framework如何支持这一概念。

The only thing required to enable snapshotting is the configuration of a SnapshotTriggerDefinition on the aggregate. The job of creation, storage, and retrieval of snapshots, is all taken care of for us.

启用快照的唯一要求是在聚合上配置一个SnapshotTriggerDefinition。创建、存储和检索快照的工作,都由我们来处理。

You can find the implementation of the Order application and the code snippets over on GitHub. For any additional questions on this topic, also check out Discuss AxonIQ.

你可以在GitHub上找到订单应用程序的实现和代码片段over。关于这个主题的任何其他问题,也请查看Discuss AxonIQ.