运用Spring注解实现Netty服务器端UDP应用程序


Netty是JBOSS针对网络开发的一套应用框架,它也是在NIO的基础上发展起来的。netty基于异步的事件驱动,具有高性能、高扩展性等特性,它提供了统一的底层协议接口,使得开发者从底层的网络协议(比如 TCP/IP、UDP)中解脱出来。就使用来说,开发者只要参考 Netty提供的若干例子和它的指南文档,就可以放手开发基于Netty的服务端程序了。

Netty 的详细介绍:请点这里
Netty 的下载地址:请点这里

相关阅读:Netty源码学习笔记

netty有几个比较重要的概念,在此,仅做介绍,详细可以参考netty文档或源码。

1). channelBuffer: 是 Netty 的一个基本数据结构,这个数据结构存储了一个字节序列。 类似于 NIO 的 ByteBuffer,但操作起来比ByteBuffer更为简单方便。

2). ChannelFactory 是一个创建和管理 Channel 通道及其相关资源的工厂接口,它处理所有的 I/O 请求并产生相应的 I/O ChannelEvent 通道事件。

3).ChannelHandler是所有I/O ChannelEvent事件的响应类,所有消息,包括netty通信异常事件,均在该响应类内处理。

4).*** Bootstrap 是一个设置服务的帮助类。你甚至可以在这个服务中直接设置一个 Channel 通道。

现在以实现一个UDP协议下的服务器应用程序为例,演示netty通过spring注解开发服务器端。(在此以maven工具管理项目开发)

首先,是导入关联jar的POM文件:

<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>
  <groupId>com.byd.example</groupId>
  <artifactId>nettyTest</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
  <name>nettyTest</name>
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
    <groupId>slf4j</groupId>
    <artifactId>slf4j-jdk14</artifactId> 
    <version>1.6.1</version>
    </dependency>
    <dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId> 
    <version>1.2.9</version>
    </dependency>
    <dependency>
    <groupId>org.jboss.netty</groupId> 
    <artifactId>netty</artifactId>
    <version>3.2.0.BETA1</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId> 
    <version>3.0.2.RELEASE</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-expression</artifactId> 
    <version>3.0.2.RELEASE</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId> 
    <version>3.0.2.RELEASE</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId> 
    <version>3.0.2.RELEASE</version>
    </dependency>
  </dependencies>
  <repositories> 
    <repository> 
      <id>repository.jboss.org</id> 
          <url>http://repository.jboss.org/nexus/content/groups/public/</url> 
        <snapshots> 
          <enabled>false</enabled> 
        </snapshots> 
      </repository> 
    </repositories> 
    <build>
 
    </build>
</project>

更多详情请继续阅读第2页的内容

  • 1
  • 2
  • 3
  • 4
  • 下一页

相关内容