The originality of zero reprint please indicate the original source, thank you!
Say in front
The previous article has carried on the preliminary discussion to the Netty: Netty, mainly introduced the next why we need to study Netty, Netty Introduction and so on; This article goes on to the contents of the previous article. In order to facilitate everyone to get started quickly, to understand the practice of Netty features, quick to get started an example so that we have a more direct understanding.
Theme
- Version selection and description
- Quick Start
- Summarize
Version selection and description
version of the choice , this is a very straightforward but often need to face the problem, especially when the open source project, we choose the version of the time there are many considerations, compare the JDK version selection, zookeeper version of the choice, Tomcat version selection, Spring Series version selection and so on, subsequent empty we can discuss the open source of these versions of the choice and upgrade issues, today the focus is on the Netty version of the choice above.
Netty Current Release Notes
At present, Netty large version is divided into three: 3.x, 4.x, 5.x.
Let's see what the official website says:
Note: The recommended version is 4.x, then we look at a lot of projects using Netty, and indeed use the 4.x version, it is understood that dubbo3.x will upgrade netty4.0.x to 4.1.x version.
See official website:
Note: If it is used in the project then we may choose 4.1.x, such as other versions, need to consider stability, etc., generally rarely choose the latest, but we are to learn netty, then I recommend the latest version: 4.1.21.Final version. , because when we finished, it may have been a long time, the 21 version is estimated to be stable, and are all 4.1x version, generally only a few minor changes, even if the subsequent selection of the new version of learning should not be particularly difficult.
netty4.1.x Brief Introduction
Recommended Crossing network New and noteworthy in 4.1(http://netty.io/wiki/ new-and-noteworthy-in-4.1.html), first understand that there are those general changes (do not think before useless, see there is no need to see also some of its highlights, follow-up may want to focus on learning to understand thinking)
Major changes:
- Android Support
- Channelhandlercontext.attr (..) = = Channel.attr (..)
- Channel.hasattr (...)
- Easier and more precise buffer leak tracking
- Pooledbytebufallocator as the default allocator
- Globally unique channel ID
- Embeddedchannel Usability
- Ability to use Executor instead of threadfactory
- Class Loader friendliness
- Bytebufallocator.calculatenewcapacity ()
Note: specific details remember to go to the official website to see: http://netty.io/wiki/new-and-noteworthy-in-4.1.html, this time can probably understand, follow-up empty slowly look.
Quick Start
Say so much, we have a simple demo to simply feel the next Netty.
MAVEN Build Project
Create a new MAVEN project, and then go to GitHub to find the Netty corresponding example code:
Put in the project, code structure
Add the following to the Pom.xml:
<dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>4.1.21.Final</version></dependency>
Slightly in the original demo based on the modification of something, to solve the problem of half-pack, sticky package (follow-up detailed introduction)
and information such as sending content.
I've submitted the code to GitHub, and the small partners you need can download it directly: Https://github.com/lirenzuo/nettydemo.
Run effect
Can be compared with the code written using NIO, NIO related basic Article two, found that using Netty write a TCP originally so simple, are encapsulated.
Summarize
The use of Netty can be very convenient and fast to build a network development environment, so that we do not care about some such as the client facing the disconnection, network flash, half-packet read and write, failure cache, network congestion and exception stream processing, etc., so the use of Netty network development is very appropriate, This article is just a primer, follow up with a demo can run up, when the breakpoint what is good to learn.
If you feel that there is a harvest, welcome to praise, attention, add the public number "ingenuity Zero", read more wonderful history!!!
Netty (i): Introductory article