从轴突聚合调用第三方服务是个好主意吗

Is it good idea call third party services from axon aggregate

我有一个轴突集合体。它处理命令,并且在应用事件之前必须调用第三方服务来验证某些参数,根据此验证我是否应用事件。这是好的做法吗?或者我在发送命令之前进行了验证?

@Aggregate
public class SomeAggregate {
[...]
   @CommandHandler
   public void someHandler() {
     if(thirdPartyService.invoke) {
       apply(...)
     }
   }
}

如果它是非阻塞(域)服务,类似于有限状态机,则可以从聚合内调用,因为它很可能很快就会完成。 但是,'third party service' 对我来说听起来像是外拨电话,这可能需要一些时间。

当 Axon 加载聚合时,它会阻塞聚合,因此其他线程无法更改其上的 state/handle 命令。 第三方服务意味着聚合被阻止的时间更长。

因此,我建议您不要在聚合中调用第三方服务。 要么在进入聚合之前调用服务,要么在命令处理完成后执行补偿操作以恢复决策。两者中哪一个在您的场景中最有意义,取决于您的领域。然而,我认为通过第三方服务进行“预验证”是最合理的选择。

视情况而定。如果您的第三方服务有副作用并且不是幂等的,那么我不确定该怎么做(我仍在努力弄清楚)。

如果它确实有副作用,那么我希望聚合阻塞/锁定并使用聚合的状态/历史来仔细管理这样的交互

@Aggregate
public class SomeAggregate {
[...]
   @CommandHandler
   public void someHandler() {
    /*
        Reason about whether its appropriate to send a request. 
        e.g. if a request has been sent but no response has been received,
        then depending on the third party service it might be in an indeterminate state.
        Instead of trying to interact with it, it might be better 
        to notify someone instead. 
    */
    rejectIfNotSafe()      

     /*
         Effectively locks this interaction / any other instances in the same path
         should get a concurrent modification exception when trying to commit this event.
     */
     commit(new ThirdPartyServiceRequested())

     if(thirdPartyService.invoke) {
       apply(...)
     }
   }
}

但是 Axon 的 'unit of work' 意味着在命令处理程序完成之前不会发布/提交发出的事件,所以我们不能以这种方式守卫。

有什么想法吗?