netty深入学习之一: 入门篇

时间:2023-12-21 23:37:38

netty深入学习之一: 入门篇

本文代码下载:

http://download.csdn.net/detail/cheungmine/8497549

1)Netty是什么

Netty是Java NIO之上的网络库(API)。Netty 提供异步的、事件驱动的网络应用程序框架和工具,用以快速开发高性能、高可靠性的网络服务器和客户端程序。

2)Netty的特性

统一的API,适用于不同的协议(阻塞和非阻塞)。基于灵活、可扩展的事件驱动模型。高度可定制的线程模型。可靠的无连接数据Socket支持(UDP)性能。更好的吞吐量,低延迟。更省资源,尽量减少不必要的内存拷贝。完整的SSL/ TLS和STARTTLS的支持。能在Applet和Android的限制环境运行良好。健壮性:不再因过快、过慢或超负载连接导致OutOfMemoryError。不再有在高速网络环境下NIO读写频率丌一致的问题。易用:完善的Java doc,用户指南和样例,简洁简单。仅依赖于JDK1.5。

3)Netty快速上手

俗话说,万事开头难,因此我构建了一个Netty项目模板:avro-channel。目的是结合Netty和Avro实现异步数据传输和RPC(远程过程调用)。avro-channel最初的目的是为了实现一套RPC服务,发现了Netty功能如此强大之后,就想使用Netty构建整个后端体系。因此,本文的例子代码提供了一个简单的Netty入门:HelloWorldServer和HelloWorldClient。

构建项目很简单,第一步,手工安装依赖jar到你的机器上,进入项目pom.xml所在目录:

$ python ./mvn-install-lib.py

上面这个命令把lib/下的jar包安装到你的机器上。然后执行下面的命令构建:

$ mvn clean compile package assembly:assembly

最后打开2个终端a,b,分别执行下面的服务器和客户端:

a 启动服务器:
        $ java -jar ./target/avro-channel-1.0-PRE.jar

        $ java -cp ./target/avro-channel-1.0-PRE.jar avro.channel.server.netty.HelloWorldServer
b 启动客户端:
        $ java -cp ./target/avro-channel-1.0-PRE.jar avro.channel.client.netty.HelloWorldClient

4)例子工程结构

avro-channel是标准的maven(3.05)工程,目录结构如下:

netty深入学习之一: 入门篇

lib/netty包含jboss.netty和io.netty二个版本系列。最后让我们看看完整的pom.xml:

<project
    xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="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 rpc. created by zhangliang</description>

    <!--
        build all:
            $ mvn clean compile package assembly:assembly
    -->
    <groupId>avro.channel</groupId>
    <artifactId>avro-channel</artifactId>
    <packaging>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>
        <avro.version>1.7.7</avro.version>
        <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: HelloWorldClient -->
        <main.class>avro.channel.server.netty.HelloWorldServer</main.class>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>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>
            <version>${jboss.netty.version}</version>
        </dependency>

        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId>
            <version>${io.netty.version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.avro</groupId>
            <artifactId>avro</artifactId>
            <version>${avro.version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.avro</groupId>
            <artifactId>avro-ipc</artifactId>
            <version>${avro.version}</version>
        </dependency>

        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-core-asl</artifactId>
            <version>${jackson.version}</version>
        </dependency>

        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>${jackson.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${compiler-plugin.version}</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.avro</groupId>
                <artifactId>avro-maven-plugin</artifactId>
                <version>${avro.version}</version>

                <executions>
                    <execution>
                        <id>schemas</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>schema</goal>
                            <goal>protocol</goal>
                            <goal>idl-protocol</goal>
                        </goals>

                        <configuration>
                            <sourceDirectory>${project.basedir}/src/main/avro/</sourceDirectory>
                            <outputDirectory>${project.basedir}/src/main/java/</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <!--
                1) Build project jar and copy all dependencies into lib/:
                    $ mvn clean compile
                    $ mvn package

                2) Start 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

                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>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>${main.class}</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <!--
                1) Package all jars in one: ${name}-jar-with-dependencies.jar
                    $ mvn assembly:assembly

                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.server.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</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>

        </plugins>
    </build>
</project>