`
liuzhengqiu0127
  • 浏览: 7239 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

Netty入门

阅读更多

引入Netty背景

 

目前使用JDK的NIO类库进行开发问题较多如下:

1,NIO的类库和API繁杂,使用麻烦,需要熟练掌握Selector,ServerSocketChannel,SocketChannel,ByteBuffer等。

2,需要具备其他的额外技能做铺垫,例如熟悉Java多线程编程。

3,可靠性能力补齐,工作量和难度都非常大。

4,JDK NIO的BUG,如epoll bug,它会导致Selector空轮询,最终导致CPU 100%。

 

Netty是业界最流行的NIO框架之一,它的健壮性,功能,性能,可定制性和可扩展性在同类框架中都是首屈一指的,已经得到成百上千的商用项目的验证。

Netty的优点总结如下:

API使用简单,开发门槛低。

功能强大,预置了多种编解码功能,支持多种主流协议。

性能高,通过与其他业界主流的NIO框架对比,Netty的综合性能最优。

成熟,稳定,Netty修复了已经发现的所有JDK NIO BUG,业务开发人员不需要再为NIO的BUG而烦恼。

经历了大规模的商业应用考验,质量得到验证。

 

Netty原理介绍

Netty是基于NIO的多线程设计的Reactors模式。增加线程扩展性,主要应用与多核处理器中。

Worker线程,Reactors要快速触发handlers。handlers的处理降低了Reactor的速度,需要将非I/O操作分离到其他线程中处理。

Reactor线程可以使用IO操作饱和,分布负载到其他reactors,负载均衡来匹配CPU和IO之间的速度差异。

 

原理图如下:

 

mainReactor:用于服务端接受客户端的连接。

subReactor:用于进行SocketChannel的网络读写。

queued tasks和worker threads:作为handler类,用于编解码(序列化和反序列化)以及进行数据的相关处理动作。

 

Netty实例

 

netty的服务端代码如下:

package com.huawei.netty.test;

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;

/**
 * Created by liuzhengqiu on 2017/10/15.
 */
public class NettyServer
{
    public void bind(int port) throws Exception
    {
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try
        {
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            serverBootstrap.group(bossGroup,workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .option(ChannelOption.SO_BACKLOG,1024)
                    .childHandler(new ChildChannelHandler());
            ChannelFuture channelFuture = serverBootstrap.bind(port).sync();
            channelFuture.channel().closeFuture().sync();
        }
        finally
        {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }

    private class ChildChannelHandler extends ChannelInitializer<SocketChannel>
    {

        @Override
        protected void initChannel(SocketChannel socketChannel) throws Exception
        {
            socketChannel.pipeline().addLast(new NettyServerHandler());
        }
    }

    public static void main(String[] args) throws Exception {
        new NettyServer().bind(8080);
    }
}

 

package com.huawei.netty.test;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.util.Date;

/**
 * Created by liuzhengqiu on 2017/10/15.
 */
public class NettyServerHandler extends ChannelInboundHandlerAdapter
{
    @Override
    public void channelRead(ChannelHandlerContext ctx,Object msg) throws Exception
    {
        ByteBuf byteBuf = (ByteBuf) msg;
        byte[] req = new byte[byteBuf.readableBytes()];
        byteBuf.readBytes(req);
        String body = new String(req,"UTF-8");
        System.out.println("The time server receive order :" + body);

        String currentTime = "hello world".equalsIgnoreCase(body)?new Date().toString():"bad order";
        ByteBuf resp = Unpooled.copiedBuffer(currentTime.getBytes());
        ctx.write(resp);
    }

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

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx,Throwable cause)
    {
        ctx.close();
    }
}

 

客户端代码如下:

 

package com.huawei.netty.test;

import io.netty.bootstrap.Bootstrap;
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.NioSocketChannel;

/**
 * Created by liuzhengqiu on 2017/10/15.
 */
public class NettyClient
{
    public void connect(int port,String host) throws Exception
    {
        EventLoopGroup group = new NioEventLoopGroup();
        try
        {
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(group).channel(NioSocketChannel.class)
                    .option(ChannelOption.TCP_NODELAY,true)
                    .handler(new ChannelInitializer<SocketChannel>() {

                        @Override
                        protected void initChannel(SocketChannel socketChannel) throws Exception {
                            socketChannel.pipeline().addLast(new NettyClientHandler());
                        }
                    });
            ChannelFuture f = bootstrap.connect(host,port).sync();
            f.channel().closeFuture().sync();
        }
        finally {
            group.shutdownGracefully();
        }
    }

    public static void main(String[] args) throws Exception {
        new NettyClient().connect(8080,"127.0.0.1");
    }
}

 

package com.huawei.netty.test;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

/**
 * Created by liuzhengqiu on 2017/10/15.
 */
public class NettyClientHandler extends ChannelInboundHandlerAdapter
{
    private final ByteBuf msg;

    public NettyClientHandler()
    {
        byte[] req = "hello world".getBytes();
        msg = Unpooled.buffer(req.length);
        msg.writeBytes(req);
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx)
    {
        ctx.writeAndFlush(msg);
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx,Object msg) throws Exception
    {
        ByteBuf buf = (ByteBuf) msg;
        byte[] req = new byte[buf.readableBytes()];
        buf.readBytes(req);

        String body = new String(req,"UTF-8");
        System.out.println("Now is :"+body);
    }
}


Netty服务端和客户端总结

netty服务端通过一个Map保存所有连接上来的客户端SocketChannel,客户端的ID作为Map的Key。每次服务器端如果要向某个客户端发送消息,只需根据ClientId取出对应的SocketChannel,往里面写入message即可。心跳检测通过IdleEvent事件,定时向服务器发送Ping消息,检测SocketChannel是否终断。

 

  • 大小: 215.4 KB
0
0
分享到:
评论

相关推荐

    Netty入门教程文档

    Netty入门教程文档 Netty是Java的网络编程框架,广泛应用于数据采集服务中,本文将对Netty的基本概念和应用进行详细介绍,并将其与ETL技术结合,讲解如何使用Netty进行数据流转和处理。 1. ETL概述 ETL(Extract...

    Netty 入门与实战:仿写微信 IM 即时通讯系统.rar

    Netty 入门与实战:仿写微信 IM 即时通讯系统,掘金小册子,netty教程。章节齐全无缺失,排版非常不错。 1.仿微信IM系统简介 1 2.Netty是什么? 2 3.服务端启动流程 8 4.客户端启动流程 11 5.实战:客户端与服务端双向...

    Netty 入门与实战:仿写微信 IM 即时通讯系统.zip

    《Netty 入门与实战:仿写微信 IM 即时通讯系统》是一本深入浅出的教程,旨在帮助读者掌握使用Netty构建高效、稳定、高性能的即时通讯系统的方法。通过模仿微信IM的实现,本书将理论知识与实践案例相结合,使读者...

    Netty 入门与实战:仿写微信 IM 即时通讯系统.pdf

    Netty 入门与实战:仿写微信 IM 即时通讯系统

    netty入门到精通.txt

    netty入门到精通

    Netty 入门与实战:仿写微信 IM 即时通讯系统

    在本文中,我们将深入探讨Netty框架,并通过实战项目——仿写微信IM即时通讯系统,来深入了解其在高性能网络应用中的应用。Netty是Java领域中一个高效的异步事件驱动的网络应用程序框架,它为快速开发可维护的高性能...

    Netty入门与实战:仿写微信IM即时通讯系统

    Netty实现IM通讯

    netty入门进阶之前

    文件"72-第2章_20-netty入门-jdk-future-480P 清晰-AVC.Cover.jpg"、"73-第2章_21-netty入门-netty-future-480P 清晰-AVC.Cover.jpg"和"74-第2章_22-netty入门-netty-promise-480P 清晰-AVC.Cover.jpg"详细解释了...

    Netty 入门与实战

    Netty 是一个高性能、异步事件驱动的网络应用程序框架,用于快速开发可维护的高性能协议服务器和客户端。这个框架在Java领域广泛应用于构建高并发、低延迟的网络应用,如分布式系统、游戏服务器、金融交易系统等。...

    netty入门示例工程

    本工程采用maven+netty4.1.0+PrefixedStringDecoder+json技术,包括客户端和服务端。先运行服务端SampleServer,再去等客户端SampleClient。示例中发的是心跳包,其中消息格式定义为msgType + msgNo + content(json...

    netty入门案例.zip

    这个“netty入门案例.zip”文件提供了一个简单的 Netty 应用示例,旨在帮助初学者快速理解并掌握 Netty 的基本概念和使用方法。下面将详细介绍这个入门案例中的关键知识点。 首先,Netty 的核心是其 ChannelHandler...

    netty 入门Reactor示例

    在学习"Netty入门Reactor示例"时,你可以按照以下步骤进行: 1. **创建服务器端:** - 首先,你需要创建一个ServerBootstrap实例,配置BossGroup和WorkerGroup。 - 然后,添加自定义的...

    netty入门例子--(不是翻译官方文档)

    这个“netty入门例子”旨在帮助初学者理解Netty的基本用法和特性,而不是简单地翻译官方文档,它提供了几个开发模板,以便于深入理解Netty中的消息异步通信机制和TCP通信的封装。 首先,Netty的核心是它的异步模型...

    netty入门与实战-netty-learn.zip

    这个“netty入门与实战-netty-learn.zip”文件包含了学习 Netty 的资源,帮助初学者理解并掌握这个强大的网络库。Netty-learn-master 可能是一个包含源代码、教程文档或者示例项目的目录。 Netty 的核心概念: 1. ...

    Netty的入门经典例子

    "Netty 入门经典例子" Netty 是一个异步的、事件驱动的网络编程框架和工具,使用 Netty 可以快速开发出可维护的、high-performance 的协议服务及其客户端应用。Netty 相当简化和流线化了网络应用的编程开发过程,...

    掘金-Netty 入门与实战-netty-demo.zip

    这个压缩包“掘金-Netty 入门与实战-netty-demo.zip”包含了关于 Netty 的入门教程和实战示例,特别是通过 "netty-demo-pipeline-channelHandler" 这个文件名我们可以推测,它可能包含了一个展示 Netty ...

Global site tag (gtag.js) - Google Analytics