逝于寂寞 2016-05-10 02:53 采纳率: 0%
浏览 2467

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以上就就会出现无法连接的状态了……求解!!

  • 写回答

1条回答

  • loglay 2019-10-10 16:16
    关注

    是客户端内存打满了吗?

    评论

报告相同问题?

悬赏问题

  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型