英文:
spring microservices multiple tasks handle
问题
I've a question related to microservices world. Let's assume I've two microservices, microservice A does a request to microservice B. Microservice B perform some "long" operations and makes a request to another microservice C. In result microservice A waits for result from microservice B. How to handle such a long operation if the microservice A is "connected" to front end service, where user initiates the operation and waits for the result of that operation.
在微服务世界中,我有一个与微服务相关的问题。假设我有两个微服务,微服务A向微服务B发出请求。微服务B执行一些“长时间”操作,并向另一个微服务C发出请求。结果微服务A等待来自微服务B的结果。如果微服务A与前端服务“连接”,用户发起操作并等待操作的结果,如何处理这样长时间的操作。
Is there any good way to handle long-running tasks in those microservices? What would you suggest to me with that case to be the most efficient and scalable in Spring boot? Thank you in advance.
在这些微服务中,有没有处理长时间任务的好方法?对于这种情况,在Spring Boot中,您有什么建议以实现最有效和可扩展的解决方案?提前感谢您。
英文:
I've a question related to microservices world. Let's assume I've two microservices, microservice A does a request to microservice B. Microservice B perform some "long" operations and makes a request to another microservice C. In result microservice A waits for result from microservice B. How to handle such a long operation if the microservice A is "connected" to front end service, where user initiates the operation and waits for the result of that operation.
Is there any good way to handle long-running tasks in those microservices? What would you suggest to me with that case to be the most efficient and scalable in Spring boot? Thank you in advance.
答案1
得分: 0
- 同步
这是一个高级别的情况,使用 RestTemplate
、WebClient
、FeignClient
等实现,其中你的微服务 A 将等待来自微服务 B 的响应。
适用于快速操作,覆盖了大多数用例。
- 异步
正如 @taleodor 提到的,另一种方法是实现对其他微服务的异步调用,它将仅返回一个 运行 id
或 确认 id
,以指示长时间运行的过程已启动。要获取该过程的状态,您可以考虑通过消息队列,如 ActiveMQ
或 Apache Kafka
来实现,它们基于生产者/消费者或 发布者/订阅者 (pub/sub)
的概念工作,在这里您的长时间运行的微服务将在完成后发布事件,而微服务 A 或 B 将订阅该事件并读取响应!
除了消息队列,您的微服务 A 还可以实现一个 定时任务
,以根据如何实现来检查进度完成或资源更新!
英文:
On a high-level, let's segregate the API calls in two major types
- Synchronous
- Asynchronous
Synchronous
This is an ideal situation implemented using RestTemplate
, WebClient
, FeignClient
etc where in your Microservice A would wait for response from Microservice B.
Ideal for quick operations and covers majority use case
Asynchronous
As @taleodor mentioned, the other way would be to implement an async
call to other Microservice, which would only return a Run id
or Acknowledgement id
to indicate the long running process started. To get status of that process, you may think of implementing it through a Message queue
such as ActiveMQ
or Apache Kafka
which works on the concept of producer/consumer or Publisher/subscriber (pub/sub)
where in your long running microservice will publish the event once completed, and Microservice A or B will subscribe to that event and read the response !
Apart of message queues, your Microservice A can also implement a scheduled task
to check for progress completion or resource updation based on how to implement it !
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论