世上无难事,只要肯登攀。——毛泽东
Netty的入门使用
常见的http服务器有Tomcat、jetty等,netty也可以方便的开发一个Http服务器。
想要完整的实现一个高性能、功能完善的http服务器非常的复杂,本文仅为了方便理解 Netty 网络应用开发的基本过程,所以只实现最基本的请求响应的流程:
- 搭建 HTTP 服务器,配置相关参数并启动。
- 从浏览器或者终端发起 HTTP 请求。
- 成功得到服务端的响应结果。
准备工作
先创建一个maven项目,引入netty依赖,这里使用4.1.52.Final稳定版本
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.52.Final</version>
</dependency>
服务端启动类
简单梳理一下流程:首先创建引导器;然后配置线程模型,通过引导器绑定业务逻辑处理器,并配置一些网络参数,最后绑定端口,就可以实现服务器的启动了。
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.*;
import io.netty.util.CharsetUtil;
import java.net.InetSocketAddress;
/**
* @PackageName: com.netty.demo.easy
* @author: youjp
* @create: 2020-11-23 16:33
* @description: TODO netty简单服务端:这里使用的是主从Reactor多线程模式
* @Version: 1.0
*/
public class HttpServer {
public void start(int port) throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup(); //主
EventLoopGroup workGroup = new NioEventLoopGroup();//从
try {
ServerBootstrap bootstrap = new ServerBootstrap(); //创建引导器
bootstrap.group(bossGroup, workGroup)
.channel(NioServerSocketChannel.class) // 推荐 Netty 服务端采用 NioServerSocketChannel 作为 Channel 的类型
.localAddress(new InetSocketAddress(port))
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline()
.addLast("codec", new HttpServerCodec())// HTTP 编解码
.addLast("compressor", new HttpContentCompressor())// HttpContent 压缩
.addLast("aggregator", new HttpObjectAggregator(65536)) // HTTP 消息聚合
.addLast("handler", new HttpServerHandler());
}
}).childOption(ChannelOption.SO_KEEPALIVE, true); // 设置为 true 代表启用了 TCP SO_KEEPALIVE 属性,TCP 会主动探测连接状态,即连接保活
ChannelFuture f = bootstrap.bind().sync();
System.out.println("Http Server started, Listening on " + port);
f.channel().closeFuture().sync();
} catch (Exception e) {
e.printStackTrace();
} finally {
workGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
}
/**
* 业务自定义的逻辑处理类:它是入站 ChannelInboundHandler 类型的处理器,负责接收解码后的 HTTP 请求数据,并将请求处理结果写回客户端。
*/
public class HttpServerHandler extends SimpleChannelInboundHandler<FullHttpRequest> {
@Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest msg) throws Exception {
Channel channel = ctx.channel(); // 获取通道
System.out.println(channel.remoteAddress()); // 显示客户端的远程地址
String content = String.format("Receive http request, uri: %s, method: %s, content: %s%n", msg.uri(), msg.method(), msg.content().toString(CharsetUtil.UTF_8));
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.wrappedBuffer(content.getBytes()));
ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}
}
public static void main(String[] args) throws Exception {
new HttpServer().start(8088);
}
}
通过上面的代码,我们可以完成 HTTP 服务器最基本的请求响应流程,测试步骤如下:
- 运行httpServer代码
2.浏览器发起 HTTP 请求( http://localhost:8088/test) 测试服务端响应
通过上述一个简单的 HTTP 服务示例,我们基本熟悉了 Netty 的编程模式。下面我将结合这个例子对 Netty 的引导器展开详细的介绍。
引导器实践
Netty 服务端的启动过程大致分为三个步骤:
- 配置线程池
- channel初始化
- 端口绑定
下面,我将逐一为大家介绍每一步具体需要做哪些工作。
配置线程池
Reactor设计也叫反应器模式,基于IO复用和线程池的结合,采用基于事件驱动的设计,能实现一个线程处理大量的事物
Netty 是采用 Reactor 模型进行开发的,可以非常容易切换三种 Reactor 模式:单线程模式、多线程模式、主从多线程模式。
Reactor单线程模式
Reactor 单线程模式即所有 I/O 操作都由一个线程完成,所以只需要启动一个 EventLoopGroup 即可。
EventLoopGroup group = new NioEventLoopGroup(1);
ServerBootstrap b = new ServerBootstrap();
b.group(group)
Reactor多线程模式
Reactor单线程模式有非常严重的性能瓶颈,因此多线程模式出现了,在netty中使用Reactor多线程模式和单线程模式非常相似,区分是NioEventLoopGroup可以不需要任何参数,它默认会启动2倍CPU核数的线程。当然,你也可以手动设置固定线程数。
EventLoopGroup group = new NioEventLoopGroup();
ServerBootstrap b = new ServerBootstrap();
b.group(group)
Reactor主从多线程模式
在大多数的场景下,我们采用的是主从多线程Reactor模式。Boss是主Reactor, Worker是从Reactor.它们分别应用于不同的NioEventLoopGroup,主Reactor负责处理请求(Accept),然后把channel注册到从Reactor上,从Reactor主要负责channel生命周期的所有I/O事件。
EventLoopGroup bossGroup = new NioEventLoopGroup(); //boss工作组
EventLoopGroup workGroup = new NioEventLoopGroup(); //worker工作组
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(bossGroup, workGroup)
Reactor三种模式形象比喻
餐厅一般有接待员和服务员,接待员负责在门口接待顾客,服务员负责全程服务顾客。Reactor的三种线程模型可以用接待员和服务员类比;
-
单Reactor单线程模型:接待员和服务员是同一个人,一直为顾客服务。客流量较少适合
-
单Reactor多线程模型:一个接待员,多个服务员。客流量大,一个人忙不过来,由专门的接待员在门口接待顾客,然后安排好桌子后,由一个服务员一直服务,一般每个服务员负责一片中的几张桌子
-
多Reactor多线程模型:多个接待员,多个服务员。这种就是客流量太大了,一个接待员忙不过来了
设置Channel通道类型
NIO模型是netty中最成熟并被广泛 使用的模型,因此,推荐netty服务端采用NioServerSocketChannel作为Channel的类型,客户端采用NioSocketChannel.设置的方式如下:
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.channel(NioServerSocketChannel.class);
Netty 提供了多种类型的Channel 实现类,例如 OioServerSocketChannel、EpollServerSocketChannel 等。你可以按需切换。
注册 ChannelHandler
在 Netty 中可以通过 ChannelPipeline 去注册多个 ChannelHandler,每个 ChannelHandler 各司其职,这样就可以实现最大化的代码复用,充分体现了 Netty 设计的优雅之处。那么如何通过引导器添加多个 ChannelHandler 呢?其实很简单,我们看下 HTTP 服务器代码示例
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline()
.addLast("codec", new HttpServerCodec())// HTTP 编解码
.addLast("compressor", new HttpContentCompressor())// HttpContent 压缩
.addLast("aggregator", new HttpObjectAggregator(65536)) // HTTP 消息聚合
.addLast("handler", new HttpServerHandler()); //自定义业务逻辑处理器
}
})
ServerBootstrap 的 childHandler() 方法需要注册一个 ChannelHandler。
ChannelInitializer是实现了 ChannelHandler接口的匿名类,通过实例化 ChannelInitializer 作为 ServerBootstrap 的参数。
Channel 初始化时都会绑定一个 管道-Pipeline,它主要用于服务编排。Pipeline 管理了多个 ChannelHandler。I/O 事件依次在 ChannelHandler 中传播。
ChannelHandler 负责业务逻辑处理。上述 HTTP 服务器示例中使用链式的方式加载了多个 ChannelHandler,包含HTTP 编解码处理器、HTTPContent 压缩处理器、HTTP 消息聚合处理器、自定义业务逻辑处理器。
在这里结合 HTTP 请求-响应的场景,分析下数据在 ChannelPipeline 中的流向。当服务端收到 HTTP 请求后,会依次经过 HTTP 编解码处理器、HTTPContent 压缩处理器、HTTP 消息聚合处理器、自定义业务逻辑处理器分别处理后,再将最终结果通过 HTTPContent 压缩处理器、HTTP 编解码处理器写回客户端。
设置 Channel 参数
Netty 提供了十分便捷的方法,用于设置 Channel 参数。关于 Channel 的参数数量非常多,如果每个参数都需要自己设置,那会非常繁琐。幸运的是 Netty 提供了默认参数设置,实际场景下默认参数已经满足我们的需求,我们仅需要修改自己关系的参数即可。
bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
ServerBootstrap 设置 Channel 属性有option和childOption两个方法,option 主要负责设置 Boss 线程组,而 childOption 对应的是 Worker 线程组。
这里我列举了经常使用的参数含义,你可以结合业务场景,按需设置。
参数 | 含义 |
---|---|
SO_KEEPALIVE | 设置为 true 代表启用了 TCP SO_KEEPALIVE 属性,TCP 会主动探测连接状态,即连接保活 |
SO_BACKLOG | 已完成三次握手的请求队列最大长度,同一时刻服务端可能会处理多个连接,在高并发海量连接的场景下,该参数应适当调大 |
TCP_NODELAY | Netty 默认是 true,表示立即发送数据。如果设置为 false 表示启用 Nagle 算法,该算法会将 TCP 网络数据包累积到一定量才会发送,虽然可以减少报文发送的数量,但是会造成一定的数据延迟。Netty 为了最小化数据传输的延迟,默认禁用了 Nagle 算法 |
SO_SNDBUF | TCP 数据发送缓冲区大小 |
SO_RCVBUF | TCP数据接收缓冲区大小,TCP数据接收缓冲区大小 |
SO_LINGER | 设置延迟关闭的时间,等待缓冲区中的数据发送完成 |
CONNECT_TIMEOUT_MILLIS | 建立连接的超时时间 |
端口绑定
在完成上述 Netty 的配置之后,bind() 方法会真正触发启动,sync() 方法则会阻塞,直至整个启动过程完成,具体使用方式如下:
ChannelFuture f = b.bind().sync();
bind() 方法涉及的细节比较多,在这里就先不做展开了。后续有笔记学习的话再介绍。
总结
关于如何使用引导器开发一个 Netty 网络应用我们就介绍完了,服务端的启动过程一定离不开配置线程池、Channel 初始化、端口绑定三个步骤,在 Channel 初始化的过程中最重要的就是绑定用户实现的自定义业务逻辑。是不是特别简单?
Http客户端类
使用netty进行客户端开发同服务端类似。这里我提供一下源码可以自行运行测试
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.http.*;
import java.net.URI;
import java.nio.charset.StandardCharsets;
/**
* @PackageName: com.netty.demo.easy
* @author: youjp
* @create: 2020-11-24 16:32
* @description: TODO netty客户端
* @Version: 1.0
*/
public class HttpClient {
public void connect(String host,int port) throws Exception{
EventLoopGroup group=new NioEventLoopGroup();
try {
Bootstrap bootstrap=new Bootstrap();
bootstrap.group(group);
bootstrap.channel(NioSocketChannel.class);
bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
bootstrap.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new HttpResponseDecoder());
ch.pipeline().addLast(new HttpRequestEncoder());
ch.pipeline().addLast(new HttpClientHandler());
}
});
ChannelFuture f = bootstrap.connect(host, port).sync();
URI uri = new URI("http://127.0.0.1:8088");
String content = "hello world";
DefaultFullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET,
uri.toASCIIString(), Unpooled.wrappedBuffer(content.getBytes(StandardCharsets.UTF_8)));
request.headers().set(HttpHeaderNames.HOST, host);
request.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
request.headers().set(HttpHeaderNames.CONTENT_LENGTH, request.content().readableBytes());
f.channel().write(request);
f.channel().flush();
f.channel().closeFuture().sync();
} catch (Exception e) {
e.printStackTrace();
} finally {
group.shutdownGracefully();
}
}
/**
* 客户端业务处理类
*/
public class HttpClientHandler extends ChannelInboundHandlerAdapter{
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof HttpContent) {
HttpContent content = (HttpContent) msg;
ByteBuf buf = content.content();
System.out.println(buf.toString(io.netty.util.CharsetUtil.UTF_8));
buf.release();
}
}
}
public static void main(String[] args) throws Exception {
HttpClient client = new HttpClient();
client.connect("127.0.0.1", 8088);
}
}
有兴趣的老爷,可以关注我的公众号【一起收破烂】,回复【006】获取2021最新java面试资料以及简历模型120套哦~