使用netty做服务器占用CPU较高,求破
刚学netty不久,现做的一个项目,采用netty框架做服务器,多个客户端分不同端口会连续不停地发一些不定长的数据包过来,需解码后作存储,现linux下,CPU5%-8%...
刚学netty不久,现做的一个项目,采用netty框架做服务器,多个客户端分不同端口会连续不停地发一些不定长的数据包过来,需解码后作存储,现linux下,CPU5%-8%左右,开了服务器后,CPU20%+,开启一个客户端发数据过来后,流量越大,CPU占用越高,100KB/s左右大概CPU30%-40%,流量到1M/s时,CPU占用到60%+了,求分析,看是哪出问题,须将CPU占用降低啊
1、服务器:
//创建服务器
ServerBootstrap bootstrap = new ServerBootstrap(
new NioServerSocketChannelFactory(
Executors.newCachedThreadPool(),//boss线程池
Executors.newCachedThreadPool()//worker线程池
,500));
bootstrap.setOption("reuseAddress", true);//端口重用
bootstrap.setOption("child.tcpNoDelay", true);//无延迟
bootstrap.setOption("child.receiveBufferSize", 10485760);//设置接收缓冲区大小
bootstrap.setOption("child.bufferFactory",
new HeapChannelBufferFactory(USE_LITTLE_ENDIAN ? ByteOrder.LITTLE_ENDIAN : ByteOrder.BIG_ENDIAN)
);
ChannelPipelineFactory pf = new ProbeServerPipelineFactory(this, probeDb, READER_TIMEOUT, WRITER_TIMEOUT);
bootstrap.setPipelineFactory(pf);
//监听多个端口
String [] port = LISTEN_PORT.split(",");
for(int i=0;i<port.length;i++) {
bootstrap.bind(new InetSocketAddress(Integer.valueOf(port[i])));
}
2、解码class ProbeMessageDecoder extends ReplayingDecoder<VoidEnum>
String tz = Commands.decodeString(buffer, TZ_LEN);
if(!"TZ".equals(tz)) {
return null;
}
int dataLength = buffer.readUnsignedShort();
short operationCode = buffer.readShort();
byte errorCode = buffer.readByte();
byte flags = buffer.readByte();
short sequenceNumber = buffer.readShort();
ChannelBuffer pdu = buffer.readBytes(dataLength); 展开
1、服务器:
//创建服务器
ServerBootstrap bootstrap = new ServerBootstrap(
new NioServerSocketChannelFactory(
Executors.newCachedThreadPool(),//boss线程池
Executors.newCachedThreadPool()//worker线程池
,500));
bootstrap.setOption("reuseAddress", true);//端口重用
bootstrap.setOption("child.tcpNoDelay", true);//无延迟
bootstrap.setOption("child.receiveBufferSize", 10485760);//设置接收缓冲区大小
bootstrap.setOption("child.bufferFactory",
new HeapChannelBufferFactory(USE_LITTLE_ENDIAN ? ByteOrder.LITTLE_ENDIAN : ByteOrder.BIG_ENDIAN)
);
ChannelPipelineFactory pf = new ProbeServerPipelineFactory(this, probeDb, READER_TIMEOUT, WRITER_TIMEOUT);
bootstrap.setPipelineFactory(pf);
//监听多个端口
String [] port = LISTEN_PORT.split(",");
for(int i=0;i<port.length;i++) {
bootstrap.bind(new InetSocketAddress(Integer.valueOf(port[i])));
}
2、解码class ProbeMessageDecoder extends ReplayingDecoder<VoidEnum>
String tz = Commands.decodeString(buffer, TZ_LEN);
if(!"TZ".equals(tz)) {
return null;
}
int dataLength = buffer.readUnsignedShort();
short operationCode = buffer.readShort();
byte errorCode = buffer.readByte();
byte flags = buffer.readByte();
short sequenceNumber = buffer.readShort();
ChannelBuffer pdu = buffer.readBytes(dataLength); 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询