【Netty整理01-快速入門】Netty簡單使用Demo(已驗證)

多處摘抄或手打,爲了十積分厚着臉皮標爲原創,慚愧慚愧~本篇文章用於快速入門搭建一個簡單的nettyhtml

應用,如想稍微深刻系統的瞭解,請參照本人下一篇博客,連接:java

 

參考地址:編程

官方文檔:http://netty.io/wiki/user-guide-for-4.x.htmlbootstrap

文案部分:數組

百度百科:https://baike.baidu.com/item/Netty/10061624?fr=aladdin服務器

NIO:https://blog.csdn.net/skiof007/article/details/52873421網絡

代碼部分多線程

https://blog.csdn.net/hangge110/article/details/51613988框架

更多參考:dom

各個對象以及常量的做用對照源碼:https://blog.csdn.net/CoffeeAndIce/article/details/78987542

源代碼學習1:https://www.cnblogs.com/katsura/p/5991428.html

源代碼學習2:https://www.cnblogs.com/stevenczp/p/7581940.html

demo02:https://blog.csdn.net/Howinfun/article/details/81283721

 

Netty

 

    Netty是由JBOSS提供的一個java開源框架。Netty提供異步的、事件驅動的網絡應用程序框架和工具,用以快速開發高性能、高可靠性的網絡服務器和客戶端程序。

也就是說,Netty 是一個基於NIO的客戶、服務器端編程框架,使用Netty 能夠確保你快速和簡單的開發出一個網絡應用,例如實現了某種協議的客戶、服務端應用。Netty至關於簡化和流線化了網絡應用的編程開發過程,例如:基於TCP和UDP的socket服務開發。

 

NIO基本概念:點擊打開連接

IO的方式一般分爲幾種,同步阻塞的BIO、同步非阻塞的NIO、異步非阻塞的AIO......

 

DEMO(手打https://blog.csdn.net/hangge110/article/details/51613988):

一、服務端

package com.zx.test;

import com.lk.netty.mulchat.dome.ServerIniterHandler;
import com.lk.netty.mulchat.dome.ServerMain;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import org.omg.CORBA.PRIVATE_MEMBER;

/**
 * 服務端生產者對象
 * This is an netty Server
 * @author  ZX
 * @date  2018年6月7日
 * Netty中,通信的雙方創建鏈接後,會把數據按照ByteBuf的方式進行傳輸,
 * 例如http協議中,就是經過HttpRequestDecoder對ByteBuf數據流進行處理,轉換成http的對象。
 * 深刻學習:
 * https://www.cnblogs.com/katsura/p/5991428.html
 * https://www.cnblogs.com/stevenczp/p/7581940.html
 */
public class ServerTest {
    /**
     * 服務端口
     */
    private int port=9999;
    /**
     * 開啓服務的方法
     */
    public void StartNetty(){

        /**建立兩個EventLoop的組,EventLoop 這個至關於一個處理線程,
         是Netty接收請求和處理IO請求的線程。不理解的話能夠百度NIO圖解*/
        /* 
        相關資料:NioEventLoopGroup是一個處理I/O操做的多線程事件循環。
        Netty爲不一樣類型的傳輸提供了各類EventLoopGroup實現。
        在本例中,咱們正在實現一個服務器端應用程序,所以將使用兩個NioEventLoopGroup。
        第一個,一般稱爲「boss」,接受傳入的鏈接。
        第二個,一般稱爲「worker」,當boss接受鏈接並註冊被接受的鏈接到worker時,處理被接受鏈接的流量。
        使用了多少線程以及如何將它們映射到建立的通道取決於EventLoopGroup實現,甚至能夠經過構造函數進行配置。
        */
        EventLoopGroup acceptor = new NioEventLoopGroup();
        EventLoopGroup worker = new NioEventLoopGroup();
        try {
            //一、建立啓動類
            ServerBootstrap bootstrap = new ServerBootstrap();
            //二、配置啓動參數等
            /**設置循環線程組,前者用於處理客戶端鏈接事件,後者用於處理網絡IO(server使用兩個參數這個)
             *public ServerBootstrap group(EventLoopGroup group)
             *public ServerBootstrap group(EventLoopGroup parentGroup, EventLoopGroup childGroup)
             */
            bootstrap.group(acceptor,worker);
            /**設置選項
            * 參數:Socket的標準參數(key,value),可自行百度
            * eg:
            * bootstrap.option(ChannelOption.SO_BACKLOG, 1024);
            *bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
            * */
            bootstrap.option(ChannelOption.SO_BACKLOG, 1024);

            //用於構造socketchannel工廠
            bootstrap.channel(NioServerSocketChannel.class);
            /**
             * 傳入自定義客戶端Handle(服務端在這裏搞事情)
             */
            bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
                @Override
                public void initChannel(SocketChannel ch) throws Exception {
                    // 註冊handler
                    ch.pipeline().addLast(new SimpleServerHandler());
                }
            });

            // 綁定端口,開始接收進來的鏈接
            ChannelFuture f = bootstrap.bind(port).sync();


            // 等待服務器 socket 關閉 。
            f.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            acceptor.shutdownGracefully();
            worker.shutdownGracefully();
        }

    }



    public static void main(String[] args) {
        new ServerTest().StartNetty();
    }
}

 

 

二、服務端處理類

 

 

package com.zx.test;

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

public class SimpleServerHandler extends ChannelInboundHandlerAdapter {
    /**
     * 本方法用於讀取客戶端發送的信息
     * @param ctx
     * @param msg
     * @throws Exception
     */
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        System.out.println("SimpleServerHandler.channelRead");
        ByteBuf result = (ByteBuf) msg;
        byte[] result1 = new byte[result.readableBytes()];
        // msg中存儲的是ByteBuf類型的數據,把數據讀取到byte[]中
        result.readBytes(result1);
        String resultStr = new String(result1);
        // 接收並打印客戶端的信息
        System.out.println("Client said:" + resultStr);
        // 釋放資源,這行很關鍵
        result.release();

        // 向客戶端發送消息
        String response = "hello client!";
        // 在當前場景下,發送的數據必須轉換成ByteBuf數組
        ByteBuf encoded = ctx.alloc().buffer(4 * response.length());
        encoded.writeBytes(response.getBytes());
        ctx.write(encoded);
        ctx.flush();
    }

    /**
     * 本方法用做處理異常
     * @param ctx
     * @param cause
     * @throws Exception
     */
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        // 當出現異常就關閉鏈接
        cause.printStackTrace();
        ctx.close();
    }

    /**
     * 信息獲取完畢後操做
     * @param ctx
     * @throws Exception
     */
    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        ctx.flush();
    }

}

 

三、客戶端

 

package com.zx.test;
import io.netty.bootstrap.Bootstrap;
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.channel.socket.nio.NioSocketChannel;

/**
 * 客戶端消費者對象
 * This is an netty Client
 * @author ZX
 * @date   2018年6月7日
 */
public class ClientTest {
    public void connect(String host, int port) throws Exception {
        EventLoopGroup worker = new NioEventLoopGroup();
        try {
            Bootstrap b = new Bootstrap();
            /**
             *EventLoop的組
             */
            b.group(worker);
            /**
             * 用於構造socketchannel工廠
             */
            b.channel(NioSocketChannel.class);
            /**設置選項
             * 參數:Socket的標準參數(key,value),可自行百度
               保持呼吸,不要斷氣!
             * */
            b.option(ChannelOption.SO_KEEPALIVE, true);
            /**
             * 自定義客戶端Handle(客戶端在這裏搞事情)
             */
            b.handler(new ChannelInitializer<SocketChannel>() {
                @Override
                public void initChannel(SocketChannel ch) throws Exception {
                    ch.pipeline().addLast(new SimpleClientHandler());
                }
            });
            /** 開啓客戶端監聽*/
            ChannelFuture f = b.connect(host, port).sync();
            /**等待數據直到客戶端關閉*/
            f.channel().closeFuture().sync();
        } finally {
            worker.shutdownGracefully();
        }
    }

    public static void main(String[] args) throws Exception {
        ClientTest client=new ClientTest();
        client.connect("127.0.0.1", 9999);

    }
}

 

四、客戶端處理類

 

package com.zx.test;


import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
public class SimpleClientHandler extends ChannelInboundHandlerAdapter {
    /**
     * 本方法用於接收服務端發送過來的消息
     * @param ctx
     * @param msg
     * @throws Exception
     */
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        System.out.println("SimpleClientHandler.channelRead");
        ByteBuf result = (ByteBuf) msg;
        byte[] result1 = new byte[result.readableBytes()];
        result.readBytes(result1);
        System.out.println("Server said:" + new String(result1));
        result.release();
    }

    /**
     * 本方法用於處理異常
     * @param ctx
     * @param cause
     * @throws Exception
     */
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        // 當出現異常就關閉鏈接
        cause.printStackTrace();
        ctx.close();
    }


    /**
     * 本方法用於向服務端發送信息
     * @param ctx
     * @throws Exception
     */
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        String msg = "hello Server!";
        ByteBuf encoded = ctx.alloc().buffer(4 * msg.length());
        encoded.writeBytes(msg.getBytes());
        ctx.write(encoded);
        ctx.flush();
    }
}

五、運行結果:

服務端:

SimpleServerHandler.channelRead
Client said:hello Server!
客戶端:
SimpleClientHandler.channelRead

Server said:hello client!

 

後話:

      好了,看完整篇博文,複製代碼,跑起來了!而後你也有了解決問題的思路,無論對錯能解決問題了。可是每一個對象,每一個參數是什麼意思能搞懂嗎?博主不才,雖然博主後來再次添加了不少註釋,可是對不少對象以及參數理解不深,也不知道本身的用法是不是出於設計者本意!

      因此,下一篇博主會稍微深刻一些介紹對象以及使用,會讓你更加了解本身使用的這個NIO中間件工具,如果想成爲專家大牛,就本身去下載源碼分析吧~博主目前在拓展知識體系階段,尚未如此多精力去深刻,這個須要不少技巧。

    廢話很少說了,下一篇地址:https://blog.csdn.net/the_fool_/article/details/80623299

相關文章
相關標籤/搜索