首页 > 代码库 > Netty 4.x 用户手册
Netty 4.x 用户手册
前言
问题
现在我们使用通用的应用程序和库来进行通信,例如:我们通常用一个HTTP 客户端库通过Web service调用远程服务从一个web服务器上获取信息。然而,一个通用的协议或其实现有时候不能很好地扩展。这就好比我们不使用HTTP 服务器传输超大文件,电子邮件信息,和接近实时的信息,比如财务信息和多人游戏数据。这些场景需要高度优化的协议实现。例如,你可能想要实现一个基于Ajax的聊天应用系统,流媒体,大型文件传输的HTTP 服务器。你甚至可能想设计和实现一种全新的协议能够精确的满足你的需求。另一种不可避免的情况就是,你不得不处理一个遗留专有协议来确保可以和老系统通信。在这种情况下,重要的是,我们能够多快的实现协议,而不会牺牲最终应用程序的稳定性和性能。
Nowadays we use general purpose applications or libraries to communicate with each other. For example, we often use an HTTP client library to retrieve information from a web server and to invoke a remote procedure call via web services. However, a general purpose protocol or its implementation sometimes does not scale very well. It is like how we don‘t use a general purpose HTTP server to exchange huge files, e-mail messages, and near-realtime messages such as financial information and multiplayer game data. What‘s required is a highly optimized protocol implementation which is dedicated to a special purpose. For example, you might want to implement an HTTP server which is optimized for AJAX-based chat application, media streaming, or large file transfer. You could even want to design and implement a whole new protocol which is precisely tailored to your need. Another inevitable case is when you have to deal with a legacy proprietary protocol to ensure the interoperability with an old system. What matters in this case is how quickly we can implement that protocol while not sacrificing the stability and performance of the resulting application.
解决方案
Netty 项目 是一个异步事件驱动的网络应用程序框架,可以快速开发可维护的高性能,高扩展性协议服务器和客户端的工具。
换句话说,Netty是一个NIO客户端服务器框架,它能够快速简单的开发网络应用,比如协议服务器和客户端。它极大的简化了网络编程,比如TCP和UDP的Socket服务器开发。
“快速和简单” 并不意味着应用程序会有维护性和性能的问题。Netty的设计仔细,通过从实现大量的协议,比如FTP,SMTP,HTTP和各种二级制和基于文本的遗留协议中获得经验,因此,Netty成功的找到了方法来同时实现了简单开发,性能,稳定性和灵活性。
一些用户可能也发现一些其他的框架声称有相同的优点,你可能会问什么让Netty如此与众不同。答案就是我们打造的观念。Netty从第一天起,就想给用户最舒服的体验,不管是API还是实现。它不是有形的东西但是你会意识到这种观念,让你的生活更简单,就好像你读这个指南和用Netty的时候。
The Netty project is an effort to provide an asynchronous event-driven network application framework and tooling for the rapid development of maintainable high-performance · high-scalability protocol servers and clients.
In other words, Netty is an NIO client server framework which enables quick and easy development of network applications such as protocol servers and clients. It greatly simplifies and streamlines network programming such as TCP and UDP socket server development.
‘Quick and easy‘ does not mean that a resulting application will suffer from a maintainability or a performance issue. Netty has been designed carefully with the experiences earned from the implementation of a lot of protocols such as FTP, SMTP, HTTP, and various binary and text-based legacy protocols. As a result, Netty has succeeded to find a way to achieve ease of development, performance, stability, and flexibility without a compromise.
Some users might already have found other network application framework that claims to have the same advantage, and you might want to ask what makes Netty so different from them. The answer is the philosophy it is built on. Netty is designed to give you the most comfortable experience both in terms of the API and the implementation from the day one. It is not something tangible but you will realize that this philosophy will make your life much easier as you read this guide and play with Netty.
Netty Components Diagram
入门
本章将介绍Netty的核心架构,用一些简单的例子让你快速入门。当你结束本章的时候,你可以立刻写一个基于Netty的客户端服务器程序。
如果你喜欢自上而下的学习东西,你也许想从第二章,架构介绍开始,然后在回到这里。
This chapter tours around the core constructs of Netty with simple examples to let you get started quickly. You will be able to write a client and a server on top of Netty right away when you are at the end of this chapter.
If you prefer top-down approach in learning something, you might want to start from Chapter 2, Architectural Overview and get back here.
入门之前
本章中运行例子的最小需求只有两个,最新的Netty版本和JDK 1.6以上。最新的Netty版本在 Netty下载页 可用。为了下载正确版本的JDK,请参考JDK的提供商网站
正如你读的那样,你可能有很多问题关于这章中是用的class,当你想更多的知道它们的时候,请参考API reference。为了你的方便,所有的class都链接到了API reference上,而且,如果有任何错误的信息,语法和拼写上的错误,或者你有好的建议提升我们的文档,请立刻联系 Netty社区
The minimum requirements to run the examples which are introduced in this chapter are only two; the latest version of Netty and JDK 1.6 or above. The latest version of Netty is available in the project download page. To download the right version of JDK, please refer to your preferred JDK vendor‘s web site.
As you read, you might have more questions about the classes introduced in this chapter. Please refer to the API reference whenever you want to know more about them. All class names in this document are linked to the online API reference for your convenience. Also, please don‘t hesitate to contact the Netty project community and let us know if there‘s any incorrect information, errors in grammar and typo, and if you have a good idea to improve the documentation
示例
下面的这些示例你可以从netty安装包的example jar解压查看源码
-
Writing a Discard Server
-
Looking into the Received Data
-
Writing an Echo Server
-
Writing a Time Server
-
Writing a Time Client
-
Dealing with a Stream-based Transport
-
Speaking in POJO instead of
ByteBuf
-
Shutting Down Your Application
总结
在这个章节,我们很快的学习了如何基于Netty写一个完成的网络应用程序
在io.netty.example包中还有很多示例可以查看
In this chapter, we had a quick tour of Netty with a demonstration on how to write a fully working network application on top of Netty.
There is more detailed information about Netty in the upcoming chapters. We also encourage you to review the Netty examples in the io.netty.example
package.
Please also note that the community is always waiting for your questions and ideas to help you and keep improving Netty and its documentation based on your feedback.
Netty 4.x 用户手册