使用spring boot和axon实现saga模式-- 下

这是我们用Axon和Spring 实现Saga模式系列的第3部分。

我们在上一篇文章中开始实现了Saga模式。

回顾一下,下面是我们正在进行的大的章节。如果您是直接阅读这篇文章,我强烈建议您阅读本系列之前的文章。

在第1篇中,我们讨论了Saga的基本知识。我们还讨论了使用Axon框架和springboot来构建我们的应用程序。如果你不确定什么是Saga模式,我强烈建议你先阅读了这篇文章,然后回到本文中来。

接下来,在第2部分中,我们实现了应用程序的两个主要部分。我们基本上实现了订单服务和核心api。

在第3部分(本文)中,我们将继续我们的实现。我们将实现构成Saga模式实现一部分的其他服务。

最后,在第4部分,我们将测试我们的应用程序。

让我们继续下面的步骤。

Payment Service的实现

支付服务是另一个典型的Spring Boot应用程序。在我们的应用程序上下文中,该服务负责在创建订单之后创建发票。

下面是该服务的POM.xml依赖项。它与Order Service服务完全相似:

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- Axon -->
        <dependency>
            <groupId>org.axonframework</groupId>
            <artifactId>axon-spring-boot-starter</artifactId>
            <version>4.0.3</version>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>

        <!-- Swagger -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>

        <dependency>
            <groupId>com.progressivecoder.saga-pattern</groupId>
            <artifactId>core-apis</artifactId>
            <version>${project.version}</version>
        </dependency>

        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <version>1</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
</dependencies>

支付聚合

支付聚合是这项服务的核心。它存储与发票相关的信息以及与订单聚合的关系。

@Aggregate
public class InvoiceAggregate {

    @AggregateIdentifier
    private String paymentId;

    private String orderId;

    private InvoiceStatus invoiceStatus;

    public InvoiceAggregate() {
    }

    @CommandHandler
    public InvoiceAggregate(CreateInvoiceCommand createInvoiceCommand){
        AggregateLifecycle.apply(new InvoiceCreatedEvent(createInvoiceCommand.paymentId, createInvoiceCommand.orderId));
    }

    @EventSourcingHandler
    protected void on(InvoiceCreatedEvent invoiceCreatedEvent){
        this.paymentId = invoiceCreatedEvent.paymentId;
        this.orderId = invoiceCreatedEvent.orderId;
        this.invoiceStatus = InvoiceStatus.PAID;
    }
}

如您所见,它是一个非常简单的实体类。而在实际的生产案例中,这将有很多其他细节。然而,对于我们的示例应用程序,我故意保持它的简单性。

与订单服务类似,我们使用事件溯源来存储聚合信息。如果您想了解更多关于事件源的信息,我有一篇关于使用Axon和Spring Boot的事件源的详细文章。

为了进一步说明,支付聚合有一个用于处理Create Invoice Command的程序。当收到此命令时,将创建一个新的支付实例,并发布一个Invoice created事件。

Application Properties File

我们将在另一个端口上运行支付服务。为此,我们将server.port 配置到application.properties上。

另外,请注意,我们提供了一个application.name属性。这是一个很好的实践,因为这将帮助我们在应用程序连接到Axon服务器时轻松地识别它。

spring.application.name=payment-service
server.port=8081

运输服务的实现(Shipping Service Implementation)

运输服务的职责是创建一个运输(shipment)。该服务等待来自Order Management Saga的发出对应的命令。在收到命令后,它会创建一个运输(shipment)。

在这个服务中我们还是继续使用spring boot。

下面是这个Pom文 件的依赖:

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- Axon -->
        <dependency>
            <groupId>org.axonframework</groupId>
            <artifactId>axon-spring-boot-starter</artifactId>
            <version>4.0.3</version>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>

        <!-- Swagger -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>

        <dependency>
            <groupId>com.progressivecoder.saga-pattern</groupId>
            <artifactId>core-apis</artifactId>
            <version>${project.version}</version>
        </dependency>

        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <version>1</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
</dependencies>

运输聚合(Shipping Aggregate)

运输聚合是运输服务的核心。这个聚合存储了运输和订单之间的关系。

下面是他的定义:

@Aggregate
public class ShippingAggregate {

    @AggregateIdentifier
    private String shippingId;

    private String orderId;

    private String paymentId;

    public ShippingAggregate() {
    }

    @CommandHandler
    public ShippingAggregate(CreateShippingCommand createShippingCommand){
        AggregateLifecycle.apply(new OrderShippedEvent(createShippingCommand.shippingId, createShippingCommand.orderId, createShippingCommand.paymentId));
    }

    @EventSourcingHandler
    protected void on(OrderShippedEvent orderShippedEvent){
        this.shippingId = orderShippedEvent.shippingId;
        this.orderId = orderShippedEvent.orderId;
    }
}

可以看到,该聚合具有用于处理Create Shipping Command的能力。该命令由Order Management Saga发出来的。

一旦收到命令,聚合将发布订单运输事件。它还更新聚合实例上的值。

配置文件

spring.application.name=shipping-service
server.port=8082

设置Axon Server

我们的下一步是启动Axon服务器。在顶层设计上来看,Axon服务器主要用于促进我们的Saga模式实现中的各种服务之间的通信。如果你想知道更多,我有一个详细的文章介绍Axon服务器。

要运行Axon服务器,我们可以从AxonIQ站点下载zip格式的Axon服务器JAR文件。


图片.png

如您所见,有几种方式可以供我们选择。我们可以将Axon服务器作为Docker映像运行,也可以下载整个平台。但是,我们将使用Axon服务器作为ZIP文件的方法。

下载ZIP文件并将其解压缩到系统中的某个位置。然后,您只需进入该目录,并在命令提示符或终端中运行以下命令来运行Axon Server。

java -jar axonserver.jar

Axon服务器会在端口8024上运行。

图片.png

接下来,你可以访问http://localhost:8024。如果一切正常,您将看到下面的屏幕信息。

图片.png

启动其他服务

现在Axon服务器已经启动并运行,我们还需要启动作为Saga模式实现一部分的其他服务。它们是订单服务、支付服务和运输服务。

要启动这三个服务,我们可以在maven多模块(multi-maven)项目结构的根目录下使用下面的命令。

clean package spring-boot:run -pl order-service --also-make

clean package spring-boot:run -pl payment-service --also-make

clean package spring-boot:run -pl shipping-service --also-make

基本上,这里我们只是启动各个服务。also-make参数确保项目的依赖项也被绑定到创建的JAR文件中。

三个应用程序成功启动后,我们可以在Axon服务器仪表板的概述页面中看到它们。

图片.png

基本上,该视图表示所有服务现在都已注册到Axon服务器的服务中。

在Commands view 卡片上,我们还可以看到在我们的Saga中定义的所有命令。在这个页面上,我们还可以跟踪Saga中发出的命令的数量,等等。


图片.png

总结

到此,我们实现Saga模式的项目就可以进行测试了。然而,由于这篇文章已经很长了,我们将在下一篇文章中讨论测试。

至此,我们基本上完成了Order Management Saga的Saga模式实现。代码可以在Github上找到,以供参考。

在下一篇文章中,我们将继续测试我们的Order Management Saga。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,670评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,928评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,926评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,238评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,112评论 4 356
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,138评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,545评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,232评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,496评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,596评论 2 310
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,369评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,226评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,600评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,906评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,185评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,516评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,721评论 2 335

推荐阅读更多精彩内容