Axon Framework 仅为某些命令实现 IntervalRetryScheduler

Axon Framework Implementing IntervalRetryScheduler only for some commands

我有一个 Saga,Saga 在特定事件上向不同的微服务发送命令。 一些微服务可能比其他微服务宕机更多,所以我想配置一个带有 RetryScheduler 的 CommandGateway 并实现我自己的 IntervalRetryScheduler 以便我能够对每个 RuntimeException 进行重试,但只对某些 Axon 命令进行重试(这是帮了大忙 ).

一切都按预期工作,我唯一担心的是,如果某些命令将使用默认 CommandGateway 发送,而某些命令将使用内置自定义重试的自定义 CommandGateway 发送,是否会出现任何问题?

现在我不会使用自定义 CommandGateway,即使是没有重试的命令也是如此

我采用了独特的 CommandGateway bean 方法

    @Bean
    public CommandGateway commandGateway(){
        Configurer configurer = DefaultConfigurer.defaultConfiguration();
        CommandBus commandBus = configurer.buildConfiguration().commandBus();
        CommandGateway commandGateway = DefaultCommandGateway.builder().commandBus(commandBus).build();
        return commandGateway;
    }

    @Bean
    public CommandGateway commandGatewayWithRetry(){
        Configurer configurer = DefaultConfigurer.defaultConfiguration();
        CommandBus commandBus = configurer.buildConfiguration().commandBus();
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
        RetryScheduler rs = IntervalRetrySchedulerImpl.builder().retryExecutor(scheduledExecutorService).maxRetryCount(5).retryInterval(1000).build();
        CommandGateway commandGateway = DefaultCommandGateway.builder().commandBus(commandBus).retryScheduler(rs).build();
        return commandGateway;
    }

从这里开始,您可以从多个角度出发。

如果您打算使用 RetryScheduler/CommandGateway 想法,您可以执行以下任一操作。

  • 配置不同的 CommandGateway beans,有或没有 RetryScheduler
  • 具体说明从 Sagas 抛出的重试异常类型,以便您的 RetryScheduler 可以针对重试是或否进行调整。
  • 构建自定义命令网关(如 here 所述)。从那时起,当涉及到某些命令的行为方式时,您可以根据自己的意愿尽可能具体。

但是,我认为此 Axon Usergroup post 中建议的解决方案在您的情况下更值得遵循。 总结建议的方法,想法是使用 Axon 提供的 Deadline 机制在 Saga 本身中安排重试。

这样,如果微服务不可用(我假设这是您要解决的问题),您可以让命令失败,并让 Saga 本身在一定时间后重试该操作。

希望对您有所帮助!