如何设计具有多个持久连接的服务器

发布于 2024-07-26 02:00:47 字数 334 浏览 6 评论 0原文

我正在设计一个应用程序,其中许多客户端连接到中央服务器。 该服务器保持这些连接,每半小时发送一次保持活动状态。 服务器有一个嵌入式 HTTP 服务器,它为客户端连接提供接口(例如 http://server/isClientConnected ?id=id)。 我想知道解决这个问题的最佳方法是什么。 我当前的实现是用Java实现的,我只有一个以ID为键的Map,但是为每个连接启动一个线程,我不知道这是否真的是最好的方法。 任何指示将不胜感激。
谢谢,
艾萨克·沃勒

I am designing a application where many clients connect to a central server. This server keeps these connections, sending keep-alives every half-hour. The server has a embedded HTTP server, which provides a interface to the client connections (ex. http://server/isClientConnected?id=id). I was wondering what is the best way to go about this. My current implementation is in Java, and I just have a Map with ID's as the key, but a thread is started for each connection, and I don't know if this is really the best way to do this. Any pointers would be appreciated.
Thanks,
Isaac Waller

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

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

发布评论

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

评论(4

柠檬心 2024-08-02 02:00:47

使用 java.nio 包,如本页所述: 使用 Java NIO 构建高度可扩展的服务器。 另请仔细阅读此页:高度可扩展的基于 NIO 的服务器的体系结构。

就我个人而言,我不会打扰 NIO 内部结构,并使用像 Apache MINAxSocket。 NIO 很复杂,而且很容易以非常隐晦的方式出错。 如果你想让它“正常工作”,那就使用框架。

Use the java.nio package, as described on this page: Building Highly Scalable Servers with Java NIO. Also read this page very carefully: Architecture of a Highly Scalable NIO-Based Server.

Personally I'd not bother with the NIO internals and use a framework like Apache MINA or xSocket. NIO is complicated and easy to get wrong in very obscure ways. If you want it to "just work", then use a framework.

擦肩而过的背影 2024-08-02 02:00:47

通过每个连接一个线程,您通常可以在一台机器上扩展到大约 10,000 个连接。 对于 Windows 32 计算机,您可能会达到 1,000 个连接左右的限制。

为了避免这种情况,您可以更改程序的设计,或者可以横向扩展。 您必须权衡开发成本和硬件成本。

每个用户单个线程,具有单个连续连接通常是最简单的编程模型。 我会坚持使用这个模型,直到您达到当前硬件的极限。 那时,我会决定要么更改代码,要么添加更多硬件。

With a single thread per connection you can usually scale up to about 10,000 connections on a single machine. For a Windows 32 machine, you probably will hit a limit around 1,000 connections.

To avoid this, you can either change the design of your program, or you can scale out (horizontal). You have to weight the cost of development with the cost of hardware.

The single thread per user, with a single continuous connection is usually the easiest programming model. I would stick with this model until you reach the limits of your current hardware. At that point, I would decide to either change the code, or add more hardware.

墨洒年华 2024-08-02 02:00:47

如果客户端将连接很长一段时间,则为每个客户端分配一个线程可能会出现问题。 服务器上的每个线程都需要一定量的资源(例如堆栈内存)。

您可以使用 Jetty Continuations 通过使用异步 servlet,以更少的线程处理客户端请求。

If the clients will be connected for long periods of time, allocating a thread per client can be problematic. Each thread on the server requires a certain amount of resources (memory for the stack, for example).

You could use Jetty Continuations to handle the client request with fewer threads by using asynchronous servlets.

划一舟意中人 2024-08-02 02:00:47

阅读有关 Reactor 模式的更多信息。 Java 中有一个实现(它使用通道而不是客户端的线程)。
它易于实施且非常高效。

Read more about the the Reactor pattern. There is an implementation for that in Java (it uses channels instead of thread for client).
It is easy to implement and very efficient.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文