Netty--Reactor模式
目錄
?
Reactor線程模型
Reactor單線程模型
線程的職責
應用場景
Reactor多線程模型
線程職責
應用場景
主從Reactor多線程模型
Netty模型
Netty單線程模型
Netty多線程模型
Netty主從模型
Reactor線程模型
Reactor單線程模型
Reactor單線程模型,指的是所有的I/O操作都在同一個NIO線程上面完成。
線程的職責
應用場景
用于小場景,高并發,高負載不合適。
Reactor多線程模型
Reactor多線程模型與單線程模型最大區別就是單獨有一組NIO線程處理I/O操作。
線程職責
主線程負責TCP接收,發送/接收數據。
工作線程負責I/O操作。
1個NIO線程可以同時處理N條鏈路,但是1個鏈路只對應1個NIO線程,防止發生并發操作問題。
應用場景
應用于大多數場景。不適用于百萬級場景,因為主線程負責所有連接,可能會導致性能問題。
主從Reactor多線程模型
服務端用于接收客戶端連接的不再是1個單獨的NIO線程,而是一個獨立的NIO線程池。由從線程池處理所有Socket連接。
Netty模型
Netty單線程模型
?public void bind(int port)
?{
???? EventLoopGroup reactorGroup = new NioEventLoopGroup();
???? try {
???????? ServerBootstrap b = new ServerBootstrap();
??????? //boss和worker是同一個group。
???????? b.group(reactorGroup, reactorGroup)
???????? .channel(NioServerSocketChannel.class)
???????? .childHandler(new ChannelInitializer<SocketChannel>()
???????? {
???????????? @Override
???????????? protected void initChannel(SocketChannel ch) throws Exception
???????????? {
???????????????? ch.pipeline().addLast("http-codec", new HttpServerCodec());
???????????????? ch.pipeline().addLast("aggregator", new HttpObjectAggregator(65536));
???????????????? ch.pipeline().addLast("http-chunked", new ChunkedWriteHandler());
??????????????? ?
???????????? }
???????? }
???????? );
???????? Channel ch = b.bind(port).sync().channel();
???????? ch.closeFuture().sync();
???? }
???? catch (InterruptedException e)
???? {
??????? e.printStackTrace();
???? }
???? finally
???? {
??????? reactorGroup.shutdownGracefully();
???? }
?}
?
Netty多線程模型
?public void bind(int port)
?{
?????? //NioEventLoopGroup參數為1,表示僅有1個線程。
??????? EventLoopGroup acceptorGroup = new NioEventLoopGroup(1);
??????? NioEventLoopGroup ioGroup = new NioEventLoopGroup();
???? try {
???????? ServerBootstrap b = new ServerBootstrap();
??????? //boss和worker是不同的組
???????? b.group(acceptorGroup, ioGroup)
???????? .channel(NioServerSocketChannel.class)
???????? .childHandler(new ChannelInitializer<SocketChannel>()
???????? {
???????????? @Override
???????????? protected void initChannel(SocketChannel ch) throws Exception
???????????? {
???????????????? ch.pipeline().addLast("http-codec", new HttpServerCodec());
???????????????? ch.pipeline().addLast("aggregator", new HttpObjectAggregator(65536));
???????????????? ch.pipeline().addLast("http-chunked", new ChunkedWriteHandler());
??????????????? ?
???????????? }
???????? }
???????? );
???????? Channel ch = b.bind(port).sync().channel();
???????? ch.closeFuture().sync();
???? }
???? catch (InterruptedException e)
???? {
??????? e.printStackTrace();
???? }
???? finally
???? {
??????? reactorGroup.shutdownGracefully();
???? }
?}
Netty主從模型
public void bind(int port)
?{
?? //NioEventLoopGroup參數未設置,表示僅有多個線程(默認:Runtime.getRuntime().availableProcessors())。
??????? EventLoopGroup acceptorGroup = new NioEventLoopGroup();
??????? NioEventLoopGroup ioGroup = new NioEventLoopGroup();
???? try {
???????? ServerBootstrap b = new ServerBootstrap();
??????? //boss和worker是不同的組
???????? b.group(acceptorGroup, ioGroup)
???????? .channel(NioServerSocketChannel.class)
???????? .childHandler(new ChannelInitializer<SocketChannel>()
???????? {
???????????? @Override
???????????? protected void initChannel(SocketChannel ch) throws Exception
???????????? {
???????????????? ch.pipeline().addLast("http-codec", new HttpServerCodec());
???????????????? ch.pipeline().addLast("aggregator", new HttpObjectAggregator(65536));
???????????????? ch.pipeline().addLast("http-chunked", new ChunkedWriteHandler());
??????????????? ?
???????????? }
???????? }
???????? );
???????? Channel ch = b.bind(port).sync().channel();
???????? ch.closeFuture().sync();
???? }
???? catch (InterruptedException e)
???? {
??????? e.printStackTrace();
???? }
???? finally
???? {
??????? reactorGroup.shutdownGracefully();
???? }
?}
Netty默認模式是主從多線程模式,worker沒有線程池,由subReactor處理I/O操作,讀完已收到的數據到ChannelBuffer中,之后觸發ChannelPipeline中的ChannelHandler流。
?
?
?
總結
以上是生活随笔為你收集整理的Netty--Reactor模式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: NIO--Selector
- 下一篇: Netty--ByteBuf