我有一个 Saga,Saga 在特定事件上向不同的微服务发送命令。 某些微服务可能比其他微服务停机更多,因此,我想使用 RetryScheduler 配置 CommandGateway,并实现我自己的 IntervalRetryScheduler,以便我能够对每个 RuntimeException 进行重试,但仅限于某些 Axon 命令(这是一个很大的帮助 Why does the RetryScheduler in Axon Framework not retry after a NoHandlerForCommandException? )。
一切都按预期进行,我唯一担心的是,某些命令将使用默认 CommandGateway 发送,而另一些命令将使用内置自定义重试功能的自定义 CommandGateway 发送,是否会出现任何问题?
目前,即使对于不重试的命令,我也不会使用自定义 CommandGateway
我采用了独特的 CommandGateway beans 方法
@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
bean,可以使用或不使用RetryScheduler
- 具体说明 Sagas 中抛出的重试异常类型,以便您的
RetryScheduler
可以针对重试"is"或“否”进行调整。 - 构建自定义命令网关(如所述 here )。从那时起,当涉及到某些命令的行为方式时,您可以按照自己的意愿具体化。
但是,我认为此 Axon Usergroup 中建议的解决方案根据您的情况,该帖子更值得关注。 总结建议的方法,其想法是使用 Deadline 在 Saga 本身中安排重试。 Axon提供的机制。
这样,如果微服务不可用(我认为这是您要解决的问题),您可以让命令失败,并让 Saga 本身在一定的超时后重试该操作。
希望这对您有帮助!
关于Axon框架仅为某些命令实现IntervalRetryScheduler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57904576/