1.Netty提供的一些现成的工具

1.1 心跳检测

心跳检测本质上就是检查通道里是否有数据进出

https://gitee.com/spitman/learnnetty/blob/master/src/main/java/org/zyj/io/example/case4/MyHeatBeatHandler.java

这个例子可以看看心跳检测的原理

1.2 Protobuf

  • io.netty.handler.codec.protobuf.ProtobufEncoder
  • io.netty.handler.codec.protobuf.ProtobufDecoder

https://gitee.com/spitman/learnnetty/blob/master/src/main/java/org/zyj/io/example/case6/ProtobufTestClient.java
https://gitee.com/spitman/learnnetty/blob/master/src/main/java/org/zyj/io/example/case6/ProtobufTestServer.java

1.2.1 支持多种不同的Protobuf格式

  • 定义多种protobuf

https://gitee.com/spitman/learnnetty/blob/master/src/main/java/org/zyj/io/example/codec/protobuf/MyData.proto

  • server

https://gitee.com/spitman/learnnetty/blob/master/src/main/java/org/zyj/io/example/case7/ProtobufMultiMsgTestServer.java

  • handler

https://gitee.com/spitman/learnnetty/blob/master/src/main/java/org/zyj/io/example/case7/ProtobufMultiMsgServerHandler.java

1.3 HTTP

  • io.netty.handler.codec.http.HttpServerCodec

https://gitee.com/spitman/learnnetty/blob/master/src/main/java/org/zyj/io/example/case2/HttpServer.java

1.4 WebSocket

  • io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler

https://gitee.com/spitman/learnnetty/blob/master/src/main/java/org/zyj/io/example/case5/WebsocketServer.java

1.5 其他协议

参见: io.netty.handler.codec


2. Netty官方示例

https://github.com/netty/netty/tree/4.1/example


3. 看看开源软件是怎么用Netty的

3.1 RocketMQ

https://gitee.com/apache/rocketmq/blob/develop/remoting/src/main/java/org/apache/rocketmq/remoting/netty/NettyRemotingServer.java

3.2 Cassandra

https://gitee.com/apache/Cassandra/blob/trunk/src/java/org/apache/cassandra/transport/Server.java