Netty is an asynchronous event-driven network application framework 
for rapid development of maintainable high performance protocol servers & clients

学习前,建议了解下java NIO相关知识,有助于对Netty中对象的理解。

NIO介绍,博客地址:https://blog.csdn.net/the_fool_/article/details/83000648

AIO、BIO、NIO的区别,博客地址:https://blog.csdn.net/anxpp/article/details/51512200

Netty如何封装Java NIO,博客地址::https://blog.csdn.net/tjreal/article/details/79751342

应用场景https://blog.csdn.net/LIAN_XL/article/details/79799072

官网:https://netty.io/

版本:目前最新版本为4.1.30

环境:JDK 5 (Netty 3.x) or 6 (Netty 4.x) is enough

八卦:Netty5被干掉的原因?极大增加了复杂度,效率却没有提升,作者干掉了master分支。

模块,结构:

支持的链接类型:普通链接、长连接、心跳检测等。

使用:官方的简单demo,用于介绍Netty 创建过程:

完整代码:https://blog.csdn.net/the_fool_/article/details/80611148

1、根据实际业务创建handle处理类,处理客户端请求实际信息:

package com.xxx.ann.netty.mostsimple;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.ReferenceCountUtil; /**
* Handles a server-side channel.
* 实际上自定义的业务流程使用的是本类
*/
public class DiscardServerHandler extends ChannelInboundHandlerAdapter { // (1)
/**
* Server读取信息的方法
* @param ctx
* @param msg
*/
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
ByteBuf in = (ByteBuf) msg;
try {
while (in.isReadable()) { // (1)
System.out.print((char) in.readByte());
System.out.flush();
}
// 向客户端发送消息
String response = "hello client!";
// 在当前场景下,发送的数据必须转换成ByteBuf数组
ByteBuf encoded = ctx.alloc().buffer(4 * response.length());
encoded.writeBytes(response.getBytes());
ctx.write(encoded);
ctx.flush();
} finally {
ReferenceCountUtil.release(msg); // (2)
}
} /**
* 处理异常
* @param ctx
* @param cause
*/
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { // (4)
// Close the connection when an exception is raised.
cause.printStackTrace();
ctx.close();
}
}

2、Server 端代码:

package com.xxx.ann.netty.mostsimple;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel; /**
* server 端
*/
public class DiscardServer {
private int port;
public DiscardServer(int port){
this.port=port;
} public void run() throws Exception{
/**EventLoopGroup:IO操作的多线程组
*boss:
* accepts an incoming connection 处理链接
*worker:
* handles the traffic of the accepted connection once
* the boss accepts the connection and registers the accepted
* connection to the worker 处理实际逻辑
* 使用多少线程以及如何将它们映射到创建的通道取决于EventLoopGroup实现,甚至可以通过构造函数进行配置。
* */
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
/**ServerBootstrap是一个建立服务器的助手类。可以直接使用通道设置服务器。
*/
ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup)
/**Here, we specify to use the NioServerSocketChannel class which is used to
* instantiate a new Channel to accept incoming connections.*/
.channel(NioServerSocketChannel.class)
/***
* 这里指定处理程序将始终由新接受的通道进行处理
* ChannelInitializer:是专门帮助用户配置新通道的特殊处理程序。
* 实际使用需要抽取到顶级类并自定义业务逻辑代码
*/
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new DiscardServerHandler());
}
})
/**特定通道实现参数,具体含义:https://netty.io/4.1/api/io/netty/channel/ChannelOption.html
*option() is for the NioServerSocketChannel that accepts incoming connections
* childOption() is for the Channels accepted by the parent ServerChannel
* */
.option(ChannelOption.SO_BACKLOG, 128) .childOption(ChannelOption.SO_KEEPALIVE, true);
/** Bind and start to accept incoming connections.*/
ChannelFuture f = b.bind(port).sync(); /**
* Wait until the server socket is closed.
* In this example, this does not happen, but you can do that to gracefully
* shut down your server.
* */
f.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
} public static void main(String[] args) throws Exception {
int port=8081;
new DiscardServer(port).run();
}
}

3、浏览器进入licalhost:8081或者telnet localhost 8081可访问服务端,可在控制台查看到已经建立连接并访问成功(本demo不会成功返回数据)。

更多参考资料:https://mp.weixin.qq.com/s/IyWWeI7oWjvMgIWwizbLqA

【Netty4】深入学习Netty的更多相关文章

  1. Netty4.0学习笔记系列之一:Server与Client的通讯

    http://blog.csdn.net/u013252773/article/details/21046697 本文是学习Netty的第一篇文章,主要对Netty的Server和Client间的通讯 ...

  2. Netty学习——Netty和Protobuf的整合(二)

    Netty学习——Netty和Protobuf的整合(二) 这程序是有瑕疵的,解码器那里不通用,耦合性太强,有两个很明显的问题,但是要怎么解决呢?如:再加一个内部类型 Person2,之前的代码就不能 ...

  3. Netty学习——Netty和Protobuf的整合(一)

    Netty学习——Netty和Protobuf的整合 Protobuf作为序列化的工具,将序列化后的数据,通过Netty来进行在网络上的传输 1.将proto文件里的java包的位置修改一下,然后再执 ...

  4. 深入学习Netty(1)——传统BIO编程

    前言 之前看过Dubbo源码,Nacos等源码都涉及到了Netty,虽然遇到的时候查查资料,后面自己也有私下学习Netty并实践,但始终没有形成良好的知识体系,Netty对想要在Java开发上不断深入 ...

  5. 深入学习Netty(2)——传统NIO编程

    前言 学习Netty编程,避免不了从了解Java 的NIO编程开始,这样才能通过比较让我们对Netty有更深的了解,才能知道Netty大大的好处.传统的NIO编程code起来比较麻烦,甚至有遗留Bug ...

  6. 深入学习Netty(3)——传统AIO编程

    前言 之前已经整理过了BIO.NIO两种I/O的相关博文,每一种I/O都有其特点,但相对开发而言,肯定是要又高效又简单的I/O编程才是真正需要的,在之前的NIO博文(深入学习Netty(2)--传统N ...

  7. 深入学习Netty(4)——Netty编程入门

    前言 从学习过BIO.NIO.AIO编程之后,就能很清楚Netty编程的优势,为什么选择Netty,而不是传统的NIO编程.本片博文是Netty的一个入门级别的教程,同时结合时序图与源码分析,以便对N ...

  8. 深入学习Netty(5)——Netty是如何解决TCP粘包/拆包问题的?

    前言 学习Netty避免不了要去了解TCP粘包/拆包问题,熟悉各个编解码器是如何解决TCP粘包/拆包问题的,同时需要知道TCP粘包/拆包问题是怎么产生的. 在此博文前,可以先学习了解前几篇博文: 深入 ...

  9. Netty实现丢弃服务协议(Netty4.X学习一)

    何为丢弃服务(Discard Protocol),丢弃服务就是一个协议,是最简单的协议,它的作用是接受到什么就丢弃什么,它对调试网路状态有一定的用处.基于TCP的丢弃服务,服务器实现了丢弃丢弃协议,服 ...

随机推荐

  1. OpenMP中的同步和互斥

    在多线程编程中必须考虑到不同的线程对同一个变量进行读写访问引起的数据竞争问题.如果线程间没有互斥机制,则不同线程对同一变量的访问顺序是不确定的,有可能导致错误的执行结果. OpenMP中有两种不同类型 ...

  2. "Redis客户端连接数一直降不下来"的有关问题解决 good

    [线上问题] "Redis客户端连接数一直降不下来"的问题解决 前段时间,上线了新的 Redis缓存(Cache)服务,准备替换掉 Memcached. 为什么要将 Memcach ...

  3. 简明Python3教程 19.附录 FLOSS

    FLOSS Free/Libre and Open Source Software, in short, FLOSS is based on the concept of a community, w ...

  4. Netty In Action中文版 - 第六章:ChannelHandler

    本章介绍 ChannelPipeline ChannelHandlerContext ChannelHandler Inbound vs outbound(入站和出站) 接受连接或创建他们仅仅是你的应 ...

  5. ASP.NET Core MVC 设计模式 - ASP.NET Core 基础教程 - 简单教程,简单编程

    原文:ASP.NET Core MVC 设计模式 - ASP.NET Core 基础教程 - 简单教程,简单编程 ASP.NET Core MVC 设计模式 上一章节中,我们提到 ASP.NET Co ...

  6. WPF 设置控件阴影后,引发的Y轴位置变化问题

    原文:WPF 设置控件阴影后,引发的Y轴位置变化问题 背景 最近遇到一个动画执行时,文本位置变化的问题.如下图: 如果你仔细看的话,当星星变小时,文本往下降了几个像素. 貌似有点莫名其妙,因为控件之间 ...

  7. WPF与缓动(三) 指数缓动

    原文:WPF与缓动(三) 指数缓动 WPF与缓动(三) 指数缓动                                                                     ...

  8. OpenWrt 主的发展版本号trunk MT7620N 无线驱动程序bug

    周边环境: OpenWrt Development Trunk: svn co svn://svn.openwrt.org/openwrt/trunk/ BUG: 1. 无线无法建立连接. 2. 无线 ...

  9. 你所不知道的 Kindle - 阅读微信公众号文章

    Kindle 是一款非常优秀的阅读设备,它为我们提供了非常舒服的阅读体验,并且配合强大的亚马逊图书资源,应该是目前最好的阅读设备之一.Kindle 在已有的成就下还一直在努力提升用户体验.为中国用户开 ...

  10. LeapMotion Demo3

    原文:LeapMotion Demo3   从Github及其他论坛下载一些LeapMotion的例子,部分例子由于SDK的更新有一些小Bug, 已修复,感兴趣的可以下载:       http:// ...