开发者社区> 问答> 正文

最近看netty发现发送并发请求数总是多于接受到的请求数而且数字不固定,请大神帮忙? 400 报错

最近看netty发现发送并发请求数总是多于接受到的请求数而且数字不固定,请大神帮忙? 400 报错

看了一下网上的例子自己做了一下然后使用jmeter 测试了一下发现测试总是不对查资料有说系统问题的有说是java虚拟机内存小的设置以后问题也没有得到解决。

EchoServer.java

package com.gmbsh.Server;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;

public class EchoServer {
	public void bind(int port) throws Exception{
		EventLoopGroup bossGroup = new NioEventLoopGroup();
		EventLoopGroup wordGroup = new NioEventLoopGroup();
		try {
			ServerBootstrap bootstrap = new ServerBootstrap();
			bootstrap.group(bossGroup,wordGroup)
			.channel(NioServerSocketChannel.class)
//			.option(ChannelOption.SO_BACKLOG, 100)
//			.childOption(ChannelOption.SO_KEEPALIVE, true)
//			.childOption(ChannelOption.SO_REUSEADDR, true)
//			.handler(new LoggingHandler(LogLevel.INFO))
			.childHandler(new ChannelInitializer<SocketChannel>() {
					@Override
					protected void initChannel(SocketChannel arg0) throws Exception {
						// TODO Auto-generated method stub
						ByteBuf bytebuf = Unpooled.copiedBuffer("$_".getBytes());//定义一个分隔符进行区分信息的条数
						arg0.pipeline().addLast(new DelimiterBasedFrameDecoder(1024, bytebuf));//加载格式识别信息
//						arg0.pipeline().addLast("readTimeoutHanlder",new ReadTimeoutHandler(50));//加载格式识别信息
						arg0.pipeline().addLast(new StringDecoder());//自动将信息转换成字符串
						arg0.pipeline().addLast(new EchoServerHandler());//自定义解码
					}
			});
			ChannelFuture f = bootstrap.bind(port).sync();//绑定端口同步等待成功
			f.channel().closeFuture().sync();//等待服务端口关闭
		} finally{
			bossGroup.shutdownGracefully();
			wordGroup.shutdownGracefully();
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int port = 9000;
		try {
			new EchoServer().bind(port);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}



EchoServerHandler.java

package com.gmbsh.Server;





import java.net.InetSocketAddress;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;

public class EchoServerHandler extends ChannelHandlerAdapter{
	static int counter=1;
	static int contentco=1;
	static int closeCount = 1;
	@Override
	public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
		// TODO Auto-generated method stub
		System.out.println("链接数量:"+ counter++);

	}

	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
		System.out.println("收到数据数量:"+ contentco++);
		// TODO Auto-generated method stub

	}

	@Override
	public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
		// TODO Auto-generated method stub
		System.out.println("ChannelHandlerContext flush");
		ctx.flush();
	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
		// TODO Auto-generated method stub
		System.out.println("ChannelHandlerContext is close:"+ ++closeCount);
		cause.printStackTrace();
		ctx.close();//发生异常关闭链路
	}
}



展开
收起
爱吃鱼的程序员 2020-05-29 20:31:32 828 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    你把数据贴出来啊

    2020-05-29 20:31:33
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载