我正在编写一个应用程序,其中客户端和服务器都是使用Netty编写的,服务器应该(显然)同时支持许多客户端。我试图通过创建共享一个EventLoopGroup
的1000个客户端并在一台机器上运行所有内容来测试它。
最初,由于超时,我有多个客户端有时无法连接。在服务器上增加SO_TIMEOUT_MILLIS
并在服务器上将SO_BACKLOG
设置为numberOfClients
此问题。但是,我仍然得到connection reset by peer
或
io.netty.channel.AbstractChannel$AnnotatedConnectException: syscall:getsockopt(..) failed: Connection refused: localhost/127.0.0.1:8080
at io.netty.channel.unix.Socket.finishConnect(..)(Unknown Source)
Caused by: io.netty.channel.unix.Errors$NativeConnectException: syscall:getsockopt(..) failed: Connection refused
... 1 more
客户端有时(特别是当我增加客户端数量时)。服务器端LoggingHandler
的输出似乎没有显示任何尝试从这些通道绑定到客户端的端口进行连接。尝试使用Nio*
代替Epoll*
类型也没有任何帮助。
是否还需要设置其他选项以允许更多连接(可能在服务器端,如果它真的是一个拒绝/重置连接)?
为了简化这种情况,我删除了自己的逻辑,因此客户端只需连接websocket并在握手成功后关闭通道。 根据我的理解,Netty通常不会处理10000个同时没有做多次的websocket连接的问题。
ulimit -n
是1000000,ulimit -u
是772794,所以两者都不是问题。
这是代码(在Kotlin中,但Java翻译应该清楚):
package netty
import io.netty.bootstrap.Bootstrap
import io.netty.bootstrap.ServerBootstrap
import io.netty.channel.*
import io.netty.handler.codec.http.HttpClientCodec
import io.netty.handler.codec.http.HttpObjectAggregator
import io.netty.handler.codec.http.HttpServerCodec
import io.netty.handler.codec.http.websocketx.WebSocketClientHandshakerFactory
import io.netty.handler.codec.http.websocketx.WebSocketClientProtocolHandler
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler
import io.netty.handler.codec.http.websocketx.WebSocketVersion
import io.netty.handler.codec.http.websocketx.extensions.compression.WebSocketClientCompressionHandler
import io.netty.handler.codec.http.websocketx.extensions.compression.WebSocketServerCompressionHandler
import io.netty.handler.logging.LogLevel
import io.netty.handler.logging.LoggingHandler
import org.junit.Test
import java.net.URI
@Suppress("OverridingDeprecatedMember")
class NettyTest {
private fun channelInitializer(f: (Channel) -> Unit) = object : ChannelInitializer<Channel>() {
override fun initChannel(ch: Channel) {
f(ch)
}
}
private val numberOfClients = 10000
private val maxHttpContentLength = 65536
@Test
fun manyClients() {
// set up server
val bossLoopGroup = EpollEventLoopGroup(1)
val workerLoopGroup = EpollEventLoopGroup()
val serverChannelFactory = ChannelFactory { EpollServerSocketChannel() }
val clientLoopGroup = EpollEventLoopGroup()
val clientChannelFactory = ChannelFactory { EpollSocketChannel() }
val serverChannel = ServerBootstrap().channelFactory(serverChannelFactory).group(bossLoopGroup, workerLoopGroup).handler(LoggingHandler(LogLevel.DEBUG)).childHandler(channelInitializer {
it.pipeline().addLast(
HttpServerCodec(),
HttpObjectAggregator(maxHttpContentLength),
WebSocketServerCompressionHandler(),
WebSocketServerProtocolHandler("/", null, true, maxHttpContentLength)/*,
myServerHandler*/
)
}).option(ChannelOption.SO_BACKLOG, numberOfClients).bind("localhost", 8080).sync().channel()
println("Server started")
try {
// set up clients
val url = URI("ws://localhost")
val futures = List(numberOfClients) { clientIndex ->
val handshaker = WebSocketClientHandshakerFactory.newHandshaker(url, WebSocketVersion.V13, null, true, null)
val promise = clientLoopGroup.next().newPromise<Channel>()
val connectFuture = Bootstrap().channelFactory(clientChannelFactory).group(clientLoopGroup).handler(channelInitializer {
it.pipeline().addLast(
HttpClientCodec(),
HttpObjectAggregator(maxHttpContentLength),
WebSocketClientCompressionHandler.INSTANCE,
WebSocketClientProtocolHandler(handshaker, true),
object : ChannelInboundHandlerAdapter() {
override fun userEventTriggered(ctx: ChannelHandlerContext, evt: Any) {
if (evt == WebSocketClientProtocolHandler.ClientHandshakeStateEvent.HANDSHAKE_COMPLETE) {
promise.setSuccess(ctx.channel())
println("Client $clientIndex handshake successful")
}
}
override fun exceptionCaught(ctx: ChannelHandlerContext, cause: Throwable) {
promise.setFailure(cause)
}
})
}).option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 120000).connect("localhost", 8080)
Pair(promise, connectFuture)
}
for ((promise, connectFuture) in futures) {
connectFuture.sync()
try {
promise.sync()
} finally { connectFuture.channel().close().sync() }
}
} finally {
try { serverChannel.close().sync() } finally {
workerLoopGroup.shutdownGracefully()
bossLoopGroup.shutdownGracefully()
clientLoopGroup.shutdownGracefully()
}
}
}
}
答案 0 :(得分:0)
只有1个线程可以接受传入连接:bossLoopGroup = EpollEventLoopGroup(1)
。也许这还不足以接受客户端连接群。
我建议以老板,工人和客户的身份共享一个EventLoopGroup
,并使用默认的线程数(Netty请考虑使用内核数)。因此,您不会有未充分使用/过度使用的线程池。
如果要使用不同的线程池运行测试,请创建具有显式大小的测试,为bossLoopGroup
使用多个线程。