我正在开发休息服务。 有两种发布方法。
第一种方法:
post /something
第二种方法:
post /something/{id}/details
发展这种互动的最佳方法是什么?
答案 0 :(得分:0)
由于您知道第二个端点正在处理的内容,因此您知道要在数据库中检查哪些数据。
我建议仅检查数据库中是否存在数据,然后继续执行逻辑。
如果您要为正在使用的端点安排任务,则会为应用程序增加不必要的状态填充。
始终尝试使端点尽可能无状态。
答案 1 :(得分:0)
我认为您可以使用事件驱动的体系结构来实现您想要的目标!
赞:
post /something
1. Receive the first part of data
2. Save to database
3. Return the answer with id
4. Make async actions with data
5. Raise an event with async-something-process-done
第二种方法:
post /something/{id}/details
1. Receive the second part of data with id;
2. Checks that first method is completed (I don't know
man! Create a flag, attribute... whatever!);
3. If the something was previously processed then
process whole details data using a service, function
or what ever you have implemented to do this process.
Else
store whole details data in a temporary storage.
现在...在应用程序的其他部分,您可以添加一个事件监听器,它将监听async-something-process-done
,并且您必须实现一个处理程序以获取something
的ID,然后进行搜索对于存储在临时存储中的something details
,如果有一些详细信息,则将从临时存储中检索到的something details
传递给您的服务,功能或您在后期实现中使用的实现此过程的方法
这种体系结构需要一些良好的设计来分离系统各部分的职责(接收帖子,验证有效负载,存储在db中,存储在临时存储中,处理某些事情和处理某些细节)。
看看这些参考文献。
https://microservices.io/patterns/data/event-driven-architecture.html
https://www.redhat.com/en/topics/integration/what-is-event-driven-architecture