Netty
#服务端
#客户端
EventLoop
EventLoopServer
EventLoopClient
Java
@Slf4j
publicclassEventLoopClient{
publicstaticvoidmain(String[]args)throwsInterruptedException{
// 1.启动类
Channelchannel=newBootstrap()
// 2.添加EventLoop
.group(newNioEventLoopGroup())
// 3.选择客户端channel实现
.channel(NioSocketChannel.class)
// 4.添加处理器
.handler(newChannelInitializer<NioSocketChannel>(){
@Override//连接初始化调用
protectedvoidinitChannel(NioSocketChannelch)throwsException{
ch.pipeline().addLast(newStringEncoder());
}
//5.链接到服务器
})
.connect(newInetSocketAddress("localhost",8080))
.sync()
.channel();
System.out.println(channel);
System.out.println("");
}
}
SO_BACKLOG
ByteBuf 源码
Java
// 池化技术
static{
StringallocType=SystemPropertyUtil.get(
"io.netty.allocator.type",PlatformDependent.isAndroid()?"unpooled" : "pooled");
allocType=allocType.toLowerCase(Locale.US).trim();
ByteBufAllocatoralloc;
if("unpooled".equals(allocType)){
alloc=UnpooledByteBufAllocator.DEFAULT;
logger.debug("-Dio.netty.allocator.type: {}",allocType);
}elseif("pooled".equals(allocType)){
alloc=PooledByteBufAllocator.DEFAULT;
logger.debug("-Dio.netty.allocator.type: {}",allocType);
}else{
alloc=PooledByteBufAllocator.DEFAULT;
logger.debug("-Dio.netty.allocator.type: pooled (unknown: {})",allocType);
}
DEFAULT_ALLOCATOR=alloc;
//是否使用堆内存 默认采用直接内存
// We should always prefer direct buffers by default if we can use a Cleaner to release direct buffers.
DIRECT_BUFFER_PREFERRED=CLEANER!=NOOP
&&!SystemPropertyUtil.getBoolean("io.netty.noPreferDirect",false);
if(logger.isDebugEnabled()){
logger.debug("-Dio.netty.noPreferDirect: {}",!DIRECT_BUFFER_PREFERRED);
}