NestJS tcp微服务负载均衡

发布于 2025-01-19 13:30:59 字数 213 浏览 2 评论 0原文

我有一个 Nestjs 应用程序,如下:

  1. 网关 - HTTP
  2. 微服务 1 - TCP (ms1)
  3. 微服务​​ 2 - TCP (ms2)

网关调用 ms1 和 ms2。 ms1 和 ms2 是相同的重复微服务。

我想加载 ms1 和 ms2 之间的余额。 在nestjs中如何实现这一点?

谢谢

I have a nestjs app as follows:

  1. Gateway - HTTP
  2. Microservice1 - TCP (ms1)
  3. Microservice2 - TCP (ms2)

Gateway calling ms1 and ms2.
ms1 and ms2 are the same duplicated microservice.

I would like to load the balance between ms1 and ms2.
How do accomplish that in nestjs?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟火散人牵绊 2025-01-26 13:30:59

许多云平台都有内置的负载平衡器,我建议使用它们。从我的角度来看,这个TCP故事存在一个根本的问题。我认为负载平衡器无法正确分发TCP数据包,因为它不知道哪个数据包是逻辑单元的。

由于Nestjs文档说:“ ...从根本上是使用与HTTP不同的传输层的应用程序”,因此您可以简单地创建一个单独运行但仍可以通过HTTP访问的新巢应用程序,因为差异仅在IN中无论如何,运输协议和云内置负载平衡器都可以处理HTTP。

这意味着:

  1. 网关-HTTP
  2. Microservice -A(n实例)-HTTP

通信通过HTTPModule进行:

@Injectable()
export class SomeService {
  constructor(private httpService: HttpService) {}

  findAll(): Observable<AxiosResponse<Entity[]>> {
    return this.httpService.get('http://host-ms-a:port/my-entities');
  }
}

Many cloud platforms have a built-in load balancer and I recommend using them. There is a fundamental problem with this TCP story from my point of view. I think that a load balancer could not distribute the TCP packets correctly because it would not know which packets belong together as a logical unit.

Since the NestJS documentation says: "... a microservice is fundamentally an application that uses a different transport layer than HTTP", you could simply create a new Nest application that runs separately but is still accessible via HTTP, because the difference is only in the transport protocol and the cloud built-in load balancer can handle HTTP in any case.

That mean:

  1. Gateway - HTTP
  2. Microservice-a (n instances) - HTTP

Communication goes via the HttpModule:

@Injectable()
export class SomeService {
  constructor(private httpService: HttpService) {}

  findAll(): Observable<AxiosResponse<Entity[]>> {
    return this.httpService.get('http://host-ms-a:port/my-entities');
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文