2 icebounder icebounder 于 2016.05.10 10:53 提问

netty测试高并发的问题

先上代码,大家帮忙看下。
服务端:
package Server;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
public class HelloServer {
private static final int port = 7878;

public void start() throws InterruptedException {

  EventLoopGroup bossGroup = new NioEventLoopGroup();
  EventLoopGroup workGroup = new NioEventLoopGroup();
  try {

        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workGroup);
        b.channel(NioServerSocketChannel.class);
        b.option(ChannelOption.SO_BACKLOG, 1024);
        b.childHandler(new SCCHandler());

        // 绑定端口
        ChannelFuture f = b.bind(port).sync();

        // 等待服务端监听端口关闭
        f.channel().closeFuture().sync();

    } finally {

        // 优雅的退出
        bossGroup.shutdownGracefully();
        workGroup.shutdownGracefully();
    }

}

public static void main(String[] args) {
try {
new HelloServer().start();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

客户端测试高并发的DEMO:

package Client;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;

public class HelloClient {
public static void main(String[] args) throws Exception {
ScheduledExecutorService scheduledThreadPool = Executors
.newScheduledThreadPool(3000);
for (int i = 0; i < 3000; i++) {
scheduledThreadPool.schedule(new Runnable() {
public void run() {
try {
System.out.println("go");
new HelloClient("192.168.2.88", 7878).start();
} catch (Exception e) {
System.out.println("压力测试失败->" + e.toString());
}
}
}, 200, TimeUnit.MILLISECONDS);
}
System.out.println("压力测试提交完成");
}

private final String host;
private final int port;

public HelloClient(String host, int port) {
    this.host = host;
    this.port = port;
}

public void start() throws Exception {
    System.out.println("链接服务器开始");
    EventLoopGroup group = new NioEventLoopGroup();
    try {
        Bootstrap b = new Bootstrap();
        b.group(group);
        b.channel(NioSocketChannel.class);

        // b.remoteAddress(new InetSocketAddress(host, port));

        b.option(ChannelOption.TCP_NODELAY, true);
        b.handler(new ClientCCHandler());

        // ChannelFuture f = b.connect().sync();

        ChannelFuture f = b.connect(host, port);

        f.channel().closeFuture().sync();
    } finally {
        group.shutdownGracefully();
    }
}

}

到5000以上就就会出现无法连接的状态了……求解!!

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!