linugb118--java space

Java

Netty 源码研究

 

org.jboss.netty.bootstrap
本身 Netty 可以作为一个server存在的,因此他存在启动入口,他具有client启动,server启动以及connectionless 启动(比如UDP)
1.基类bootstrap:他包含ChannelFactory,ChannelPipeline,ChannelPipelineFactory。
ClientBootstrap: 有connect()方法
ConnectionlessBootstrap:有connect(),bind()方法
ServerBootstrap:有bind()方法

2.org.jboss.netty.buffer
netty自己提供了buffer 来取代nio 中的java.nio.ByteBuffer,他与nio中的byteBuffer相比,有下面的几个特点
1>可以根据自己需要自己定义buffer type
2>只是buffer type改变而不拷贝buffer的内容,这样可以减少开销
3>动态大小的buffer type 类似于stringbuffer
4>不需要调用flip()方法
5>更快的性能


3.org.jboss.netty.channel
channel的核心api,包括异步和事件驱动等各种传送接口
org.jboss.netty.channel.group
channel group,里面包含一系列open的channel

org.jboss.netty.channel.local
一种虚拟的运输方式,能允许同一个虚拟机上的两个部分可以互相通信
org.jboss.netty.channel.socket
TCP,UDP端口接口,主要继承channel

org.jboss.netty.channel.socket.nio
基于nio端口channel的具体实现

org.jboss.netty.channel.socket.oio
基于老的io端口的channel的具体实现

org.jboss.netty.channel.socket.http
基于http的客户端和相应的server端的实现,可以在有防火墙的情况下进行工作

需要做的事情
a. 将http tunnel 作为servlet进行配置
web.xml
<servlet>
 *     <servlet-name>NettyTunnelingServlet</servlet-name>
 *      <servlet-class>org.jboss.netty.channel.socket.http.HttpTunnelingServlet</servlet-class>
 *     <!--
 *       The name of the channel, this should be a registered local channel.
 *       See LocalTransportRegister.
 *     -->
 *     <init-param>
 *       <param-name>endpoint</param-name>
 *       <param-value>local:myLocalServer</param-value>
 *     </init-param>
 *     <load-on-startup>1</load-on-startup>
 *   </servlet>
 *
 *   <servlet-mapping>
 *     <servlet-name>NettyTunnelingServlet</servlet-name>
 *     <url-pattern>/netty-tunnel</url-pattern>
 *   </servlet-mapping>
接下来需要将你的基于netty的server app绑定到上面的http servlet
你可以这样写
 *
 * public class LocalEchoServerRegistration {
 *
 *     private final ChannelFactory factory = new DefaultLocalServerChannelFactory();
 *     private volatile Channel serverChannel;
 *
 *     public void start() {
 *         ServerBootstrap serverBootstrap = new ServerBootstrap(factory);
 *         EchoHandler handler = new EchoHandler();
 *         serverBootstrap.getPipeline().addLast("handler", handler);
 *
 *         // Note that "myLocalServer" is the endpoint which was specified in web.xml.
 *         serverChannel = serverBootstrap.bind(new LocalAddress("myLocalServer"));
 *     }
 *
 *     public void stop() {
 *         serverChannel.close();
 *     }
 * }

然后在Ioc framework(JBoss Microcontainer,Guice,Spring)中定义bean
<bean name="my-local-echo-server"
     class="org.jboss.netty.example.http.tunnel.LocalEchoServerRegistration" />

这样http servlet 就可以了

b. 连接http tunnel
构造client
 * ClientBootstrap b = new ClientBootstrap(
 *         new HttpTunnelingClientSocketChannelFactory(
 *                 new NioClientSocketChannelFactory(...)));
 *
 * // Configure the pipeline (or pipeline factory) here.
 * ...
 *
 * // The host name of the HTTP server
 * b.setOption("serverName", "example.com");
 * // The path to the HTTP tunneling Servlet, which was specified in in web.xml
 * b.setOption("serverPath", "contextPath/netty-tunnel");
 * b.connect(new InetSocketAddress("example.com", 80);

4.org.jboss.netty.container
各种容器的兼容
org.jboss.netty.container.microcontainer
JBoss Microcontainer 整合接口

org.jboss.netty.container.osgi
OSGi framework 整合接口

org.jboss.netty.container.spring
Spring framework 整合接口

5.org.jboss.netty.handler
处理器
org.jboss.netty.handler.codec.base64
Base64 编码

org.jboss.netty.handler.codec.compression
压缩格式

org.jboss.netty.handler.codec.embedder
嵌入模式下编码和解码,即使没有真正的io环境也能使用
org.jboss.netty.handler.codec.frame
可扩展的接口,重新评估基于流的数据的排列和内容
org.jboss.netty.handler.codec.http.websocket
websocket相关的编码和解码,
参考
http://en.wikipedia.org/wiki/Web_Sockets

org.jboss.netty.handler.codec.http
http的编码解码以及类型信息

org.jboss.netty.handler.codec.oneone
一个对象到另一对象的自定义抽象接口,如果有自己编码需要继承该抽象类

org.jboss.netty.handler.codec.protobuf
Google Protocol Buffers的编码解码
Google Protocol Buffers参考下面
http://code.google.com/p/protobuf/

org.jboss.netty.handler.codec.replay

org.jboss.netty.handler.codec.rtsp
Real_Time_Streaming_Protocol的编码解码
Real_Time_Streaming_Protocol 参考下面wiki
http://en.wikipedia.org/wiki/Real_Time_Streaming_Protocol

org.jboss.netty.handler.codec.serialization
将序列化的对象转换到byte buffer的相关实现

org.jboss.netty.handler.codec.string
字符串编码解码,比如utf8编码等,继承oneone包的接口

org.jboss.netty.handler.execution
基于java.util.concurrent.Executor的实现

org.jboss.netty.handler.queue
将event存入内部队列的处理

org.jboss.netty.handler.ssl
基于javax.net.ssl.SSLEngine的SSL以及TLS实现
参考
http://en.wikipedia.org/wiki/Transport_Layer_Security

org.jboss.netty.handler.stream
异步写入大数据,不会产生outOfMemory 也不会花费很多内存

org.jboss.netty.handler.timeout
通过jboss.netty.util.Timer来对读写超时或者闲置链接的通知

6.org.jboss.netty.logging
根据不同的log framework 实现的类

7.org.jboss.netty.util
nettyutil类

org.jboss.netty.util.internal
netty内部util类,不被外部使用

posted on 2010-11-09 15:08 linugb118 阅读(4147) 评论(2)  编辑  收藏

Feedback

# re: Netty 源码研究 2012-05-24 15:14

http://www.blogjava.net/linugb118/archive/2010/11/09/Netty.html
Netty 源码研究:
b.setOption("serverName", "example.com");
// The path to the HTTP tunneling Servlet, which was specified in web.xml
b.setOption("serverPath", "contextPath/netty-tunnel");
b.connect(new InetSocketAddress("example.com", 80);
请问下博主那个example.com代表什么?
netty配置成servlet,能正常处理请求吗?  回复  更多评论   

# re: Netty 源码研究[未登录] 2012-05-24 17:22 linugb118

example.com 代表网址,可以配置成servlet,只是他们之间io的请求变成nio方式,这是netty框架的本质@胡
  回复  更多评论   



只有注册用户登录后才能发表评论。


网站导航:
 

My Links

Blog Stats

常用链接

留言簿(1)

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜