下面列出了怎么用io.netty.channel.unix.UnixChannelOption的API类实例代码及写法,或者点击链接到github查看源代码。
protected void config(ServerBootstrap bootstrap) throws Exception{
//允许在同一端口上启动同一服务器的多个实例,只要每个实例捆绑一个不同的本地IP地址即可
bootstrap.option(ChannelOption.SO_REUSEADDR, true)
//netty boos的默认内存分配器
// .option(ChannelOption.ALLOCATOR, ByteBufAllocatorX.INSTANCE)
//用于构造服务端套接字ServerSocket对象,标识当服务器请求处理线程全满时,用于临时存放已完成三次握手的请求的队列的最大长度
// .option(ChannelOption.SO_BACKLOG, 1024) // determining the number of connections queued
//禁用Nagle算法,即数据包立即发送出去 (在TCP_NODELAY模式下,假设有3个小包要发送,第一个小包发出后,接下来的小包需要等待之前的小包被ack,在这期间小包会合并,直到接收到之前包的ack后才会发生)
// .childOption(ChannelOption.TCP_NODELAY, true)
//开启TCP/IP协议实现的心跳机制
.childOption(ChannelOption.SO_KEEPALIVE, true)
//netty的work默认内存分配器
.childOption(ChannelOption.ALLOCATOR, ByteBufAllocatorX.INSTANCE);
// .childOption(ChannelOption.ALLOCATOR, ByteBufAllocator.DEFAULT);
if(enableEpoll){
//允许使用同一个端口, 内核实现的负载均衡. 需要 Linux kernel >= 3.9
bootstrap.option(UnixChannelOption.SO_REUSEPORT, true);
}
}
public UdpServerChannel(int ioThreads) {
if (ioThreads < 1) {
throw new IllegalArgumentException("IO threads cound can't be less than 1");
}
boolean epollAvailabe = Epoll.isAvailable();
if (!epollAvailabe) {
ioThreads = 1;
}
group = epollAvailabe ? new EpollEventLoopGroup(ioThreads) : new NioEventLoopGroup(ioThreads);
Class<? extends DatagramChannel> channel = epollAvailabe ? EpollDatagramChannel.class : NioDatagramChannel.class;
ChannelInitializer<Channel> initializer = new ChannelInitializer<Channel>() {
final ReadRouteChannelHandler ioReadRoute = new ReadRouteChannelHandler();
@Override
protected void initChannel(Channel ioChannel) throws Exception {
ioChannel.pipeline().addLast(ioReadRoute);
}
};
while (ioThreads-- > 0) {
Bootstrap ioBootstrap = new Bootstrap().group(group).channel(channel).handler(initializer);
if (epollAvailabe) {
ioBootstrap.option(UnixChannelOption.SO_REUSEPORT, true);
}
ioBootstraps.add(ioBootstrap);
}
}