上一篇【第67篇】Netty Epoll传输——Linux下性能更好的原生传输层
下一篇【第69篇】Netty与gRPC——高性能RPC框架的底层网络秘密


一、KQueue vs NIO

平台NIO原生
LinuxNioEventLoopEpollEventLoop
macOSNioEventLoopKQueueEventLoop

二、KQueue使用

import io.netty.channel.kqueue.KQueue;
import io.netty.channel.kqueue.KQueueEventLoopGroup;
import io.netty.channel.kqueue.KQueueServerSocketChannel;

if (KQueue.isAvailable()) {
    EventLoopGroup boss = new KQueueEventLoopGroup(1);
    EventLoopGroup worker = new KQueueEventLoopGroup();
    ServerBootstrap b = new ServerBootstrap()
        .group(boss, worker)
        .channel(KQueueServerSocketChannel.class);
}

三、跨平台自动选择

public static Class<? extends ServerChannel> getBestServerChannel() {
    if (Epoll.isAvailable()) return EpollServerSocketChannel.class;
    if (KQueue.isAvailable()) return KQueueServerSocketChannel.class;
    return NioServerSocketChannel.class;
}

public static EventLoopGroup getBestEventLoopGroup(int threads) {
    if (Epoll.isAvailable()) return new EpollEventLoopGroup(threads);
    if (KQueue.isAvailable()) return new KQueueEventLoopGroup(threads);
    return new NioEventLoopGroup(threads);
}

四、macOS开发建议

环境传输层理由
开发(macOS)NIO兼容性最好
开发(macOS)KQueue功能类似Epoll
生产(Linux)Epoll性能最佳

上一篇【第67篇】Netty Epoll传输——Linux下性能更好的原生传输层
下一篇【第69篇】Netty与gRPC——高性能RPC框架的底层网络秘密


Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐