Netty Epoll 和 Nio

入门

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslContextBuilder;
import io.netty.handler.ssl.util.SelfSignedCertificate;
import io.netty.util.concurrent.DefaultEventExecutorGroup;
import io.netty.util.concurrent.EventExecutorGroup;

/**
* Netty 启动器,初始化 spring boot 的时候会自动加载
*/
public class Server {
private static final Logger LOG = LoggerFactory.getLogger(Server.class);

/*
* NioEventLoopGroup 默认创建数量为 CPU * 2,类型为 NioEventLoop 的实例。
* 每个 NioEventLoop 实例都持有一个线程,以及一个类型为 LinkedBlockingQueue 的任务队列
*/
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
// 业务线程
EventExecutorGroup eventExecutorGroup = new DefaultEventExecutorGroup(16);
static final boolean isSsl = System.getProperty("ssl") != null;

/**
* 初始化 Netty
*/
public void startNetty() {
try {
final SslContext sslCtx;
if (isSsl) {
SelfSignedCertificate ssc = new SelfSignedCertificate();
sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
} else {
sslCtx = null;
}
// 引导辅助程序
ServerBootstrap bs = new ServerBootstrap();
// 通过NIO方式来接收连接和处理连接
bs.group(bossGroup, workerGroup);
// 设置NIO的Channel
bs.channel(NioServerSocketChannel.class);
bs.childHandler(new HttpChannelInitializer(sslCtx, eventExecutorGroup));

bs.option(ChannelOption.SO_BACKLOG, 1024); // 连接数
// bs.childOption(ChannelOption.SO_KEEPALIVE, true); // 是否长链接
bs.childOption(ChannelOption.TCP_NODELAY, true); // 是否TCP延迟

// 配置完成,开始绑定server,通过调用sync同步方法阻塞直到绑定成功
Channel ch = bs.bind(9999).sync().channel();
LOG.info("Server startup. listen on {}-{}", (isSsl ? "https" : "http"), 9999);
// 应用程序会一直等待,直到Channel关闭
ch.closeFuture().sync();
} catch (Exception e) {
LOG.error("server startup failed. {}", e.getMessage(), e);
} finally {
LOG.info("释放资源...");
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
eventExecutorGroup.shutdownGracefully();
bossGroup = null;
workerGroup = null;
eventExecutorGroup = null;
}
}

public static void main(String[] args) {
new Server().startNetty();
}
}

优化:Epoll 和 Nio

1
2
3
4
5
6
private static final boolean isEpoll = Epoll.isAvailable();
private EventLoopGroup bossGroup = isEpoll ? new EpollEventLoopGroup() : new NioEventLoopGroup();
private EventLoopGroup workerGroup = isEpoll ? new EpollEventLoopGroup() : new NioEventLoopGroup();
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(bossGroup, workerGroup)
.channel(isEpoll ? EpollServerSocketChannel.class : NioServerSocketChannel.class);
  • 本文作者: forever杨
  • 本文链接: https://blog.yl-online.top/posts/a8f7cc4d.html
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。如果文章内容对你有用,请记录到你的笔记中。本博客站点随时会停止服务,请不要收藏、转载!