1. 該啟動方式無需單獨開線程啟動netty,dubbo的NettyService就是此方式
public static void main(String[] args) {
ServerBootstrap serverBootstrap = new ServerBootstrap();
// boos接受新連接線程,主要負責創(chuàng)建新連接(從dubbo copy 過來)
NioEventLoopGroup boos = new NioEventLoopGroup(1,
new DefaultThreadFactory("NettyServerBoss", true));
// worker負責讀取數(shù)據(jù)的線程,主要用于讀取數(shù)據(jù)以及業(yè)務邏輯處理(從dubbo copy 過來)
NioEventLoopGroup worker = new NioEventLoopGroup(10,
new DefaultThreadFactory("NettyServerWorker", true));
try {
serverBootstrap
.group(boos, worker)
.channel(NioServerSocketChannel.class)
.childHandler(new SimpleServiceChannelInitializer());
ChannelFuture channelFuture = serverBootstrap.bind(10086);
channelFuture.syncUninterruptibly();
channelFuture.channel();
//該啟動方式為了阻塞主線程
Thread.sleep(200000);
} catch (Exception e) {
} finally {
boos.shutdownGracefully();
worker.shutdownGracefully();
}
}