简体   繁体   English

编写Java服务器以处理多个并发客户端

[英]Writing Java server to handle multiple simultaneous clients

I need some advice in building a Java server that handles multiple clients at the same time. 我需要一些建议来同时处理多个客户端的Java服务器。 The clients need to remain connected for fairly long periods of time. 客户需要在相当长的时间内保持连接。 I'm currently using blocking IO and spawning a thread to read from each client that connects to the server, but this is obviously not scalable. 我目前正在使用阻塞IO并生成一个线程来从连接到服务器的每个客户端读取,但这显然是不可扩展的。

I've found a few options, including using Selector or Executor with fixed size thread pools. 我找到了一些选项,包括使用具有固定大小线程池的Selector或Executor。 I am not too familiar with either one, so which would be the best solution here? 我对这两者都不太熟悉,所以这是最好的解决方案吗? Thanks! 谢谢!

It depends on your definition of scalable. 这取决于您对可伸缩性的定义。 The system you have described with a single thread per connection is scalable up to hundreds may be even a couple of thousand concurrent connections, it will hit a wall at some point. 您描述的每个连接使用单个线程的系统可扩展到数百个,甚至可能是几千个并发连接,它会在某个时刻出现问题。

Your question says that your clients connect and stay connected for an extended period of time, it would be possible to have a single IO thread to handle the reading and writing, but have the processing of the request dispatched to another thread using an Executor. 您的问题是您的客户端连接并保持连接一段时间,可能有一个IO线程来处理读取和写入,但是使用Executor将请求的处理分派到另一个线程。

There are frameworks/servers that are already written to handle this sort of event driven design. 已经编写了用于处理这种事件驱动设计的框架/服务器。 Have a look at: 看一下:

It's worth noting that the world is full of failed startups & software products that had really scalable architecture. 值得注意的是,世界上充满了失败的初创公司和具有真正可扩展架构的软件产品。 Scaling is a nice problem to have, better to have the problem than not to have it and no customers. 扩展是一个很好的问题,更好地解决问题而不是没有客户。

using multiple threads is scalable. 使用多个线程是可扩展的。 Apache for example does this, and some sites using it get many visitors. 例如Apache就是这样做的,一些使用它的网站会吸引很多访问者。 However, another approach would indeed be using selector, though I have no experience using it. 但是,另一种方法确实会使用选择器,尽管我没有使用它的经验。

After all, this seems like a question, which religion is the best. 毕竟,这似乎是一个问题,哪种宗教是最好的。

there's a lot of framework for this kind of job, examples 这个工作有很多框架,例子

  1. Netty 网状
  2. Apache MINA Apache MINA

Independently of scalability every server application has it's limits. 独立于可扩展性,每个服务器应用程序都有其限制。 By using blocking IO, one of your limits will be the number of threads that the VM can spawn because the approach you take is "one-thread-per-client". 通过使用阻塞IO,您的一个限制将是VM可以生成的线程数,因为您采用的方法是“每个客户端一个线程”。 With NIO (of which Selector is one of the classes), the approach is "one-thread-per-request" which will run out of threads much latter. 使用NIO(其中Selector是其中一个类),该方法是“每个请求一个线程”,后者将耗尽线程。

Horizontal scalability ( http://en.wikipedia.org/wiki/Scalability#Scale_horizontally_vs._vertically ) of your app will not depend on either of these choices. 应用程序的水平可伸缩性( http://en.wikipedia.org/wiki/Scalability#Scale_horizo​​ntally_vs._vertically )将不依赖于这两种选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM