1. Netty是什么,它可以做什么
Netty是由JBOSS提供的一个java开源框架。Netty提供异步的、事件驱动的网络应用程序框架和工具,用以快速开发高性能、高可靠性的网络服务器和客户端程序。
简单来说,Netty是一个对java nio的封装,用于快速简单的开发高性能网络应用程序的工具。
参考资料: netty官网: http://netty.io/
权威书籍:《Netty In Action》 (https://download.csdn.net/download/weixin_37068368/10377062)
官方样例:https://github.com/netty/netty/tree/4.1/example/src/main/java/io/netty/example
参考博客:https://blog.csdn.net/h348592532/article/details/52816148#
样例代码github地址:https://github.com/WeedOutWorld/techDemo/tree/master
2. Netty 入门demo
配置服务端与客户端,自定义协议。配置完成项目后,先启动服务端EchoServer.java,再启动客户端EchoClient.java
2.1. 新建一个maven项目,并导入Netty依赖包
工程结构:
maven依赖:
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.10.Final</version>
</dependency>
2.2. 服务端
Netty服务端主要有两部分构成:服务端配置启动类,业务逻辑处理类(异步回调接口)
服务端配置启动类:
package org.ych.techDemo.netty.nettyServer;
import org.ych.techDemo.netty.encoder.MessageDecoder;
import org.ych.techDemo.netty.encoder.MessageEncoder;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslContextBuilder;
import io.netty.handler.ssl.util.SelfSignedCertificate;
/**
* Echoes back any received data from a client.
*/
public final class EchoServer {
static final boolean SSL = System.getProperty("ssl") != null;
static final int PORT = Integer.parseInt(System.getProperty("port", "8007"));
public static void main(String[] args) throws Exception {
System.out.println("EchoServer.main start");
// Configure SSL.
final SslContext sslCtx;
if (SSL) {
SelfSignedCertificate ssc = new SelfSignedCertificate();
sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
} else {
sslCtx = null;
}
// Configure the server.
/*步骤
* 创建一个ServerBootstrap b实例用来配置启动服务器
* b.group指定NioEventLoopGroup来接收处理新连接
* b.channel指定通道类型
* b.option设置一些参数
* b.handler设置日志记录
* b.childHandler指定连接请求,后续调用的channelHandler
* b.bind设置绑定的端口
* b.sync阻塞直至启动服务
*/
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
//.option(ChannelOption.SO_BACKLOG, 100)
//.handler(new LoggingHandler(LogLevel.INFO))
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
if (sslCtx != null) {
p.addLast(sslCtx.newHandler(ch.alloc()));
}
p.addLast(new MessageDecoder());
p.addLast(new MessageEncoder());
//p.addLast(new LoggingHandler(LogLevel.INFO));
//p.addLast("encoder", new MessageEncoder());
//p.addLast("decoder", new MessageDecoder