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
我正在设计一个应用程序,其中许多客户端连接到*服务器。该服务器保持这些连接,每隔半小时发送一次保持活动。服务器有一个嵌入式HTTP服务器,它为客户端连接提供接口(例如http:// server / isClientConnected?id = id)。我想知道最好的方法是什么。我目前的实现是用Java编写的,我只有一个带有ID的Map作为键,但是为每个连接启动了一个线程,我不知道这是否真的是最好的方法。任何指针将不胜感激。谢谢,艾萨克沃勒
4 个解决方案
#1
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.
使用java.nio包,如本页所述:使用Java NIO构建高度可伸缩的服务器。还要非常仔细地阅读本页:高度可扩展的基于NIO的服务器的体系结构。
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.
就个人而言,我不打扰NIO内部并使用像Apache MINA或xSocket这样的框架。 NIO很复杂,很容易以非常模糊的方式出错。如果您希望它“正常工作”,那么使用框架。
#2
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.
通过每个连接一个线程,您通常可以在一台计算机上扩展到大约10,000个连接。对于Windows 32计算机,您可能会达到大约1,000个连接的限制。
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.
每个用户的单个线程,通过单个连续连接通常是最简单的编程模型。我会坚持使用这个模型,直到达到当前硬件的极限。那时,我会决定更改代码,或添加更多硬件。
#3
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.
您可以使用Jetty Continuations通过使用异步servlet以更少的线程处理客户端请求。
#4
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.
阅读有关Reactor模式的更多信息。在Java中有一个实现(它使用通道代替客户端的线程)。它易于实施且非常高效。
#1
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.
使用java.nio包,如本页所述:使用Java NIO构建高度可伸缩的服务器。还要非常仔细地阅读本页:高度可扩展的基于NIO的服务器的体系结构。
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.
就个人而言,我不打扰NIO内部并使用像Apache MINA或xSocket这样的框架。 NIO很复杂,很容易以非常模糊的方式出错。如果您希望它“正常工作”,那么使用框架。
#2
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.
通过每个连接一个线程,您通常可以在一台计算机上扩展到大约10,000个连接。对于Windows 32计算机,您可能会达到大约1,000个连接的限制。
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.
每个用户的单个线程,通过单个连续连接通常是最简单的编程模型。我会坚持使用这个模型,直到达到当前硬件的极限。那时,我会决定更改代码,或添加更多硬件。
#3
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.
您可以使用Jetty Continuations通过使用异步servlet以更少的线程处理客户端请求。
#4
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.
阅读有关Reactor模式的更多信息。在Java中有一个实现(它使用通道代替客户端的线程)。它易于实施且非常高效。