美文网首页
Netty学习

Netty学习

作者: 学学学q | 来源:发表于2021-04-20 16:37 被阅读0次
1.Netty是什么?

Netty是一个异步的、基于事件驱动的网络应用框架,可以用来快速开发可维护性的高性能协议的服务器和客户端。

Netty是一个NIO客户端服务端框架,它可以加速和简化网络应用程序的开发,比如协议服务和客户端。它极大的简化和流程化了网络编程,比如使用TCP和UDP的socket服务。
其结构图如下:


结构图.png
2.简单使用
  • 服务端
 public void run() throws Exception {
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();

        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .option(ChannelOption.SO_BACKLOG, 128)
                    .childOption(ChannelOption.SO_BACKLOG, 128)
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            ch.pipeline().addLast(new TimeServerHandler());
                        }
                    });

            // bind and start to accept incoming connections
            ChannelFuture f = b.bind(port).sync();
            // shut down your server
            f.channel().closeFuture().sync();
        } finally {
            workerGroup.shutdownGracefully();
            bossGroup.shutdownGracefully();
        }
    }
  • 服务端handler
public class TimeServerHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
        ctx.write(msg);
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) {
        ctx.flush();
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        // Close the connection when an exception is raised.
        cause.printStackTrace();
        ctx.close();
    }
}
  • 客户端
 public static void main(String[] args) throws Exception {
        String host = "127.0.0.1";
        int port = 8080;

        EventLoopGroup workerGroup = new NioEventLoopGroup();

        try {
            Bootstrap b = new Bootstrap();
            b.group(workerGroup);
            b.channel(NioSocketChannel.class);
            b.option(ChannelOption.SO_KEEPALIVE, true);
            b.handler(new ChannelInitializer<SocketChannel>() {
                @Override
                protected void initChannel(SocketChannel ch) throws Exception {
                    ch.pipeline().addLast(new TimeClientHandler());
                }
            });

            // Start the client
            ChannelFuture f = b.connect(host, port).sync();
            // Wait until the connection is closed
            f.channel().closeFuture().sync();
        } finally {
            workerGroup.shutdownGracefully();
        }
    }
  • 客户端Handler
public class TimeClientHandlerextends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
        ctx.write(msg);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        // Close the connection when an exception is raised.
        cause.printStackTrace();
        ctx.close();
    }
}

相关文章

  • Netty学习之Netty介绍

    Netty学习之Netty介绍 前言 本周开始学习Netty,主要的参考资料是《Netty In Action》原...

  • java-netty

    netty常用API学习 netty简介 Netty是基于Java NIO的网络应用框架. Netty是一个NIO...

  • Netty | 第2章 Netty 简介《Netty In Ac

    前言 参考资料: 《Netty In Action》; 本系列为 Netty 学习笔记,本篇介绍总结Netty 简...

  • Netty 编码解码

    参考来源 Netty实践 Netty 4.x学习笔记 - Channel和Pipeline Netty 编码器和解...

  • netty

    最近有个项目要用到netty,对于netty进行了研究,简单的总结一下。 学习netty的意义 学习网络编程,怎样...

  • Netty学习之数据传输

    Netty学习之数据传输 前言 在前面的小节中,我们简略地学习了Netty及Netty的核心组件,在本小节中,我们...

  • NIO-01

    最近开始学习Netty,在学习Netty之前,先学习下NIO 相对于传统的Socket。Nio提供了SocketC...

  • netty性能优化

    关于netty的学习和介绍,可以去github看官方文档,这里良心推荐《netty实战》和《netty权威指南》两...

  • Netty异步回调模式-Future和Promise剖析

    学习目标 为什么了解Netty异步监听? Netty如何实现异步监听的? Future简介 我们知道Netty的I...

  • Netty学习之EventLoop&Threading

    Netty学习之EventLoop&Threading Model 前言 在前面我们学习了Netty的众多组件,如...

网友评论

      本文标题:Netty学习

      本文链接:https://www.haomeiwen.com/subject/yzoelltx.html