One of the Netty in-depth study: introductory article
This article code download:
http://download.csdn.net/detail/cheungmine/8497549
1) What is Netty?
Netty is the network library (API) above Java NiO. Netty provides asynchronous, event-driven network application frameworks and tools for high-speed development of high-performance, high-reliability network servers and client programs.
2) Characteristics of Netty
a unified API. Suitable for different protocols (clogging and non-clogging). Based on a flexible, scalable event-driven model. A highly customizable threading model. Reliable, non-connected data socket Support (UDP) performance. Better throughput, low latency. Save resources and minimize unnecessary memory copies. Complete SSL/TLS and STARTTLS support. Can operate well in the applet and Android restricted environments. Robustness: OutOfMemoryError is no longer caused by too fast, too slow, or over-load connections. There is no longer a consistent problem of NIO read-write frequency in high-speed network environment. Easy to use: Good Java doc, User Guide and sample, simple and concise. dependent on JDK1.5 only.
3) Netty High speed to get started
Saying goes. Everything starts hard, so I built a Netty project template: Avro-channel. The goal is to combine Netty and Avro to implement asynchronous transfer of data and RPC (remote procedure Call). Avro-channel was originally designed to implement a set of RPC services. After discovering that Netty is so powerful, you want to use Netty to build the entire backend system. Therefore, the sample code in this article provides a simple Netty primer: helloworldserver and helloworldclient.
Building a project is very easy. The first step. Manually install the dependent jar onto your machine and enter the folder where the project Pom.xml:
$ python./mvn-install-lib.py
The above command installs the jar package under lib/on your machine. Then run the following command to build:
$ mvn Clean Compile package assembly:assembly
Finally, open 2 terminals, a, B. Run the following servers and clients, respectively:
A start the server :
$ java-jar./target/avro-channel-1.0-pre.jar
Or
$ JAVA-CP./target/avro-channel-1.0-pre.jar Avro.channel.server.netty.HelloWorldServer
b Start the client :
$ JAVA-CP./target/avro-channel-1.0-pre.jar avro.channel.client.netty.HelloWorldClient
4) Sample engineering structure
Avro-channel is the standard maven (3.05) project, with folder structures such as the following:
Lib/netty includes a series of two version numbers for Jboss.netty and Io.netty. Finally, let's look at the complete pom.xml:
<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:s chemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > < Modelversion>4.0.0</modelversion> <description>avro-channel is a Java framework for data transport and R Pc. Created by Zhangliang</description> <!--build All: $ mvn Clean Compile package Assembly:ass embly-<groupId>avro.channel</groupId> <artifactId>avro-channel</artifactId> < ;p ackaging>jar</packaging> <version>1.0-PRE</version> <name>avro-channel</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8< /project.build.sourceencoding> <compiler-plugin.version>2.3.2</compiler-plugin.version> <a vro.version>1.7.7</avro.version&Gt <slf4j.version>1.7.10</slf4j.version> <jackson.version>1.9.13</jackson.version> < Jboss.netty.version>3.2.10.final</jboss.netty.version> <io.netty.version>5.0.0.alpha2</ io.netty.version> <!--test server Class:helloworldserver-<!--test client Class:helloworl Dclient-<main.class>avro.channel.server.netty.HelloWorldServer</main.class> </PROPERTIES&G T <dependencies> <dependency> <groupId>junit</groupId> <ARTIFACTID&G T;junit</artifactid> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifa Ctid>slf4j-api</artifactid> <version>${slf4j.version}</version> </dependency> <depenDency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>org.jboss.netty</groupId> <artifactId>netty</artifactId> <vers ion>${jboss.netty.version}</version> </dependency> <dependency> <groupid& Gt;io.netty</groupid> <artifactId>netty-all</artifactId> <version>${io.netty .version}</version> </dependency> <dependency> <GROUPID>ORG.APACHE.AVRO&L T;/groupid> <artifactId>avro</artifactId> <version>${avro.version}</version& Gt </dependency> <dependency> <groupId>org.apache.avro</groupId> <art Ifactid>avRo-ipc</artifactid> <version>${avro.version}</version> </dependency> < Dependency> <groupId>org.codehaus.jackson</groupId> <artifactid>jackson-core-as l</artifactid> <version>${jackson.version}</version> </dependency> <de Pendency> <groupId>org.codehaus.jackson</groupId> <artifactid>jackson-mapper-as l</artifactid> <version>${jackson.version}</version> </dependency> </depen dencies> <build> <plugins> <plugin> <groupid>org.apache.mav En.plugins</groupid> <artifactId>maven-compiler-plugin</artifactId> <ve Rsion>${compiler-plugin.version}</version> <configuration> <source> 1.6</source> <target>1.6</target> <encoding>UTF-8</encoding> &L t;/configuration> </plugin> <plugin> <groupid>org.apache.avro< ;/groupid> <artifactId>avro-maven-plugin</artifactId> <VERSION>${AVRO.V Ersion}</version> <executions> <execution> < Id>schemas</id> <phase>generate-sources</phase> <goa Ls> <goal>schema</goal> <goal>protocol</goal > <goal>idl-protocol</goal> </goals> <configuration> <sourcedirectory>${project.basedir}/src/main/avro/</sou Rcedirectory> <outputDirectory>${project.basedir}/src/main/java/</outputDirectory> </configuration> </execution> </executions> </PLUGIN&G T <!--1) Build project jar and copy all dependencies to lib/: $ mvn Clean Compile $ MVN Package 2) Start Server: $ java-jar./TARGET/AVRO-CHANNEL-1.0-PR E.jar or $ java-cp./target/avro-channel-1.0-pre.jar Avro.channel.server.netty.Hello Worldserver 3) Start Client: $ JAVA-CP./target/avro-channel-1.0-pre.jar Avro.channel.client.netty.HelloWorldClient--<plugin> <groupid>org. Apache.maven.plugins</groupid> <artifactId>maven-jar-plugin</artifactId> & Lt;configuration> <archive> <manifest> <ADDCL Asspath>true</addclasspath> <classpathPrefix>lib/</classpathPrefix> <mainClass>${main.class}</mainClass> </manifest> </archive> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactid>maven-dependency-plugin& lt;/artifactid> <executions> <execution> <id> ;copy</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> & Lt;configuration> <outputDirectory>${project.build.directory}/lib</outputDirectory> </configuration> </execution> </executions> </plugin& Gt <!--1) package all jars in one: ${name}-jar-with-dependencies.jar $ mvn Assembly:ass embly 2) Run server: $ java-jar./target/avro-channel-1.0-pre-jar-with-dependencies.jar or $ JAVA-CP./target/avro-channel-1.0-pre-jar-with-dependencies.jar avro.channel.se Rver.netty.HelloWorldServer 3) Run client: $ JAVA-CP./target/avro-channel-1.0-pre-jar- With-dependencies.jar Avro.channel.client.netty.HelloWorldClient-<plugin> <artifactId>maven-assembly-plugin</artifactId> <version>2.2</version> <configuration> <archive> <manifest> < ;mainclass>${main.class}</mainclass> </manifest> </archive> <descriptorRefs> <descriptorref>jar-with-dependencies</descripto rref> </descriptorRefs> </configuration> </plugin> </plugins> </build></project>
One of the Netty in-depth study: introductory article