欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Netty源码分析(一):Netty总览

程序员文章站 2022-07-02 16:37:31
作为当前最流行的网络通信框架,Netty在互联网领域大放异彩,本系列将详细介绍Netty(4.1.22.Final)。 代码事例 服务端 客户端 运行流程 服务器 客户端 总结 本篇篇幅较短,只是简单贴了一段netty example下面的代码,并梳理了一下netty的流程。接下来的几篇会详细介绍n ......

作为当前最流行的网络通信框架,netty在互联网领域大放异彩,本系列将详细介绍netty(4.1.22.final)。

代码事例

服务端

public final class echoserver {
    // 从启动参数判断是否使用ssl
    static final boolean ssl = system.getproperty("ssl") != null;
    // 获取端口(默认8007)
    static final int port = integer.parseint(system.getproperty("port", "8007"));

    public static void main(string[] args) throws exception {
        // 配置ssl
        final sslcontext sslctx;
        if (ssl) {
            selfsignedcertificate ssc = new selfsignedcertificate();
            sslctx = sslcontextbuilder.forserver(ssc.certificate(), ssc.privatekey()).build();
        } else {
            sslctx = null;
        }
        // 配置服务器
        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()));
                            }
                            // 添加一个自定义的handler
                            p.addlast(new echoserverhandler());
                        }
                    });
            // 启动服务器
            channelfuture f = b.bind(port).sync();
            // 等待至连接断开
            f.channel().closefuture().sync();
        } finally {
            // 关闭服务器资源
            bossgroup.shutdowngracefully();
            workergroup.shutdowngracefully();
        }
    }
}

客户端

public final class echoclient {
    // 获取是否使用ssl
    static final boolean ssl = system.getproperty("ssl") != null;
    // 获取服配置的服务器地址(默认本地)
    static final string host = system.getproperty("host", "127.0.0.1");
    // 获取端口(默认8007)
    static final int port = integer.parseint(system.getproperty("port", "8007"));
    // 首次发送消息的大小
    static final int size = integer.parseint(system.getproperty("size", "256"));

    public static void main(string[] args) throws exception {
        // 配置ssl
        final sslcontext sslctx;
        if (ssl) {
            sslctx = sslcontextbuilder.forclient()
                    .trustmanager(insecuretrustmanagerfactory.instance).build();
        } else {
            sslctx = null;
        }
        // 配置客户端
        eventloopgroup group = new nioeventloopgroup();
        try {
            bootstrap b = new bootstrap();
            b.group(group)
                    .channel(niosocketchannel.class)
                    // 禁用nagle算法
                    .option(channeloption.tcp_nodelay, true)
                    .handler(new channelinitializer<socketchannel>() {
                        @override
                        public void initchannel(socketchannel ch) throws exception {
                            channelpipeline p = ch.pipeline();
                            if (sslctx != null) {
                                p.addlast(sslctx.newhandler(ch.alloc(), host, port));
                            }
                            //p.addlast(new logginghandler(loglevel.info));
                            p.addlast(new echoclienthandler());
                        }
                    });
            // 启动客户端
            channelfuture f = b.connect(host, port).sync();
            //等待至连接断开
            f.channel().closefuture().sync();
        } finally {
            // 关闭客户端资源
            group.shutdowngracefully();
        }
    }
}

运行流程

服务器

Netty源码分析(一):Netty总览

客户端

Netty源码分析(一):Netty总览

总结

本篇篇幅较短,只是简单贴了一段netty-example下面的代码,并梳理了一下netty的流程。接下来的几篇会详细介绍netty服务端和客户端启动时做了哪些事情。
我目前正在尝试在给netty添加注释:https://github.com/kamijyoudouma/nettyforanalysis.git ,有兴趣的童鞋可以关注一下。


本篇到此结束,如果读完觉得有收获的话,欢迎点赞、关注、加公众号【贰级天災】,查阅更多精彩历史!!!

Netty源码分析(一):Netty总览