`
海浪儿
  • 浏览: 273495 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

netty 4.x源码分析

阅读更多

netty 4.x源码分析

 

       服务端需要经过socket、bind、accept、read、write等步骤,客户端需要经过socket、connect、read、write等步骤,后续此系列文章会对每一个步骤如何发生进行分析。

 

  1. netty4源码分析-线程的创建
  2. netty4源码分析-socket
  3. netty4源码分析-bind
  4.  Netty4源码分析-NioEventLoop实现的线程运行逻辑

  5. netty4源码分析-connect
  6. netty4源码分析-accept
  7. netty4源码分析-write
  8. netty4源码分析-flush
  9. netty4源码分析-read
  10. 通信总结

以下是服务端和客户端的测试代码,后面的分析以这份代码为基础,目前使用的Netty的版本是4.0.4.Final及以上

 

服务端:
/*
 * Copyright 2012 The Netty Project
 *
 * The Netty Project licenses this file to you under the Apache License,
 * version 2.0 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at:
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package io.netty.example.echo;

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;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;

/**
 * Echoes back any received data from a client.
 */
public class EchoServer {

    private final int port;

    public EchoServer(int port) {
        this.port = port;
    }

    public void run() throws Exception {
        // Configure the server.
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        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 {
                     ch.pipeline().addLast(
                             //new LoggingHandler(LogLevel.INFO),
                             new EchoServerHandler());
                 }
             });

            // Start the server.
            ChannelFuture f = b.bind(port).sync();

            // Wait until the server socket is closed.
            f.channel().closeFuture().sync();
        } finally {
            // Shut down all event loops to terminate all threads.
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }

    public static void main(String[] args) throws Exception {
        int port;
        if (args.length > 0) {
            port = Integer.parseInt(args[0]);
        } else {
            port = 8080;
        }
        new EchoServer(port).run();
    }
}

 

/*
 * Copyright 2012 The Netty Project
 *
 * The Netty Project licenses this file to you under the Apache License,
 * version 2.0 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at:
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package io.netty.example.echo;

import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Handler implementation for the echo server.
 */
@Sharable
public class EchoServerHandler extends ChannelInboundHandlerAdapter {

    private static final Logger logger = Logger.getLogger(
            EchoServerHandler.class.getName());

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

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

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        // Close the connection when an exception is raised.
        logger.log(Level.WARNING, "Unexpected exception from downstream.", cause);
        ctx.close();
    }
}

 

客户端:
/*
 * Copyright 2012 The Netty Project
 *
 * The Netty Project licenses this file to you under the Apache License,
 * version 2.0 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at:
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package io.netty.example.echo;

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;

/**
 * Sends one message when a connection is open and echoes back any received
 * data to the server.  Simply put, the echo client initiates the ping-pong
 * traffic between the echo client and server by sending the first message to
 * the server.
 */
public class EchoClient {

    private final String host;
    private final int port;
    private final int firstMessageSize;

    public EchoClient(String host, int port, int firstMessageSize) {
        this.host = host;
        this.port = port;
        this.firstMessageSize = firstMessageSize;
    }

    public void run() throws Exception {
        // Configure the client.
        EventLoopGroup group = new NioEventLoopGroup();
        try {
            Bootstrap b = new Bootstrap();
            b.group(group)
             .channel(NioSocketChannel.class)
             .option(ChannelOption.TCP_NODELAY, true)
             .handler(new ChannelInitializer<SocketChannel>() {
                 @Override
                 public void initChannel(SocketChannel ch) throws Exception {
                     ch.pipeline().addLast(
                             //new LoggingHandler(LogLevel.INFO),
                             new EchoClientHandler(firstMessageSize));
                 }
             });

            // Start the client.
            ChannelFuture f = b.connect(host, port).sync();

            // Wait until the connection is closed.
            f.channel().closeFuture().sync();
        } finally {
            // Shut down the event loop to terminate all threads.
            group.shutdownGracefully();
        }
    }

    public static void main(String[] args) throws Exception {
        // Print usage if no argument is specified.
        if (args.length < 2 || args.length > 3) {
            System.err.println(
                    "Usage: " + EchoClient.class.getSimpleName() +
                    " <host> <port> [<first message size>]");
            return;
        }

        // Parse options.
        final String host = args[0];
        final int port = Integer.parseInt(args[1]);
        final int firstMessageSize;
        if (args.length == 3) {
            firstMessageSize = Integer.parseInt(args[2]);
        } else {
            firstMessageSize = 256;
        }

        new EchoClient(host, port, firstMessageSize).run();
    }
}

 

/*
 * Copyright 2012 The Netty Project
 *
 * The Netty Project licenses this file to you under the Apache License,
 * version 2.0 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at:
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package io.netty.example.echo;

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

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Handler implementation for the echo client.  It initiates the ping-pong
 * traffic between the echo client and server by sending the first message to
 * the server.
 */
public class EchoClientHandler extends ChannelInboundHandlerAdapter {

    private static final Logger logger = Logger.getLogger(
            EchoClientHandler.class.getName());

    private final ByteBuf firstMessage;

    /**
     * Creates a client-side handler.
     */
    public EchoClientHandler(int firstMessageSize) {
        if (firstMessageSize <= 0) {
            throw new IllegalArgumentException("firstMessageSize: " + firstMessageSize);
        }
        firstMessage = Unpooled.buffer(firstMessageSize);
        for (int i = 0; i < firstMessage.capacity(); i ++) {
            firstMessage.writeByte((byte) i);
        }
    }

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

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

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

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        // Close the connection when an exception is raised.
        logger.log(Level.WARNING, "Unexpected exception from downstream.", cause);
        ctx.close();
    }
}

 

分享到:
评论
7 楼 海浪儿 2015-07-20  
zhaoxjmail 写道
6.netty4源码分析-accept 提示404错误!


谢谢指出,已修复
6 楼 zhaoxjmail 2015-07-17  
6.netty4源码分析-accept 提示404错误!
5 楼 海浪儿 2014-04-25  
海浪儿 写道
1、如何让client发送消息
client与server建立连接后,会触发一个channelActive的inbound事件,你只要实现一个handler,让他继承ChannelInboundHandlerAdapter,然后在方法channelActive里就能实现消息发送的逻辑(调用ChannelHandlerContext的writeAndFlush方法)


2、如果让server接受消息
server接受到client的消息后,会触发一个channelRead的事件,你只要实现一个handler,让他继承ChannelInboundHandlerAdapter,然后在方法channelRead里就能拿到客户端发送的消息

当然这其中还需要加入一些编解码的handler,这些netty大部分都有现成的
可以参照netty自带的echo和snoop的例子


也可以参照这个总结:http://xw-z1985.iteye.com/blog/1973205
4 楼 海浪儿 2014-04-25  
1、如何让client发送消息
client与server建立连接后,会触发一个channelActive的inbound事件,你只要实现一个handler,让他继承ChannelInboundHandlerAdapter,然后在方法channelActive里就能实现消息发送的逻辑(调用ChannelHandlerContext的writeAndFlush方法)


2、如果让server接受消息
server接受到client的消息后,会触发一个channelRead的事件,你只要实现一个handler,让他继承ChannelInboundHandlerAdapter,然后在方法channelRead里就能拿到客户端发送的消息

当然这其中还需要加入一些编解码的handler,这些netty大部分都有现成的
可以参照netty自带的echo和snoop的例子
3 楼 281460530 2014-04-24  
请问如何让client 与server发送消息~Server 如何接受消息呢。
2 楼 海浪儿 2014-01-13  
谢谢提醒,已修正
1 楼 vavi 2014-01-13  
麻烦lz把上文的链接改成非admin连接哈,不然会报无权限错误

相关推荐

    Netty4.x源码分析详解

    Netty 的源码分析主要包括以下几个方面: 1. **I/O 模型**:理解 Netty 如何利用 NIO 实现非阻塞 I/O,包括 Channel、Selector 和 Socket 的工作原理。 2. **Pipeline 实现**:分析 Handler 的添加、移除和调用链的...

    Netty3.x 源码

    Netty3.x 版本是其早期的一个版本,虽然现在最新的版本已经发展到了4.x,但3.x版本仍然具有学习价值,特别是对于理解Netty的基本原理和设计模式。 首先,让我们来看看`pom.xml`文件。这是Maven项目对象模型的配置...

    netty4 sources 源码

    源码分析对于理解Netty的工作原理、优化性能以及定制化开发非常有帮助。 Netty 的核心特性包括: 1. **异步事件驱动**:Netty 使用非阻塞I/O(NIO)模型,通过事件循环(EventLoop)和通道(Channel)来处理网络...

    架构师之路分布式系列课程(SpringMybatisNettyRPC).zip

    │ 01 spring源码分析.mp4 │ 02 spring源码分析.mp4 │ 03 spring源码分析03.mp4 │ 04 spring源码分析04.mp4 │ 05 spring源码分析05.mp4 │ 06 spring源码分析06_1.mp4 │ 07 spring源码分析06_2.mp4 │ ...

    架构师之路分布式系列课程(SpringMybatisNettyRPC).rar

    │ 01 spring源码分析.mp4 │ 02 spring源码分析.mp4 │ 03 spring源码分析03.mp4 │ 04 spring源码分析04.mp4 │ 05 spring源码分析05.mp4 │ 06 spring源码分析06_1.mp4 │ 07 spring源码分析06_2.mp4 │ ...

    架构师之路分布式系列课程(SpringMybatisNettyRPC).7z

    │ 01 spring源码分析.mp4 │ 02 spring源码分析.mp4 │ 03 spring源码分析03.mp4 │ 04 spring源码分析04.mp4 │ 05 spring源码分析05.mp4 │ 06 spring源码分析06_1.mp4 │ 07 spring源码分析06_2.mp4 │ ...

    架构师之路分布式系列课程(SpringMybatisNettyRPC) - 副本.zip

    │ 01 spring源码分析.mp4 │ 02 spring源码分析.mp4 │ 03 spring源码分析03.mp4 │ 04 spring源码分析04.mp4 │ 05 spring源码分析05.mp4 │ 06 spring源码分析06_1.mp4 │ 07 spring源码分析06_2.mp4 │ ...

    架构师之路分布式系列课程(SpringMybatisNettyRPC) - 副本 - 副本.zip

    │ 01 spring源码分析.mp4 │ 02 spring源码分析.mp4 │ 03 spring源码分析03.mp4 │ 04 spring源码分析04.mp4 │ 05 spring源码分析05.mp4 │ 06 spring源码分析06_1.mp4 │ 07 spring源码分析06_2.mp4 │ ...

    netty-netty-4.1.28.Final:netty-netty-4.1.28。最终源码分析

    JDK 5(用于3.x)或6(用于4.0+)足以运行基于Netty的应用程序。 分支机构看 所有版本的开发都在名称与&lt;majorVersion&gt;.相同的每个分支中进行。 例如,3.9和4.0的开发分别位于和。 与JDK 9结合使用 Netty可以在模块...

    jboss netty5.0

    源码分析可以帮助开发者理解其非阻塞 I/O 模型、线程模型、事件循环(EventLoop)和通道(Channel)等核心概念。同时,提供的“所有jar包”包含了运行 Netty 应用所需的依赖,这些依赖可能包括编解码器、SSL/TLS ...

    《netty实战》http协议、自定义协议、自定义RPC模块学习源码.zip

    Netty 是一个高性能、异步...通过分析和学习这些示例,你可以掌握Netty在实际应用中的技巧,加深对网络编程的理解,提高开发效率。同时,对于自定义协议和RPC模块的学习,也能帮助你更好地应对各种复杂通信场景的需求。

    java源码编辑-drools:Drools是用Java语言编写的开放源码规则引擎,使用Rete算法对所编写的规则求值。Drools允许使用声

    源码分析篇 :triangular_ruler: DDD领域驱动设计 :electric_plug: 中间件开发 :ghost: JavaAgent全链路监控 :artist_palette: 架构框架搭建 转载分享 建立本开源项目的初衷是基于个人学习与工作中对 Java 相关技术栈...

    Netty HTTP协议简单实现

    7. **源码分析与调试** 阅读并理解 Netty 源码可以帮助我们更好地掌握其工作原理。Netty 的代码结构清晰,模块化设计使得我们可以针对具体需求进行定制。在实际项目中,可以使用 IDE 的调试功能逐步跟踪请求处理...

    netty 配套资源-代码、文档

    学习 Netty 的资源通常包括源码分析、官方文档、示例项目以及社区提供的教程。压缩包中的“代码”部分可能包含了 Netty 的示例代码或者已经实现的协议处理器,这对于理解框架的工作原理非常有帮助。而“文档”部分...

    elasticsearch-7.10.2 源码

    源码分析可以从以下几个方面入手: 1. **模块划分**:Elasticsearch 源码包括了客户端、服务器端、核心库、插件等多个模块,了解这些模块的职责有助于深入理解代码。 2. **网络通信**:基于 Netty 实现,负责节点间...

    apache dubbo 3.0.7源码

    源码分析可以帮助开发者深入理解其内部机制,提升开发和优化服务的能力。 1. **设计模式与架构** - **服务提供者(Provider)**:提供服务的模块,暴露服务接口供消费者调用。 - **服务消费者(Consumer)**:...

    JAVA上百实例源码以及开源项目源代码

    图片到图片装载器、绘制火焰效果的X坐标,Y坐标、得到X坐标,Y坐标值、绘制火焰效果Image…… Java加密解密工具集 JCT v1.0源码包 5个目标文件 内容索引:JAVA源码,综合应用,JCT,加密解密 WDSsoft的一款免费源代码 JCT ...

    手机网络游戏 jsp java

    在描述中提到的"代码完整"意味着这是一个可能包含了整个项目源码的压缩包,包括服务器端的Java代码、JSP页面以及可能的数据库脚本和其他资源配置文件。开发者或研究者可以通过分析这些代码来学习手机网络游戏的开发...

Global site tag (gtag.js) - Google Analytics