我一个操作中可能会同时调用多个依赖,在调用时比如某个服务调用失败了,那么是保证所有外部服务调用成功,这个操作才算成功,还是直接跳过这个操作。比如,我在博问中提了一个问题,这里面假设后端我的问题已经提交成功了,但是园豆悬赏服务却失败了。那么来说,这问题是提问成功了,只是没有把园豆加上去。
目前考虑了用类似于消息队列的模式来进行预防,调用失败时,将数据存入数据裤,然后通过轮询进行再次执行。
参考:
Challenge #3: How to achieve consistency across multiple microservices
A good solution for this problem is to use eventual consistency between microservices articulated through event-driven communication and a publish-and-subscribe system.
Implementing event-based communication between microservices (integration events)
You can use events to implement business transactions that span multiple services, which gives you eventual consistency between those services. An eventually consistent transaction consists of a series of distributed actions. At each action, the microservice updates a business entity and publishes an event that triggers the next action.
提交问题后向事件总线发出一个事件“谁提问悬赏了多少园豆”,园豆服务订阅了这个事件,只有园豆服务成功处理了这个事件,对应的订阅才会被移除,园豆服务挂了,未处理的事件留在事件总线中,园豆服务恢复后把这些滞留的事件处理掉
@dudu: 事件总线好像和消息队列的处理方式差不多,去了解了解。