Netty server demo

Netty server demo

一、簡介

Netty是一個高性能、異步事件驅動的NIO框架,它提供了對TCP、UDP和文件傳輸的支持,作爲一個異步NIO框架,Netty的所有IO操作都是異步非阻塞的,通過Future-Listener機制,用戶可以方便的主動獲取或者通過通知機制獲得IO操作結果

二、maven依賴

<!-- https://mvnrepository.com/artifact/io.netty/netty-transport -->
<dependency>
    <groupId>io.netty</groupId>
    <artifactId>netty-transport</artifactId>
    <version>4.1.6.Final</version>
</dependency>

<!-- https://mvnrepository.com/artifact/io.netty/netty-handler -->
<dependency>
    <groupId>io.netty</groupId>
    <artifactId>netty-handler</artifactId>
    <version>4.1.6.Final</version>
</dependency>

三、示列代碼

package netty;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
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;

import java.util.concurrent.ExecutionException;


/**
 * Created by zhengyong on 16/10/21.
 */
public class NettyServer {

    private static ServerBootstrap bootstrap;

    public static void main(String[] args) throws InterruptedException, ExecutionException {

        EventLoopGroup boss = new NioEventLoopGroup(1);
        EventLoopGroup worker = new NioEventLoopGroup();

        bootstrap = new ServerBootstrap();
        bootstrap.group(boss,worker).channel(NioServerSocketChannel.class).handler(new LoggingHandler(LogLevel.INFO))
                .childHandler(new ChannelInitializer<SocketChannel>() {
            @Override
            public void initChannel(SocketChannel ch) throws Exception {
                ChannelPipeline p = ch.pipeline();
                p.addLast(new ConsoleHandler());
            }
        });

        // Start the client.
        ChannelFuture ch = bootstrap.bind(8082).sync();

        System.out.println("start server success, you can telnet 127.0.0.1 8082 to send massage for this server");

        // Wait until the connection is closed.
        ch.channel().closeFuture().sync();

    }
}
package netty;

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

import java.io.UnsupportedEncodingException;

/**
 * Created by zhengyong on 16/10/21.
 */
public class ConsoleHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws UnsupportedEncodingException {

        System.out.println(msg);
        ctx.write(msg);
    }

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

四、運行結果

telnet 控制檯

telnet image

idea控制檯

start server success, you can telnet 127.0.0.1 8082 to send massage for this server
PooledUnsafeDirectByteBuf(ridx: 0, widx: 7, cap: 1024)
PooledUnsafeDirectByteBuf(ridx: 0, widx: 7, cap: 1024)
五、參考
  1. http://www.infoq.com/cn/articles/netty-high-performance
  2. https://my.oschina.net/plucury/blog/192577
發佈了172 篇原創文章 · 獲贊 21 · 訪問量 35萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章