简体   繁体   English

服务器和客户端中都包含Java NIO?

[英]Java NIO in both server and client?

I am new to Java NIO.I have a small doubt. 我是Java NIO的新手,我对此表示怀疑。 If I use the NIO instead of a socket client, should the server also be using NIO or does it not matter? 如果我使用NIO而不是套接字客户端,那么服务器是否也应该使用NIO还是没关系?

I am concerned about application scalability. 我担心应用程序的可伸缩性。 I am looking at around 500-1000 client requests per seconds per server. 我正在查看每台服务器每秒大约500-1000个客户端请求。 Since I would send my data to atleast three different servers, I am ideally looking at around 1500 client requests per second. 由于我会将数据发送到至少三个不同的服务器,因此理想情况下,我每秒查看大约1500个客户端请求。 For this, I already have a socket pool implementation in place which does a fairly decent job. 为此,我已经有一个套接字池实现,可以完成相当不错的工作。

What I have is a pool of socket connections for each server.Each thread picks up an available socket connection from the pool and sends it to the server 我所拥有的是每个服务器的套接字连接池。每个线程从该池中获取一个可用的套接字连接并将其发送到服务器

I am trying to find out if NIO can help or is better in any way than a socket client. 我试图找出NIO是否可以提供帮助,或者比套接字客户端更好。 How about blocking ? 封锁如何? Normal client would either block or timeout. 普通客户端将阻止或超时。

It doesn't matter. 没关系 The TCP stream will be unaffected by your choice of NIO. TCP流将不受您选择的NIO的影响。

imho you won't get any more performance out of your client by using NIO. 恕我直言,使用NIO不会使您的客户端获得更多性能。 The bottleneck will be either the server or the network. 瓶颈将是服务器或网络。 Also, NIO is more complex and it's likely that you'll get something wrong that will hurt performance. 另外,NIO更为复杂,您可能会遇到一些会影响性能的错误信息。

I would start by doing a socket implementation to get it up and running more quickly. 我将从做一个套接字实现开始,以使其更快地运行。 If you follow some common patterns/principles like Single Responsibility Principle it would be easy to switch implementation later on (if you or your users have proved that your applications performance is the bottleneck which I find highly unlikely). 如果您遵循一些常见的模式/原则,例如“单一职责原则”,那么以后很容易切换实现(如果您或您的用户已证明您的应用程序性能是瓶颈,我认为这是非常不可能的)。

Update 更新

What NIO or any other asynchronous framework does it to let multiple operations share the same threads. NIO或任何其他异步框架执行的操作是为了让多个操作共享同一线程。 Having one thread per connection when dealing with a lot of connections is a waste of resources since all threads will not be active all the time. 在处理大量连接时,每个连接只有一个线程会浪费资源,因为所有线程不会一直处于活动状态。

Using NIO for a client will not give you any benefits if you have just a few connections. 如果您只有几个连接,那么将NIO用于客户端将不会给您带来任何好处。 Having a thread+socket per connection doesn't consume that much resources and it will be easier to handle the connection. 每个连接有一个线程+套接字不会消耗那么多资源,并且处理连接会更容易。

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

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