我正在发展休息服务。有两种post方法。
第一种方法:
post /somethingdatabase
第二种方法:
post /something/{id}/detailscompleted
。
发展这种互动的最佳方式是什么?
第二种方法检查数据库是否为completion
。
发布于 2020-06-09 07:20:29
因为您知道在第二个端点中正在做什么,所以您知道要在数据库中检查哪些数据。
我建议只检查数据库中是否存在数据,然后继续执行您的逻辑。
如果您要为正在使用的端点安排任务,那么u将向应用程序添加不必要的状态充实。
始终尝试使您的端点尽可能无状态。
发布于 2020-06-10 16:01:51
我认为你可以使用事件驱动的架构来实现你想要的!
如下所示:
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传递给您的服务、函数或您在post实现中使用的执行此过程的任何实现。
这种体系结构需要一些很好的设计来隔离系统中各个部分的职责(接收帖子、验证有效负载、在db中存储、存储在临时存储中、处理某些内容和处理一些细节)。
请看参考文献:
https://microservices.io/patterns/data/event-driven-architecture.html
https://www.redhat.com/en/topics/integration/what-is-event-driven-architecture
https://stackoverflow.com/questions/62262838
复制相似问题