我们可以使用 Web-Sockets 进行微服务之间的通信吗? [英] Can we use Web-Sockets for Communication between Microservices?

查看:80
本文介绍了我们可以使用 Web-Sockets 进行微服务之间的通信吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个用例,我们有两个微服务,微服务 A 将一个长时间运行的任务推送到 微服务 B.

We have a use case where we have two microservices, Microservice A pushes a long-running task to Microservice B.

Microservice B 开始处理任务并不断更新任务的状态.现在 Microservice A 必须不断轮询 Microservice B 以获取任务状态的更新.

Microservice B starts processing the task and keeps updating the status of the task. Now Microservice A has to constantly poll Microservice Bfor an update on the status of the task.

我们当前的设置中没有队列.

We don't have queues in our current setup.

所以,我们想到在Microservice AMicroservice B 之间创建一个web socket,以便微服务B 可以将状态更新推送到<代码>微服务A.这种设计是否会违反 Web 套接字 的任何原则,并且与恒定轮询相比,它会是一种更好的方法吗?

So, We thought of creating a web socket between Microservice A and Microservice B so that Microservice B can push the status updates to Microservice A. Would this design violate any of the principles of Web sockets and also will it be a better approach compared to constant polling?

推荐答案

最推荐的选项是添加一个队列:

The most recommended option would be to add a queue :

  • 这将减少微服务 A 和微服务 B 之间的耦合
  • 这将允许微服务 C 也对任务状态/结果感兴趣,而无需对微服务 B 进行任何更改

如果你仍然使用 websocket,你将不得不考虑可扩展性的情况(如果你有两个 MicroserviceB 实例,调用哪一个),失败(如果一个服务失败,谁重新运行套接字...),以及其他一些.这就是为什么在微服务环境中进行异步调用不是最佳选择的原因.

If you still go for websocket, you would have to consider cases of scalability (what if you have two instances of MicroserviceB, which one to call), failures (what if one service fails, who re-runs the socket...), and a few others. That's why it's not the best option to do asynchronous calls in a Microservices environnement.

这篇关于我们可以使用 Web-Sockets 进行微服务之间的通信吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆