What is the Netty
Here’s a scenario: You’re developing a new mission-critical application for a large company, and the system must be able to support 150,000 concurrent users with no performance penalty.
Based on the need for high throughput, high availability and low cost, Netty, the excellent network framework that masks NIO’s underlying layer, became the preferred choice.
Netty is asynchronous and event-driven, non-blocking network calls so we don’t have to wait for an operation to complete, and selectors allow us to monitor events on many connections with fewer threads.
— Netty in Action
Section 1: Server startup process
A server-side startup demo
Public class NettyServer {public static void main(String[] args) { NioEventLoopGroup bossGroup = new NioEventLoopGroup(); NioEventLoopGroup workerGroup = new NioEventLoopGroup(); NioEventLoopGroup(); ServerBootstrap ServerBootstrap = new ServerBootstrap(); ServerBootstrap // Configure two thread groups. group(bossGroup, The NIO workerGroup) / / specified use the transmission channel. The channel (NioServerSocketChannel. Class) / / bind (localhost: 8080). LocalAddress (new InitSocketAddress(8080)) // Create a ChannelInitializer for the bootstrap class. When a new connection is created, a subchannel for the system will be created. ChannelInitializer will add one of your handlers to the Channel's ChannelPipeline. ChildHandler (new ChannelInitializer<NioSocketChannel>() { Protected void initChannel(NioSocketChannel ch) {// There will be a handler bound to handle the business logic, not specifying hanler ch.pipeline.addlast (handler); }}); }}Copy the code
These are the main code components for creating a server:
- Create two thread groups for different roles
- To create a
ServerBootstrap
Instance boot server configuration - Specify IO model (NIO)
- Binding address
- Initialize each channel with handler
ServerBootstrap Other configurations
-
channelFactory
If you cannot create a Channel through the default no-argument constructor, you can provide a channelFactory
-
option
Set some properties for the ServerChannel (specifying the ChannelOption to use for the newly created ServerChannel’s ChannelConfig). Note that this option will be set to Channel via the bind method, and setting or changing ChannelOption again after the bind method is called will have no effect.
-
childOption
Set some TCP low-level properties for each connection. Specifies the current subchannel
ChannelOption applied to the subchannel’s ChannelConfig when received
-
attr
We can specify some custom properties for the server’s channel, NioServerSocketChannel, which we can then retrieve via channel.attr(). Specify a property on ServerChannel that will be set to the channel using the bind method. If it is set, the change will not take effect.
-
childAttr
Give each connection a custom property, which we can then retrieve via channel.attr().
-
bind
Bind ServerChannel and return a ChannelFuture that will be notified when the bind operation completes.
The resources
-
Netty Introduction and Actual Combat: Imitation writing wechat IM Instant Messaging System
Booklet address: juejin.cn/book/684473…
Github address: github.com/lightningMa…
-
Netty in Action
This article is written and licensed BY CC BY 3.0CN. Can be reproduced freely, quote, but the author must be signed and indicate where the article originated. If reprinted to wechat official account, please add the author’s official qr code at the end of the article.